reptree 0.7.0 → 0.8.0

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,3 +1,8 @@
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;
@@ -163,33 +168,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
163
168
  * for working with vertices in a RepTree.
164
169
  */
165
170
  declare class Vertex {
166
- private tree;
167
171
  private state;
172
+ private _tree;
168
173
  constructor(tree: RepTree, state: VertexState);
174
+ /** Returns the tree this vertex belongs to. */
175
+ get tree(): RepTree;
176
+ private set tree(value);
177
+ /** Returns the ID of this vertex. */
169
178
  get id(): string;
179
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
170
180
  get name(): string | undefined;
181
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
171
182
  set name(name: string);
183
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
172
184
  get createdAt(): Date;
173
- get path(): string;
185
+ /** Returns the ID of the parent vertex of this vertex. */
174
186
  get parentId(): string | null;
187
+ /** Returns the parent vertex of this vertex. */
175
188
  get parent(): Vertex | undefined;
189
+ /** Returns the children vertices of this vertex. */
176
190
  get children(): Vertex[];
191
+ /** Returns the IDs of the children vertices of this vertex. */
177
192
  get childrenIds(): string[];
193
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
194
+ * E.g root -> grandparent -> parent.
195
+ * Doesn't include this vertex in the array.
196
+ */
197
+ get ancestors(): Vertex[];
198
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
199
+ get treeId(): string;
200
+ /** Returns the root vertex of the tree this vertex belongs to. */
201
+ get root(): Vertex;
178
202
  getAsTypedObject<T>(): T;
179
203
  getChildrenAsTypedArray<T>(): T[];
204
+ /** Creates a new child vertex of this vertex. */
180
205
  newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
206
+ /** Creates a new named child vertex of this vertex. */
181
207
  newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
208
+ /** Sets a property on this vertex. */
182
209
  setProperty(key: string, value: VertexPropertyType): void;
210
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
183
211
  setTransientProperty(key: string, value: VertexPropertyType): void;
212
+ /** Promotes all transient (temporary) properties to persistent properties. */
184
213
  commitTransients(): void;
214
+ /** Sets multiple properties on this vertex. */
185
215
  setProperties(props: Record<string, VertexPropertyType> | object): void;
216
+ /** Returns the value of a property on this vertex. */
186
217
  getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
218
+ /** Returns all properties on this vertex. */
187
219
  getProperties(): Record<string, VertexPropertyType>;
188
220
  findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
189
221
  findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
190
222
  findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
191
223
  findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
224
+ /** Observes changes to this vertex. */
192
225
  observe(listener: (events: VertexChangeEvent[]) => void): () => void;
226
+ /** Observes changes to the children of this vertex. */
193
227
  observeChildren(listener: (children: Vertex[]) => void): () => void;
194
228
  observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
195
229
  delete(): void;
@@ -242,6 +276,7 @@ declare class RepTree {
242
276
  getParent(vertexId: string): Vertex | undefined;
243
277
  getChildren(vertexId: string): Vertex[];
244
278
  getChildrenIds(vertexId: string): string[];
279
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
245
280
  getAncestors(vertexId: string): Vertex[];
246
281
  getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
247
282
  getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
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;
@@ -163,33 +168,62 @@ declare function bindVertex<T extends Record<string, unknown>>(tree: RepTree, id
163
168
  * for working with vertices in a RepTree.
164
169
  */
165
170
  declare class Vertex {
166
- private tree;
167
171
  private state;
172
+ private _tree;
168
173
  constructor(tree: RepTree, state: VertexState);
174
+ /** Returns the tree this vertex belongs to. */
175
+ get tree(): RepTree;
176
+ private set tree(value);
177
+ /** Returns the ID of this vertex. */
169
178
  get id(): string;
179
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
170
180
  get name(): string | undefined;
181
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
171
182
  set name(name: string);
183
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
172
184
  get createdAt(): Date;
173
- get path(): string;
185
+ /** Returns the ID of the parent vertex of this vertex. */
174
186
  get parentId(): string | null;
187
+ /** Returns the parent vertex of this vertex. */
175
188
  get parent(): Vertex | undefined;
189
+ /** Returns the children vertices of this vertex. */
176
190
  get children(): Vertex[];
191
+ /** Returns the IDs of the children vertices of this vertex. */
177
192
  get childrenIds(): string[];
193
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
194
+ * E.g root -> grandparent -> parent.
195
+ * Doesn't include this vertex in the array.
196
+ */
197
+ get ancestors(): Vertex[];
198
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
199
+ get treeId(): string;
200
+ /** Returns the root vertex of the tree this vertex belongs to. */
201
+ get root(): Vertex;
178
202
  getAsTypedObject<T>(): T;
179
203
  getChildrenAsTypedArray<T>(): T[];
204
+ /** Creates a new child vertex of this vertex. */
180
205
  newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex;
206
+ /** Creates a new named child vertex of this vertex. */
181
207
  newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex;
208
+ /** Sets a property on this vertex. */
182
209
  setProperty(key: string, value: VertexPropertyType): void;
210
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
183
211
  setTransientProperty(key: string, value: VertexPropertyType): void;
212
+ /** Promotes all transient (temporary) properties to persistent properties. */
184
213
  commitTransients(): void;
214
+ /** Sets multiple properties on this vertex. */
185
215
  setProperties(props: Record<string, VertexPropertyType> | object): void;
216
+ /** Returns the value of a property on this vertex. */
186
217
  getProperty(key: string, includingTransient?: boolean): VertexPropertyType | undefined;
218
+ /** Returns all properties on this vertex. */
187
219
  getProperties(): Record<string, VertexPropertyType>;
188
220
  findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[];
189
221
  findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined;
190
222
  findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined;
191
223
  findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[];
224
+ /** Observes changes to this vertex. */
192
225
  observe(listener: (events: VertexChangeEvent[]) => void): () => void;
226
+ /** Observes changes to the children of this vertex. */
193
227
  observeChildren(listener: (children: Vertex[]) => void): () => void;
194
228
  observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void;
195
229
  delete(): void;
@@ -242,6 +276,7 @@ declare class RepTree {
242
276
  getParent(vertexId: string): Vertex | undefined;
243
277
  getChildren(vertexId: string): Vertex[];
244
278
  getChildrenIds(vertexId: string): string[];
279
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
245
280
  getAncestors(vertexId: string): Vertex[];
246
281
  getVertexProperty(vertexId: string, key: string, includingTransient?: boolean): VertexPropertyType | undefined;
247
282
  getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]>;
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);