@codesandbox/crdt-tree
Advanced tools
Comparing version 1.3.0-beta.2 to 1.3.0-beta.3
@@ -437,2 +437,3 @@ 'use strict'; | ||
this.tree = this.state.tree; | ||
this.emitter = this.state.emitter; | ||
} | ||
@@ -439,0 +440,0 @@ /** Get a node by its id */ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e,r=(t=require("mitt"))&&"object"==typeof t&&"default"in t?t.default:t;(e=exports.Ordering||(exports.Ordering={}))[e.Equal=0]="Equal",e[e.Greater=1]="Greater",e[e.Less=2]="Less";var i=function(){function t(t,e){void 0===e&&(e=0),this.actorId=t,this.counter=e}var e=t.prototype;return e.inc=function(){return new t(this.actorId,this.counter+1)},e.tick=function(){return this.counter+=1,new t(this.actorId,this.counter)},e.merge=function(e){return new t(this.actorId,Math.max(this.counter,e.counter))},e.compare=function(t){return this.counter>t.counter?exports.Ordering.Greater:this.counter<t.counter?exports.Ordering.Less:this.actorId>t.actorId?exports.Ordering.Greater:this.actorId<t.actorId?exports.Ordering.Less:exports.Ordering.Equal},e.valueOf=function(){return this.toString()},e.toString=function(){return String(this.counter).padStart(10,"0")+":"+this.actorId},t}();function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=new Array(e);r<e;r++)i[r]=t[r];return i}function o(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return n(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(){function t(){this.nodes=new Map,this.children=new Map,this.size=this.nodes.size}var e=t.prototype;return e.remove=function(t){var e=this.nodes.get(t);if(e){var r=this.children.get(e.parentId);r&&(r.delete(t),0===r.size&&this.children.delete(e.parentId),this.nodes.delete(t))}},e.addNode=function(t,e){var r=this.children.get(e.parentId);r||(r=new Set,this.children.set(e.parentId,r)),r.add(t),this.nodes.set(t,e)},e.get=function(t){return this.nodes.get(t)},e.isAncestor=function(t,e){for(var r,i=t;r=this.get(i);){if(r.parentId===e)return!0;i=r.parentId}return!1},e.printNode=function(t,e){var r;void 0===e&&(e=0);var i=this.get(t),n=t+" "+(i?""+JSON.stringify(i.metadata):""),o=" ".repeat(2*e);console.log(o+n);for(var a=0,s=Array.from(null!=(r=this.children.get(t))?r:[]);a<s.length;a++)this.printNode(s[a],e+1)},t}(),s=function(t,e){this.parentId=t,this.metadata=e},d=function(){function t(t){var e;void 0===t&&(t={}),this.operationLog=[],this.tree=new a,this.emitter=r(),this.conflictHandler=null!=(e=t.conflictHandler)?e:function(){return!1}}var e=t.prototype;return e.addLogEntry=function(t){this.operationLog.unshift(t)},e.applyOp=function(t){if(0===this.operationLog.length){var e=this.doOperation(t);this.addLogEntry(e)}else{var r=this.operationLog[0].op;if(t.timestamp===r.timestamp&&console.log("op with timestamp equal to previous op ignored. (not applied). Every op must have a unique timestamp."),t.timestamp<r.timestamp){var i=this.operationLog.shift();this.undoOp(i),this.applyOp(t),this.redoOp(i)}if(t.timestamp>r.timestamp){var n=this.doOperation(t);this.addLogEntry(n)}}},e.applyOps=function(t){for(var e,r=o(t);!(e=r()).done;)this.applyOp(e.value)},e.doOperation=function(t){var e=this.tree.get(t.id);if(t.id===t.parentId||this.tree.isAncestor(t.parentId,t.id))return{op:t,oldNode:e};if(this.conflictHandler(t,this.tree))return{op:t,oldNode:e};this.tree.remove(t.id);var r=new s(t.parentId,t.metadata);return this.tree.addNode(t.id,r),this.emitter.emit("intermediaryOp",{id:t.id,metadata:t.metadata,parent:this.flattenTree(t.parentId,this.tree)}),{op:t,oldNode:e}},e.undoOp=function(t){var e;if(this.tree.remove(t.op.id),t.oldNode){var r=new s(t.oldNode.parentId,t.oldNode.metadata);this.tree.addNode(t.op.id,r),this.emitter.emit("intermediaryOp",{id:t.op.id,metadata:t.op.metadata,parent:t.oldNode&&this.flattenTree(null==(e=t.oldNode)?void 0:e.parentId,this.tree)})}},e.redoOp=function(t){var e=this.doOperation(t.op);this.addLogEntry(e)},e.flattenTree=function(t,e){var r,i,n=null==(r=e.get(t))?void 0:r.parentId;return{id:t,metadata:null==(i=e.get(t))?void 0:i.metadata,parent:n&&this.flattenTree(n,e)}},t}(),p=function(){function t(t,e){void 0===e&&(e={}),this.latestTimeByReplica=new Map,this.time=new i(t),this.state=new d(e),this.tree=this.state.tree}var e=t.prototype;return e.get=function(t){return this.tree.get(t)},e.opMove=function(t,e,r){return{timestamp:this.time.inc(),metadata:e,id:t,parentId:r}},e.opMoves=function(t){for(var e,r=[],i=o(t);!(e=i()).done;){var n=e.value;r.push({timestamp:this.time.tick(),id:n[0],metadata:n[1],parentId:n[2]})}return r},e.applyOp=function(t){var e;this.time=this.time.merge(t.timestamp);var r=t.timestamp.actorId,i=null!=(e=this.latestTimeByReplica.get(r))?e:0;t.timestamp<=i?(console.log("Clock not increased, current timestamp "+i.toString()+", provided is "+t.timestamp.toString()+"."),console.log("Dropping operation.")):this.latestTimeByReplica.set(r,t.timestamp),this.state.applyOp(t)},e.applyOps=function(t){for(var e,r=o(t);!(e=r()).done;)this.applyOp(e.value)},t}();exports.Clock=i,exports.State=d,exports.Tree=a,exports.TreeNode=s,exports.TreeReplica=p; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e,r=(t=require("mitt"))&&"object"==typeof t&&"default"in t?t.default:t;(e=exports.Ordering||(exports.Ordering={}))[e.Equal=0]="Equal",e[e.Greater=1]="Greater",e[e.Less=2]="Less";var i=function(){function t(t,e){void 0===e&&(e=0),this.actorId=t,this.counter=e}var e=t.prototype;return e.inc=function(){return new t(this.actorId,this.counter+1)},e.tick=function(){return this.counter+=1,new t(this.actorId,this.counter)},e.merge=function(e){return new t(this.actorId,Math.max(this.counter,e.counter))},e.compare=function(t){return this.counter>t.counter?exports.Ordering.Greater:this.counter<t.counter?exports.Ordering.Less:this.actorId>t.actorId?exports.Ordering.Greater:this.actorId<t.actorId?exports.Ordering.Less:exports.Ordering.Equal},e.valueOf=function(){return this.toString()},e.toString=function(){return String(this.counter).padStart(10,"0")+":"+this.actorId},t}();function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=new Array(e);r<e;r++)i[r]=t[r];return i}function o(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return n(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(){function t(){this.nodes=new Map,this.children=new Map,this.size=this.nodes.size}var e=t.prototype;return e.remove=function(t){var e=this.nodes.get(t);if(e){var r=this.children.get(e.parentId);r&&(r.delete(t),0===r.size&&this.children.delete(e.parentId),this.nodes.delete(t))}},e.addNode=function(t,e){var r=this.children.get(e.parentId);r||(r=new Set,this.children.set(e.parentId,r)),r.add(t),this.nodes.set(t,e)},e.get=function(t){return this.nodes.get(t)},e.isAncestor=function(t,e){for(var r,i=t;r=this.get(i);){if(r.parentId===e)return!0;i=r.parentId}return!1},e.printNode=function(t,e){var r;void 0===e&&(e=0);var i=this.get(t),n=t+" "+(i?""+JSON.stringify(i.metadata):""),o=" ".repeat(2*e);console.log(o+n);for(var a=0,s=Array.from(null!=(r=this.children.get(t))?r:[]);a<s.length;a++)this.printNode(s[a],e+1)},t}(),s=function(t,e){this.parentId=t,this.metadata=e},d=function(){function t(t){var e;void 0===t&&(t={}),this.operationLog=[],this.tree=new a,this.emitter=r(),this.conflictHandler=null!=(e=t.conflictHandler)?e:function(){return!1}}var e=t.prototype;return e.addLogEntry=function(t){this.operationLog.unshift(t)},e.applyOp=function(t){if(0===this.operationLog.length){var e=this.doOperation(t);this.addLogEntry(e)}else{var r=this.operationLog[0].op;if(t.timestamp===r.timestamp&&console.log("op with timestamp equal to previous op ignored. (not applied). Every op must have a unique timestamp."),t.timestamp<r.timestamp){var i=this.operationLog.shift();this.undoOp(i),this.applyOp(t),this.redoOp(i)}if(t.timestamp>r.timestamp){var n=this.doOperation(t);this.addLogEntry(n)}}},e.applyOps=function(t){for(var e,r=o(t);!(e=r()).done;)this.applyOp(e.value)},e.doOperation=function(t){var e=this.tree.get(t.id);if(t.id===t.parentId||this.tree.isAncestor(t.parentId,t.id))return{op:t,oldNode:e};if(this.conflictHandler(t,this.tree))return{op:t,oldNode:e};this.tree.remove(t.id);var r=new s(t.parentId,t.metadata);return this.tree.addNode(t.id,r),this.emitter.emit("intermediaryOp",{id:t.id,metadata:t.metadata,parent:this.flattenTree(t.parentId,this.tree)}),{op:t,oldNode:e}},e.undoOp=function(t){var e;if(this.tree.remove(t.op.id),t.oldNode){var r=new s(t.oldNode.parentId,t.oldNode.metadata);this.tree.addNode(t.op.id,r),this.emitter.emit("intermediaryOp",{id:t.op.id,metadata:t.op.metadata,parent:t.oldNode&&this.flattenTree(null==(e=t.oldNode)?void 0:e.parentId,this.tree)})}},e.redoOp=function(t){var e=this.doOperation(t.op);this.addLogEntry(e)},e.flattenTree=function(t,e){var r,i,n=null==(r=e.get(t))?void 0:r.parentId;return{id:t,metadata:null==(i=e.get(t))?void 0:i.metadata,parent:n&&this.flattenTree(n,e)}},t}(),p=function(){function t(t,e){void 0===e&&(e={}),this.latestTimeByReplica=new Map,this.time=new i(t),this.state=new d(e),this.tree=this.state.tree,this.emitter=this.state.emitter}var e=t.prototype;return e.get=function(t){return this.tree.get(t)},e.opMove=function(t,e,r){return{timestamp:this.time.inc(),metadata:e,id:t,parentId:r}},e.opMoves=function(t){for(var e,r=[],i=o(t);!(e=i()).done;){var n=e.value;r.push({timestamp:this.time.tick(),id:n[0],metadata:n[1],parentId:n[2]})}return r},e.applyOp=function(t){var e;this.time=this.time.merge(t.timestamp);var r=t.timestamp.actorId,i=null!=(e=this.latestTimeByReplica.get(r))?e:0;t.timestamp<=i?(console.log("Clock not increased, current timestamp "+i.toString()+", provided is "+t.timestamp.toString()+"."),console.log("Dropping operation.")):this.latestTimeByReplica.set(r,t.timestamp),this.state.applyOp(t)},e.applyOps=function(t){for(var e,r=o(t);!(e=r()).done;)this.applyOp(e.value)},t}();exports.Clock=i,exports.State=d,exports.Tree=a,exports.TreeNode=s,exports.TreeReplica=p; | ||
//# sourceMappingURL=crdt-tree.cjs.production.min.js.map |
@@ -433,2 +433,3 @@ import mitt from 'mitt'; | ||
this.tree = this.state.tree; | ||
this.emitter = this.state.emitter; | ||
} | ||
@@ -435,0 +436,0 @@ /** Get a node by its id */ |
import { LogOpMove } from "./LogOpMove"; | ||
import { OpMove } from "./OpMove"; | ||
import { Tree } from "./Tree"; | ||
declare type Events<Id, Metadata> = { | ||
export declare type Events<Id, Metadata> = { | ||
/** | ||
@@ -17,3 +17,3 @@ * Intermediary operations made when reordering events based on timestamps. | ||
}; | ||
declare type Parent<Id, Metadata> = { | ||
export declare type Parent<Id, Metadata> = { | ||
id: Id; | ||
@@ -20,0 +20,0 @@ metadata?: Metadata; |
@@ -0,4 +1,5 @@ | ||
import { Emitter } from "mitt"; | ||
import { Clock } from "./Clock"; | ||
import { OpMove } from "./OpMove"; | ||
import { State } from "./State"; | ||
import { Events, State } from "./State"; | ||
import { Tree } from "./Tree"; | ||
@@ -25,2 +26,4 @@ import { TreeNode } from "./TreeNode"; | ||
tree: Tree<Id, Metadata>; | ||
/** An event emitter for updates to the state of the tree */ | ||
emitter: Emitter<Events<Id, Metadata>>; | ||
constructor(authorId: Id, options?: ReplicaOptions<Id, Metadata>); | ||
@@ -27,0 +30,0 @@ /** Get a node by its id */ |
@@ -5,3 +5,3 @@ { | ||
"author": "Matan Kushner", | ||
"version": "1.3.0-beta.2", | ||
"version": "1.3.0-beta.3", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "dist/index.js", |
@@ -23,3 +23,3 @@ // Holds Tree CRDT state and implements the core algorithm. | ||
type Events<Id, Metadata> = { | ||
export type Events<Id, Metadata> = { | ||
/** | ||
@@ -38,3 +38,3 @@ * Intermediary operations made when reordering events based on timestamps. | ||
type Parent<Id, Metadata> = { | ||
export type Parent<Id, Metadata> = { | ||
id: Id; | ||
@@ -41,0 +41,0 @@ metadata?: Metadata; |
@@ -13,5 +13,6 @@ // `TreeReplica` holds tree `State` plus lamport timestamp (actor + counter) | ||
import { Emitter } from "mitt"; | ||
import { Clock } from "./Clock"; | ||
import { OpMove } from "./OpMove"; | ||
import { State } from "./State"; | ||
import { Events, State } from "./State"; | ||
import { Tree } from "./Tree"; | ||
@@ -43,2 +44,4 @@ import { TreeNode } from "./TreeNode"; | ||
tree: Tree<Id, Metadata>; | ||
/** An event emitter for updates to the state of the tree */ | ||
emitter: Emitter<Events<Id, Metadata>>; | ||
@@ -48,3 +51,5 @@ constructor(authorId: Id, options: ReplicaOptions<Id, Metadata> = {}) { | ||
this.state = new State(options); | ||
this.tree = this.state.tree; | ||
this.emitter = this.state.emitter; | ||
} | ||
@@ -51,0 +56,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
142512
1661
0