Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Conversion to Typescript #33

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*sublime*
node_modules/
node_modules
coverage/
dist
36 changes: 32 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@
"version": "0.2.0",
"description": "A library of Conflict-Free Replicated Data Types for JavaScript.",
"type": "module",
"main": "./src/index.js",
"types": "./dist/src/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/orbitdb/crdts"
},
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"bugs": "https://github.com/orbitdb/crdts/issues",
"author": "Haad",
"homepage": "https://github.com/orbitdb/crdts",
Expand All @@ -25,16 +47,20 @@
"test": "test"
},
"scripts": {
"test": "mocha",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
"test": "mocha dist/test",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha",
"build": "mkdir -p dist/test && cp test/*.js dist/test/ && tsc"
},
"localMaintainers": [
"haad <[email protected]>",
"shamb0t <[email protected]>",
"hajamark <[email protected]>"
],
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.15.0",
"istanbul": "^0.4.5",
"mocha": "^10.2.0"
"mocha": "^10.2.0",
"typescript": "^4.9.5"
}
}
26 changes: 17 additions & 9 deletions src/2P-Set.js → src/2P-Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import GSet from './G-Set.js'
* "A comprehensive study of Convergent and Commutative Replicated Data Types"
* http://hal.upmc.fr/inria-00555588/document, "3.3.2 2P-Set"
*/
export default class TwoPSet extends CRDTSet {
export default class TwoPSet<V=unknown, T=unknown> extends CRDTSet<V, T> {
protected _added: GSet<V, T>
protected _removed: GSet<V, T>
/**
* Create a new TwoPSet instance
* @param {[Iterable]} added [Added values]
* @param {[Iterable]} removed [Removed values]
*/
constructor (added, removed) {
constructor (added: Iterable<V>, removed: Iterable<V>) {
super()
// We track the operations and state differently
// than the base class: use two GSets for operations
Expand All @@ -32,11 +34,13 @@ import GSet from './G-Set.js'
* @override
* @return {[Iterator]} [Iterator for values in the Set]
*/
values () {
// A value is included in the set if it's present in
values (): IterableIterator<V> {
// A value is included in the set if it's present in
// the add set and not present in the remove set. We can
// determine this by calculating the difference between
// adds and removes.

// @ts-ignore merge is different between GSet and CmRDTSet.
const difference = GSet.difference(this._added, this._removed)
return difference.values()
}
Expand All @@ -45,16 +49,18 @@ import GSet from './G-Set.js'
* Add a value to the Set
* @param {[Any]} value [Value to add to the Set]
*/
add (element) {
add (element: V): this {
this._added.add(element)

return this
}

/**
* Remove a value from the Set
* @override
* @param {[Any]} element [Value to remove from the Set]
*/
remove (element) {
remove (element: V) {
// Only add the value to the remove set if it exists in the add set
if (this._added.has(element)) {
this._removed.add(element)
Expand All @@ -66,7 +72,8 @@ import GSet from './G-Set.js'
* @override
* @param {[TwoPSet]} other [Set to merge with]
*/
merge (other) {
// @ts-ignore TS2416 We are modifying the signature of CRDTSet here.
merge (other: TwoPSet<V, T>) {
this._added = new GSet(this._added.toArray().concat(other._added.toArray()))
this._removed = new GSet(this._removed.toArray().concat(other._removed.toArray()))
}
Expand All @@ -75,7 +82,8 @@ import GSet from './G-Set.js'
* TwoPSet as an Object that can be JSON.stringified
* @return {[Object]} [Object in the shape of `{ values: { added: [<values>], removed: [<values>] } }`]
*/
toJSON () {
// @ts-ignore TS2416 We are modifying the signature of CRDTSet here.
toJSON (): { values: { added: V[], removed: V[] } } {
return {
values: {
added: this._added.toArray(),
Expand All @@ -89,7 +97,7 @@ import GSet from './G-Set.js'
* @param {[Object]} json [Input object to create the GSet from. Needs to be: '{ values: { added: [...], removed: [...] } }']
* @return {[TwoPSet]} [new TwoPSet instance]
*/
static from (json) {
static from<V=unknown, T=unknown> (json: { values: { added: V[], removed: V[] } }): TwoPSet<V, T> {
return new TwoPSet(json.values.added, json.values.removed)
}
}
Loading