reptree 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,7 +1,24 @@
1
+ /**
2
+ * An identifier for an operation. We use them to compare and order operations.
3
+ * It uses a counter as a Lamport clock and a peer ID in case if ops have the same counter - in that case the ops will be compared by peer ID.
4
+ * Lamport clock (Lamport timestamp): https://en.wikipedia.org/wiki/Lamport_timestamp
5
+ */
1
6
  interface OpId {
2
7
  readonly counter: number;
3
8
  readonly peerId: string;
4
9
  }
10
+ declare function createOpId(counter: number, peerId: string): OpId;
11
+ /**
12
+ * Compares two operation IDs.
13
+ * @param opIdA - The first operation ID.
14
+ * @param opIdB - The second operation ID.
15
+ * @returns 1 if opIdA is greater than opIdB, -1 if opIdA is less than opIdB, 0 if they are equal.
16
+ */
17
+ declare function compareOpId(opIdA: OpId | string, opIdB: OpId | string): number;
18
+ declare function equalsOpId(opIdA: OpId | string | null, opIdB: OpId | string | null): boolean;
19
+ declare function tryParseOpIdStr(opIdStr: string): OpId;
20
+ declare function isOpIdGreaterThan(opIdA: OpId | string, opIdB: OpId | string): boolean;
21
+ declare function opIdToString(opId: OpId): string;
5
22
 
6
23
  declare class VertexState {
7
24
  readonly id: string;
@@ -163,33 +180,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
163
180
  * for working with vertices in a RepTree.
164
181
  */
165
182
  declare class Vertex {
166
- private tree;
167
183
  private state;
184
+ private _tree;
168
185
  constructor(tree: RepTree, state: VertexState);
186
+ /** Returns the tree this vertex belongs to. */
187
+ get tree(): RepTree;
188
+ private set tree(value);
189
+ /** Returns the ID of this vertex. */
169
190
  get id(): string;
191
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
170
192
  get name(): string | undefined;
193
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
171
194
  set name(name: string);
195
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
172
196
  get createdAt(): Date;
173
- get path(): string;
197
+ /** Returns the ID of the parent vertex of this vertex. */
174
198
  get parentId(): string | null;
199
+ /** Returns the parent vertex of this vertex. */
175
200
  get parent(): Vertex | undefined;
201
+ /** Returns the children vertices of this vertex. */
176
202
  get children(): Vertex[];
203
+ /** Returns the IDs of the children vertices of this vertex. */
177
204
  get childrenIds(): string[];
205
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
206
+ * E.g root -> grandparent -> parent.
207
+ * Doesn't include this vertex in the array.
208
+ */
209
+ get ancestors(): Vertex[];
210
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
211
+ get treeId(): string;
212
+ /** Returns the root vertex of the tree this vertex belongs to. */
213
+ get root(): Vertex;
178
214
  getAsTypedObject<T>(): T;
179
215
  getChildrenAsTypedArray<T>(): T[];
216
+ /** Creates a new child vertex of this vertex. */
180
217
  newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
218
+ /** Creates a new named child vertex of this vertex. */
181
219
  newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
220
+ /** Sets a property on this vertex. */
182
221
  setProperty(key: string, value: VertexPropertyType): void;
222
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
183
223
  setTransientProperty(key: string, value: VertexPropertyType): void;
224
+ /** Promotes all transient (temporary) properties to persistent properties. */
184
225
  commitTransients(): void;
226
+ /** Sets multiple properties on this vertex. */
185
227
  setProperties(props: Record<string, VertexPropertyType> | object): void;
228
+ /** Returns the value of a property on this vertex. */
186
229
  getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
230
+ /** Returns all properties on this vertex. */
187
231
  getProperties(): Record<string, VertexPropertyType>;
188
232
  findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
189
233
  findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
190
234
  findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
191
235
  findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
236
+ /** Observes changes to this vertex. */
192
237
  observe(listener: (events: VertexChangeEvent[]) => void): () => void;
238
+ /** Observes changes to the children of this vertex. */
193
239
  observeChildren(listener: (children: Vertex[]) => void): () => void;
194
240
  observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
195
241
  delete(): void;
@@ -242,6 +288,7 @@ declare class RepTree {
242
288
  getParent(vertexId: string): Vertex | undefined;
243
289
  getChildren(vertexId: string): Vertex[];
244
290
  getChildrenIds(vertexId: string): string[];
291
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
245
292
  getAncestors(vertexId: string): Vertex[];
246
293
  getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
247
294
  getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
@@ -414,4 +461,4 @@ declare class StateVector {
414
461
 
415
462
  declare function uuid(): string;
416
463
 
417
- export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
464
+ export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, compareOpId, createOpId, equalsOpId, isAnyPropertyOp, isMoveVertexOp, isOpIdGreaterThan, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, opIdToString, tryParseOpIdStr, uuid };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,24 @@
1
+ /**
2
+ * An identifier for an operation. We use them to compare and order operations.
3
+ * It uses a counter as a Lamport clock and a peer ID in case if ops have the same counter - in that case the ops will be compared by peer ID.
4
+ * Lamport clock (Lamport timestamp): https://en.wikipedia.org/wiki/Lamport_timestamp
5
+ */
1
6
  interface OpId {
2
7
  readonly counter: number;
3
8
  readonly peerId: string;
4
9
  }
10
+ declare function createOpId(counter: number, peerId: string): OpId;
11
+ /**
12
+ * Compares two operation IDs.
13
+ * @param opIdA - The first operation ID.
14
+ * @param opIdB - The second operation ID.
15
+ * @returns 1 if opIdA is greater than opIdB, -1 if opIdA is less than opIdB, 0 if they are equal.
16
+ */
17
+ declare function compareOpId(opIdA: OpId | string, opIdB: OpId | string): number;
18
+ declare function equalsOpId(opIdA: OpId | string | null, opIdB: OpId | string | null): boolean;
19
+ declare function tryParseOpIdStr(opIdStr: string): OpId;
20
+ declare function isOpIdGreaterThan(opIdA: OpId | string, opIdB: OpId | string): boolean;
21
+ declare function opIdToString(opId: OpId): string;
5
22
 
6
23
  declare class VertexState {
7
24
  readonly id: string;
@@ -163,33 +180,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
163
180
  * for working with vertices in a RepTree.
164
181
  */
165
182
  declare class Vertex {
166
- private tree;
167
183
  private state;
184
+ private _tree;
168
185
  constructor(tree: RepTree, state: VertexState);
186
+ /** Returns the tree this vertex belongs to. */
187
+ get tree(): RepTree;
188
+ private set tree(value);
189
+ /** Returns the ID of this vertex. */
169
190
  get id(): string;
191
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
170
192
  get name(): string | undefined;
193
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
171
194
  set name(name: string);
195
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
172
196
  get createdAt(): Date;
173
- get path(): string;
197
+ /** Returns the ID of the parent vertex of this vertex. */
174
198
  get parentId(): string | null;
199
+ /** Returns the parent vertex of this vertex. */
175
200
  get parent(): Vertex | undefined;
201
+ /** Returns the children vertices of this vertex. */
176
202
  get children(): Vertex[];
203
+ /** Returns the IDs of the children vertices of this vertex. */
177
204
  get childrenIds(): string[];
205
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
206
+ * E.g root -> grandparent -> parent.
207
+ * Doesn't include this vertex in the array.
208
+ */
209
+ get ancestors(): Vertex[];
210
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
211
+ get treeId(): string;
212
+ /** Returns the root vertex of the tree this vertex belongs to. */
213
+ get root(): Vertex;
178
214
  getAsTypedObject<T>(): T;
179
215
  getChildrenAsTypedArray<T>(): T[];
216
+ /** Creates a new child vertex of this vertex. */
180
217
  newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
218
+ /** Creates a new named child vertex of this vertex. */
181
219
  newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
220
+ /** Sets a property on this vertex. */
182
221
  setProperty(key: string, value: VertexPropertyType): void;
222
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
183
223
  setTransientProperty(key: string, value: VertexPropertyType): void;
224
+ /** Promotes all transient (temporary) properties to persistent properties. */
184
225
  commitTransients(): void;
226
+ /** Sets multiple properties on this vertex. */
185
227
  setProperties(props: Record<string, VertexPropertyType> | object): void;
228
+ /** Returns the value of a property on this vertex. */
186
229
  getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
230
+ /** Returns all properties on this vertex. */
187
231
  getProperties(): Record<string, VertexPropertyType>;
188
232
  findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
189
233
  findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
190
234
  findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
191
235
  findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
236
+ /** Observes changes to this vertex. */
192
237
  observe(listener: (events: VertexChangeEvent[]) => void): () => void;
238
+ /** Observes changes to the children of this vertex. */
193
239
  observeChildren(listener: (children: Vertex[]) => void): () => void;
194
240
  observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
195
241
  delete(): void;
@@ -242,6 +288,7 @@ declare class RepTree {
242
288
  getParent(vertexId: string): Vertex | undefined;
243
289
  getChildren(vertexId: string): Vertex[];
244
290
  getChildrenIds(vertexId: string): string[];
291
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
245
292
  getAncestors(vertexId: string): Vertex[];
246
293
  getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
247
294
  getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
@@ -414,4 +461,4 @@ declare class StateVector {
414
461
 
415
462
  declare function uuid(): string;
416
463
 
417
- export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, isAnyPropertyOp, isMoveVertexOp, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, uuid };
464
+ export { type BindOptions, type BindedVertex, type JsonPrimitive, type JsonValue, type MoveVertex, type OpId, type OpIdRange, RepTree, type SchemaLike, type SetVertexProperty, StateVector, TreeState, type TreeVertexId, type TreeVertexProperty, Vertex, type VertexChangeEvent, type VertexChildrenChangeEvent, type VertexMoveEvent, type VertexOperation, type VertexPropertyChangeEvent, type VertexPropertyType, VertexState, bindVertex, compareOpId, createOpId, equalsOpId, isAnyPropertyOp, isMoveVertexOp, isOpIdGreaterThan, newMoveVertexOp, newSetTransientVertexPropertyOp, newSetVertexPropertyOp, opIdToString, tryParseOpIdStr, uuid };
package/dist/index.js CHANGED
@@ -464,18 +464,29 @@ function bindVertex(tree, id, schemaOrOptions) {
464
464
  // src/Vertex.ts
465
465
  var Vertex = class _Vertex {
466
466
  constructor(tree, state) {
467
- this.tree = tree;
468
467
  this.state = state;
468
+ this._tree = tree;
469
469
  }
470
+ /** Returns the tree this vertex belongs to. */
471
+ get tree() {
472
+ return this._tree;
473
+ }
474
+ set tree(value) {
475
+ this._tree = value;
476
+ }
477
+ /** Returns the ID of this vertex. */
470
478
  get id() {
471
479
  return this.state.id;
472
480
  }
481
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
473
482
  get name() {
474
483
  return this.getProperty("name");
475
484
  }
485
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
476
486
  set name(name) {
477
487
  this.tree.setVertexProperty(this.id, "name", name);
478
488
  }
489
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
479
490
  get createdAt() {
480
491
  const createdAt = this.getProperty("_c");
481
492
  if (!createdAt) {
@@ -483,30 +494,51 @@ var Vertex = class _Vertex {
483
494
  }
484
495
  return new Date(createdAt);
485
496
  }
486
- get path() {
487
- throw new Error("Not implemented");
488
- }
497
+ /** Returns the ID of the parent vertex of this vertex. */
489
498
  get parentId() {
490
499
  return this.state.parentId;
491
500
  }
501
+ /** Returns the parent vertex of this vertex. */
492
502
  get parent() {
493
503
  if (!this.parentId) {
494
504
  return void 0;
495
505
  }
496
506
  return this.tree.getVertex(this.parentId);
497
507
  }
508
+ /** Returns the children vertices of this vertex. */
498
509
  get children() {
499
510
  return this.tree.getChildren(this.id);
500
511
  }
512
+ /** Returns the IDs of the children vertices of this vertex. */
501
513
  get childrenIds() {
502
514
  return this.tree.getChildrenIds(this.id);
503
515
  }
516
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
517
+ * E.g root -> grandparent -> parent.
518
+ * Doesn't include this vertex in the array.
519
+ */
520
+ get ancestors() {
521
+ return this.tree.getAncestors(this.id);
522
+ }
523
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
524
+ get treeId() {
525
+ return this.root.id;
526
+ }
527
+ /** Returns the root vertex of the tree this vertex belongs to. */
528
+ get root() {
529
+ const root = this.tree.root;
530
+ if (!root) {
531
+ throw new Error("Root vertex of the tree is not set");
532
+ }
533
+ return root;
534
+ }
504
535
  getAsTypedObject() {
505
536
  return this.getProperties();
506
537
  }
507
538
  getChildrenAsTypedArray() {
508
539
  return this.children.map((v) => v.getAsTypedObject());
509
540
  }
541
+ /** Creates a new child vertex of this vertex. */
510
542
  newChild(props) {
511
543
  if (props && typeof props === "object" && "children" in props) {
512
544
  throw new Error("Passing children inside props is not supported at the moment");
@@ -514,6 +546,7 @@ var Vertex = class _Vertex {
514
546
  const normalized = _Vertex.normalizePropsForCreation(props);
515
547
  return this.tree.newVertex(this.id, normalized);
516
548
  }
549
+ /** Creates a new named child vertex of this vertex. */
517
550
  newNamedChild(name, props) {
518
551
  if (props && typeof props === "object" && "children" in props) {
519
552
  throw new Error("Passing children inside props is not supported at the moment");
@@ -521,6 +554,7 @@ var Vertex = class _Vertex {
521
554
  const normalized = _Vertex.normalizePropsForCreation(props);
522
555
  return this.tree.newNamedVertex(this.id, name, normalized);
523
556
  }
557
+ /** Sets a property on this vertex. */
524
558
  setProperty(key, value) {
525
559
  const existingValue = this.getProperty(key, false);
526
560
  if (existingValue === value) {
@@ -528,6 +562,7 @@ var Vertex = class _Vertex {
528
562
  }
529
563
  this.tree.setVertexProperty(this.id, key, value);
530
564
  }
565
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
531
566
  setTransientProperty(key, value) {
532
567
  const existingValue = this.getProperty(key);
533
568
  if (existingValue === value) {
@@ -535,17 +570,21 @@ var Vertex = class _Vertex {
535
570
  }
536
571
  this.tree.setTransientVertexProperty(this.id, key, value);
537
572
  }
573
+ /** Promotes all transient (temporary) properties to persistent properties. */
538
574
  commitTransients() {
539
575
  this.tree.commitTransients(this.id);
540
576
  }
577
+ /** Sets multiple properties on this vertex. */
541
578
  setProperties(props) {
542
579
  for (const [key, value] of Object.entries(props)) {
543
580
  this.setProperty(key, value);
544
581
  }
545
582
  }
583
+ /** Returns the value of a property on this vertex. */
546
584
  getProperty(key, includingTransient = true) {
547
585
  return this.tree.getVertexProperty(this.id, key, includingTransient);
548
586
  }
587
+ /** Returns all properties on this vertex. */
549
588
  getProperties() {
550
589
  const props = {};
551
590
  this.tree.getVertexProperties(this.id).forEach((p) => {
@@ -565,10 +604,12 @@ var Vertex = class _Vertex {
565
604
  findAllTypedChildrenWithProperty(key, value) {
566
605
  return this.findAllChildrenWithProperty(key, value).map((c) => c.getAsTypedObject());
567
606
  }
607
+ /** Observes changes to this vertex. */
568
608
  observe(listener) {
569
609
  const unobserve = this.tree.observe(this.id, listener);
570
610
  return () => unobserve();
571
611
  }
612
+ /** Observes changes to the children of this vertex. */
572
613
  observeChildren(listener) {
573
614
  const unobserve = this.tree.observe(this.id, (events) => {
574
615
  if (events.some((e) => e.type === "children")) {
@@ -898,7 +939,6 @@ var _RepTree = class _RepTree {
898
939
  this.setPropertyOps = [];
899
940
  this.propertiesAndTheirOpIds = /* @__PURE__ */ new Map();
900
941
  this.transientPropertiesAndTheirOpIds = /* @__PURE__ */ new Map();
901
- // Observers for non-structural properties are not used
902
942
  this.localOps = [];
903
943
  this.pendingMovesWithMissingParent = /* @__PURE__ */ new Map();
904
944
  this.pendingPropertiesWithMissingVertex = /* @__PURE__ */ new Map();
@@ -963,6 +1003,7 @@ var _RepTree = class _RepTree {
963
1003
  getChildrenIds(vertexId) {
964
1004
  return this.state.getChildrenIds(vertexId);
965
1005
  }
1006
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
966
1007
  getAncestors(vertexId) {
967
1008
  const ancestors = [];
968
1009
  let currentVertex = this.state.getVertex(vertexId);
@@ -1517,11 +1558,17 @@ export {
1517
1558
  Vertex,
1518
1559
  VertexState,
1519
1560
  bindVertex,
1561
+ compareOpId,
1562
+ createOpId,
1563
+ equalsOpId,
1520
1564
  isAnyPropertyOp,
1521
1565
  isMoveVertexOp,
1566
+ isOpIdGreaterThan,
1522
1567
  newMoveVertexOp,
1523
1568
  newSetTransientVertexPropertyOp,
1524
1569
  newSetVertexPropertyOp,
1570
+ opIdToString,
1571
+ tryParseOpIdStr,
1525
1572
  uuid
1526
1573
  };
1527
1574
  //# sourceMappingURL=index.js.map