@hidemikimura/vanji 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +245 -2
- package/compiler/ast.js +48 -0
- package/compiler/index.d.ts +21 -0
- package/compiler/index.js +155 -0
- package/compiler/list.js +163 -0
- package/compiler/parse-html.js +278 -0
- package/compiler/template.js +391 -0
- package/compiler/vite.d.ts +31 -0
- package/compiler/vite.js +35 -0
- package/llms.txt +143 -0
- package/package.json +61 -4
- package/skills/vanji/SKILL.md +231 -0
- package/skills/vanji/references/patterns.md +241 -0
- package/src/dev.js +3 -0
- package/src/dom.js +308 -0
- package/src/index.d.ts +85 -0
- package/src/index.js +17 -0
- package/src/internal.js +4 -0
- package/src/list.js +272 -0
- package/src/signals.js +287 -0
package/src/list.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// Fast keyed list, used by the compiler for `each(items, row => html`<one element>`, { key })`
|
|
2
|
+
// when every hole of the row can be compiled (see compiler/list.js). A row is a plain record:
|
|
3
|
+
// { el, v: item, i: index, s: scope | null, h: visit stamp, p0, p1, ...: last values, ... }
|
|
4
|
+
// - holes that only read the item are re-evaluated when the item object is replaced (same key)
|
|
5
|
+
// - `sig() === row.key ? a : b` is one subscription for the whole list (only 2 rows are touched)
|
|
6
|
+
// - holes that call functions get an effect in the row's scope (s), re-run when the item changes
|
|
7
|
+
// - events of the rows are handled by one capture listener per type on the list's parent element:
|
|
8
|
+
// it runs before the parent's own listeners and sees events that do not bubble
|
|
9
|
+
import { render, scope, runIn, dispose, untrack, onCleanup, rerun } from './signals.js';
|
|
10
|
+
import { place } from './dom.js';
|
|
11
|
+
import { DEV } from './dev.js';
|
|
12
|
+
|
|
13
|
+
const isArray = Array.isArray;
|
|
14
|
+
|
|
15
|
+
/** Text of an element whose only content is one hole. */
|
|
16
|
+
export const text = (el, v) => {
|
|
17
|
+
const t = typeof v;
|
|
18
|
+
if (t === 'string' || t === 'number') {
|
|
19
|
+
const f = el.firstChild;
|
|
20
|
+
if (f !== null && f.nodeType === 3 && f.nextSibling === null) f.data = v;
|
|
21
|
+
else el.textContent = v;
|
|
22
|
+
} else {
|
|
23
|
+
el.textContent = '';
|
|
24
|
+
place(el, v, undefined, null);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const rerunAll = s => {
|
|
29
|
+
const o = s.o;
|
|
30
|
+
if (o === null) return;
|
|
31
|
+
if (isArray(o)) for (let i = 0; i < o.length; i++) rerun(o[i]);
|
|
32
|
+
else rerun(o);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// like a native listener: a throwing handler does not stop the others; the error is still reported
|
|
36
|
+
const call = (h, node, e) => {
|
|
37
|
+
try {
|
|
38
|
+
typeof h === 'function' ? h.call(node, e) : h.handleEvent(e);
|
|
39
|
+
} catch (x) {
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
throw x;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {Element} parent element the rows live in
|
|
48
|
+
* @param {Node | null} marker rows go before it (null: the rows are the parent's only content)
|
|
49
|
+
* @param {() => any[]} items
|
|
50
|
+
* @param {{ t: () => Element, k?: string | ((item: any) => any), c: Function, u: Function,
|
|
51
|
+
* x?: Array<() => any> | null, e?: Record<string, Array<[Function, Function]>> | null, ix?: boolean }} spec
|
|
52
|
+
*/
|
|
53
|
+
export const list = (parent, marker, items, spec) => {
|
|
54
|
+
const make = spec.t, create = spec.c, patch = spec.u, ix = !!spec.ix, k = spec.k;
|
|
55
|
+
const keyOf = k == null ? v => v : typeof k === 'function' ? k : v => v[k];
|
|
56
|
+
const map = new Map(); // key -> row
|
|
57
|
+
let recs = [], prev = [], stamp = 0;
|
|
58
|
+
|
|
59
|
+
const fresh = (item, i) => create(make(), item, i);
|
|
60
|
+
const refresh = (r, item, i) => {
|
|
61
|
+
r.v = item;
|
|
62
|
+
r.i = i;
|
|
63
|
+
patch(r, item, i);
|
|
64
|
+
if (r.s !== null) rerunAll(r.s);
|
|
65
|
+
};
|
|
66
|
+
const drop = r => {
|
|
67
|
+
r.el.remove();
|
|
68
|
+
if (r.s !== null) dispose(r.s);
|
|
69
|
+
};
|
|
70
|
+
const same = (a, b) => a === b || keyOf(a) === keyOf(b);
|
|
71
|
+
|
|
72
|
+
const clearAll = () => {
|
|
73
|
+
if (marker === null) parent.textContent = '';
|
|
74
|
+
else for (let i = 0; i < recs.length; i++) recs[i].el.remove();
|
|
75
|
+
for (let i = 0; i < recs.length; i++) if (recs[i].s !== null) dispose(recs[i].s);
|
|
76
|
+
map.clear();
|
|
77
|
+
recs = [];
|
|
78
|
+
prev = [];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const update = items => {
|
|
82
|
+
const n = items.length, pn = recs.length;
|
|
83
|
+
let i, r;
|
|
84
|
+
if (!n) {
|
|
85
|
+
if (pn) clearAll();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!pn) {
|
|
89
|
+
recs = new Array(n);
|
|
90
|
+
for (i = 0; i < n; i++) {
|
|
91
|
+
recs[i] = r = fresh(items[i], i);
|
|
92
|
+
map.set(keyOf(items[i]), r);
|
|
93
|
+
parent.insertBefore(r.el, marker);
|
|
94
|
+
}
|
|
95
|
+
if (map.size !== n) duplicate(items);
|
|
96
|
+
prev = items.slice();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// 1. unchanged (same key) head and tail; replaced items are patched in place
|
|
100
|
+
// (identity is checked inline: most items are unchanged, so the key is rarely needed)
|
|
101
|
+
let ns = 0, os = 0, ne = n, oe = pn, a, b;
|
|
102
|
+
while (ns < ne && os < oe) {
|
|
103
|
+
if ((a = items[ns]) !== (b = prev[os])) {
|
|
104
|
+
if (keyOf(a) !== keyOf(b)) break;
|
|
105
|
+
refresh(recs[os], a, ns);
|
|
106
|
+
}
|
|
107
|
+
ns++, os++;
|
|
108
|
+
}
|
|
109
|
+
while (ns < ne && os < oe) {
|
|
110
|
+
if ((a = items[ne - 1]) !== (b = prev[oe - 1]) && keyOf(a) !== keyOf(b)) break;
|
|
111
|
+
ne--, oe--;
|
|
112
|
+
if (a !== b || (ix && ne !== oe)) refresh(recs[oe], a, ne);
|
|
113
|
+
}
|
|
114
|
+
if (ns === ne && os === oe) {
|
|
115
|
+
prev = items.slice();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const mid = new Array(ne - ns);
|
|
119
|
+
const after = oe < pn ? recs[oe].el : marker;
|
|
120
|
+
if (os === oe) {
|
|
121
|
+
// only insertions
|
|
122
|
+
for (i = ns; i < ne; i++) {
|
|
123
|
+
mid[i - ns] = r = fresh(items[i], i);
|
|
124
|
+
map.set(keyOf(items[i]), r);
|
|
125
|
+
parent.insertBefore(r.el, after);
|
|
126
|
+
}
|
|
127
|
+
} else if (ns === ne) {
|
|
128
|
+
// only removals
|
|
129
|
+
for (i = os; i < oe; i++) {
|
|
130
|
+
map.delete(keyOf(prev[i]));
|
|
131
|
+
drop(recs[i]);
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
// 2. rows that swapped ends need no lookup
|
|
135
|
+
let s = ns, e = ne, ps = os, pe = oe;
|
|
136
|
+
while (s < e && ps < pe) {
|
|
137
|
+
if (same(items[s], prev[ps])) {
|
|
138
|
+
r = mid[s - ns] = recs[ps];
|
|
139
|
+
if (r.v !== items[s] || (ix && s !== ps)) refresh(r, items[s], s);
|
|
140
|
+
s++, ps++;
|
|
141
|
+
} else if (same(items[e - 1], prev[pe - 1])) {
|
|
142
|
+
r = mid[e - 1 - ns] = recs[pe - 1];
|
|
143
|
+
if (r.v !== items[e - 1] || (ix && e - 1 !== pe - 1)) refresh(r, items[e - 1], e - 1);
|
|
144
|
+
e--, pe--;
|
|
145
|
+
} else if (same(items[s], prev[pe - 1]) && same(items[e - 1], prev[ps])) {
|
|
146
|
+
const a = recs[ps], b = recs[pe - 1], next = b.el.nextSibling;
|
|
147
|
+
parent.insertBefore(b.el, a.el);
|
|
148
|
+
parent.insertBefore(a.el, next);
|
|
149
|
+
mid[s - ns] = b;
|
|
150
|
+
mid[e - 1 - ns] = a;
|
|
151
|
+
if (b.v !== items[s] || ix) refresh(b, items[s], s);
|
|
152
|
+
if (a.v !== items[e - 1] || ix) refresh(a, items[e - 1], e - 1);
|
|
153
|
+
s++, e--, ps++, pe--;
|
|
154
|
+
} else break;
|
|
155
|
+
}
|
|
156
|
+
// 3. the rest is matched by key
|
|
157
|
+
if (s < e || ps < pe) {
|
|
158
|
+
stamp++;
|
|
159
|
+
let reused = 0;
|
|
160
|
+
for (i = s; i < e; i++) {
|
|
161
|
+
r = map.get(keyOf(items[i]));
|
|
162
|
+
if (r !== undefined && r.h !== stamp) {
|
|
163
|
+
r.h = stamp;
|
|
164
|
+
reused++;
|
|
165
|
+
if (r.v !== items[i] || (ix && r.i !== i)) refresh(r, items[i], i);
|
|
166
|
+
} else r = fresh(items[i], i);
|
|
167
|
+
mid[i - ns] = r;
|
|
168
|
+
}
|
|
169
|
+
if (!reused && marker === null && s === 0 && e === n && ps === 0 && pe === pn) {
|
|
170
|
+
// nothing survives: clear in one DOM call, then append in order
|
|
171
|
+
for (i = ps; i < pe; i++) if (recs[i].s !== null) dispose(recs[i].s);
|
|
172
|
+
map.clear();
|
|
173
|
+
parent.textContent = '';
|
|
174
|
+
for (i = 0; i < n; i++) {
|
|
175
|
+
map.set(keyOf(items[i]), mid[i]);
|
|
176
|
+
parent.appendChild(mid[i].el);
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
for (i = ps; i < pe; i++)
|
|
180
|
+
if (recs[i].h !== stamp) {
|
|
181
|
+
map.delete(keyOf(prev[i]));
|
|
182
|
+
drop(recs[i]);
|
|
183
|
+
}
|
|
184
|
+
let ref = e < ne ? mid[e - ns].el : after;
|
|
185
|
+
for (i = e - 1; i >= s; i--) {
|
|
186
|
+
r = mid[i - ns];
|
|
187
|
+
if (r.el.parentNode !== parent) map.set(keyOf(items[i]), r);
|
|
188
|
+
if (r.el.parentNode !== parent || r.el.nextSibling !== ref) parent.insertBefore(r.el, ref);
|
|
189
|
+
ref = r.el;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
recs = recs.slice(0, ns).concat(mid, recs.slice(oe));
|
|
195
|
+
if (map.size !== n) duplicate(items);
|
|
196
|
+
prev = items.slice();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const duplicate = items => {
|
|
200
|
+
const seen = new Set();
|
|
201
|
+
for (const item of items) {
|
|
202
|
+
const key = keyOf(item);
|
|
203
|
+
if (seen.has(key)) throw new Error(DEV ? `[vanji] each(): duplicate key "${String(key)}". Keys must be unique.` : 'vanji: duplicate key');
|
|
204
|
+
seen.add(key);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
onCleanup(() => {
|
|
209
|
+
for (let i = 0; i < recs.length; i++) if (recs[i].s !== null) dispose(recs[i].s);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
render(() => {
|
|
213
|
+
const v = items();
|
|
214
|
+
untrack(update, isArray(v) ? v : v == null ? [] : [...v]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// `sig() === row.key` holes: one subscription for the list, only the old and new rows are patched
|
|
218
|
+
if (spec.x)
|
|
219
|
+
for (const sig of spec.x) {
|
|
220
|
+
let cur = untrack(sig);
|
|
221
|
+
render(() => {
|
|
222
|
+
const v = sig();
|
|
223
|
+
if (v === cur) return;
|
|
224
|
+
const a = map.get(cur), b = map.get(v);
|
|
225
|
+
cur = v;
|
|
226
|
+
if (a !== undefined) untrack(() => patch(a, a.v, a.i));
|
|
227
|
+
if (b !== undefined) untrack(() => patch(b, b.v, b.i));
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// events of the rows: one capture listener per type on the parent. Capture runs before the parent's
|
|
232
|
+
// own (bubbling) listeners, so a row's stopPropagation() stops them, and it also sees events that
|
|
233
|
+
// do not bubble (then only a handler on the target itself runs, as natively).
|
|
234
|
+
if (spec.e)
|
|
235
|
+
for (const type in spec.e) {
|
|
236
|
+
const handlers = spec.e[type];
|
|
237
|
+
parent.addEventListener(type, e => {
|
|
238
|
+
let row = e.target;
|
|
239
|
+
while (row !== null && row.parentNode !== parent) row = row.parentNode;
|
|
240
|
+
if (row === null) return;
|
|
241
|
+
let r;
|
|
242
|
+
for (let i = 0; i < recs.length; i++)
|
|
243
|
+
if (recs[i].el === row) {
|
|
244
|
+
r = recs[i];
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
if (r === undefined) return;
|
|
248
|
+
let node = e.target;
|
|
249
|
+
Object.defineProperty(e, 'currentTarget', { configurable: true, get: () => node });
|
|
250
|
+
try {
|
|
251
|
+
for (;;) {
|
|
252
|
+
for (let j = 0; j < handlers.length; j++) {
|
|
253
|
+
if (handlers[j][0](r) !== node || node.disabled === true) continue;
|
|
254
|
+
const h = handlers[j][1](r.v, r.i);
|
|
255
|
+
if (h != null) call(h, node, e);
|
|
256
|
+
if (e.cancelBubble) return;
|
|
257
|
+
}
|
|
258
|
+
if (node === row || !e.bubbles) return;
|
|
259
|
+
node = node.parentNode;
|
|
260
|
+
}
|
|
261
|
+
} finally {
|
|
262
|
+
delete e.currentTarget;
|
|
263
|
+
}
|
|
264
|
+
}, true);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/** Run `fn` in a new scope stored on row `r` (holes of a row that call functions). */
|
|
269
|
+
export const rowScope = (r, fn) => {
|
|
270
|
+
r.s = scope();
|
|
271
|
+
runIn(r.s, fn);
|
|
272
|
+
};
|
package/src/signals.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// vanji reactive core: signal / computed / effect
|
|
2
|
+
// 3-state (CLEAN / CHECK / DIRTY) push-pull graph. Computeds are lazy and cut off
|
|
3
|
+
// propagation when their value does not change. Effects run synchronously after a write
|
|
4
|
+
// (or at the end of the outermost batch).
|
|
5
|
+
//
|
|
6
|
+
// Memory layout notes (these matter with thousands of rows):
|
|
7
|
+
// - a signal is ONE function object that is also its own graph node (no per-signal closures)
|
|
8
|
+
// - observers / sources / owned / cleanups are stored as `null | item | collection`, so the
|
|
9
|
+
// common "exactly one" case allocates nothing
|
|
10
|
+
// - template bindings store their target on the node (h/a/b) instead of in a closure
|
|
11
|
+
|
|
12
|
+
import { DEV } from './dev.js';
|
|
13
|
+
|
|
14
|
+
const CLEAN = 0, CHECK = 1, DIRTY = 2;
|
|
15
|
+
const isArray = Array.isArray;
|
|
16
|
+
|
|
17
|
+
let observer = null; // computation currently collecting dependencies
|
|
18
|
+
let owner = null; // computation / scope that owns whatever is created right now
|
|
19
|
+
let depth = 0; // batch depth (a running computation counts as a batch)
|
|
20
|
+
let queue = []; // effects waiting to run
|
|
21
|
+
let flushing = false;
|
|
22
|
+
|
|
23
|
+
// Node fields: f=fn, v=value, s=state, src=sources, obs=observers,
|
|
24
|
+
// e=kind (0 computed/scope, 1 internal effect, 2 user effect),
|
|
25
|
+
// o=owned nodes, c=cleanups, p=parent owner, x=disposed, h/a/b=binding handler + args
|
|
26
|
+
const make = (f, e, s) => ({ f, v: undefined, s, src: null, obs: null, e, o: null, c: null, p: owner, x: false, h: null, a: null, b: null });
|
|
27
|
+
|
|
28
|
+
const push = (list, item) => (list === null ? item : isArray(list) ? (list.push(item), list) : [list, item]);
|
|
29
|
+
|
|
30
|
+
const node = (f, e) => {
|
|
31
|
+
const n = make(f, e, DIRTY);
|
|
32
|
+
if (owner !== null) owner.o = push(owner.o, n);
|
|
33
|
+
return n;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const track = n => {
|
|
37
|
+
const o = observer, obs = n.obs;
|
|
38
|
+
if (obs === o) return;
|
|
39
|
+
if (obs === null) n.obs = o;
|
|
40
|
+
else if (obs instanceof Set) {
|
|
41
|
+
if (obs.has(o)) return;
|
|
42
|
+
obs.add(o);
|
|
43
|
+
} else (n.obs = new Set()).add(obs).add(o);
|
|
44
|
+
o.src = push(o.src, n);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const unsub = (s, n) => {
|
|
48
|
+
const obs = s.obs;
|
|
49
|
+
if (obs === n) s.obs = null;
|
|
50
|
+
else if (obs !== null && obs instanceof Set && obs.delete(n) && !obs.size) s.obs = null;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const mark1 = (o, state) => {
|
|
54
|
+
const was = o.s;
|
|
55
|
+
if (was < state) {
|
|
56
|
+
o.s = state;
|
|
57
|
+
if (was === CLEAN) o.e ? queue.push(o) : mark(o, CHECK);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const mark = (n, state) => {
|
|
62
|
+
const obs = n.obs;
|
|
63
|
+
if (obs !== null) {
|
|
64
|
+
if (obs instanceof Set) for (const o of obs) mark1(o, state);
|
|
65
|
+
else mark1(obs, state);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const clean = n => {
|
|
70
|
+
let list = n.o, i;
|
|
71
|
+
if (list !== null) {
|
|
72
|
+
n.o = null;
|
|
73
|
+
if (isArray(list)) for (i = 0; i < list.length; i++) dispose(list[i]);
|
|
74
|
+
else dispose(list);
|
|
75
|
+
}
|
|
76
|
+
if ((list = n.c) !== null) {
|
|
77
|
+
n.c = null;
|
|
78
|
+
if (isArray(list)) for (i = 0; i < list.length; i++) list[i]();
|
|
79
|
+
else list();
|
|
80
|
+
}
|
|
81
|
+
if ((list = n.src) !== null) {
|
|
82
|
+
n.src = null;
|
|
83
|
+
if (isArray(list)) for (i = 0; i < list.length; i++) unsub(list[i], n);
|
|
84
|
+
else unsub(list, n);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const dispose = n => {
|
|
89
|
+
clean(n);
|
|
90
|
+
n.f = n.a = n.b = null;
|
|
91
|
+
n.v = undefined;
|
|
92
|
+
n.s = CLEAN;
|
|
93
|
+
n.x = true;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const update = n => {
|
|
97
|
+
clean(n);
|
|
98
|
+
const po = observer, pw = owner;
|
|
99
|
+
observer = owner = n;
|
|
100
|
+
depth++;
|
|
101
|
+
try {
|
|
102
|
+
const v = n.h !== null ? n.h(n) : n.f();
|
|
103
|
+
if (n.e) {
|
|
104
|
+
if (n.e === 2 && typeof v === 'function') n.c = push(n.c, v);
|
|
105
|
+
} else if (!Object.is(v, n.v)) {
|
|
106
|
+
n.v = v;
|
|
107
|
+
const obs = n.obs;
|
|
108
|
+
if (obs !== null) {
|
|
109
|
+
if (obs instanceof Set) for (const o of obs) o.s = DIRTY;
|
|
110
|
+
else obs.s = DIRTY;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} finally {
|
|
114
|
+
observer = po;
|
|
115
|
+
owner = pw;
|
|
116
|
+
n.s = CLEAN;
|
|
117
|
+
if (!--depth) flush();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** Internal: run computation `n` again now (a list row's effects after its item was replaced). */
|
|
122
|
+
export const rerun = n => {
|
|
123
|
+
if (n.f) update(n);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// Bring a computation up to date (only recomputes when a source really changed).
|
|
127
|
+
const refresh = n => {
|
|
128
|
+
if (n.s === CHECK) {
|
|
129
|
+
const src = n.src;
|
|
130
|
+
if (isArray(src)) {
|
|
131
|
+
for (let i = 0; i < src.length && n.s === CHECK; i++) if (src[i].f) refresh(src[i]);
|
|
132
|
+
} else if (src !== null && src.f) refresh(src);
|
|
133
|
+
}
|
|
134
|
+
if (n.s === DIRTY && n.f) update(n);
|
|
135
|
+
else n.s = CLEAN;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// Ancestors first: a parent that re-runs disposes its children, so they never run stale.
|
|
139
|
+
const run = n => {
|
|
140
|
+
if (n.p !== null) run(n.p);
|
|
141
|
+
if (n.s !== CLEAN) refresh(n);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const flush = () => {
|
|
145
|
+
if (flushing) return;
|
|
146
|
+
flushing = true;
|
|
147
|
+
try {
|
|
148
|
+
for (let i = 0; i < queue.length; i++) {
|
|
149
|
+
if (i > 1e5) throw new Error(DEV ? '[vanji] effect loop: an effect keeps writing to a signal that re-triggers it' : 'vanji: effect loop');
|
|
150
|
+
run(queue[i]);
|
|
151
|
+
}
|
|
152
|
+
} finally {
|
|
153
|
+
queue = [];
|
|
154
|
+
flushing = false;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// dev only: explain a set/update/peek that lost its signal (production just uses `this`)
|
|
159
|
+
const self = s => {
|
|
160
|
+
if (DEV && (!s || s.set !== set))
|
|
161
|
+
throw new Error('[vanji] set/update/peek was called detached from its signal. Write `v => s.set(v)` instead of passing `s.set`.');
|
|
162
|
+
return s;
|
|
163
|
+
};
|
|
164
|
+
function peek() {
|
|
165
|
+
return self(this).v;
|
|
166
|
+
}
|
|
167
|
+
function set(v) {
|
|
168
|
+
const s = self(this);
|
|
169
|
+
if (!Object.is(s.v, v)) {
|
|
170
|
+
s.v = v;
|
|
171
|
+
mark(s, DIRTY);
|
|
172
|
+
if (!depth) flush();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function update_(fn) {
|
|
176
|
+
self(this).set(fn(this.v));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Reactive value. Read: `s()`. Write: `s.set(v)` / `s.update(fn)`. Untracked read: `s.peek()`. */
|
|
180
|
+
export const signal = value => {
|
|
181
|
+
const s = () => {
|
|
182
|
+
if (observer !== null) track(s);
|
|
183
|
+
return s.v;
|
|
184
|
+
};
|
|
185
|
+
s.v = value;
|
|
186
|
+
s.obs = null;
|
|
187
|
+
s.peek = peek;
|
|
188
|
+
s.set = set;
|
|
189
|
+
s.update = update_;
|
|
190
|
+
return s;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
function cpeek() {
|
|
194
|
+
const n = this.n;
|
|
195
|
+
if (n.s) refresh(n);
|
|
196
|
+
return n.v;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Derived value. Lazy, cached, and only notifies when the result changes. */
|
|
200
|
+
export const computed = fn => {
|
|
201
|
+
const n = node(fn, 0);
|
|
202
|
+
const get = () => {
|
|
203
|
+
if (n.s) refresh(n);
|
|
204
|
+
if (observer !== null) track(n);
|
|
205
|
+
return n.v;
|
|
206
|
+
};
|
|
207
|
+
get.n = n;
|
|
208
|
+
get.peek = cpeek;
|
|
209
|
+
return get;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** Run `fn` now and again whenever a signal it read changes. Returns a dispose function. */
|
|
213
|
+
export const effect = fn => {
|
|
214
|
+
const n = node(fn, 2);
|
|
215
|
+
update(n);
|
|
216
|
+
return () => dispose(n);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/** Internal effect used by compiled templates (return value is ignored). */
|
|
220
|
+
export const render = fn => {
|
|
221
|
+
update(node(fn, 1));
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** Internal: effect whose work is `h(node)`; `f`, `a`, `b` live on the node (no closure). */
|
|
225
|
+
export const bind = (h, f, a, b) => {
|
|
226
|
+
const n = node(f, 1);
|
|
227
|
+
n.h = h;
|
|
228
|
+
n.a = a;
|
|
229
|
+
n.b = b;
|
|
230
|
+
update(n);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/** Group several writes; effects run once at the end. */
|
|
234
|
+
export const batch = fn => {
|
|
235
|
+
depth++;
|
|
236
|
+
try {
|
|
237
|
+
return fn();
|
|
238
|
+
} finally {
|
|
239
|
+
if (!--depth) flush();
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** Read signals inside `fn` without subscribing to them. */
|
|
244
|
+
export const untrack = (fn, arg) => {
|
|
245
|
+
const po = observer;
|
|
246
|
+
observer = null;
|
|
247
|
+
try {
|
|
248
|
+
return fn(arg);
|
|
249
|
+
} finally {
|
|
250
|
+
observer = po;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** Internal: a detached ownership scope (not owned by the current owner). */
|
|
255
|
+
export const scope = () => make(null, 0, CLEAN);
|
|
256
|
+
|
|
257
|
+
/** Internal: run `fn(a, b)` with `n` as owner and no observer. */
|
|
258
|
+
export const runIn = (n, fn, a, b) => {
|
|
259
|
+
const po = observer, pw = owner;
|
|
260
|
+
observer = null;
|
|
261
|
+
owner = n;
|
|
262
|
+
try {
|
|
263
|
+
return fn(a, b);
|
|
264
|
+
} finally {
|
|
265
|
+
observer = po;
|
|
266
|
+
owner = pw;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** Create an ownership scope. `fn` receives `dispose`, which tears down everything created inside. */
|
|
271
|
+
export const root = fn => {
|
|
272
|
+
const n = scope();
|
|
273
|
+
return runIn(n, fn, () => dispose(n));
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/** Register `fn` to run when the current scope (component / effect / each row) is disposed or re-runs. */
|
|
277
|
+
export const onCleanup = fn => {
|
|
278
|
+
if (owner !== null) owner.c = push(owner.c, fn);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/** Run `fn` once after the current synchronous render work, when the created DOM is in the document. */
|
|
282
|
+
export const onMount = fn => {
|
|
283
|
+
const o = owner;
|
|
284
|
+
queueMicrotask(() => {
|
|
285
|
+
if (o === null || !o.x) runIn(o, fn);
|
|
286
|
+
});
|
|
287
|
+
};
|