tilia 5.0.0 → 5.1.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 +28 -6
- package/dist/index.cjs +113 -40
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +57 -5
- package/dist/index.mjs +113 -40
- package/dist/index.mjs.map +3 -3
- package/llms-rescript.md +354 -0
- package/llms-typescript.md +370 -0
- package/llms.txt +54 -0
- package/package.json +1 -1
- package/src/Tilia.mjs +115 -39
- package/src/Tilia.res +139 -70
- package/src/Tilia.resi +16 -0
package/src/Tilia.res
CHANGED
|
@@ -181,21 +181,38 @@ let setError: (root, 'a) => unit = %raw(`function (root, e) {
|
|
|
181
181
|
}
|
|
182
182
|
}`)
|
|
183
183
|
|
|
184
|
+
/** Per-proxy reactive state. Same JS object as [node] after [target] is set (see [proxify]). */
|
|
184
185
|
type rec meta<'a> = {
|
|
186
|
+
/** Backing object or array (not the Proxy). */
|
|
185
187
|
target: 'a,
|
|
188
|
+
/** Scheduler root: active observer, batch lock, GC, errors. */
|
|
186
189
|
root: root,
|
|
187
|
-
|
|
188
|
-
// on any change (including in children). The indexKey triggers on changes
|
|
189
|
-
// to "index" (adding or removing children).
|
|
190
|
+
/** Watchers per string key; [indexKey] tracks ownKeys / array length. */
|
|
190
191
|
observed: dict<watchers>,
|
|
191
|
-
|
|
192
|
+
/** Cached child branch meta (nested proxies keyed by property name). */
|
|
192
193
|
proxied: dict<meta<'a>>,
|
|
193
|
-
|
|
194
|
+
/** Per-key clear callbacks for installed computed values. */
|
|
194
195
|
computes: dict<unit => unit>,
|
|
195
|
-
|
|
196
|
+
/** Optional listeners for [changes]; each callback receives the written key string. */
|
|
197
|
+
mutable changes: nullable<Set.t<string => unit>>,
|
|
198
|
+
/** The Proxy wrapping [target]. */
|
|
196
199
|
mutable proxy: 'a,
|
|
197
200
|
}
|
|
198
201
|
|
|
202
|
+
/** Fields closed over by Proxy handlers before [target] and [proxy] exist on the object. */
|
|
203
|
+
type node<'c> = {
|
|
204
|
+
/** See [meta.root]. */
|
|
205
|
+
root: root,
|
|
206
|
+
/** See [meta.observed]. */
|
|
207
|
+
observed: dict<watchers>,
|
|
208
|
+
/** See [meta.proxied]. */
|
|
209
|
+
proxied: dict<meta<'c>>,
|
|
210
|
+
/** See [meta.computes]. */
|
|
211
|
+
computes: dict<unit => unit>,
|
|
212
|
+
/** See [meta.changes]. */
|
|
213
|
+
mutable changes: nullable<Set.t<string => unit>>,
|
|
214
|
+
}
|
|
215
|
+
|
|
199
216
|
type signal<'a> = {mutable value: 'a}
|
|
200
217
|
type readonly<'a> = {data: 'a}
|
|
201
218
|
type setter<'a> = 'a => unit
|
|
@@ -423,10 +440,7 @@ type observerRef = {mutable o: nullable<observer>}
|
|
|
423
440
|
type lastValue<'a> = {mutable v: 'a}
|
|
424
441
|
|
|
425
442
|
let rec set = (
|
|
426
|
-
|
|
427
|
-
observed: dict<watchers>,
|
|
428
|
-
proxied: dict<meta<'c>>,
|
|
429
|
-
computes: dict<unit => unit>,
|
|
443
|
+
node: node<'c>,
|
|
430
444
|
isArray: bool,
|
|
431
445
|
fromComputed: bool,
|
|
432
446
|
target: 'a,
|
|
@@ -450,6 +464,7 @@ let rec set = (
|
|
|
450
464
|
switch Reflect.set(target, key, value) {
|
|
451
465
|
| false => false
|
|
452
466
|
| true =>
|
|
467
|
+
let writeKey = key
|
|
453
468
|
let key = if isArray && key === "length" {
|
|
454
469
|
indexKey
|
|
455
470
|
} else {
|
|
@@ -457,14 +472,13 @@ let rec set = (
|
|
|
457
472
|
}
|
|
458
473
|
|
|
459
474
|
if proxiable {
|
|
460
|
-
Dict.delete(proxied, key)
|
|
475
|
+
Dict.delete(node.proxied, key)
|
|
461
476
|
}
|
|
462
477
|
if !fromComputed {
|
|
463
|
-
//
|
|
464
|
-
switch Dict.get(computes, key) {
|
|
478
|
+
// Not a computed rebuild: clear any existing compute on this key
|
|
479
|
+
switch Dict.get(node.computes, key) {
|
|
465
480
|
| Value(clear) => {
|
|
466
|
-
|
|
467
|
-
Dict.delete(computes, key)
|
|
481
|
+
Dict.delete(node.computes, key)
|
|
468
482
|
clear()
|
|
469
483
|
}
|
|
470
484
|
| _ => ()
|
|
@@ -472,26 +486,32 @@ let rec set = (
|
|
|
472
486
|
}
|
|
473
487
|
switch Typeof.dynamic(value) {
|
|
474
488
|
| Undefined | Null => {
|
|
475
|
-
|
|
489
|
+
if !fromComputed {
|
|
490
|
+
switch node.changes {
|
|
491
|
+
| Value(cbs) => Set.forEach(cbs, cb => cb(writeKey))
|
|
492
|
+
| _ => ()
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
notify(node.root, node.observed, key)
|
|
476
496
|
if !hadKey {
|
|
477
497
|
// new key: trigger index
|
|
478
|
-
notify(root, observed, indexKey)
|
|
498
|
+
notify(node.root, node.observed, indexKey)
|
|
479
499
|
}
|
|
480
500
|
true
|
|
481
501
|
}
|
|
482
502
|
| Value(_) =>
|
|
483
503
|
// Dynamic value
|
|
484
|
-
switch Dict.get(observed, key) {
|
|
504
|
+
switch Dict.get(node.observed, key) {
|
|
485
505
|
| Value(w) if w.state === Pristine && Set.size(w.observers) > 0 => {
|
|
486
506
|
// Computed value is observed: rebuild and notify if changed
|
|
487
507
|
// Put back previous value to detect changes
|
|
488
508
|
ignore(Reflect.set(target, key, prev))
|
|
489
509
|
let compile = callback =>
|
|
490
|
-
compile(
|
|
510
|
+
compile(node, isArray, target, key, callback)
|
|
491
511
|
let setter = v =>
|
|
492
|
-
ignore(set(
|
|
512
|
+
ignore(set(node, isArray, true, target, key, v))
|
|
493
513
|
let v = getValue(compile, setter, value)
|
|
494
|
-
set(
|
|
514
|
+
set(node, isArray, true, target, key, v)
|
|
495
515
|
}
|
|
496
516
|
| _ => true
|
|
497
517
|
}
|
|
@@ -501,10 +521,7 @@ let rec set = (
|
|
|
501
521
|
}
|
|
502
522
|
|
|
503
523
|
and compile = (
|
|
504
|
-
|
|
505
|
-
observed: dict<watchers>,
|
|
506
|
-
proxied: dict<meta<'c>>,
|
|
507
|
-
computes: dict<unit => unit>,
|
|
524
|
+
node: node<'c>,
|
|
508
525
|
isArray: bool,
|
|
509
526
|
target: 'a,
|
|
510
527
|
key: string,
|
|
@@ -526,23 +543,23 @@ and compile = (
|
|
|
526
543
|
lastValue.v = v
|
|
527
544
|
}
|
|
528
545
|
|
|
529
|
-
switch Dict.get(observed, key) {
|
|
546
|
+
switch Dict.get(node.observed, key) {
|
|
530
547
|
| Value(w) if Set.size(w.observers) > 0 =>
|
|
531
548
|
// We have active observers on this key.
|
|
532
549
|
// Rebuild and if the key changed, it will notify.
|
|
533
|
-
ignore(set(
|
|
550
|
+
ignore(set(node, isArray, true, target, key, rebuild()))
|
|
534
551
|
| Value(w) => {
|
|
535
552
|
// No active listeners.
|
|
536
553
|
w.state = Changed
|
|
537
|
-
Dict.delete(observed, key)
|
|
538
|
-
ignore(Dict.delete(proxied, key))
|
|
554
|
+
Dict.delete(node.observed, key)
|
|
555
|
+
ignore(Dict.delete(node.proxied, key))
|
|
539
556
|
ignore(Reflect.set(target, key, compiled))
|
|
540
557
|
}
|
|
541
558
|
| _ => {
|
|
542
559
|
// We do not have any observers: reset value.
|
|
543
560
|
// Make sure the previous proxy (if any) is removed so that it is not
|
|
544
561
|
// served in place of triggering the compute.
|
|
545
|
-
ignore(Dict.delete(proxied, key))
|
|
562
|
+
ignore(Dict.delete(node.proxied, key))
|
|
546
563
|
ignore(Reflect.set(target, key, compiled))
|
|
547
564
|
}
|
|
548
565
|
}
|
|
@@ -559,16 +576,16 @@ and compile = (
|
|
|
559
576
|
} else {
|
|
560
577
|
lastValue.v = curr
|
|
561
578
|
}
|
|
562
|
-
let o: observer = {root, notify, observing: []}
|
|
579
|
+
let o: observer = {root: node.root, notify, observing: []}
|
|
563
580
|
observer.o = Value(o)
|
|
564
581
|
|
|
565
|
-
let previous = root.observer
|
|
566
|
-
root.observer = Value(o)
|
|
582
|
+
let previous = node.root.observer
|
|
583
|
+
node.root.observer = Value(o)
|
|
567
584
|
let v = try {
|
|
568
585
|
callback()
|
|
569
586
|
} catch {
|
|
570
587
|
| _e => {
|
|
571
|
-
setError(root, %raw(`_e`))
|
|
588
|
+
setError(node.root, %raw(`_e`))
|
|
572
589
|
_clear(o)
|
|
573
590
|
switch previous {
|
|
574
591
|
| Value(previous) => _clear(previous)
|
|
@@ -580,14 +597,14 @@ and compile = (
|
|
|
580
597
|
|
|
581
598
|
// We need to reset previous observer before calling setReady so that
|
|
582
599
|
// setReady does not trigger flush.
|
|
583
|
-
root.observer = previous
|
|
600
|
+
node.root.observer = previous
|
|
584
601
|
|
|
585
602
|
// Computed should not rebuild on change.
|
|
586
603
|
|
|
587
604
|
if Array.length(o.observing) === 0 {
|
|
588
605
|
// FIXME: How to clear if the rebuild is observing itself ?
|
|
589
606
|
_clear(o)
|
|
590
|
-
Dict.delete(computes, key)
|
|
607
|
+
Dict.delete(node.computes, key)
|
|
591
608
|
} else {
|
|
592
609
|
_ready(o, false)
|
|
593
610
|
}
|
|
@@ -607,36 +624,30 @@ and compile = (
|
|
|
607
624
|
| _ => ()
|
|
608
625
|
}
|
|
609
626
|
}
|
|
610
|
-
Dict.set(computes, key, clear)
|
|
627
|
+
Dict.set(node.computes, key, clear)
|
|
611
628
|
rebuild()
|
|
612
629
|
}
|
|
613
630
|
|
|
614
631
|
let deleteProperty = (
|
|
615
|
-
|
|
616
|
-
observed: dict<watchers>,
|
|
617
|
-
proxied: dict<meta<'c>>,
|
|
618
|
-
computes: dict<unit => unit>,
|
|
632
|
+
node: node<'c>,
|
|
619
633
|
target: 'a,
|
|
620
634
|
key: string,
|
|
621
635
|
) => {
|
|
622
636
|
let res = Reflect.deleteProperty(target, key)
|
|
623
|
-
Dict.delete(proxied, key)
|
|
624
|
-
switch Dict.get(computes, key) {
|
|
637
|
+
Dict.delete(node.proxied, key)
|
|
638
|
+
switch Dict.get(node.computes, key) {
|
|
625
639
|
| Value(clear) => {
|
|
626
|
-
Dict.delete(computes, key)
|
|
640
|
+
Dict.delete(node.computes, key)
|
|
627
641
|
clear()
|
|
628
642
|
}
|
|
629
643
|
| _ => ()
|
|
630
644
|
}
|
|
631
|
-
notify(root, observed, key)
|
|
645
|
+
notify(node.root, node.observed, key)
|
|
632
646
|
res
|
|
633
647
|
}
|
|
634
648
|
|
|
635
649
|
let rec get = (
|
|
636
|
-
|
|
637
|
-
observed: dict<watchers>,
|
|
638
|
-
proxied: dict<meta<'c>>,
|
|
639
|
-
computes: dict<unit => unit>,
|
|
650
|
+
node: node<'c>,
|
|
640
651
|
meta: meta<'a>,
|
|
641
652
|
isArray: bool,
|
|
642
653
|
target: 'a,
|
|
@@ -651,35 +662,34 @@ let rec get = (
|
|
|
651
662
|
} else if key === dynamicKey {
|
|
652
663
|
%raw(`undefined`)
|
|
653
664
|
} else {
|
|
654
|
-
// is array and get length
|
|
655
665
|
let value = Reflect.get(target, key)
|
|
656
666
|
let own = Object.hasOwn(target, key)
|
|
657
667
|
if value === undefined || own {
|
|
658
|
-
switch root.observer {
|
|
668
|
+
switch node.root.observer {
|
|
659
669
|
| Value(o) =>
|
|
660
670
|
if isArray && key == "length" {
|
|
661
|
-
let w = observeKey(observed, indexKey)
|
|
671
|
+
let w = observeKey(node.observed, indexKey)
|
|
662
672
|
Array.push(o.observing, w)
|
|
663
673
|
} else {
|
|
664
|
-
let w = observeKey(observed, key)
|
|
674
|
+
let w = observeKey(node.observed, key)
|
|
665
675
|
Array.push(o.observing, w)
|
|
666
676
|
}
|
|
667
677
|
| _ => ()
|
|
668
678
|
}
|
|
669
679
|
|
|
670
680
|
if Typeof.proxiable(value) && !Object.readonly(target, key) {
|
|
671
|
-
switch Dict.get(proxied, key) {
|
|
681
|
+
switch Dict.get(node.proxied, key) {
|
|
672
682
|
| Value(m) => m.proxy
|
|
673
683
|
| _ => {
|
|
674
684
|
let compile = callback =>
|
|
675
|
-
compile(
|
|
685
|
+
compile(node, isArray, target, key, callback)
|
|
676
686
|
let setter = v =>
|
|
677
|
-
ignore(set(
|
|
687
|
+
ignore(set(node, isArray, true, target, key, v))
|
|
678
688
|
let v = getValue(compile, setter, value)
|
|
679
689
|
ignore(Reflect.set(target, key, v))
|
|
680
690
|
if Typeof.proxiable(v) {
|
|
681
|
-
let m = proxify(root, v)
|
|
682
|
-
Dict.set(proxied, key, m)
|
|
691
|
+
let m = proxify(node.root, v)
|
|
692
|
+
Dict.set(node.proxied, key, m)
|
|
683
693
|
m.proxy
|
|
684
694
|
} else {
|
|
685
695
|
v
|
|
@@ -698,24 +708,26 @@ let rec get = (
|
|
|
698
708
|
}
|
|
699
709
|
|
|
700
710
|
and proxify = (root: root, target: 'a): meta<'a> => {
|
|
701
|
-
let proxied: dict<meta<'b>> = Dict.make()
|
|
702
|
-
let observed: observed = Dict.make()
|
|
703
|
-
let computes: dict<unit => unit> = Dict.make()
|
|
704
|
-
|
|
705
711
|
switch _meta(target) {
|
|
706
|
-
| Value(m) if m.root === root =>
|
|
707
|
-
|
|
708
|
-
| Value(m) => proxify(root, m.target) /* external tree: clear */
|
|
712
|
+
| Value(m) if m.root === root => m
|
|
713
|
+
| Value(m) => proxify(root, m.target)
|
|
709
714
|
| _ =>
|
|
710
|
-
let
|
|
715
|
+
let node: node<'b> = {
|
|
716
|
+
root,
|
|
717
|
+
observed: Dict.make(),
|
|
718
|
+
proxied: Dict.make(),
|
|
719
|
+
computes: Dict.make(),
|
|
720
|
+
changes: Undefined,
|
|
721
|
+
}
|
|
722
|
+
let meta: meta<'a> = %raw(`(node.target = target, node)`)
|
|
711
723
|
let isArray = Typeof.array(target)
|
|
712
724
|
let proxy = Proxy.make(
|
|
713
725
|
target,
|
|
714
726
|
{
|
|
715
|
-
"set": set(
|
|
716
|
-
"deleteProperty": deleteProperty(
|
|
717
|
-
"get": get(
|
|
718
|
-
"ownKeys": ownKeys(root, observed, ...),
|
|
727
|
+
"set": set(node, isArray, false, ...),
|
|
728
|
+
"deleteProperty": deleteProperty(node, ...),
|
|
729
|
+
"get": get(node, meta, isArray, ...),
|
|
730
|
+
"ownKeys": ownKeys(node.root, node.observed, ...),
|
|
719
731
|
},
|
|
720
732
|
)
|
|
721
733
|
meta.proxy = proxy
|
|
@@ -996,6 +1008,63 @@ let readonly = (data: 'a) => {
|
|
|
996
1008
|
|
|
997
1009
|
let lift = s => computed(() => s.value)
|
|
998
1010
|
|
|
1011
|
+
type counter = {mutable changed: int}
|
|
1012
|
+
|
|
1013
|
+
let drain: Set.t<string> => array<string> = %raw(`function(s) {
|
|
1014
|
+
var a = Array.from(s);
|
|
1015
|
+
s.clear();
|
|
1016
|
+
return a;
|
|
1017
|
+
}`)
|
|
1018
|
+
|
|
1019
|
+
let emptyKeys: array<string> = []
|
|
1020
|
+
|
|
1021
|
+
let changed = (obj, ~guard=?) => {
|
|
1022
|
+
switch _meta(obj) {
|
|
1023
|
+
| Value(meta) => {
|
|
1024
|
+
let cbs = switch meta.changes {
|
|
1025
|
+
| Value(cbs) => cbs
|
|
1026
|
+
| _ =>
|
|
1027
|
+
let cbs = Set.make()
|
|
1028
|
+
meta.changes = Value(cbs)
|
|
1029
|
+
cbs
|
|
1030
|
+
}
|
|
1031
|
+
let keys: Set.t<string> = Set.make()
|
|
1032
|
+
let counter: counter = proxify(meta.root, {changed: 0}).proxy
|
|
1033
|
+
let cb = key => {
|
|
1034
|
+
Set.add(keys, key)
|
|
1035
|
+
counter.changed = counter.changed + 1
|
|
1036
|
+
}
|
|
1037
|
+
Set.add(cbs, cb)
|
|
1038
|
+
let read = () => ignore(Reflect.get(counter, "changed"))
|
|
1039
|
+
switch guard {
|
|
1040
|
+
| Some(guard) =>
|
|
1041
|
+
() => {
|
|
1042
|
+
if !guard() {
|
|
1043
|
+
emptyKeys
|
|
1044
|
+
} else {
|
|
1045
|
+
read()
|
|
1046
|
+
if Set.size(keys) === 0 {
|
|
1047
|
+
emptyKeys
|
|
1048
|
+
} else {
|
|
1049
|
+
drain(keys)
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
| None =>
|
|
1054
|
+
() => {
|
|
1055
|
+
read()
|
|
1056
|
+
if Set.size(keys) === 0 {
|
|
1057
|
+
emptyKeys
|
|
1058
|
+
} else {
|
|
1059
|
+
drain(keys)
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
| _ => raise("changed: argument is not a tilia proxy")
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
|
|
999
1068
|
let tilia = _ctx.tilia
|
|
1000
1069
|
let carve = _ctx.carve
|
|
1001
1070
|
let observe = _ctx.observe
|
package/src/Tilia.resi
CHANGED
|
@@ -245,6 +245,22 @@ let source: ('a, ('a, 'a => unit) => 'ignored) => 'a
|
|
|
245
245
|
*/
|
|
246
246
|
let store: (('a => unit) => 'a) => 'a
|
|
247
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Track key-level writes on a tilia-proxied object.
|
|
250
|
+
*
|
|
251
|
+
* Returns a capture function for use with `watch`. Each call creates an
|
|
252
|
+
* independent accumulator. Written keys are collected internally and drained
|
|
253
|
+
* (returned and cleared) when the capture is read by `watch`.
|
|
254
|
+
*
|
|
255
|
+
* When a `guard` is provided and returns false, keys accumulate silently
|
|
256
|
+
* without triggering the watcher. When the guard becomes true, the
|
|
257
|
+
* effect fires with all the accumulated keys.
|
|
258
|
+
*
|
|
259
|
+
* @param obj The tilia-proxied object to track.
|
|
260
|
+
* @param guard Optional reactive guard function.
|
|
261
|
+
*/
|
|
262
|
+
let changed: ('a, ~guard: unit => bool=?) => unit => array<string>
|
|
263
|
+
|
|
248
264
|
/** ---------- Internal types and functions for library developers ---------- */
|
|
249
265
|
/**
|
|
250
266
|
* Internal: Register an observer callback.
|