reptree 0.2.4 → 0.3.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/README.md CHANGED
@@ -26,7 +26,7 @@ npm install reptree
26
26
  ### Reactive vertex with Zod (optional)
27
27
 
28
28
  ```ts
29
- import { RepTree, bindVertex } from 'reptree';
29
+ import { RepTree } from 'reptree';
30
30
  import { z } from 'zod';
31
31
 
32
32
  const tree = new RepTree('peer1');
@@ -35,11 +35,7 @@ const v = root.newChild();
35
35
 
36
36
  const Person = z.object({ name: z.string(), age: z.number().int().min(0) });
37
37
 
38
- // Helper function form
39
- const person = bindVertex(tree, v.id, Person);
40
-
41
- // Or via instance method on Vertex
42
- // const person = v.bind(Person);
38
+ const person = v.bind(Person);
43
39
 
44
40
  person.name = 'Alice'; // validated and persisted
45
41
  person.age = 33; // validated and persisted
@@ -50,7 +46,7 @@ person.age = 33; // validated and persisted
50
46
  - `name` ↔ `_n`
51
47
  - `createdAt` ↔ `_c` (Date exposed, ISO stored)
52
48
 
53
- These aliases are applied by default when using `bindVertex` or `vertex.bind()`.
49
+ These aliases are applied by default when using `vertex.bind()`.
54
50
 
55
51
  ```ts
56
52
  person.name = 'Alice'; // writes _n
package/dist/index.cjs CHANGED
@@ -438,6 +438,8 @@ function toPublicObject(tree, id, internalToPublic) {
438
438
  }
439
439
  var RESERVED_METHOD_USE_TRANSIENT = "useTransient";
440
440
  var RESERVED_METHOD_COMMIT_TRANSIENTS = "commitTransients";
441
+ var VERTEX_PROPS = ["$id", "$parentId", "$parent", "$children", "$childrenIds"];
442
+ var VERTEX_METHODS = ["$moveTo", "$delete", "$observe", "$observeChildren", "$newChild", "$newNamedChild"];
441
443
  function bindVertex(tree, id, schemaOrOptions) {
442
444
  const isOptions = typeof schemaOrOptions === "object" && schemaOrOptions !== null && (Object.prototype.hasOwnProperty.call(schemaOrOptions, "aliases") || Object.prototype.hasOwnProperty.call(schemaOrOptions, "includeInternalKeys") || Object.prototype.hasOwnProperty.call(schemaOrOptions, "schema"));
443
445
  const options = isOptions ? schemaOrOptions : { schema: schemaOrOptions };
@@ -445,6 +447,7 @@ function bindVertex(tree, id, schemaOrOptions) {
445
447
  const aliases = options.aliases ?? defaultAliases;
446
448
  const includeInternalKeys = options.includeInternalKeys ?? false;
447
449
  const { publicToInternal, internalToPublic } = buildAliasMaps(aliases);
450
+ const cachedMethods = /* @__PURE__ */ new Map();
448
451
  return new Proxy({}, {
449
452
  get(_target, prop) {
450
453
  if (prop === RESERVED_METHOD_USE_TRANSIENT) {
@@ -533,6 +536,67 @@ function bindVertex(tree, id, schemaOrOptions) {
533
536
  }
534
537
  };
535
538
  }
539
+ if (typeof prop === "string") {
540
+ if (prop === "$id") return id;
541
+ if (prop === "$parentId") return tree.getVertex(id)?.parentId ?? null;
542
+ if (prop === "$parent") {
543
+ const vertex = tree.getVertex(id);
544
+ return vertex?.parent;
545
+ }
546
+ if (prop === "$children") return tree.getChildren(id);
547
+ if (prop === "$childrenIds") return tree.getChildrenIds(id);
548
+ if (prop === "$moveTo") {
549
+ if (!cachedMethods.has(prop)) {
550
+ cachedMethods.set(prop, (parent) => {
551
+ const parentId = typeof parent === "object" && parent !== null ? parent.id || parent.$id : parent;
552
+ tree.moveVertex(id, parentId);
553
+ });
554
+ }
555
+ return cachedMethods.get(prop);
556
+ }
557
+ if (prop === "$delete") {
558
+ if (!cachedMethods.has(prop)) {
559
+ cachedMethods.set(prop, () => tree.deleteVertex(id));
560
+ }
561
+ return cachedMethods.get(prop);
562
+ }
563
+ if (prop === "$observe") {
564
+ if (!cachedMethods.has(prop)) {
565
+ cachedMethods.set(prop, (listener) => tree.observe(id, listener));
566
+ }
567
+ return cachedMethods.get(prop);
568
+ }
569
+ if (prop === "$observeChildren") {
570
+ if (!cachedMethods.has(prop)) {
571
+ cachedMethods.set(prop, (listener) => {
572
+ return tree.observe(id, (events) => {
573
+ if (events.some((e) => e.type === "children")) {
574
+ listener(tree.getChildren(id));
575
+ }
576
+ });
577
+ });
578
+ }
579
+ return cachedMethods.get(prop);
580
+ }
581
+ if (prop === "$newChild") {
582
+ if (!cachedMethods.has(prop)) {
583
+ cachedMethods.set(prop, (props) => {
584
+ const vertex = tree.getVertex(id);
585
+ return vertex?.newChild(props);
586
+ });
587
+ }
588
+ return cachedMethods.get(prop);
589
+ }
590
+ if (prop === "$newNamedChild") {
591
+ if (!cachedMethods.has(prop)) {
592
+ cachedMethods.set(prop, (name, props) => {
593
+ const vertex = tree.getVertex(id);
594
+ return vertex?.newNamedChild(name, props);
595
+ });
596
+ }
597
+ return cachedMethods.get(prop);
598
+ }
599
+ }
536
600
  if (typeof prop !== "string") return void 0;
537
601
  const rule = publicToInternal.get(prop);
538
602
  if (rule) {
@@ -546,6 +610,12 @@ function bindVertex(tree, id, schemaOrOptions) {
546
610
  if (prop === RESERVED_METHOD_USE_TRANSIENT || prop === RESERVED_METHOD_COMMIT_TRANSIENTS) {
547
611
  return true;
548
612
  }
613
+ if (VERTEX_PROPS.includes(prop)) {
614
+ return true;
615
+ }
616
+ if (VERTEX_METHODS.includes(prop)) {
617
+ return true;
618
+ }
549
619
  if (schema?.shape && schema.shape[prop]) {
550
620
  const field = schema.shape[prop];
551
621
  if (field.safeParse) {
@@ -576,6 +646,12 @@ function bindVertex(tree, id, schemaOrOptions) {
576
646
  if (prop === RESERVED_METHOD_USE_TRANSIENT || prop === RESERVED_METHOD_COMMIT_TRANSIENTS) {
577
647
  return true;
578
648
  }
649
+ if (VERTEX_PROPS.includes(prop)) {
650
+ return true;
651
+ }
652
+ if (VERTEX_METHODS.includes(prop)) {
653
+ return true;
654
+ }
579
655
  const rule = publicToInternal.get(prop);
580
656
  if (rule) {
581
657
  tree.setVertexProperty(id, rule.internalKey, void 0);