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/dist/index.d.ts CHANGED
@@ -128,6 +128,19 @@ type BindedVertex<T> = T & {
128
128
  * Promote transient properties to persistent.
129
129
  */
130
130
  commitTransients(): void;
131
+ /** Vertex properties (prefixed with $ to avoid conflicts) */
132
+ $id: string;
133
+ $parentId: string | null;
134
+ $parent: Vertex | undefined;
135
+ $children: Vertex[];
136
+ $childrenIds: string[];
137
+ /** Vertex methods (prefixed with $ to avoid conflicts) */
138
+ $moveTo(parent: Vertex | BindedVertex<any> | string): void;
139
+ $delete(): void;
140
+ $observe(listener: (events: any[]) => void): () => void;
141
+ $observeChildren(listener: (children: Vertex[]) => void): () => void;
142
+ $newChild(props?: Record<string, any> | object | null): Vertex;
143
+ $newNamedChild(name: string, props?: Record<string, any> | object | null): Vertex;
131
144
  };
132
145
  /**
133
146
  * Returns a live object that proxies reads/writes to a vertex.
package/dist/index.js CHANGED
@@ -388,6 +388,8 @@ function toPublicObject(tree, id, internalToPublic) {
388
388
  }
389
389
  var RESERVED_METHOD_USE_TRANSIENT = "useTransient";
390
390
  var RESERVED_METHOD_COMMIT_TRANSIENTS = "commitTransients";
391
+ var VERTEX_PROPS = ["$id", "$parentId", "$parent", "$children", "$childrenIds"];
392
+ var VERTEX_METHODS = ["$moveTo", "$delete", "$observe", "$observeChildren", "$newChild", "$newNamedChild"];
391
393
  function bindVertex(tree, id, schemaOrOptions) {
392
394
  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"));
393
395
  const options = isOptions ? schemaOrOptions : { schema: schemaOrOptions };
@@ -395,6 +397,7 @@ function bindVertex(tree, id, schemaOrOptions) {
395
397
  const aliases = options.aliases ?? defaultAliases;
396
398
  const includeInternalKeys = options.includeInternalKeys ?? false;
397
399
  const { publicToInternal, internalToPublic } = buildAliasMaps(aliases);
400
+ const cachedMethods = /* @__PURE__ */ new Map();
398
401
  return new Proxy({}, {
399
402
  get(_target, prop) {
400
403
  if (prop === RESERVED_METHOD_USE_TRANSIENT) {
@@ -483,6 +486,67 @@ function bindVertex(tree, id, schemaOrOptions) {
483
486
  }
484
487
  };
485
488
  }
489
+ if (typeof prop === "string") {
490
+ if (prop === "$id") return id;
491
+ if (prop === "$parentId") return tree.getVertex(id)?.parentId ?? null;
492
+ if (prop === "$parent") {
493
+ const vertex = tree.getVertex(id);
494
+ return vertex?.parent;
495
+ }
496
+ if (prop === "$children") return tree.getChildren(id);
497
+ if (prop === "$childrenIds") return tree.getChildrenIds(id);
498
+ if (prop === "$moveTo") {
499
+ if (!cachedMethods.has(prop)) {
500
+ cachedMethods.set(prop, (parent) => {
501
+ const parentId = typeof parent === "object" && parent !== null ? parent.id || parent.$id : parent;
502
+ tree.moveVertex(id, parentId);
503
+ });
504
+ }
505
+ return cachedMethods.get(prop);
506
+ }
507
+ if (prop === "$delete") {
508
+ if (!cachedMethods.has(prop)) {
509
+ cachedMethods.set(prop, () => tree.deleteVertex(id));
510
+ }
511
+ return cachedMethods.get(prop);
512
+ }
513
+ if (prop === "$observe") {
514
+ if (!cachedMethods.has(prop)) {
515
+ cachedMethods.set(prop, (listener) => tree.observe(id, listener));
516
+ }
517
+ return cachedMethods.get(prop);
518
+ }
519
+ if (prop === "$observeChildren") {
520
+ if (!cachedMethods.has(prop)) {
521
+ cachedMethods.set(prop, (listener) => {
522
+ return tree.observe(id, (events) => {
523
+ if (events.some((e) => e.type === "children")) {
524
+ listener(tree.getChildren(id));
525
+ }
526
+ });
527
+ });
528
+ }
529
+ return cachedMethods.get(prop);
530
+ }
531
+ if (prop === "$newChild") {
532
+ if (!cachedMethods.has(prop)) {
533
+ cachedMethods.set(prop, (props) => {
534
+ const vertex = tree.getVertex(id);
535
+ return vertex?.newChild(props);
536
+ });
537
+ }
538
+ return cachedMethods.get(prop);
539
+ }
540
+ if (prop === "$newNamedChild") {
541
+ if (!cachedMethods.has(prop)) {
542
+ cachedMethods.set(prop, (name, props) => {
543
+ const vertex = tree.getVertex(id);
544
+ return vertex?.newNamedChild(name, props);
545
+ });
546
+ }
547
+ return cachedMethods.get(prop);
548
+ }
549
+ }
486
550
  if (typeof prop !== "string") return void 0;
487
551
  const rule = publicToInternal.get(prop);
488
552
  if (rule) {
@@ -496,6 +560,12 @@ function bindVertex(tree, id, schemaOrOptions) {
496
560
  if (prop === RESERVED_METHOD_USE_TRANSIENT || prop === RESERVED_METHOD_COMMIT_TRANSIENTS) {
497
561
  return true;
498
562
  }
563
+ if (VERTEX_PROPS.includes(prop)) {
564
+ return true;
565
+ }
566
+ if (VERTEX_METHODS.includes(prop)) {
567
+ return true;
568
+ }
499
569
  if (schema?.shape && schema.shape[prop]) {
500
570
  const field = schema.shape[prop];
501
571
  if (field.safeParse) {
@@ -526,6 +596,12 @@ function bindVertex(tree, id, schemaOrOptions) {
526
596
  if (prop === RESERVED_METHOD_USE_TRANSIENT || prop === RESERVED_METHOD_COMMIT_TRANSIENTS) {
527
597
  return true;
528
598
  }
599
+ if (VERTEX_PROPS.includes(prop)) {
600
+ return true;
601
+ }
602
+ if (VERTEX_METHODS.includes(prop)) {
603
+ return true;
604
+ }
529
605
  const rule = publicToInternal.get(prop);
530
606
  if (rule) {
531
607
  tree.setVertexProperty(id, rule.internalKey, void 0);