tilia 5.2.0 → 6.0.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 +261 -57
- package/dist/index.cjs +89 -153
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +176 -63
- package/dist/index.mjs +89 -153
- package/dist/index.mjs.map +3 -3
- package/llms.txt +127 -59
- package/package.json +2 -2
- package/rescript.json +1 -1
- package/src/Tilia.mjs +89 -152
- package/src/Tilia.res +80 -146
- package/src/Tilia.resi +24 -29
- package/llms-rescript.md +0 -387
- package/llms-typescript.md +0 -403
package/dist/index.d.ts
CHANGED
|
@@ -1,115 +1,228 @@
|
|
|
1
1
|
declare const o: unique symbol;
|
|
2
|
-
declare const r: unique symbol;
|
|
3
2
|
export type Observer = { readonly [o]: true };
|
|
3
|
+
/** A reactive holder for a single value, read and written through `value`. */
|
|
4
4
|
export type Signal<T> = { value: T };
|
|
5
|
+
/** An immutable holder: the `data` field is not tracked nor proxied. */
|
|
5
6
|
export type Readonly<T> = { readonly data: T };
|
|
7
|
+
/** Imperative update function returned alongside signals and passed to setups. */
|
|
6
8
|
export type Setter<T> = (v: T) => void;
|
|
7
|
-
|
|
9
|
+
/** Stop function returned by `observe` and `watch`. Calling it cancels the subscription. */
|
|
10
|
+
export type Cancel = () => void;
|
|
11
|
+
/** Helper passed to `carve` to declare values derived from the object under construction. */
|
|
12
|
+
export type Deriver<U> = {
|
|
13
|
+
/**
|
|
14
|
+
* Declare a derived (computed) field inside a `carve` definition. Works like
|
|
15
|
+
* `computed`, but the computation receives the carved object itself, so a
|
|
16
|
+
* field can depend on sibling fields.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const alice = carve(({ derived }) => ({
|
|
20
|
+
* birthYear: 1995,
|
|
21
|
+
* age: derived((self) => thisYear - self.birthYear),
|
|
22
|
+
* }));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
derived: <T>(fn: (p: U) => T) => T;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** A tilia context: an isolated reactive scope with its own tracking and scheduling. */
|
|
8
29
|
export type Tilia = {
|
|
9
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Transform a regular object or array into a reactive tilia proxy.
|
|
32
|
+
*
|
|
33
|
+
* Reads are tracked per key, including nested objects and arrays, so
|
|
34
|
+
* observers only re-run when a key they actually read changes. Calling
|
|
35
|
+
* `tilia` twice on the same object returns the same proxy.
|
|
36
|
+
*/
|
|
10
37
|
tilia: <T>(branch: T) => T;
|
|
11
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Build a reactive object whose derived fields can read the object itself.
|
|
40
|
+
* The callback receives a {@link Deriver} and must return the object to
|
|
41
|
+
* proxy. Use this to co-locate state and computations in one definition.
|
|
42
|
+
*/
|
|
12
43
|
carve: <T>(fn: (deriver: Deriver<T>) => T) => T;
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Run `fn` now and re-run it whenever any tilia value it read changes
|
|
46
|
+
* (push reactivity).
|
|
47
|
+
*
|
|
48
|
+
* @returns A function that stops the observation.
|
|
49
|
+
*/
|
|
50
|
+
observe: (fn: () => void) => Cancel;
|
|
51
|
+
/**
|
|
52
|
+
* React to changes with a clear separation between reading and acting:
|
|
53
|
+
* `fn` captures (reads) values and returns a result; `effect` runs with
|
|
54
|
+
* that result whenever a captured value changes. Unlike `observe`, the
|
|
55
|
+
* effect itself is not tracked, so it can freely read other reactive state.
|
|
56
|
+
*
|
|
57
|
+
* @returns A function that stops watching.
|
|
58
|
+
*/
|
|
59
|
+
watch: <T>(fn: () => T, effect: (v: T) => void) => Cancel;
|
|
60
|
+
/**
|
|
61
|
+
* Run several mutations as one batch: notifications are deferred until the
|
|
62
|
+
* batch completes, so observers see a single consistent update. Not needed
|
|
63
|
+
* inside tilia callbacks (`observe`, `computed`, ...) where batching is
|
|
64
|
+
* already active.
|
|
65
|
+
*/
|
|
18
66
|
batch: (fn: () => void) => void;
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Wrap a single value in a reactive {@link Signal}. Returns the signal and
|
|
69
|
+
* a setter: read with `s.value`, write with the setter.
|
|
70
|
+
*/
|
|
71
|
+
signal: <T>(value: T) => [Signal<T>, Setter<T>];
|
|
72
|
+
/**
|
|
73
|
+
* Create a signal whose value is computed from other reactive values and
|
|
74
|
+
* recomputed when they change.
|
|
75
|
+
*/
|
|
22
76
|
derived: <T>(fn: () => T) => Signal<T>;
|
|
23
77
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
78
|
+
* Create a reactive source value to insert into a tilia object. The setup
|
|
79
|
+
* runs on first read and re-runs when any tilia value it read changes. It
|
|
80
|
+
* receives the previous value and a setter for imperative (possibly async)
|
|
81
|
+
* updates; `initialValue` is used until the first `set`.
|
|
82
|
+
*
|
|
83
|
+
* Useful for resource loaders, state machines, or any state driven by
|
|
84
|
+
* external or asynchronous events.
|
|
26
85
|
*/
|
|
27
86
|
source: <T>(initialValue: T, fn: (previous: T, set: Setter<T>) => unknown) => T;
|
|
28
87
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
88
|
+
* Create a managed value to insert into a tilia object. The setup runs on
|
|
89
|
+
* first read and re-runs when any tilia value it read changes. It receives
|
|
90
|
+
* a setter for later imperative updates and must return the initial value.
|
|
91
|
+
*
|
|
92
|
+
* Like `source`, but the initial value is produced by the setup itself.
|
|
31
93
|
*/
|
|
32
94
|
store: <T>(fn: (set: Setter<T>) => T) => T;
|
|
33
|
-
/**
|
|
34
|
-
changing: <T>(accessor: () => Record<string, T>, guard?: () => boolean) => Changing<T>;
|
|
35
|
-
/** @internal */
|
|
95
|
+
/** @internal Register a raw observer (library authors; see `_observe`). */
|
|
36
96
|
_observe(callback: () => void): Observer;
|
|
37
97
|
};
|
|
38
98
|
|
|
39
99
|
/**
|
|
40
100
|
* Create a new tilia context with its own `tilia`, `observe`, `batch`, etc.
|
|
41
|
-
*
|
|
101
|
+
* State from different contexts is tracked and flushed independently.
|
|
102
|
+
*
|
|
103
|
+
* @param gc Number of cleared watchers to keep before triggering garbage
|
|
104
|
+
* collection (default: 50).
|
|
42
105
|
*/
|
|
43
|
-
export function make(
|
|
106
|
+
export function make(gc?: number): Tilia;
|
|
44
107
|
|
|
45
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Transform a regular object or array into a reactive tilia proxy (in the
|
|
110
|
+
* default context).
|
|
111
|
+
*
|
|
112
|
+
* Reads are tracked per key, including nested objects and arrays, so
|
|
113
|
+
* observers only re-run when a key they actually read changes. Calling
|
|
114
|
+
* `tilia` twice on the same object returns the same proxy.
|
|
115
|
+
*/
|
|
46
116
|
export function tilia<T>(branch: T): T;
|
|
47
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Build a reactive object whose derived fields can read the object itself.
|
|
119
|
+
* The callback receives a {@link Deriver} and must return the object to
|
|
120
|
+
* proxy. Use this to co-locate state and computations in one definition.
|
|
121
|
+
*/
|
|
48
122
|
export function carve<T>(fn: (deriver: Deriver<T>) => T): T;
|
|
49
|
-
/** Register a callback to re-run whenever any observed value changes (push reactivity). */
|
|
50
|
-
export function observe(fn: () => void): void;
|
|
51
123
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
124
|
+
* Run `fn` now and re-run it whenever any tilia value it read changes
|
|
125
|
+
* (push reactivity). For pull reactivity (recompute only when read), see
|
|
126
|
+
* `computed`.
|
|
127
|
+
*
|
|
128
|
+
* @returns A function that stops the observation.
|
|
129
|
+
*/
|
|
130
|
+
export function observe(fn: () => void): Cancel;
|
|
131
|
+
/**
|
|
132
|
+
* React to changes with a clear separation between reading and acting:
|
|
133
|
+
* `fn` captures (reads) values and returns a result; `effect` runs with that
|
|
134
|
+
* result whenever a captured value changes. Unlike `observe`, the effect
|
|
135
|
+
* itself is not tracked, so it can freely read other reactive state.
|
|
136
|
+
*
|
|
137
|
+
* The effect should avoid synchronously mutating the values it captures, to
|
|
138
|
+
* prevent recursive updates; use `observe` for such feedback loops.
|
|
139
|
+
*
|
|
140
|
+
* @returns A function that stops watching.
|
|
141
|
+
*/
|
|
142
|
+
export function watch<T>(fn: () => T, effect: (v: T) => void): Cancel;
|
|
143
|
+
/**
|
|
144
|
+
* Run several mutations as one batch: notifications are deferred until the
|
|
145
|
+
* batch completes, so observers see a single consistent update. Not needed
|
|
146
|
+
* inside tilia callbacks (`observe`, `computed`, ...) where batching is
|
|
147
|
+
* already active.
|
|
54
148
|
*/
|
|
55
|
-
export function watch<T>(fn: () => T, effect: (v: T) => void): void;
|
|
56
|
-
/** Run operations in a batch, blocking notifications until the batch completes. */
|
|
57
149
|
export function batch(fn: () => void): void;
|
|
58
150
|
|
|
59
|
-
/**
|
|
151
|
+
/**
|
|
152
|
+
* Create a computed value to insert into a tilia object (pull reactivity).
|
|
153
|
+
* The value is computed on first read, cached, and invalidated when any tilia
|
|
154
|
+
* value it read changes; it is only recomputed on the next read.
|
|
155
|
+
*/
|
|
60
156
|
export function computed<T>(fn: () => T): T;
|
|
61
157
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
158
|
+
* Create a reactive source value to insert into a tilia object. The setup
|
|
159
|
+
* runs on first read and re-runs when any tilia value it read changes. It
|
|
160
|
+
* receives the previous value and a setter for imperative (possibly async)
|
|
161
|
+
* updates; `initialValue` is used until the first `set`.
|
|
162
|
+
*
|
|
163
|
+
* Useful for resource loaders, state machines, or any state driven by
|
|
164
|
+
* external or asynchronous events.
|
|
64
165
|
*/
|
|
65
166
|
export function source<T>(
|
|
66
167
|
initialValue: T,
|
|
67
168
|
fn: (previous: T, set: Setter<T>) => unknown
|
|
68
169
|
): T;
|
|
69
170
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
171
|
+
* Create a managed value to insert into a tilia object. The setup runs on
|
|
172
|
+
* first read and re-runs when any tilia value it read changes. It receives a
|
|
173
|
+
* setter for later imperative updates and must return the initial value.
|
|
174
|
+
*
|
|
175
|
+
* Like `source`, but the initial value is produced by the setup itself.
|
|
72
176
|
*/
|
|
73
177
|
export function store<T>(fn: (set: Setter<T>) => T): T;
|
|
74
|
-
/**
|
|
178
|
+
/**
|
|
179
|
+
* Wrap a value in a {@link Readonly} holder. The wrapped data is not proxied
|
|
180
|
+
* nor tracked: use this to insert immutable or foreign objects (class
|
|
181
|
+
* instances, large blobs) into a tilia object without reactivity overhead.
|
|
182
|
+
*/
|
|
75
183
|
export function readonly<T>(data: T): Readonly<T>;
|
|
76
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* Wrap a single value in a reactive {@link Signal}. Returns the signal and a
|
|
186
|
+
* setter: read with `s.value`, write with the setter.
|
|
187
|
+
*/
|
|
77
188
|
export function signal<T>(value: T): [Signal<T>, Setter<T>];
|
|
78
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* Create a signal whose value is computed from other reactive values and
|
|
191
|
+
* recomputed when they change.
|
|
192
|
+
*/
|
|
79
193
|
export function derived<T>(fn: () => T): Signal<T>;
|
|
80
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* Lift a signal into a computed value to insert into a tilia object: the
|
|
196
|
+
* field tracks the signal's inner `value`.
|
|
197
|
+
*
|
|
198
|
+
* ```ts
|
|
199
|
+
* const app = tilia({ user: lift(userSignal) });
|
|
200
|
+
* app.user; // stays in sync with userSignal.value
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
81
203
|
export function lift<T>(s: Signal<T>): T;
|
|
82
|
-
export interface Changes<T> {
|
|
83
|
-
upsert: T[];
|
|
84
|
-
remove: string[];
|
|
85
|
-
}
|
|
86
|
-
export interface Changing<T> {
|
|
87
|
-
/** Capture function for `watch`. Drains accumulated changes into `{ upsert, remove }`. */
|
|
88
|
-
changes: () => Changes<T>;
|
|
89
|
-
/** Run a callback with tracking temporarily removed. Includes `batch`. */
|
|
90
|
-
mute: (fn: () => void) => void;
|
|
91
|
-
}
|
|
92
204
|
|
|
205
|
+
/** @internal Keys with observers (`live`) versus keys without (`idle`). */
|
|
206
|
+
export type Canopy = { live: Set<string>; idle: Set<string> };
|
|
207
|
+
/** @internal Inspect which keys of a tilia proxy are observed. */
|
|
208
|
+
export function _canopy<T extends object>(tree: T): Canopy;
|
|
93
209
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* `
|
|
97
|
-
* write time, `remove` contains keys of deleted entries. Last write wins per key.
|
|
98
|
-
* `mute` runs code without tracking (for inbound sync writes).
|
|
99
|
-
* Each call creates an independent accumulator. When `guard` returns false,
|
|
100
|
-
* changes accumulate silently; when it becomes true, all accumulated changes drain.
|
|
210
|
+
* @internal Register `callback` as an observer and start recording reads.
|
|
211
|
+
* Entry point for library authors building on tilia; pair with `_done` /
|
|
212
|
+
* `_ready` / `_clear`.
|
|
101
213
|
*/
|
|
102
|
-
export function changing<T>(accessor: () => Record<string, T>, guard?: () => boolean): Changing<T>;
|
|
103
|
-
|
|
104
|
-
/** @internal */
|
|
105
214
|
export function _observe(callback: () => void): Observer;
|
|
106
|
-
/** @internal */
|
|
215
|
+
/** @internal Stop recording reads for `observer` without subscribing it. */
|
|
107
216
|
export function _done(observer: Observer): void;
|
|
108
|
-
/**
|
|
217
|
+
/**
|
|
218
|
+
* @internal Stop recording and subscribe the observer to the recorded reads.
|
|
219
|
+
* If `notifyIfChanged` is true, notify immediately when a read value already
|
|
220
|
+
* changed during recording.
|
|
221
|
+
*/
|
|
109
222
|
export function _ready(observer: Observer, notifyIfChanged?: boolean): void;
|
|
110
|
-
/** @internal */
|
|
223
|
+
/** @internal Dispose of an observer that was not notified (notification disposes automatically). */
|
|
111
224
|
export function _clear(observer: Observer): void;
|
|
112
|
-
/** @internal */
|
|
225
|
+
/** @internal Get meta information on a tilia proxy (raw target, root, etc.). */
|
|
113
226
|
export function _meta<T>(tree: T): unknown;
|
|
114
|
-
/** @internal */
|
|
227
|
+
/** @internal The default tilia context (used by the top-level exports). */
|
|
115
228
|
export const _ctx: Tilia;
|