reptree 0.4.0-alpha.0 → 0.4.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 +49 -221
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -14
- package/dist/index.d.ts +41 -14
- package/dist/index.js +49 -221
- package/dist/index.js.map +1 -1
- package/package.json +1 -4
package/dist/index.cjs
CHANGED
|
@@ -434,168 +434,39 @@ function bindVertex(tree, id, schemaOrOptions) {
|
|
|
434
434
|
const options = isOptions ? schemaOrOptions : { schema: schemaOrOptions };
|
|
435
435
|
const schema = options.schema;
|
|
436
436
|
const aliases = options.aliases ?? defaultAliases;
|
|
437
|
-
const { publicToInternal
|
|
437
|
+
const { publicToInternal } = buildAliasMaps(aliases);
|
|
438
438
|
const obj = {};
|
|
439
|
-
let isObserverUpdate = false;
|
|
440
|
-
const propsToDefine = /* @__PURE__ */ new Set();
|
|
441
|
-
if (schema?.shape) {
|
|
442
|
-
Object.keys(schema.shape).forEach((key) => propsToDefine.add(key));
|
|
443
|
-
aliases.forEach((alias) => propsToDefine.add(alias.publicKey));
|
|
444
|
-
}
|
|
445
|
-
const localValues = /* @__PURE__ */ new Map();
|
|
446
|
-
propsToDefine.forEach((publicKey) => {
|
|
447
|
-
const rule = publicToInternal.get(publicKey);
|
|
448
|
-
const internalKey = rule?.internalKey ?? publicKey;
|
|
449
|
-
const rawValue = tree.getVertexProperty(id, internalKey);
|
|
450
|
-
const publicValue = rule?.toPublic ? rule.toPublic(rawValue) : rawValue;
|
|
451
|
-
localValues.set(publicKey, publicValue);
|
|
452
|
-
});
|
|
453
|
-
propsToDefine.forEach((publicKey) => {
|
|
454
|
-
const rule = publicToInternal.get(publicKey);
|
|
455
|
-
const storageKey = `_${publicKey}_value`;
|
|
456
|
-
obj[storageKey] = localValues.get(publicKey);
|
|
457
|
-
Object.defineProperty(obj, publicKey, {
|
|
458
|
-
get: function() {
|
|
459
|
-
const internalKey = rule?.internalKey ?? publicKey;
|
|
460
|
-
const rawValue = tree.getVertexProperty(id, internalKey, true);
|
|
461
|
-
const publicValue = rule?.toPublic ? rule.toPublic(rawValue) : rawValue;
|
|
462
|
-
if (this[storageKey] !== publicValue) {
|
|
463
|
-
this[storageKey] = publicValue;
|
|
464
|
-
}
|
|
465
|
-
return publicValue;
|
|
466
|
-
},
|
|
467
|
-
set: function(value) {
|
|
468
|
-
if (schema?.shape && schema.shape[publicKey]) {
|
|
469
|
-
const field = schema.shape[publicKey];
|
|
470
|
-
if (field.safeParse) {
|
|
471
|
-
const res = field.safeParse(value);
|
|
472
|
-
if (!res.success) throw new Error(`Invalid value for ${publicKey}`);
|
|
473
|
-
value = res.data;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
this[storageKey] = value;
|
|
477
|
-
if (!isObserverUpdate) {
|
|
478
|
-
const internalKey = rule?.internalKey ?? publicKey;
|
|
479
|
-
const internalValue = rule?.toInternal ? rule.toInternal(value) : value;
|
|
480
|
-
tree.setVertexProperty(id, internalKey, internalValue);
|
|
481
|
-
}
|
|
482
|
-
},
|
|
483
|
-
enumerable: true,
|
|
484
|
-
configurable: true
|
|
485
|
-
});
|
|
486
|
-
});
|
|
487
|
-
const cachedMethods = /* @__PURE__ */ new Map();
|
|
488
|
-
const getCachedMethod = (name, factory) => {
|
|
489
|
-
if (!cachedMethods.has(name)) {
|
|
490
|
-
cachedMethods.set(name, factory());
|
|
491
|
-
}
|
|
492
|
-
return cachedMethods.get(name);
|
|
493
|
-
};
|
|
494
439
|
Object.defineProperties(obj, {
|
|
495
|
-
$id:
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
configurable: true
|
|
502
|
-
},
|
|
503
|
-
$parentId: {
|
|
504
|
-
get: () => tree.getVertex(id)?.parentId ?? null,
|
|
505
|
-
set: () => {
|
|
506
|
-
},
|
|
507
|
-
// No-op setter
|
|
508
|
-
enumerable: false,
|
|
509
|
-
configurable: true
|
|
510
|
-
},
|
|
511
|
-
$parent: {
|
|
512
|
-
get: () => tree.getVertex(id)?.parent,
|
|
513
|
-
set: () => {
|
|
514
|
-
},
|
|
515
|
-
// No-op setter
|
|
516
|
-
enumerable: false,
|
|
517
|
-
configurable: true
|
|
518
|
-
},
|
|
519
|
-
$children: {
|
|
520
|
-
get: () => tree.getChildren(id),
|
|
521
|
-
set: () => {
|
|
522
|
-
},
|
|
523
|
-
// No-op setter
|
|
524
|
-
enumerable: false,
|
|
525
|
-
configurable: true
|
|
526
|
-
},
|
|
527
|
-
$childrenIds: {
|
|
528
|
-
get: () => tree.getChildrenIds(id),
|
|
529
|
-
set: () => {
|
|
530
|
-
},
|
|
531
|
-
// No-op setter
|
|
532
|
-
enumerable: false,
|
|
533
|
-
configurable: true
|
|
534
|
-
},
|
|
440
|
+
$vertex: { get: () => tree.getVertex(id), enumerable: false, configurable: true },
|
|
441
|
+
$id: { get: () => id, enumerable: false, configurable: true },
|
|
442
|
+
$parentId: { get: () => tree.getVertex(id)?.parentId ?? null, enumerable: false, configurable: true },
|
|
443
|
+
$parent: { get: () => tree.getVertex(id)?.parent, enumerable: false, configurable: true },
|
|
444
|
+
$children: { get: () => tree.getChildren(id), enumerable: false, configurable: true },
|
|
445
|
+
$childrenIds: { get: () => tree.getChildrenIds(id), enumerable: false, configurable: true },
|
|
535
446
|
$moveTo: {
|
|
536
|
-
|
|
447
|
+
value: (parent) => {
|
|
537
448
|
const parentId = typeof parent === "object" && parent !== null ? parent.id || parent.$id : parent;
|
|
538
449
|
tree.moveVertex(id, parentId);
|
|
539
|
-
}),
|
|
540
|
-
set: () => {
|
|
541
450
|
},
|
|
542
|
-
// No-op setter
|
|
543
451
|
enumerable: false,
|
|
544
|
-
configurable: true
|
|
545
|
-
|
|
546
|
-
$delete: {
|
|
547
|
-
get: () => getCachedMethod("$delete", () => () => tree.deleteVertex(id)),
|
|
548
|
-
set: () => {
|
|
549
|
-
},
|
|
550
|
-
// No-op setter
|
|
551
|
-
enumerable: false,
|
|
552
|
-
configurable: true
|
|
553
|
-
},
|
|
554
|
-
$observe: {
|
|
555
|
-
get: () => getCachedMethod("$observe", () => (listener) => tree.observe(id, listener)),
|
|
556
|
-
set: () => {
|
|
557
|
-
},
|
|
558
|
-
// No-op setter
|
|
559
|
-
enumerable: false,
|
|
560
|
-
configurable: true
|
|
452
|
+
configurable: true,
|
|
453
|
+
writable: false
|
|
561
454
|
},
|
|
455
|
+
$delete: { value: () => tree.deleteVertex(id), enumerable: false, configurable: true, writable: false },
|
|
456
|
+
$observe: { value: (listener) => tree.observe(id, listener), enumerable: false, configurable: true, writable: false },
|
|
562
457
|
$observeChildren: {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
}
|
|
568
|
-
});
|
|
458
|
+
value: (listener) => tree.observe(id, (events) => {
|
|
459
|
+
if (events.some((e) => e.type === "children")) {
|
|
460
|
+
listener(tree.getChildren(id));
|
|
461
|
+
}
|
|
569
462
|
}),
|
|
570
|
-
set: () => {
|
|
571
|
-
},
|
|
572
|
-
// No-op setter
|
|
573
463
|
enumerable: false,
|
|
574
|
-
configurable: true
|
|
464
|
+
configurable: true,
|
|
465
|
+
writable: false
|
|
575
466
|
},
|
|
576
|
-
$newChild: {
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
return vertex?.newChild(props);
|
|
580
|
-
}),
|
|
581
|
-
set: () => {
|
|
582
|
-
},
|
|
583
|
-
// No-op setter
|
|
584
|
-
enumerable: false,
|
|
585
|
-
configurable: true
|
|
586
|
-
},
|
|
587
|
-
$newNamedChild: {
|
|
588
|
-
get: () => getCachedMethod("$newNamedChild", () => (name, props) => {
|
|
589
|
-
const vertex = tree.getVertex(id);
|
|
590
|
-
return vertex?.newNamedChild(name, props);
|
|
591
|
-
}),
|
|
592
|
-
set: () => {
|
|
593
|
-
},
|
|
594
|
-
// No-op setter
|
|
595
|
-
enumerable: false,
|
|
596
|
-
configurable: true
|
|
597
|
-
},
|
|
598
|
-
useTransient: {
|
|
467
|
+
$newChild: { value: (props) => tree.getVertex(id).newChild(props), enumerable: false, configurable: true, writable: false },
|
|
468
|
+
$newNamedChild: { value: (name, props) => tree.getVertex(id).newNamedChild(name, props), enumerable: false, configurable: true, writable: false },
|
|
469
|
+
$useTransients: {
|
|
599
470
|
value: function(fn) {
|
|
600
471
|
const transientProxy = new Proxy({}, {
|
|
601
472
|
set(_, prop, value) {
|
|
@@ -618,103 +489,60 @@ function bindVertex(tree, id, schemaOrOptions) {
|
|
|
618
489
|
fn(transientProxy);
|
|
619
490
|
},
|
|
620
491
|
enumerable: false,
|
|
621
|
-
configurable: true
|
|
622
|
-
|
|
623
|
-
commitTransients: {
|
|
624
|
-
value: function() {
|
|
625
|
-
tree.commitTransients(id);
|
|
626
|
-
},
|
|
627
|
-
enumerable: false,
|
|
628
|
-
configurable: true
|
|
492
|
+
configurable: true,
|
|
493
|
+
writable: false
|
|
629
494
|
},
|
|
495
|
+
$commitTransients: { value: () => tree.commitTransients(id), enumerable: false, configurable: true, writable: false },
|
|
630
496
|
equals: {
|
|
631
497
|
value: function(other) {
|
|
632
498
|
if (other && typeof other === "object" && "$id" in other) {
|
|
633
499
|
return other.$id === id;
|
|
634
500
|
}
|
|
635
|
-
return
|
|
501
|
+
return false;
|
|
636
502
|
},
|
|
637
503
|
enumerable: false,
|
|
638
|
-
configurable: true
|
|
504
|
+
configurable: true,
|
|
505
|
+
writable: false
|
|
639
506
|
}
|
|
640
507
|
});
|
|
641
|
-
tree.observe(id, (events) => {
|
|
642
|
-
isObserverUpdate = true;
|
|
643
|
-
for (const e of events) {
|
|
644
|
-
if (e.type === "property") {
|
|
645
|
-
const propEvent = e;
|
|
646
|
-
const rule = internalToPublic.get(propEvent.key);
|
|
647
|
-
if (rule) {
|
|
648
|
-
const publicKey = rule.publicKey;
|
|
649
|
-
const publicValue = rule.toPublic ? rule.toPublic(propEvent.value) : propEvent.value;
|
|
650
|
-
const storageKey = `_${publicKey}_value`;
|
|
651
|
-
if (storageKey in obj) {
|
|
652
|
-
obj[storageKey] = publicValue;
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
isObserverUpdate = false;
|
|
658
|
-
});
|
|
659
|
-
if (schema) {
|
|
660
|
-
return obj;
|
|
661
|
-
}
|
|
662
508
|
const proxy = new Proxy(obj, {
|
|
663
|
-
get(target, prop) {
|
|
509
|
+
get(target, prop, receiver) {
|
|
664
510
|
if (typeof prop !== "string") {
|
|
665
|
-
return target
|
|
511
|
+
return Reflect.get(target, prop, receiver);
|
|
666
512
|
}
|
|
667
|
-
if (prop in target
|
|
668
|
-
return target
|
|
513
|
+
if (prop in target) {
|
|
514
|
+
return Reflect.get(target, prop, receiver);
|
|
669
515
|
}
|
|
670
516
|
const rule = publicToInternal.get(prop);
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
const result = rule.toPublic ? rule.toPublic(rawValue2) : rawValue2;
|
|
675
|
-
return result;
|
|
676
|
-
}
|
|
677
|
-
const rawValue = tree.getVertexProperty(id, prop);
|
|
678
|
-
return rawValue;
|
|
517
|
+
const internalKey = rule?.internalKey ?? prop;
|
|
518
|
+
const rawValue = tree.getVertexProperty(id, internalKey, true);
|
|
519
|
+
return rule?.toPublic ? rule.toPublic(rawValue) : rawValue;
|
|
679
520
|
},
|
|
680
521
|
set(target, prop, value) {
|
|
681
522
|
if (typeof prop !== "string") {
|
|
682
|
-
target
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
523
|
+
return Reflect.set(target, prop, value);
|
|
524
|
+
}
|
|
525
|
+
if (schema?.shape && schema.shape[prop]) {
|
|
526
|
+
const field = schema.shape[prop];
|
|
527
|
+
if (field.safeParse) {
|
|
528
|
+
const res = field.safeParse(value);
|
|
529
|
+
if (!res.success) throw new Error(`Invalid value for ${prop}`);
|
|
530
|
+
value = res.data;
|
|
531
|
+
}
|
|
689
532
|
}
|
|
690
533
|
const rule = publicToInternal.get(prop);
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
tree.setVertexProperty(id, internalKey, internalValue);
|
|
695
|
-
return true;
|
|
696
|
-
}
|
|
697
|
-
tree.setVertexProperty(id, prop, value);
|
|
534
|
+
const internalKey = rule?.internalKey ?? prop;
|
|
535
|
+
const internalValue = rule?.toInternal ? rule.toInternal(value) : value;
|
|
536
|
+
tree.setVertexProperty(id, internalKey, internalValue);
|
|
698
537
|
return true;
|
|
699
538
|
},
|
|
700
|
-
deleteProperty(
|
|
539
|
+
deleteProperty(_target, prop) {
|
|
701
540
|
if (typeof prop !== "string") {
|
|
702
|
-
delete target[prop];
|
|
703
541
|
return true;
|
|
704
542
|
}
|
|
705
|
-
const storageKey = `_${prop}_value`;
|
|
706
|
-
if (storageKey in target) {
|
|
707
|
-
delete target[storageKey];
|
|
708
|
-
}
|
|
709
|
-
if (prop in target) {
|
|
710
|
-
delete target[prop];
|
|
711
|
-
}
|
|
712
543
|
const rule = publicToInternal.get(prop);
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
return true;
|
|
716
|
-
}
|
|
717
|
-
tree.setVertexProperty(id, prop, void 0);
|
|
544
|
+
const internalKey = rule?.internalKey ?? prop;
|
|
545
|
+
tree.setVertexProperty(id, internalKey, void 0);
|
|
718
546
|
return true;
|
|
719
547
|
}
|
|
720
548
|
});
|