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.cjs CHANGED
@@ -501,18 +501,29 @@ function bindVertex(tree, id, schemaOrOptions) {
501
501
  // src/Vertex.ts
502
502
  var Vertex = class _Vertex {
503
503
  constructor(tree, state) {
504
- this.tree = tree;
505
504
  this.state = state;
505
+ this._tree = tree;
506
506
  }
507
+ /** Returns the tree this vertex belongs to. */
508
+ get tree() {
509
+ return this._tree;
510
+ }
511
+ set tree(value) {
512
+ this._tree = value;
513
+ }
514
+ /** Returns the ID of this vertex. */
507
515
  get id() {
508
516
  return this.state.id;
509
517
  }
518
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
510
519
  get name() {
511
520
  return this.getProperty("name");
512
521
  }
522
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
513
523
  set name(name) {
514
524
  this.tree.setVertexProperty(this.id, "name", name);
515
525
  }
526
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
516
527
  get createdAt() {
517
528
  const createdAt = this.getProperty("_c");
518
529
  if (!createdAt) {
@@ -520,30 +531,51 @@ var Vertex = class _Vertex {
520
531
  }
521
532
  return new Date(createdAt);
522
533
  }
523
- get path() {
524
- throw new Error("Not implemented");
525
- }
534
+ /** Returns the ID of the parent vertex of this vertex. */
526
535
  get parentId() {
527
536
  return this.state.parentId;
528
537
  }
538
+ /** Returns the parent vertex of this vertex. */
529
539
  get parent() {
530
540
  if (!this.parentId) {
531
541
  return void 0;
532
542
  }
533
543
  return this.tree.getVertex(this.parentId);
534
544
  }
545
+ /** Returns the children vertices of this vertex. */
535
546
  get children() {
536
547
  return this.tree.getChildren(this.id);
537
548
  }
549
+ /** Returns the IDs of the children vertices of this vertex. */
538
550
  get childrenIds() {
539
551
  return this.tree.getChildrenIds(this.id);
540
552
  }
553
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
554
+ * E.g root -> grandparent -> parent.
555
+ * Doesn't include this vertex in the array.
556
+ */
557
+ get ancestors() {
558
+ return this.tree.getAncestors(this.id);
559
+ }
560
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
561
+ get treeId() {
562
+ return this.root.id;
563
+ }
564
+ /** Returns the root vertex of the tree this vertex belongs to. */
565
+ get root() {
566
+ const root = this.tree.root;
567
+ if (!root) {
568
+ throw new Error("Root vertex of the tree is not set");
569
+ }
570
+ return root;
571
+ }
541
572
  getAsTypedObject() {
542
573
  return this.getProperties();
543
574
  }
544
575
  getChildrenAsTypedArray() {
545
576
  return this.children.map((v) => v.getAsTypedObject());
546
577
  }
578
+ /** Creates a new child vertex of this vertex. */
547
579
  newChild(props) {
548
580
  if (props && typeof props === "object" && "children" in props) {
549
581
  throw new Error("Passing children inside props is not supported at the moment");
@@ -551,6 +583,7 @@ var Vertex = class _Vertex {
551
583
  const normalized = _Vertex.normalizePropsForCreation(props);
552
584
  return this.tree.newVertex(this.id, normalized);
553
585
  }
586
+ /** Creates a new named child vertex of this vertex. */
554
587
  newNamedChild(name, props) {
555
588
  if (props && typeof props === "object" && "children" in props) {
556
589
  throw new Error("Passing children inside props is not supported at the moment");
@@ -558,6 +591,7 @@ var Vertex = class _Vertex {
558
591
  const normalized = _Vertex.normalizePropsForCreation(props);
559
592
  return this.tree.newNamedVertex(this.id, name, normalized);
560
593
  }
594
+ /** Sets a property on this vertex. */
561
595
  setProperty(key, value) {
562
596
  const existingValue = this.getProperty(key, false);
563
597
  if (existingValue === value) {
@@ -565,6 +599,7 @@ var Vertex = class _Vertex {
565
599
  }
566
600
  this.tree.setVertexProperty(this.id, key, value);
567
601
  }
602
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
568
603
  setTransientProperty(key, value) {
569
604
  const existingValue = this.getProperty(key);
570
605
  if (existingValue === value) {
@@ -572,17 +607,21 @@ var Vertex = class _Vertex {
572
607
  }
573
608
  this.tree.setTransientVertexProperty(this.id, key, value);
574
609
  }
610
+ /** Promotes all transient (temporary) properties to persistent properties. */
575
611
  commitTransients() {
576
612
  this.tree.commitTransients(this.id);
577
613
  }
614
+ /** Sets multiple properties on this vertex. */
578
615
  setProperties(props) {
579
616
  for (const [key, value] of Object.entries(props)) {
580
617
  this.setProperty(key, value);
581
618
  }
582
619
  }
620
+ /** Returns the value of a property on this vertex. */
583
621
  getProperty(key, includingTransient = true) {
584
622
  return this.tree.getVertexProperty(this.id, key, includingTransient);
585
623
  }
624
+ /** Returns all properties on this vertex. */
586
625
  getProperties() {
587
626
  const props = {};
588
627
  this.tree.getVertexProperties(this.id).forEach((p) => {
@@ -602,10 +641,12 @@ var Vertex = class _Vertex {
602
641
  findAllTypedChildrenWithProperty(key, value) {
603
642
  return this.findAllChildrenWithProperty(key, value).map((c) => c.getAsTypedObject());
604
643
  }
644
+ /** Observes changes to this vertex. */
605
645
  observe(listener) {
606
646
  const unobserve = this.tree.observe(this.id, listener);
607
647
  return () => unobserve();
608
648
  }
649
+ /** Observes changes to the children of this vertex. */
609
650
  observeChildren(listener) {
610
651
  const unobserve = this.tree.observe(this.id, (events) => {
611
652
  if (events.some((e) => e.type === "children")) {
@@ -935,7 +976,6 @@ var _RepTree = class _RepTree {
935
976
  this.setPropertyOps = [];
936
977
  this.propertiesAndTheirOpIds = /* @__PURE__ */ new Map();
937
978
  this.transientPropertiesAndTheirOpIds = /* @__PURE__ */ new Map();
938
- // Observers for non-structural properties are not used
939
979
  this.localOps = [];
940
980
  this.pendingMovesWithMissingParent = /* @__PURE__ */ new Map();
941
981
  this.pendingPropertiesWithMissingVertex = /* @__PURE__ */ new Map();
@@ -1000,6 +1040,7 @@ var _RepTree = class _RepTree {
1000
1040
  getChildrenIds(vertexId) {
1001
1041
  return this.state.getChildrenIds(vertexId);
1002
1042
  }
1043
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
1003
1044
  getAncestors(vertexId) {
1004
1045
  const ancestors = [];
1005
1046
  let currentVertex = this.state.getVertex(vertexId);