@sigil-dev/runtime 0.9.28 → 0.9.30
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/index.ts +1671 -1672
- package/package.json +1 -1
- package/reactivity/map.ts +23 -12
- package/reactivity/set.ts +14 -11
- package/transition.ts +69 -69
package/index.ts
CHANGED
|
@@ -1,1672 +1,1671 @@
|
|
|
1
|
-
export { __asyncBoundary } from "./async-boundary.ts";
|
|
2
|
-
export { Boundary } from "./boundary";
|
|
3
|
-
export {
|
|
4
|
-
easeInCubic,
|
|
5
|
-
easeInOutCubic,
|
|
6
|
-
easeInOutQuad,
|
|
7
|
-
easeInOutQuart,
|
|
8
|
-
easeInOutQuint,
|
|
9
|
-
easeInOutSine,
|
|
10
|
-
easeInQuad,
|
|
11
|
-
easeInQuart,
|
|
12
|
-
easeInQuint,
|
|
13
|
-
easeInSine,
|
|
14
|
-
easeOutCubic,
|
|
15
|
-
easeOutQuad,
|
|
16
|
-
easeOutQuart,
|
|
17
|
-
easeOutQuint,
|
|
18
|
-
easeOutSine,
|
|
19
|
-
linear,
|
|
20
|
-
} from "./easing.ts";
|
|
21
|
-
export { ReactiveMap } from "./reactivity/map.ts";
|
|
22
|
-
export { ReactiveSet } from "./reactivity/set.ts";
|
|
23
|
-
export { mountTransition, removeAnimated } from "./transition.ts";
|
|
24
|
-
|
|
25
|
-
export const SIGIL_SAFE = Symbol.for("sigil.safe");
|
|
26
|
-
export class SafeHtml {
|
|
27
|
-
[SIGIL_SAFE] = true;
|
|
28
|
-
constructor(public v: string) {}
|
|
29
|
-
toString() {
|
|
30
|
-
return this.v;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let currentEffect: (() => void) | null = null;
|
|
35
|
-
let currentDeps: Set<Set<() => void>> | null = null;
|
|
36
|
-
|
|
37
|
-
// ─── ownership ───
|
|
38
|
-
//
|
|
39
|
-
// Every component call gets an owner whose parent is the component that
|
|
40
|
-
// rendered it, so context is looked up through the tree, not one global map.
|
|
41
|
-
// Effects remember the owner they were created under and run under it again:
|
|
42
|
-
// list rows, slots and directives built in a later update still see the
|
|
43
|
-
// context of the component that declared them.
|
|
44
|
-
|
|
45
|
-
export interface Owner {
|
|
46
|
-
parent: Owner | null;
|
|
47
|
-
context: Map<symbol, unknown> | null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let currentOwner: Owner | null = null;
|
|
51
|
-
|
|
52
|
-
export function getOwner(): Owner | null {
|
|
53
|
-
return currentOwner;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Run fn() under `owner`, as if called from inside that component. */
|
|
57
|
-
export function runWithOwner<T>(owner: Owner | null, fn: () => T): T {
|
|
58
|
-
const prev = currentOwner;
|
|
59
|
-
currentOwner = owner;
|
|
60
|
-
try {
|
|
61
|
-
return fn();
|
|
62
|
-
} finally {
|
|
63
|
-
currentOwner = prev;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** `<Comp {...props} />`: the compiler calls components through this. */
|
|
68
|
-
export function __component<P, R>(Comp: (props: P) => R, props: P): R {
|
|
69
|
-
return runWithOwner({ parent: currentOwner, context: null }, () =>
|
|
70
|
-
Comp(props),
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* `await x` in an async component compiles to `__resume(await x, owner)`:
|
|
76
|
-
* the code after an await runs under the component's owner again, and the
|
|
77
|
-
* owner is put back once that stretch of synchronous code is done.
|
|
78
|
-
*/
|
|
79
|
-
export function __resume<T>(value: T, owner: Owner | null): T {
|
|
80
|
-
const prev = currentOwner;
|
|
81
|
-
currentOwner = owner;
|
|
82
|
-
queueMicrotask(() => {
|
|
83
|
-
if (currentOwner === owner) currentOwner = prev;
|
|
84
|
-
});
|
|
85
|
-
return value;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
let batchDepth = 0;
|
|
89
|
-
let scheduledFlush = false;
|
|
90
|
-
const pendingEffects = new Set<() => void>();
|
|
91
|
-
const pendingPreEffects = new Set<() => void>();
|
|
92
|
-
|
|
93
|
-
/** Runs of one effect within one flush before it counts as an update loop. */
|
|
94
|
-
const MAX_RUNS_PER_FLUSH = 100;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Run queued effects until none are left. One at a time, and pre effects
|
|
98
|
-
* (memos, `$effect.pre`) first: a memo settles before any effect that reads
|
|
99
|
-
* it runs, and an effect queued again while it is still pending runs again
|
|
100
|
-
* instead of being lost in the set already being walked.
|
|
101
|
-
*/
|
|
102
|
-
function flush() {
|
|
103
|
-
scheduledFlush = true; // schedules made during the flush are drained here
|
|
104
|
-
const runs = new Map<() => void, number>();
|
|
105
|
-
try {
|
|
106
|
-
while (pendingPreEffects.size || pendingEffects.size) {
|
|
107
|
-
const queue = pendingPreEffects.size ? pendingPreEffects : pendingEffects;
|
|
108
|
-
const effect = queue.values().next().value!;
|
|
109
|
-
queue.delete(effect);
|
|
110
|
-
const n = (runs.get(effect) ?? 0) + 1;
|
|
111
|
-
runs.set(effect, n);
|
|
112
|
-
if (n > MAX_RUNS_PER_FLUSH) {
|
|
113
|
-
pendingPreEffects.clear();
|
|
114
|
-
pendingEffects.clear();
|
|
115
|
-
throw new Error(
|
|
116
|
-
`[sigil] an effect re-ran ${MAX_RUNS_PER_FLUSH} times in one update: it writes state it also reads (infinite update loop).`,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
try {
|
|
120
|
-
effect();
|
|
121
|
-
} catch (e) {
|
|
122
|
-
// one failing effect must not stop the rest; still reported
|
|
123
|
-
queueMicrotask(() => {
|
|
124
|
-
throw e;
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
} finally {
|
|
129
|
-
scheduledFlush = false;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function scheduleEffect(fn: () => void) {
|
|
134
|
-
const target = (fn as any).__sigil_pre ? pendingPreEffects : pendingEffects;
|
|
135
|
-
target.add(fn);
|
|
136
|
-
if (batchDepth === 0 && !scheduledFlush) {
|
|
137
|
-
scheduledFlush = true;
|
|
138
|
-
queueMicrotask(flush);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const targetDepsMap = new WeakMap<
|
|
143
|
-
object,
|
|
144
|
-
Map<string | symbol, Set<() => void>>
|
|
145
|
-
>();
|
|
146
|
-
|
|
147
|
-
function getDeps(target: object, key: string | symbol): Set<() => void> {
|
|
148
|
-
let deps = targetDepsMap.get(target);
|
|
149
|
-
if (!deps) {
|
|
150
|
-
deps = new Map();
|
|
151
|
-
targetDepsMap.set(target, deps);
|
|
152
|
-
}
|
|
153
|
-
let set = deps.get(key);
|
|
154
|
-
if (!set) {
|
|
155
|
-
set = new Set();
|
|
156
|
-
deps.set(key, set);
|
|
157
|
-
}
|
|
158
|
-
return set;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function track(target: object, key: string | symbol) {
|
|
162
|
-
if (currentEffect && currentDeps) {
|
|
163
|
-
const deps = getDeps(target, key);
|
|
164
|
-
deps.add(currentEffect);
|
|
165
|
-
currentDeps.add(deps); // Set, deduplicates automatically
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function trigger(target: object, key: string | symbol) {
|
|
170
|
-
const deps = targetDepsMap.get(target);
|
|
171
|
-
if (!deps) return;
|
|
172
|
-
const set = deps.get(key);
|
|
173
|
-
if (!set) return;
|
|
174
|
-
for (const fn of [...set]) {
|
|
175
|
-
scheduleEffect(fn);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/** Replace a reactive object's contents. A symbol, so no data key can clash. */
|
|
180
|
-
const REPLACE = Symbol("sigil.replace");
|
|
181
|
-
|
|
182
|
-
const proxyCache = new WeakMap<object, object>();
|
|
183
|
-
const proxyToTarget = new WeakMap<object, object>();
|
|
184
|
-
const reactiveTargets = new WeakSet<object>();
|
|
185
|
-
const ARRAY_MUTATORS = new Set([
|
|
186
|
-
"push",
|
|
187
|
-
"pop",
|
|
188
|
-
"shift",
|
|
189
|
-
"unshift",
|
|
190
|
-
"splice",
|
|
191
|
-
"sort",
|
|
192
|
-
"reverse",
|
|
193
|
-
"fill",
|
|
194
|
-
]);
|
|
195
|
-
|
|
196
|
-
function createReactiveProxy<T extends object>(target: T): T {
|
|
197
|
-
const existing = proxyCache.get(target);
|
|
198
|
-
if (existing) return existing as T;
|
|
199
|
-
|
|
200
|
-
const proxy = new Proxy(target, {
|
|
201
|
-
get(target, key, receiver) {
|
|
202
|
-
if (
|
|
203
|
-
Array.isArray(target) &&
|
|
204
|
-
typeof key === "string" &&
|
|
205
|
-
ARRAY_MUTATORS.has(key)
|
|
206
|
-
) {
|
|
207
|
-
return (...args: any[]) => {
|
|
208
|
-
const result = (target as any)[key](...args);
|
|
209
|
-
// sort/reverse/splice/shift move items between indexes, so
|
|
210
|
-
// every tracked index (and length) may now read differently
|
|
211
|
-
const deps = targetDepsMap.get(target);
|
|
212
|
-
if (deps) for (const k of [...deps.keys()]) trigger(target, k);
|
|
213
|
-
return result;
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
if (key === REPLACE) {
|
|
217
|
-
return (newValue: T) => {
|
|
218
|
-
const oldKeys = new Set(Object.keys(target));
|
|
219
|
-
const newKeys = new Set(Object.keys(newValue));
|
|
220
|
-
for (const k of oldKeys) {
|
|
221
|
-
if (!newKeys.has(k)) {
|
|
222
|
-
delete (target as any)[k];
|
|
223
|
-
trigger(target, k);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
for (const k of newKeys) {
|
|
227
|
-
(target as any)[k] = (newValue as any)[k];
|
|
228
|
-
trigger(target, k);
|
|
229
|
-
}
|
|
230
|
-
if (Array.isArray(target)) {
|
|
231
|
-
// delete leaves holes — length must shrink to the new value's,
|
|
232
|
-
// or iteration (spread, reconcile) sees phantom trailing slots
|
|
233
|
-
target.length = (newValue as unknown[]).length;
|
|
234
|
-
trigger(target, "length");
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (typeof key === "symbol") {
|
|
240
|
-
return Reflect.get(target, key, receiver);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
track(target, key);
|
|
244
|
-
const value = target[key as keyof T];
|
|
245
|
-
|
|
246
|
-
if (typeof value === "object" && value !== null) {
|
|
247
|
-
return createReactiveProxy(value);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return value;
|
|
251
|
-
},
|
|
252
|
-
|
|
253
|
-
set(target, key, newValue, receiver) {
|
|
254
|
-
const oldValue = target[key as keyof T];
|
|
255
|
-
const result = Reflect.set(target, key, newValue, receiver);
|
|
256
|
-
if (oldValue !== newValue) {
|
|
257
|
-
trigger(target, key as string | symbol);
|
|
258
|
-
if (
|
|
259
|
-
Array.isArray(target) &&
|
|
260
|
-
typeof key === "string" &&
|
|
261
|
-
!Number.isNaN(Number(key))
|
|
262
|
-
) {
|
|
263
|
-
trigger(target, "length");
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return result;
|
|
267
|
-
},
|
|
268
|
-
|
|
269
|
-
deleteProperty(target, key) {
|
|
270
|
-
const result = Reflect.deleteProperty(target, key);
|
|
271
|
-
trigger(target, key as string | symbol);
|
|
272
|
-
return result;
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
proxyCache.set(target, proxy);
|
|
277
|
-
proxyToTarget.set(proxy, target);
|
|
278
|
-
reactiveTargets.add(target);
|
|
279
|
-
return proxy as T;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export type Signal<T> = {
|
|
283
|
-
(): T;
|
|
284
|
-
set: (next: T) => void;
|
|
285
|
-
peek: () => T;
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
export function batch(fn: () => void) {
|
|
289
|
-
batchDepth++;
|
|
290
|
-
try {
|
|
291
|
-
fn();
|
|
292
|
-
} finally {
|
|
293
|
-
batchDepth--;
|
|
294
|
-
}
|
|
295
|
-
if (batchDepth === 0) flush();
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
export function tick(): Promise<void> {
|
|
299
|
-
return new Promise((resolve) => queueMicrotask(resolve));
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// ─── signals ───
|
|
303
|
-
//
|
|
304
|
-
// One kind of signal for every value. A deep signal (`$state`) keeps any
|
|
305
|
-
// object it holds behind a reactive proxy, whichever value it starts or ends
|
|
306
|
-
// up with; a raw signal (`$state.raw`, memos) stores values as they are.
|
|
307
|
-
|
|
308
|
-
const signals = new WeakSet<object>();
|
|
309
|
-
|
|
310
|
-
/** Whether `value` is a signal getter. */
|
|
311
|
-
export function isSignal(value: unknown): value is Signal<unknown> {
|
|
312
|
-
return typeof value === "function" && signals.has(value);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const isPlain = (v: object) => {
|
|
316
|
-
const proto = Object.getPrototypeOf(v);
|
|
317
|
-
return proto === Object.prototype || proto === null;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
/** An object a deep signal holds, behind its proxy (never a proxy of a proxy). */
|
|
321
|
-
function reactive<T>(value: T): T {
|
|
322
|
-
if (typeof value !== "object" || value === null) return value;
|
|
323
|
-
if (proxyToTarget.has(value)) return value;
|
|
324
|
-
return createReactiveProxy(value);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function makeSignal<T>(initial: T, deep: boolean): Signal<T> {
|
|
328
|
-
const subs = new Set<() => void>();
|
|
329
|
-
let value = deep ? reactive(initial) : initial;
|
|
330
|
-
|
|
331
|
-
const getter = () => {
|
|
332
|
-
if (currentEffect && currentDeps) {
|
|
333
|
-
subs.add(currentEffect);
|
|
334
|
-
currentDeps.add(subs);
|
|
335
|
-
// a held array: track its length too, so push/pop/splice re-run
|
|
336
|
-
// readers that hand the array on without reading it themselves
|
|
337
|
-
const target = deep && proxyToTarget.get(value as object);
|
|
338
|
-
if (target && Array.isArray(target)) track(target, "length");
|
|
339
|
-
}
|
|
340
|
-
return value;
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
getter.set = (next: T) => {
|
|
344
|
-
if (deep) {
|
|
345
|
-
// plain object to plain object, or array to array: replace the
|
|
346
|
-
// contents in place, so the object keeps its identity and only the
|
|
347
|
-
// keys that changed notify their readers
|
|
348
|
-
const target = proxyToTarget.get(value as object);
|
|
349
|
-
if (
|
|
350
|
-
target &&
|
|
351
|
-
typeof next === "object" &&
|
|
352
|
-
next !== null &&
|
|
353
|
-
!proxyToTarget.has(next) &&
|
|
354
|
-
(Array.isArray(target)
|
|
355
|
-
? Array.isArray(next)
|
|
356
|
-
: !Array.isArray(next) && isPlain(target) && isPlain(next))
|
|
357
|
-
) {
|
|
358
|
-
(value as any)[REPLACE](next);
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
next = reactive(next);
|
|
362
|
-
if (Object.is(value, next)) return;
|
|
363
|
-
} else if (
|
|
364
|
-
// an equal primitive changes nothing; a raw object always notifies,
|
|
365
|
-
// since it may be set to the same object after being mutated
|
|
366
|
-
Object.is(value, next) &&
|
|
367
|
-
(typeof next !== "object" || next === null) &&
|
|
368
|
-
typeof next !== "function"
|
|
369
|
-
) {
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
value = next;
|
|
373
|
-
for (const fn of [...subs]) scheduleEffect(fn);
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
// untracked read; a deep signal's object comes back unwrapped
|
|
377
|
-
getter.peek = () =>
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
*
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
if (
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
*
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
const
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
const
|
|
472
|
-
const
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
const
|
|
514
|
-
const
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
//
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
const pending
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
const
|
|
546
|
-
const
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
//
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
*
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
let
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
const
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
const
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
//
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
effect
|
|
618
|
-
//
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
*
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
const
|
|
695
|
-
const
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
let
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
versionCounter
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
*
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
)
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
//
|
|
779
|
-
//
|
|
780
|
-
//
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
const
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
node &&
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
node
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
*
|
|
807
|
-
*
|
|
808
|
-
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
)
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
node
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
//
|
|
839
|
-
//
|
|
840
|
-
//
|
|
841
|
-
//
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
*
|
|
864
|
-
*
|
|
865
|
-
*
|
|
866
|
-
*
|
|
867
|
-
*
|
|
868
|
-
*
|
|
869
|
-
*
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
*
|
|
900
|
-
*
|
|
901
|
-
*
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
*
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
*
|
|
993
|
-
*
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
if (value
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
if (
|
|
1008
|
-
|
|
1009
|
-
name === "
|
|
1010
|
-
name === "
|
|
1011
|
-
name === "
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
//
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
"
|
|
1022
|
-
"
|
|
1023
|
-
"border-image-
|
|
1024
|
-
"border-image-
|
|
1025
|
-
"
|
|
1026
|
-
"
|
|
1027
|
-
"
|
|
1028
|
-
"
|
|
1029
|
-
"flex",
|
|
1030
|
-
"flex-
|
|
1031
|
-
"
|
|
1032
|
-
"
|
|
1033
|
-
"
|
|
1034
|
-
"grid-
|
|
1035
|
-
"grid-column",
|
|
1036
|
-
"grid-column-
|
|
1037
|
-
"grid-
|
|
1038
|
-
"grid-row",
|
|
1039
|
-
"grid-row-
|
|
1040
|
-
"
|
|
1041
|
-
"line-
|
|
1042
|
-
"
|
|
1043
|
-
"
|
|
1044
|
-
"
|
|
1045
|
-
"
|
|
1046
|
-
"
|
|
1047
|
-
"
|
|
1048
|
-
"stroke-
|
|
1049
|
-
"stroke-
|
|
1050
|
-
"stroke-
|
|
1051
|
-
"stroke-
|
|
1052
|
-
"
|
|
1053
|
-
"
|
|
1054
|
-
"
|
|
1055
|
-
"
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
if (
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
!
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
const
|
|
1082
|
-
const
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
if (
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
if (
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
*
|
|
1117
|
-
*
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
)
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
*
|
|
1137
|
-
*
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
*
|
|
1155
|
-
*
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
)
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
*
|
|
1177
|
-
*
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
1182
|
-
*
|
|
1183
|
-
*
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
const
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
let
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
if (cur.data === "g")
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
let
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
if (
|
|
1218
|
-
if (n.data === "g")
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
1229
|
-
*
|
|
1230
|
-
*
|
|
1231
|
-
*
|
|
1232
|
-
*
|
|
1233
|
-
*
|
|
1234
|
-
*
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
let
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
//
|
|
1248
|
-
|
|
1249
|
-
let
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
if (
|
|
1254
|
-
if (node.data === "s")
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
const run
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1267
|
-
*
|
|
1268
|
-
*
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
const
|
|
1273
|
-
const
|
|
1274
|
-
const
|
|
1275
|
-
|
|
1276
|
-
//
|
|
1277
|
-
//
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1318
|
-
*
|
|
1319
|
-
*
|
|
1320
|
-
*
|
|
1321
|
-
*
|
|
1322
|
-
*
|
|
1323
|
-
*
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
const
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
nodesVar.
|
|
1353
|
-
|
|
1354
|
-
)
|
|
1355
|
-
|
|
1356
|
-
const
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
(
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
...all.slice(
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
)
|
|
1380
|
-
if (
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
*
|
|
1398
|
-
*
|
|
1399
|
-
*
|
|
1400
|
-
*
|
|
1401
|
-
*
|
|
1402
|
-
*
|
|
1403
|
-
*
|
|
1404
|
-
*
|
|
1405
|
-
*
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
let
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
}
|
|
1425
|
-
//
|
|
1426
|
-
|
|
1427
|
-
let
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
if (
|
|
1431
|
-
if (node.data === "g")
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1450
|
-
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
*
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
//
|
|
1462
|
-
//
|
|
1463
|
-
|
|
1464
|
-
let
|
|
1465
|
-
let
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
let
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
if (
|
|
1473
|
-
if (node.data === "g")
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
parent.appendChild(
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
value ===
|
|
1513
|
-
value ===
|
|
1514
|
-
typeof value === "
|
|
1515
|
-
typeof value === "
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
if (
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
*
|
|
1553
|
-
*
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
)
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
//
|
|
1570
|
-
//
|
|
1571
|
-
//
|
|
1572
|
-
//
|
|
1573
|
-
//
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
*
|
|
1582
|
-
*
|
|
1583
|
-
*
|
|
1584
|
-
*
|
|
1585
|
-
*
|
|
1586
|
-
*
|
|
1587
|
-
*
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
*
|
|
1627
|
-
*
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
*
|
|
1640
|
-
*
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
}
|
|
1
|
+
export { __asyncBoundary } from "./async-boundary.ts";
|
|
2
|
+
export { Boundary } from "./boundary";
|
|
3
|
+
export {
|
|
4
|
+
easeInCubic,
|
|
5
|
+
easeInOutCubic,
|
|
6
|
+
easeInOutQuad,
|
|
7
|
+
easeInOutQuart,
|
|
8
|
+
easeInOutQuint,
|
|
9
|
+
easeInOutSine,
|
|
10
|
+
easeInQuad,
|
|
11
|
+
easeInQuart,
|
|
12
|
+
easeInQuint,
|
|
13
|
+
easeInSine,
|
|
14
|
+
easeOutCubic,
|
|
15
|
+
easeOutQuad,
|
|
16
|
+
easeOutQuart,
|
|
17
|
+
easeOutQuint,
|
|
18
|
+
easeOutSine,
|
|
19
|
+
linear,
|
|
20
|
+
} from "./easing.ts";
|
|
21
|
+
export { ReactiveMap } from "./reactivity/map.ts";
|
|
22
|
+
export { ReactiveSet } from "./reactivity/set.ts";
|
|
23
|
+
export { mountTransition, removeAnimated } from "./transition.ts";
|
|
24
|
+
|
|
25
|
+
export const SIGIL_SAFE = Symbol.for("sigil.safe");
|
|
26
|
+
export class SafeHtml {
|
|
27
|
+
[SIGIL_SAFE] = true;
|
|
28
|
+
constructor(public v: string) {}
|
|
29
|
+
toString() {
|
|
30
|
+
return this.v;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let currentEffect: (() => void) | null = null;
|
|
35
|
+
let currentDeps: Set<Set<() => void>> | null = null;
|
|
36
|
+
|
|
37
|
+
// ─── ownership ───
|
|
38
|
+
//
|
|
39
|
+
// Every component call gets an owner whose parent is the component that
|
|
40
|
+
// rendered it, so context is looked up through the tree, not one global map.
|
|
41
|
+
// Effects remember the owner they were created under and run under it again:
|
|
42
|
+
// list rows, slots and directives built in a later update still see the
|
|
43
|
+
// context of the component that declared them.
|
|
44
|
+
|
|
45
|
+
export interface Owner {
|
|
46
|
+
parent: Owner | null;
|
|
47
|
+
context: Map<symbol, unknown> | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let currentOwner: Owner | null = null;
|
|
51
|
+
|
|
52
|
+
export function getOwner(): Owner | null {
|
|
53
|
+
return currentOwner;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Run fn() under `owner`, as if called from inside that component. */
|
|
57
|
+
export function runWithOwner<T>(owner: Owner | null, fn: () => T): T {
|
|
58
|
+
const prev = currentOwner;
|
|
59
|
+
currentOwner = owner;
|
|
60
|
+
try {
|
|
61
|
+
return fn();
|
|
62
|
+
} finally {
|
|
63
|
+
currentOwner = prev;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** `<Comp {...props} />`: the compiler calls components through this. */
|
|
68
|
+
export function __component<P, R>(Comp: (props: P) => R, props: P): R {
|
|
69
|
+
return runWithOwner({ parent: currentOwner, context: null }, () =>
|
|
70
|
+
Comp(props),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* `await x` in an async component compiles to `__resume(await x, owner)`:
|
|
76
|
+
* the code after an await runs under the component's owner again, and the
|
|
77
|
+
* owner is put back once that stretch of synchronous code is done.
|
|
78
|
+
*/
|
|
79
|
+
export function __resume<T>(value: T, owner: Owner | null): T {
|
|
80
|
+
const prev = currentOwner;
|
|
81
|
+
currentOwner = owner;
|
|
82
|
+
queueMicrotask(() => {
|
|
83
|
+
if (currentOwner === owner) currentOwner = prev;
|
|
84
|
+
});
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let batchDepth = 0;
|
|
89
|
+
let scheduledFlush = false;
|
|
90
|
+
const pendingEffects = new Set<() => void>();
|
|
91
|
+
const pendingPreEffects = new Set<() => void>();
|
|
92
|
+
|
|
93
|
+
/** Runs of one effect within one flush before it counts as an update loop. */
|
|
94
|
+
const MAX_RUNS_PER_FLUSH = 100;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Run queued effects until none are left. One at a time, and pre effects
|
|
98
|
+
* (memos, `$effect.pre`) first: a memo settles before any effect that reads
|
|
99
|
+
* it runs, and an effect queued again while it is still pending runs again
|
|
100
|
+
* instead of being lost in the set already being walked.
|
|
101
|
+
*/
|
|
102
|
+
function flush() {
|
|
103
|
+
scheduledFlush = true; // schedules made during the flush are drained here
|
|
104
|
+
const runs = new Map<() => void, number>();
|
|
105
|
+
try {
|
|
106
|
+
while (pendingPreEffects.size || pendingEffects.size) {
|
|
107
|
+
const queue = pendingPreEffects.size ? pendingPreEffects : pendingEffects;
|
|
108
|
+
const effect = queue.values().next().value!;
|
|
109
|
+
queue.delete(effect);
|
|
110
|
+
const n = (runs.get(effect) ?? 0) + 1;
|
|
111
|
+
runs.set(effect, n);
|
|
112
|
+
if (n > MAX_RUNS_PER_FLUSH) {
|
|
113
|
+
pendingPreEffects.clear();
|
|
114
|
+
pendingEffects.clear();
|
|
115
|
+
throw new Error(
|
|
116
|
+
`[sigil] an effect re-ran ${MAX_RUNS_PER_FLUSH} times in one update: it writes state it also reads (infinite update loop).`,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
effect();
|
|
121
|
+
} catch (e) {
|
|
122
|
+
// one failing effect must not stop the rest; still reported
|
|
123
|
+
queueMicrotask(() => {
|
|
124
|
+
throw e;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} finally {
|
|
129
|
+
scheduledFlush = false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function scheduleEffect(fn: () => void) {
|
|
134
|
+
const target = (fn as any).__sigil_pre ? pendingPreEffects : pendingEffects;
|
|
135
|
+
target.add(fn);
|
|
136
|
+
if (batchDepth === 0 && !scheduledFlush) {
|
|
137
|
+
scheduledFlush = true;
|
|
138
|
+
queueMicrotask(flush);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const targetDepsMap = new WeakMap<
|
|
143
|
+
object,
|
|
144
|
+
Map<string | symbol, Set<() => void>>
|
|
145
|
+
>();
|
|
146
|
+
|
|
147
|
+
function getDeps(target: object, key: string | symbol): Set<() => void> {
|
|
148
|
+
let deps = targetDepsMap.get(target);
|
|
149
|
+
if (!deps) {
|
|
150
|
+
deps = new Map();
|
|
151
|
+
targetDepsMap.set(target, deps);
|
|
152
|
+
}
|
|
153
|
+
let set = deps.get(key);
|
|
154
|
+
if (!set) {
|
|
155
|
+
set = new Set();
|
|
156
|
+
deps.set(key, set);
|
|
157
|
+
}
|
|
158
|
+
return set;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function track(target: object, key: string | symbol) {
|
|
162
|
+
if (currentEffect && currentDeps) {
|
|
163
|
+
const deps = getDeps(target, key);
|
|
164
|
+
deps.add(currentEffect);
|
|
165
|
+
currentDeps.add(deps); // Set, deduplicates automatically
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function trigger(target: object, key: string | symbol) {
|
|
170
|
+
const deps = targetDepsMap.get(target);
|
|
171
|
+
if (!deps) return;
|
|
172
|
+
const set = deps.get(key);
|
|
173
|
+
if (!set) return;
|
|
174
|
+
for (const fn of [...set]) {
|
|
175
|
+
scheduleEffect(fn);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Replace a reactive object's contents. A symbol, so no data key can clash. */
|
|
180
|
+
const REPLACE = Symbol("sigil.replace");
|
|
181
|
+
|
|
182
|
+
const proxyCache = new WeakMap<object, object>();
|
|
183
|
+
const proxyToTarget = new WeakMap<object, object>();
|
|
184
|
+
const reactiveTargets = new WeakSet<object>();
|
|
185
|
+
const ARRAY_MUTATORS = new Set([
|
|
186
|
+
"push",
|
|
187
|
+
"pop",
|
|
188
|
+
"shift",
|
|
189
|
+
"unshift",
|
|
190
|
+
"splice",
|
|
191
|
+
"sort",
|
|
192
|
+
"reverse",
|
|
193
|
+
"fill",
|
|
194
|
+
]);
|
|
195
|
+
|
|
196
|
+
function createReactiveProxy<T extends object>(target: T): T {
|
|
197
|
+
const existing = proxyCache.get(target);
|
|
198
|
+
if (existing) return existing as T;
|
|
199
|
+
|
|
200
|
+
const proxy = new Proxy(target, {
|
|
201
|
+
get(target, key, receiver) {
|
|
202
|
+
if (
|
|
203
|
+
Array.isArray(target) &&
|
|
204
|
+
typeof key === "string" &&
|
|
205
|
+
ARRAY_MUTATORS.has(key)
|
|
206
|
+
) {
|
|
207
|
+
return (...args: any[]) => {
|
|
208
|
+
const result = (target as any)[key](...args);
|
|
209
|
+
// sort/reverse/splice/shift move items between indexes, so
|
|
210
|
+
// every tracked index (and length) may now read differently
|
|
211
|
+
const deps = targetDepsMap.get(target);
|
|
212
|
+
if (deps) for (const k of [...deps.keys()]) trigger(target, k);
|
|
213
|
+
return result;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
if (key === REPLACE) {
|
|
217
|
+
return (newValue: T) => {
|
|
218
|
+
const oldKeys = new Set(Object.keys(target));
|
|
219
|
+
const newKeys = new Set(Object.keys(newValue));
|
|
220
|
+
for (const k of oldKeys) {
|
|
221
|
+
if (!newKeys.has(k)) {
|
|
222
|
+
delete (target as any)[k];
|
|
223
|
+
trigger(target, k);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const k of newKeys) {
|
|
227
|
+
(target as any)[k] = (newValue as any)[k];
|
|
228
|
+
trigger(target, k);
|
|
229
|
+
}
|
|
230
|
+
if (Array.isArray(target)) {
|
|
231
|
+
// delete leaves holes — length must shrink to the new value's,
|
|
232
|
+
// or iteration (spread, reconcile) sees phantom trailing slots
|
|
233
|
+
target.length = (newValue as unknown[]).length;
|
|
234
|
+
trigger(target, "length");
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (typeof key === "symbol") {
|
|
240
|
+
return Reflect.get(target, key, receiver);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
track(target, key);
|
|
244
|
+
const value = target[key as keyof T];
|
|
245
|
+
|
|
246
|
+
if (typeof value === "object" && value !== null) {
|
|
247
|
+
return createReactiveProxy(value);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return value;
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
set(target, key, newValue, receiver) {
|
|
254
|
+
const oldValue = target[key as keyof T];
|
|
255
|
+
const result = Reflect.set(target, key, newValue, receiver);
|
|
256
|
+
if (oldValue !== newValue) {
|
|
257
|
+
trigger(target, key as string | symbol);
|
|
258
|
+
if (
|
|
259
|
+
Array.isArray(target) &&
|
|
260
|
+
typeof key === "string" &&
|
|
261
|
+
!Number.isNaN(Number(key))
|
|
262
|
+
) {
|
|
263
|
+
trigger(target, "length");
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
deleteProperty(target, key) {
|
|
270
|
+
const result = Reflect.deleteProperty(target, key);
|
|
271
|
+
trigger(target, key as string | symbol);
|
|
272
|
+
return result;
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
proxyCache.set(target, proxy);
|
|
277
|
+
proxyToTarget.set(proxy, target);
|
|
278
|
+
reactiveTargets.add(target);
|
|
279
|
+
return proxy as T;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export type Signal<T> = {
|
|
283
|
+
(): T;
|
|
284
|
+
set: (next: T) => void;
|
|
285
|
+
peek: () => T;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
export function batch(fn: () => void) {
|
|
289
|
+
batchDepth++;
|
|
290
|
+
try {
|
|
291
|
+
fn();
|
|
292
|
+
} finally {
|
|
293
|
+
batchDepth--;
|
|
294
|
+
}
|
|
295
|
+
if (batchDepth === 0) flush();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function tick(): Promise<void> {
|
|
299
|
+
return new Promise((resolve) => queueMicrotask(resolve));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ─── signals ───
|
|
303
|
+
//
|
|
304
|
+
// One kind of signal for every value. A deep signal (`$state`) keeps any
|
|
305
|
+
// object it holds behind a reactive proxy, whichever value it starts or ends
|
|
306
|
+
// up with; a raw signal (`$state.raw`, memos) stores values as they are.
|
|
307
|
+
|
|
308
|
+
const signals = new WeakSet<object>();
|
|
309
|
+
|
|
310
|
+
/** Whether `value` is a signal getter. */
|
|
311
|
+
export function isSignal(value: unknown): value is Signal<unknown> {
|
|
312
|
+
return typeof value === "function" && signals.has(value);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const isPlain = (v: object) => {
|
|
316
|
+
const proto = Object.getPrototypeOf(v);
|
|
317
|
+
return proto === Object.prototype || proto === null;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** An object a deep signal holds, behind its proxy (never a proxy of a proxy). */
|
|
321
|
+
function reactive<T>(value: T): T {
|
|
322
|
+
if (typeof value !== "object" || value === null) return value;
|
|
323
|
+
if (proxyToTarget.has(value)) return value;
|
|
324
|
+
return createReactiveProxy(value);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function makeSignal<T>(initial: T, deep: boolean): Signal<T> {
|
|
328
|
+
const subs = new Set<() => void>();
|
|
329
|
+
let value = deep ? reactive(initial) : initial;
|
|
330
|
+
|
|
331
|
+
const getter = () => {
|
|
332
|
+
if (currentEffect && currentDeps) {
|
|
333
|
+
subs.add(currentEffect);
|
|
334
|
+
currentDeps.add(subs);
|
|
335
|
+
// a held array: track its length too, so push/pop/splice re-run
|
|
336
|
+
// readers that hand the array on without reading it themselves
|
|
337
|
+
const target = deep && proxyToTarget.get(value as object);
|
|
338
|
+
if (target && Array.isArray(target)) track(target, "length");
|
|
339
|
+
}
|
|
340
|
+
return value;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
getter.set = (next: T) => {
|
|
344
|
+
if (deep) {
|
|
345
|
+
// plain object to plain object, or array to array: replace the
|
|
346
|
+
// contents in place, so the object keeps its identity and only the
|
|
347
|
+
// keys that changed notify their readers
|
|
348
|
+
const target = proxyToTarget.get(value as object);
|
|
349
|
+
if (
|
|
350
|
+
target &&
|
|
351
|
+
typeof next === "object" &&
|
|
352
|
+
next !== null &&
|
|
353
|
+
!proxyToTarget.has(next) &&
|
|
354
|
+
(Array.isArray(target)
|
|
355
|
+
? Array.isArray(next)
|
|
356
|
+
: !Array.isArray(next) && isPlain(target) && isPlain(next))
|
|
357
|
+
) {
|
|
358
|
+
(value as any)[REPLACE](next);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
next = reactive(next);
|
|
362
|
+
if (Object.is(value, next)) return;
|
|
363
|
+
} else if (
|
|
364
|
+
// an equal primitive changes nothing; a raw object always notifies,
|
|
365
|
+
// since it may be set to the same object after being mutated
|
|
366
|
+
Object.is(value, next) &&
|
|
367
|
+
(typeof next !== "object" || next === null) &&
|
|
368
|
+
typeof next !== "function"
|
|
369
|
+
) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
value = next;
|
|
373
|
+
for (const fn of [...subs]) scheduleEffect(fn);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// untracked read; a deep signal's object comes back unwrapped
|
|
377
|
+
getter.peek = () => (deep && proxyToTarget.get(value as object)) || value;
|
|
378
|
+
|
|
379
|
+
signals.add(getter);
|
|
380
|
+
return getter as Signal<T>;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** `$state(v)`: objects it holds, now or later, are deeply reactive. */
|
|
384
|
+
export function createSignal<T>(initial: T): Signal<T> {
|
|
385
|
+
return makeSignal(initial, true);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** `$state.raw(v)`: values are stored as they are, only reassignment notifies. */
|
|
389
|
+
export function createRawSignal<T>(initial: T): Signal<T> {
|
|
390
|
+
return makeSignal(initial, false);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Kept for code compiled by older versions, which wrapped reads of project
|
|
395
|
+
* imports in it. The compiler now knows statically which imports are state.
|
|
396
|
+
*/
|
|
397
|
+
export function readStore<T>(value: T): T {
|
|
398
|
+
return isSignal(value) ? (value() as T) : value;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// A2: $state.snapshot — deep un-proxy a signal value
|
|
402
|
+
function deepUnproxy<T>(value: T): T {
|
|
403
|
+
if (isSignal(value)) return deepUnproxy(value() as T);
|
|
404
|
+
if (typeof value !== "object" || value === null) return value;
|
|
405
|
+
const raw = proxyToTarget.get(value as object);
|
|
406
|
+
if (!raw) return value; // not a proxy, return as-is
|
|
407
|
+
if (Array.isArray(raw)) {
|
|
408
|
+
return raw.map(deepUnproxy) as T;
|
|
409
|
+
}
|
|
410
|
+
const result: Record<string, unknown> = {};
|
|
411
|
+
for (const key of Object.keys(raw)) {
|
|
412
|
+
result[key] = deepUnproxy((raw as Record<string, unknown>)[key]);
|
|
413
|
+
}
|
|
414
|
+
return result as T;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function snapshot<T>(value: T): T {
|
|
418
|
+
return deepUnproxy(value);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// A3: $effect.tracking() — check if inside reactive context
|
|
422
|
+
export function tracking(): boolean {
|
|
423
|
+
return currentEffect !== null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Run fn() outside of any reactive context: signal reads inside fn() do not
|
|
428
|
+
* register dependencies on the enclosing effect.
|
|
429
|
+
*/
|
|
430
|
+
export function untrack<T>(fn: () => T): T {
|
|
431
|
+
const prevEffect = currentEffect;
|
|
432
|
+
const prevDeps = currentDeps;
|
|
433
|
+
currentEffect = null;
|
|
434
|
+
currentDeps = null;
|
|
435
|
+
try {
|
|
436
|
+
return fn();
|
|
437
|
+
} finally {
|
|
438
|
+
currentEffect = prevEffect;
|
|
439
|
+
currentDeps = prevDeps;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// A4: $inspect — dev-only effect logging
|
|
444
|
+
export function createInspectEffect(
|
|
445
|
+
getValues: () => unknown[],
|
|
446
|
+
cb?: (vals: unknown[]) => void,
|
|
447
|
+
): void {
|
|
448
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV === "production")
|
|
449
|
+
return;
|
|
450
|
+
createEffect(() => {
|
|
451
|
+
const vals = getValues();
|
|
452
|
+
if (cb) cb(vals);
|
|
453
|
+
else console.log(...vals);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// A5: $effect.pre — effect that flushes before DOM update
|
|
458
|
+
export function createEffectPre(
|
|
459
|
+
fn: () => undefined | (() => void),
|
|
460
|
+
): () => void {
|
|
461
|
+
let cleanup: (() => void) | undefined;
|
|
462
|
+
const ownDeps = new Set<Set<() => void>>();
|
|
463
|
+
|
|
464
|
+
const owner = currentOwner;
|
|
465
|
+
const effect = () => {
|
|
466
|
+
if (cleanup) cleanup();
|
|
467
|
+
for (const depSet of ownDeps) depSet.delete(effect);
|
|
468
|
+
ownDeps.clear();
|
|
469
|
+
|
|
470
|
+
const prevEffect = currentEffect;
|
|
471
|
+
const prevDeps = currentDeps;
|
|
472
|
+
const prevOwner = currentOwner;
|
|
473
|
+
currentEffect = effect;
|
|
474
|
+
currentDeps = ownDeps;
|
|
475
|
+
currentOwner = owner;
|
|
476
|
+
try {
|
|
477
|
+
cleanup = fn();
|
|
478
|
+
} finally {
|
|
479
|
+
currentEffect = prevEffect;
|
|
480
|
+
currentDeps = prevDeps;
|
|
481
|
+
currentOwner = prevOwner;
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
(effect as any).__sigil_pre = true;
|
|
486
|
+
effect();
|
|
487
|
+
|
|
488
|
+
const dispose = () => {
|
|
489
|
+
for (const depSet of ownDeps) depSet.delete(effect);
|
|
490
|
+
ownDeps.clear();
|
|
491
|
+
if (cleanup) cleanup();
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
((globalThis as any).__sigilEffectScope as (() => void)[] | undefined)?.push(
|
|
495
|
+
dispose,
|
|
496
|
+
);
|
|
497
|
+
return dispose;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// A9: Profiler effect timing (dev)
|
|
501
|
+
export function createProfilerEffect(
|
|
502
|
+
_name: string,
|
|
503
|
+
fn: () => undefined | (() => void),
|
|
504
|
+
): () => void {
|
|
505
|
+
return createEffect(() => {
|
|
506
|
+
if (
|
|
507
|
+
typeof process !== "undefined" &&
|
|
508
|
+
process.env.NODE_ENV === "production"
|
|
509
|
+
) {
|
|
510
|
+
return fn();
|
|
511
|
+
}
|
|
512
|
+
const _start = performance.now();
|
|
513
|
+
const cleanup = fn();
|
|
514
|
+
const _end = performance.now();
|
|
515
|
+
// console.log(`[Profiler] ${name}: ${(end - start).toFixed(2)}ms`);
|
|
516
|
+
return cleanup;
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Deferred effect mode for hydration: effects created while the flag is set
|
|
521
|
+
// are queued instead of run immediately, then flushed after DOM insertion.
|
|
522
|
+
const deferredEffectQueue: (() => void)[] = [];
|
|
523
|
+
// the queue of the innermost collectEffects() call, when there is one
|
|
524
|
+
let collectingQueue: (() => void)[] | null = null;
|
|
525
|
+
|
|
526
|
+
export function flushDeferredEffects(): void {
|
|
527
|
+
const pending = deferredEffectQueue.splice(0);
|
|
528
|
+
for (const run of pending) run();
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Run fn() with every effect it creates held back, and return its value with
|
|
533
|
+
* a flush() that runs them. For building DOM that is inserted later: a client
|
|
534
|
+
* navigation builds the next page while the old one is still showing, and an
|
|
535
|
+
* effect (or a use: directive) that measures or queries its element needs it
|
|
536
|
+
* in the document. The queue belongs to this call, so two navigations in
|
|
537
|
+
* flight cannot run each other's effects; an effect disposed before the
|
|
538
|
+
* flush (its page was replaced first) never runs.
|
|
539
|
+
*/
|
|
540
|
+
export function collectEffects<T>(fn: () => T): {
|
|
541
|
+
value: T;
|
|
542
|
+
flush: () => void;
|
|
543
|
+
} {
|
|
544
|
+
const g = globalThis as any;
|
|
545
|
+
const prevFlag = g.__sigilDeferEffects;
|
|
546
|
+
const prevQueue = collectingQueue;
|
|
547
|
+
const queue: (() => void)[] = [];
|
|
548
|
+
collectingQueue = queue;
|
|
549
|
+
g.__sigilDeferEffects = true;
|
|
550
|
+
let value: T;
|
|
551
|
+
try {
|
|
552
|
+
value = fn();
|
|
553
|
+
} finally {
|
|
554
|
+
g.__sigilDeferEffects = prevFlag;
|
|
555
|
+
collectingQueue = prevQueue;
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
value,
|
|
559
|
+
// one effect that throws must not stop the rest (or the page swap the
|
|
560
|
+
// flush runs in); the error is still reported, as an uncaught one
|
|
561
|
+
flush: () => {
|
|
562
|
+
for (const run of queue.splice(0)) {
|
|
563
|
+
try {
|
|
564
|
+
run();
|
|
565
|
+
} catch (e) {
|
|
566
|
+
queueMicrotask(() => {
|
|
567
|
+
throw e;
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
},
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: please shut up its a weird type
|
|
576
|
+
/**
|
|
577
|
+
* Set while `probeStatic` renders a component that was compiled as static, so
|
|
578
|
+
* it can notice an effect that really tracks something.
|
|
579
|
+
*/
|
|
580
|
+
let activeProbe: { live: number; listeners: number } | null = null;
|
|
581
|
+
|
|
582
|
+
export function createEffect(fn: () => void | (() => void)): () => void {
|
|
583
|
+
let cleanup: (() => void) | undefined;
|
|
584
|
+
let disposed = false;
|
|
585
|
+
const ownDeps = new Set<Set<() => void>>();
|
|
586
|
+
|
|
587
|
+
const owner = currentOwner;
|
|
588
|
+
const effect = () => {
|
|
589
|
+
if (cleanup) cleanup();
|
|
590
|
+
for (const depSet of ownDeps) depSet.delete(effect);
|
|
591
|
+
ownDeps.clear();
|
|
592
|
+
|
|
593
|
+
const prevEffect = currentEffect;
|
|
594
|
+
const prevDeps = currentDeps;
|
|
595
|
+
const prevOwner = currentOwner;
|
|
596
|
+
currentEffect = effect;
|
|
597
|
+
currentDeps = ownDeps;
|
|
598
|
+
currentOwner = owner;
|
|
599
|
+
try {
|
|
600
|
+
//@ts-expect-error kill yourself
|
|
601
|
+
cleanup = fn();
|
|
602
|
+
} finally {
|
|
603
|
+
currentEffect = prevEffect;
|
|
604
|
+
currentDeps = prevDeps;
|
|
605
|
+
currentOwner = prevOwner;
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
// In deferred mode (hydration), queue the effect instead of running immediately.
|
|
610
|
+
// This ensures the DOM is inserted before effect callbacks query it.
|
|
611
|
+
if ((globalThis as any).__sigilDeferEffects) {
|
|
612
|
+
(collectingQueue ?? deferredEffectQueue).push(() => {
|
|
613
|
+
if (!disposed) effect();
|
|
614
|
+
});
|
|
615
|
+
} else {
|
|
616
|
+
effect();
|
|
617
|
+
// an effect that read a signal is live; one that read only plain
|
|
618
|
+
// values (a prop that never changes) tracks nothing and is not
|
|
619
|
+
if (activeProbe && ownDeps.size > 0) activeProbe.live++;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const dispose = () => {
|
|
623
|
+
disposed = true;
|
|
624
|
+
for (const depSet of ownDeps) depSet.delete(effect);
|
|
625
|
+
ownDeps.clear();
|
|
626
|
+
if (cleanup) cleanup();
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
((globalThis as any).__sigilEffectScope as (() => void)[] | undefined)?.push(
|
|
630
|
+
dispose,
|
|
631
|
+
);
|
|
632
|
+
return dispose;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Run fn() and return a dispose function that tears down every effect
|
|
637
|
+
* created during that call (including effects inside child createMemo calls).
|
|
638
|
+
* Supports nesting: inner scopes don't leak to outer ones.
|
|
639
|
+
*/
|
|
640
|
+
export function withEffectScope(fn: () => void): () => void {
|
|
641
|
+
const prev = (globalThis as any).__sigilEffectScope;
|
|
642
|
+
const disposers: (() => void)[] = [];
|
|
643
|
+
(globalThis as any).__sigilEffectScope = disposers;
|
|
644
|
+
try {
|
|
645
|
+
fn();
|
|
646
|
+
} finally {
|
|
647
|
+
(globalThis as any).__sigilEffectScope = prev;
|
|
648
|
+
}
|
|
649
|
+
return () => {
|
|
650
|
+
for (const d of disposers) d();
|
|
651
|
+
disposers.length = 0;
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
type EffectScopeGlobal = { __sigilEffectScope?: (() => void)[] };
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Run fn() with a detached effect scope. Effects created inside are invisible
|
|
659
|
+
* to enclosing scopes (the scope array is replaced, not merged) and live until
|
|
660
|
+
* dispose() is called — for app bootstrap, routers, and long-lived listeners.
|
|
661
|
+
*/
|
|
662
|
+
export function createRoot<T>(fn: (dispose: () => void) => T): T {
|
|
663
|
+
const g = globalThis as EffectScopeGlobal;
|
|
664
|
+
const prev = g.__sigilEffectScope;
|
|
665
|
+
const disposers: (() => void)[] = [];
|
|
666
|
+
g.__sigilEffectScope = disposers;
|
|
667
|
+
const dispose = () => {
|
|
668
|
+
for (const d of disposers.splice(0)) d();
|
|
669
|
+
};
|
|
670
|
+
try {
|
|
671
|
+
return fn(dispose);
|
|
672
|
+
} finally {
|
|
673
|
+
g.__sigilEffectScope = prev;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
export interface Resource<T> {
|
|
678
|
+
data: () => T | undefined;
|
|
679
|
+
loading: () => boolean;
|
|
680
|
+
error: () => Error | undefined;
|
|
681
|
+
refetch: () => void;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Async data source with reactive re-fetching. Re-runs when `source()` changes
|
|
686
|
+
* or refetch() is called; stale responses are discarded by request id.
|
|
687
|
+
*/
|
|
688
|
+
export function createResource<T>(
|
|
689
|
+
fetcher: (source: unknown, info: { refetch: () => void }) => Promise<T>,
|
|
690
|
+
source?: () => unknown,
|
|
691
|
+
): Resource<T> {
|
|
692
|
+
// raw: the fetched value is handed back as is, not wrapped in a proxy
|
|
693
|
+
const data = createRawSignal<T | undefined>(undefined);
|
|
694
|
+
const loading = createSignal(true);
|
|
695
|
+
const error = createSignal<Error | undefined>(undefined);
|
|
696
|
+
const version = createSignal(0);
|
|
697
|
+
let requestCount = 0;
|
|
698
|
+
let versionCounter = 0;
|
|
699
|
+
|
|
700
|
+
const refetch = () => {
|
|
701
|
+
versionCounter++;
|
|
702
|
+
version.set(versionCounter);
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
createEffect(() => {
|
|
706
|
+
const src = source?.();
|
|
707
|
+
version(); // track refetch bumps
|
|
708
|
+
const id = ++requestCount;
|
|
709
|
+
loading.set(true);
|
|
710
|
+
error.set(undefined);
|
|
711
|
+
fetcher(src, { refetch }).then(
|
|
712
|
+
(result) => {
|
|
713
|
+
if (id !== requestCount) return; // stale response
|
|
714
|
+
data.set(result);
|
|
715
|
+
loading.set(false);
|
|
716
|
+
},
|
|
717
|
+
(err: unknown) => {
|
|
718
|
+
if (id !== requestCount) return;
|
|
719
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
720
|
+
loading.set(false);
|
|
721
|
+
},
|
|
722
|
+
);
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
return { data, loading, error, refetch };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* A derived value. Computed immediately, even while effects are deferred
|
|
730
|
+
* (hydration, collectEffects): code reading it during render needs the value,
|
|
731
|
+
* not undefined. Recomputes as a pre effect, so it is fresh before any plain
|
|
732
|
+
* effect that reads it runs, and an unchanged result notifies no one.
|
|
733
|
+
*/
|
|
734
|
+
export function createMemo<T>(fn: () => T): Signal<T> {
|
|
735
|
+
const inner = createRawSignal<T>(undefined as T);
|
|
736
|
+
createEffectPre(() => {
|
|
737
|
+
inner.set(fn());
|
|
738
|
+
return undefined;
|
|
739
|
+
});
|
|
740
|
+
return inner;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Reconcile a list of keyed DOM nodes.
|
|
745
|
+
* anchor.before(node) handles both insert and move natively.
|
|
746
|
+
* @param claimNode - Optional hydration callback. Called with (element, key) for
|
|
747
|
+
* server-rendered elements removed from the DOM. Returns the element to use,
|
|
748
|
+
* or null to discard.
|
|
749
|
+
*/
|
|
750
|
+
const lastItemFor = new WeakMap<object, unknown>();
|
|
751
|
+
/** The position a row was built at, for rows whose body reads the index. */
|
|
752
|
+
const lastIndexFor = new WeakMap<object, number>();
|
|
753
|
+
|
|
754
|
+
export function reconcile<T>(
|
|
755
|
+
anchor: Partial<Comment>,
|
|
756
|
+
keyMap: Map<unknown, Node>,
|
|
757
|
+
newItems: T[],
|
|
758
|
+
getKey: (item: T, index: number) => unknown,
|
|
759
|
+
createNode: (item: T, index: number) => Node,
|
|
760
|
+
/**
|
|
761
|
+
* The row body reads the index (`list.map((item, i) => …)`). Row bodies
|
|
762
|
+
* bake what they read at creation, so a row that moved is rebuilt to show
|
|
763
|
+
* its new position, the same way a row whose item changed is.
|
|
764
|
+
*/
|
|
765
|
+
usesIndex = false,
|
|
766
|
+
): void {
|
|
767
|
+
const newKeys = new Set(newItems.map((item, i) => getKey(item, i)));
|
|
768
|
+
|
|
769
|
+
// Remove nodes no longer in list (or claim them for hydration)
|
|
770
|
+
for (const [key, node] of keyMap) {
|
|
771
|
+
if (!newKeys.has(key)) {
|
|
772
|
+
node.parentNode?.removeChild(node);
|
|
773
|
+
keyMap.delete(key);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Insert/move in order — anchor.before() handles both cases.
|
|
778
|
+
// A surviving node whose item instance changed is rebuilt: compiled row
|
|
779
|
+
// bodies bake item reads at creation time, so immutable-style updates
|
|
780
|
+
// (new object, same key) must swap the node to show fresh content.
|
|
781
|
+
for (let i = 0; i < newItems.length; i++) {
|
|
782
|
+
const item = newItems[i]!;
|
|
783
|
+
const key = getKey(item, i);
|
|
784
|
+
let node = keyMap.get(key);
|
|
785
|
+
if (
|
|
786
|
+
node &&
|
|
787
|
+
((lastItemFor.has(node) && !Object.is(lastItemFor.get(node), item)) ||
|
|
788
|
+
(usesIndex && lastIndexFor.get(node) !== i))
|
|
789
|
+
) {
|
|
790
|
+
node.parentNode?.removeChild(node);
|
|
791
|
+
keyMap.delete(key);
|
|
792
|
+
node = undefined;
|
|
793
|
+
}
|
|
794
|
+
if (!node) {
|
|
795
|
+
node = createNode(item, i);
|
|
796
|
+
lastItemFor.set(node, item);
|
|
797
|
+
lastIndexFor.set(node, i);
|
|
798
|
+
keyMap.set(key, node);
|
|
799
|
+
}
|
|
800
|
+
anchor.before?.(node);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Claim an existing element from a hydration pool by tag name, or create a new one.
|
|
806
|
+
* Removes the claimed node from the pool array.
|
|
807
|
+
* When falling back to createElement, appends to parent if provided (SPA mode).
|
|
808
|
+
* SVG elements (and children of SVG parents) are created with createElementNS —
|
|
809
|
+
* plain createElement puts them in the HTML namespace where they render as
|
|
810
|
+
* invisible unknown elements (killed CSR-rendered inline SVG, e.g. topbar icons).
|
|
811
|
+
*/
|
|
812
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
813
|
+
|
|
814
|
+
const claimedNodes = new WeakSet<Node>();
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* True when `claim()` took this element from server HTML rather than creating
|
|
818
|
+
* it. Hydration skips the element's static attributes then: the server
|
|
819
|
+
* already rendered them, and writing them again would wipe anything a page
|
|
820
|
+
* script changed before hydration ran (an added class, a data-* flag).
|
|
821
|
+
*/
|
|
822
|
+
export function isClaimed(el: Node): boolean {
|
|
823
|
+
return claimedNodes.has(el);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
export function claim(
|
|
827
|
+
nodes: ChildNode[],
|
|
828
|
+
tag: string,
|
|
829
|
+
parent?: Element,
|
|
830
|
+
): Element {
|
|
831
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
832
|
+
const node = nodes[i];
|
|
833
|
+
if (
|
|
834
|
+
node instanceof Element &&
|
|
835
|
+
node.tagName.toLowerCase() === tag.toLowerCase()
|
|
836
|
+
) {
|
|
837
|
+
// SSR injects a _csrf hidden input as the first child of every <form>
|
|
838
|
+
// (rendering/index.ts). The compiled hydration code doesn't know about
|
|
839
|
+
// it, so it must never be claimed — claiming it shifts the whole chain
|
|
840
|
+
// by one and duplicates the last form child (e.g. two display_name
|
|
841
|
+
// inputs). Skip it; it stays in place as a live DOM node.
|
|
842
|
+
if (
|
|
843
|
+
tag.toLowerCase() === "input" &&
|
|
844
|
+
node.hasAttribute("data-grimoire-csrf")
|
|
845
|
+
) {
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
nodes.splice(i, 1);
|
|
849
|
+
claimedNodes.add(node);
|
|
850
|
+
return node;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
const el =
|
|
854
|
+
tag.toLowerCase() === "svg" || parent?.namespaceURI === SVG_NS
|
|
855
|
+
? document.createElementNS(SVG_NS, tag)
|
|
856
|
+
: document.createElement(tag);
|
|
857
|
+
if (parent) parent.appendChild(el);
|
|
858
|
+
return el;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* Write a JSX prop to an element. On HTML elements this is `el[name] = value`
|
|
863
|
+
* (preserves IDL semantics for value/checked/etc) whenever the element has a
|
|
864
|
+
* writable property of that name. Names with no writable property go through
|
|
865
|
+
* setAttribute instead, like SSR does: some IDL attributes are getter-only
|
|
866
|
+
* (`input.list`, `input.form`, `button.form`), and assigning them throws and
|
|
867
|
+
* aborts the whole render; unknown names would otherwise land as a bare JS
|
|
868
|
+
* expando the DOM never sees. On SVG-namespace elements everything is
|
|
869
|
+
* attribute-routed — IDL property assignment throws there too (`svg.width`).
|
|
870
|
+
*/
|
|
871
|
+
// Never attribute-routed, even inside SVG.
|
|
872
|
+
const SVG_PROP_ONLY = new Set(["textContent", "innerHTML", "outerHTML"]);
|
|
873
|
+
|
|
874
|
+
// Per-prototype cache of "can `el[name] = v` be assigned?".
|
|
875
|
+
const writableCache = new WeakMap<object, Map<string, boolean>>();
|
|
876
|
+
|
|
877
|
+
function isWritableProp(el: Element, name: string): boolean {
|
|
878
|
+
const proto = Object.getPrototypeOf(el) as object;
|
|
879
|
+
let cache = writableCache.get(proto);
|
|
880
|
+
if (!cache) writableCache.set(proto, (cache = new Map()));
|
|
881
|
+
let writable = cache.get(name);
|
|
882
|
+
if (writable === undefined) {
|
|
883
|
+
writable = false;
|
|
884
|
+
for (let o: object | null = proto; o; o = Object.getPrototypeOf(o)) {
|
|
885
|
+
const d = Object.getOwnPropertyDescriptor(o, name);
|
|
886
|
+
if (d) {
|
|
887
|
+
writable = !!(d.set || d.writable);
|
|
888
|
+
break;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
cache.set(name, writable);
|
|
892
|
+
}
|
|
893
|
+
// Own properties (expandos set earlier) are always assignable.
|
|
894
|
+
return writable || Object.hasOwn(el, name);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* React-style camelCase names for SVG presentation attributes, mapped to the
|
|
899
|
+
* attribute names SVG actually uses (`strokeWidth` → `stroke-width`). Only
|
|
900
|
+
* names whose real spelling is kebab-case are here: `viewBox`, `refX`,
|
|
901
|
+
* `markerWidth` and friends really are camelCase and pass through as written.
|
|
902
|
+
*
|
|
903
|
+
* No HTML attribute has any of these names, so they map on every element.
|
|
904
|
+
* That keeps the server (which can't see namespaces) and the client agreeing.
|
|
905
|
+
*
|
|
906
|
+
* The compiler keeps a copy for SSR (compiler/src/babel/util/svg-attrs.ts); a
|
|
907
|
+
* test checks the two stay identical.
|
|
908
|
+
*/
|
|
909
|
+
export const SVG_ATTR: Record<string, string> = {
|
|
910
|
+
accentHeight: "accent-height",
|
|
911
|
+
alignmentBaseline: "alignment-baseline",
|
|
912
|
+
arabicForm: "arabic-form",
|
|
913
|
+
baselineShift: "baseline-shift",
|
|
914
|
+
capHeight: "cap-height",
|
|
915
|
+
clipPath: "clip-path",
|
|
916
|
+
clipRule: "clip-rule",
|
|
917
|
+
colorInterpolation: "color-interpolation",
|
|
918
|
+
colorInterpolationFilters: "color-interpolation-filters",
|
|
919
|
+
colorProfile: "color-profile",
|
|
920
|
+
colorRendering: "color-rendering",
|
|
921
|
+
dominantBaseline: "dominant-baseline",
|
|
922
|
+
enableBackground: "enable-background",
|
|
923
|
+
fillOpacity: "fill-opacity",
|
|
924
|
+
fillRule: "fill-rule",
|
|
925
|
+
floodColor: "flood-color",
|
|
926
|
+
floodOpacity: "flood-opacity",
|
|
927
|
+
fontFamily: "font-family",
|
|
928
|
+
fontSize: "font-size",
|
|
929
|
+
fontSizeAdjust: "font-size-adjust",
|
|
930
|
+
fontStretch: "font-stretch",
|
|
931
|
+
fontStyle: "font-style",
|
|
932
|
+
fontVariant: "font-variant",
|
|
933
|
+
fontWeight: "font-weight",
|
|
934
|
+
glyphName: "glyph-name",
|
|
935
|
+
glyphOrientationHorizontal: "glyph-orientation-horizontal",
|
|
936
|
+
glyphOrientationVertical: "glyph-orientation-vertical",
|
|
937
|
+
horizAdvX: "horiz-adv-x",
|
|
938
|
+
horizOriginX: "horiz-origin-x",
|
|
939
|
+
imageRendering: "image-rendering",
|
|
940
|
+
letterSpacing: "letter-spacing",
|
|
941
|
+
lightingColor: "lighting-color",
|
|
942
|
+
markerEnd: "marker-end",
|
|
943
|
+
markerMid: "marker-mid",
|
|
944
|
+
markerStart: "marker-start",
|
|
945
|
+
overlinePosition: "overline-position",
|
|
946
|
+
overlineThickness: "overline-thickness",
|
|
947
|
+
paintOrder: "paint-order",
|
|
948
|
+
pointerEvents: "pointer-events",
|
|
949
|
+
renderingIntent: "rendering-intent",
|
|
950
|
+
shapeRendering: "shape-rendering",
|
|
951
|
+
stopColor: "stop-color",
|
|
952
|
+
stopOpacity: "stop-opacity",
|
|
953
|
+
strikethroughPosition: "strikethrough-position",
|
|
954
|
+
strikethroughThickness: "strikethrough-thickness",
|
|
955
|
+
strokeDasharray: "stroke-dasharray",
|
|
956
|
+
strokeDashoffset: "stroke-dashoffset",
|
|
957
|
+
strokeLinecap: "stroke-linecap",
|
|
958
|
+
strokeLinejoin: "stroke-linejoin",
|
|
959
|
+
strokeMiterlimit: "stroke-miterlimit",
|
|
960
|
+
strokeOpacity: "stroke-opacity",
|
|
961
|
+
strokeWidth: "stroke-width",
|
|
962
|
+
textAnchor: "text-anchor",
|
|
963
|
+
textDecoration: "text-decoration",
|
|
964
|
+
textRendering: "text-rendering",
|
|
965
|
+
transformOrigin: "transform-origin",
|
|
966
|
+
underlinePosition: "underline-position",
|
|
967
|
+
underlineThickness: "underline-thickness",
|
|
968
|
+
unicodeBidi: "unicode-bidi",
|
|
969
|
+
unicodeRange: "unicode-range",
|
|
970
|
+
unitsPerEm: "units-per-em",
|
|
971
|
+
vAlphabetic: "v-alphabetic",
|
|
972
|
+
vHanging: "v-hanging",
|
|
973
|
+
vIdeographic: "v-ideographic",
|
|
974
|
+
vMathematical: "v-mathematical",
|
|
975
|
+
vectorEffect: "vector-effect",
|
|
976
|
+
vertAdvY: "vert-adv-y",
|
|
977
|
+
vertOriginX: "vert-origin-x",
|
|
978
|
+
vertOriginY: "vert-origin-y",
|
|
979
|
+
wordSpacing: "word-spacing",
|
|
980
|
+
writingMode: "writing-mode",
|
|
981
|
+
xHeight: "x-height",
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
// JSX prop name → attribute name, where they differ.
|
|
985
|
+
const PROP_ATTR: Record<string, string> = {
|
|
986
|
+
className: "class",
|
|
987
|
+
htmlFor: "for",
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* null/undefined remove the attribute. Booleans follow HTML for plain names
|
|
992
|
+
* (true → present, false → absent) but are spelled out for dashed ones,
|
|
993
|
+
* because `aria-hidden="true"` and `data-open="false"` carry their value.
|
|
994
|
+
*/
|
|
995
|
+
function setAttr(el: Element, attr: string, value: unknown): void {
|
|
996
|
+
if (value == null) el.removeAttribute(attr);
|
|
997
|
+
else if (typeof value === "boolean" && !attr.includes("-")) {
|
|
998
|
+
if (value) el.setAttribute(attr, "");
|
|
999
|
+
else el.removeAttribute(attr);
|
|
1000
|
+
} else el.setAttribute(attr, String(value));
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// `el.href = undefined` would store the string "undefined": clear instead.
|
|
1004
|
+
function clearProp(el: Element, name: string): void {
|
|
1005
|
+
const rec = el as unknown as Record<string, unknown>;
|
|
1006
|
+
if (typeof rec[name] === "boolean") rec[name] = false;
|
|
1007
|
+
else if (
|
|
1008
|
+
name === "value" ||
|
|
1009
|
+
name === "innerHTML" ||
|
|
1010
|
+
name === "textContent" ||
|
|
1011
|
+
name === "innerText"
|
|
1012
|
+
)
|
|
1013
|
+
rec[name] = "";
|
|
1014
|
+
else el.removeAttribute(PROP_ATTR[name] ?? name.toLowerCase());
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// CSS properties that take plain numbers. Every other number gets "px",
|
|
1018
|
+
// the same list React uses. Keep in sync with the SSR copy in the compiler.
|
|
1019
|
+
const UNITLESS = new Set([
|
|
1020
|
+
"animation-iteration-count",
|
|
1021
|
+
"aspect-ratio",
|
|
1022
|
+
"border-image-outset",
|
|
1023
|
+
"border-image-slice",
|
|
1024
|
+
"border-image-width",
|
|
1025
|
+
"column-count",
|
|
1026
|
+
"columns",
|
|
1027
|
+
"fill-opacity",
|
|
1028
|
+
"flex",
|
|
1029
|
+
"flex-grow",
|
|
1030
|
+
"flex-shrink",
|
|
1031
|
+
"flood-opacity",
|
|
1032
|
+
"font-weight",
|
|
1033
|
+
"grid-area",
|
|
1034
|
+
"grid-column",
|
|
1035
|
+
"grid-column-end",
|
|
1036
|
+
"grid-column-start",
|
|
1037
|
+
"grid-row",
|
|
1038
|
+
"grid-row-end",
|
|
1039
|
+
"grid-row-start",
|
|
1040
|
+
"line-clamp",
|
|
1041
|
+
"line-height",
|
|
1042
|
+
"opacity",
|
|
1043
|
+
"order",
|
|
1044
|
+
"orphans",
|
|
1045
|
+
"scale",
|
|
1046
|
+
"stop-opacity",
|
|
1047
|
+
"stroke-dasharray",
|
|
1048
|
+
"stroke-dashoffset",
|
|
1049
|
+
"stroke-miterlimit",
|
|
1050
|
+
"stroke-opacity",
|
|
1051
|
+
"stroke-width",
|
|
1052
|
+
"tab-size",
|
|
1053
|
+
"widows",
|
|
1054
|
+
"z-index",
|
|
1055
|
+
"zoom",
|
|
1056
|
+
]);
|
|
1057
|
+
|
|
1058
|
+
/** fontSize → font-size, WebkitMask → -webkit-mask, --accent stays. */
|
|
1059
|
+
export function cssName(key: string): string {
|
|
1060
|
+
if (key.startsWith("--")) return key;
|
|
1061
|
+
const kebab = key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
1062
|
+
return kebab.startsWith("ms-") ? `-${kebab}` : kebab;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/** One style declaration value: numbers get px unless the property is unitless. */
|
|
1066
|
+
export function cssValue(name: string, value: unknown): string | null {
|
|
1067
|
+
if (value == null || value === false || value === "") return null;
|
|
1068
|
+
if (
|
|
1069
|
+
typeof value === "number" &&
|
|
1070
|
+
value !== 0 &&
|
|
1071
|
+
!name.startsWith("--") &&
|
|
1072
|
+
!UNITLESS.has(name)
|
|
1073
|
+
)
|
|
1074
|
+
return `${value}px`;
|
|
1075
|
+
return String(value);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
/** `style={{ marginTop: 4 }}` → one declaration; null/false removes it. */
|
|
1079
|
+
export function setStyleProp(el: Element, key: string, value: unknown): void {
|
|
1080
|
+
const style = (el as HTMLElement).style;
|
|
1081
|
+
const name = cssName(key);
|
|
1082
|
+
const v = cssValue(name, value);
|
|
1083
|
+
if (v === null) style.removeProperty(name);
|
|
1084
|
+
else style.setProperty(name, v);
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
function setStyle(el: Element, value: unknown): void {
|
|
1088
|
+
if (value == null || value === false) el.removeAttribute("style");
|
|
1089
|
+
else if (typeof value === "object") {
|
|
1090
|
+
el.removeAttribute("style");
|
|
1091
|
+
for (const [k, v] of Object.entries(value)) setStyleProp(el, k, v);
|
|
1092
|
+
} else el.setAttribute("style", String(value));
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
export function setProp(el: Element, name: string, value: unknown): void {
|
|
1096
|
+
if (name === "style") setStyle(el, value);
|
|
1097
|
+
else if (el.namespaceURI === SVG_NS) {
|
|
1098
|
+
if (SVG_PROP_ONLY.has(name))
|
|
1099
|
+
(el as unknown as Record<string, unknown>)[name] = value;
|
|
1100
|
+
else setAttr(el, PROP_ATTR[name] ?? SVG_ATTR[name] ?? name, value);
|
|
1101
|
+
} else if (!name.includes("-") && isWritableProp(el, name)) {
|
|
1102
|
+
if (value == null) clearProp(el, name);
|
|
1103
|
+
else (el as unknown as Record<string, unknown>)[name] = value;
|
|
1104
|
+
} else {
|
|
1105
|
+
setAttr(el, PROP_ATTR[name] ?? SVG_ATTR[name] ?? name, value);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
const SPREAD_PROP: Record<string, string> = {
|
|
1110
|
+
class: "className",
|
|
1111
|
+
for: "htmlFor",
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* `<div {...props}>`: listeners for on* functions, everything else through
|
|
1116
|
+
* setProp. `attrs: false` attaches only the listeners (hydrating a claimed
|
|
1117
|
+
* element, whose attributes are already in the server HTML).
|
|
1118
|
+
*/
|
|
1119
|
+
export function spread(
|
|
1120
|
+
el: Element,
|
|
1121
|
+
props: Record<string, unknown> | null | undefined,
|
|
1122
|
+
attrs = true,
|
|
1123
|
+
): void {
|
|
1124
|
+
if (!props) return;
|
|
1125
|
+
for (const [key, value] of Object.entries(props)) {
|
|
1126
|
+
if (/^on[A-Z]/.test(key) && typeof value === "function") {
|
|
1127
|
+
el.addEventListener(key.slice(2).toLowerCase(), value as EventListener);
|
|
1128
|
+
} else if (attrs && key !== "children" && key !== "key") {
|
|
1129
|
+
setProp(el, SPREAD_PROP[key] ?? key, value);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* Claim an existing Text node from a hydration pool, or create a new empty one.
|
|
1136
|
+
* Removes the claimed node from the pool array.
|
|
1137
|
+
* When falling back to createTextNode, appends to parent if provided (SPA mode).
|
|
1138
|
+
*/
|
|
1139
|
+
export function claimText(nodes: ChildNode[], parent?: Element): Text {
|
|
1140
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1141
|
+
const node = nodes[i];
|
|
1142
|
+
if (node instanceof Text) {
|
|
1143
|
+
nodes.splice(i, 1);
|
|
1144
|
+
return node;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
const text = document.createTextNode("");
|
|
1148
|
+
if (parent) parent.appendChild(text);
|
|
1149
|
+
return text;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* Claim an existing Comment node from a hydration pool, or create a new one.
|
|
1154
|
+
* Removes the claimed node from the pool array.
|
|
1155
|
+
* When falling back to createComment, appends to parent if provided (SPA mode).
|
|
1156
|
+
*/
|
|
1157
|
+
export function claimComment(
|
|
1158
|
+
nodes: ChildNode[],
|
|
1159
|
+
text: string = "",
|
|
1160
|
+
parent?: Element,
|
|
1161
|
+
): Comment {
|
|
1162
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1163
|
+
const node = nodes[i];
|
|
1164
|
+
if (node instanceof Comment && node.data === text) {
|
|
1165
|
+
nodes.splice(i, 1);
|
|
1166
|
+
return node;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
const comment = document.createComment(text);
|
|
1170
|
+
if (parent) parent.appendChild(comment);
|
|
1171
|
+
return comment;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* Hydrate a dynamic slot: take the server-rendered content between `anchor`
|
|
1176
|
+
* (its `<!--g-->`) and the matching `<!--/g-->` out of the DOM and out of the
|
|
1177
|
+
* hydration pool, and return it so the slot's first update replaces it.
|
|
1178
|
+
*
|
|
1179
|
+
* The match counts nesting. A fragment branch puts its children directly in
|
|
1180
|
+
* the slot, so a dynamic child of it (even an empty `{cond ? x : null}`, which
|
|
1181
|
+
* renders `<!--g--><!--/g-->`) sits at the same level; stopping at the first
|
|
1182
|
+
* `<!--/g-->` left the rest of the branch in place, and the slot then mounted
|
|
1183
|
+
* it a second time next to it.
|
|
1184
|
+
*/
|
|
1185
|
+
export function claimSlot(nodes: ChildNode[], anchor: Comment): ChildNode[] {
|
|
1186
|
+
const out: ChildNode[] = [];
|
|
1187
|
+
const drop = (n: ChildNode) => {
|
|
1188
|
+
const i = nodes.indexOf(n);
|
|
1189
|
+
if (i !== -1) nodes.splice(i, 1);
|
|
1190
|
+
};
|
|
1191
|
+
let depth = 0;
|
|
1192
|
+
let n = anchor.nextSibling;
|
|
1193
|
+
while (n) {
|
|
1194
|
+
const cur = n;
|
|
1195
|
+
n = n.nextSibling;
|
|
1196
|
+
if (cur instanceof Comment) {
|
|
1197
|
+
if (cur.data === "g") depth++;
|
|
1198
|
+
else if (cur.data === "/g" && depth-- === 0) {
|
|
1199
|
+
drop(cur);
|
|
1200
|
+
cur.remove();
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
drop(cur);
|
|
1205
|
+
cur.remove();
|
|
1206
|
+
out.push(cur);
|
|
1207
|
+
}
|
|
1208
|
+
return out;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
/** index of the `<!--/g-->` closing the slot opened at `start`, counting nesting; -1 if none */
|
|
1212
|
+
function matchingClose(nodes: ChildNode[], start: number): number {
|
|
1213
|
+
let depth = 0;
|
|
1214
|
+
for (let i = start + 1; i < nodes.length; i++) {
|
|
1215
|
+
const n = nodes[i];
|
|
1216
|
+
if (!(n instanceof Comment)) continue;
|
|
1217
|
+
if (n.data === "g") depth++;
|
|
1218
|
+
else if (n.data === "/g" && depth-- === 0) return i;
|
|
1219
|
+
}
|
|
1220
|
+
return -1;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Skip a component the compiler proved static.
|
|
1225
|
+
*
|
|
1226
|
+
* The server wraps such a use in `<!--s-->…<!--/s-->`. This takes that whole run
|
|
1227
|
+
* out of the hydration pool, so nothing claims its nodes by accident (`claim`
|
|
1228
|
+
* and `claimText` take the first match in the pool, wherever it is), and
|
|
1229
|
+
* returns its first real node. The component is never called: no claiming, no
|
|
1230
|
+
* effects, no listeners.
|
|
1231
|
+
*
|
|
1232
|
+
* Returns null when there is no well-formed run, and the caller renders the
|
|
1233
|
+
* component normally. That is also what a client render does: with an empty
|
|
1234
|
+
* pool there is nothing to skip.
|
|
1235
|
+
*/
|
|
1236
|
+
export function skipStatic(nodes: ChildNode[]): ChildNode | null {
|
|
1237
|
+
let open = -1;
|
|
1238
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1239
|
+
const node = nodes[i];
|
|
1240
|
+
if (node instanceof Comment && node.data === "s") {
|
|
1241
|
+
open = i;
|
|
1242
|
+
break;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
if (open === -1) return null;
|
|
1246
|
+
// static runs nest (a static component can contain a static use), so the
|
|
1247
|
+
// closing marker is matched by depth
|
|
1248
|
+
let close = open + 1;
|
|
1249
|
+
let depth = 0;
|
|
1250
|
+
for (; close < nodes.length; close++) {
|
|
1251
|
+
const node = nodes[close];
|
|
1252
|
+
if (!(node instanceof Comment)) continue;
|
|
1253
|
+
if (node.data === "s") depth++;
|
|
1254
|
+
else if (node.data === "/s" && depth-- === 0) break;
|
|
1255
|
+
}
|
|
1256
|
+
if (close >= nodes.length) return null;
|
|
1257
|
+
const run = nodes.splice(open, close - open + 1);
|
|
1258
|
+
for (const node of run) claimedNodes.add(node);
|
|
1259
|
+
return run.find((n) => !(n instanceof Comment)) ?? run[0] ?? null;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* Dev only: check that a use the compiler called static really is.
|
|
1264
|
+
*
|
|
1265
|
+
* Renders it once into detached nodes and throws if that created an effect
|
|
1266
|
+
* that tracks a signal, or attached an event listener. Either means the
|
|
1267
|
+
* analysis was wrong, and in production the skipped component would render
|
|
1268
|
+
* correctly and then do nothing.
|
|
1269
|
+
*/
|
|
1270
|
+
export function probeStatic(name: string, render: () => unknown): void {
|
|
1271
|
+
const g = globalThis as { __sigilDeferEffects?: boolean };
|
|
1272
|
+
const probe = { live: 0, listeners: 0 };
|
|
1273
|
+
const prevProbe = activeProbe;
|
|
1274
|
+
const prevDefer = g.__sigilDeferEffects;
|
|
1275
|
+
// the object that owns addEventListener for nodes: EventTarget.prototype in
|
|
1276
|
+
// a browser, found by walking up from Node so a DOM that has its own
|
|
1277
|
+
// EventTarget class is covered too
|
|
1278
|
+
let proto: EventTarget = Node.prototype;
|
|
1279
|
+
while (proto && !Object.hasOwn(proto, "addEventListener")) {
|
|
1280
|
+
proto = Object.getPrototypeOf(proto);
|
|
1281
|
+
}
|
|
1282
|
+
const add = proto.addEventListener;
|
|
1283
|
+
activeProbe = probe;
|
|
1284
|
+
g.__sigilDeferEffects = false;
|
|
1285
|
+
proto.addEventListener = function (
|
|
1286
|
+
this: EventTarget,
|
|
1287
|
+
...args: Parameters<typeof add>
|
|
1288
|
+
) {
|
|
1289
|
+
probe.listeners++;
|
|
1290
|
+
return add.apply(this, args);
|
|
1291
|
+
};
|
|
1292
|
+
let dispose: (() => void) | undefined;
|
|
1293
|
+
pushHydrationNodes([]);
|
|
1294
|
+
try {
|
|
1295
|
+
dispose = withEffectScope(() => {
|
|
1296
|
+
render();
|
|
1297
|
+
});
|
|
1298
|
+
} finally {
|
|
1299
|
+
popHydrationNodes();
|
|
1300
|
+
proto.addEventListener = add;
|
|
1301
|
+
activeProbe = prevProbe;
|
|
1302
|
+
g.__sigilDeferEffects = prevDefer;
|
|
1303
|
+
dispose?.();
|
|
1304
|
+
}
|
|
1305
|
+
if (probe.live || probe.listeners) {
|
|
1306
|
+
throw new Error(
|
|
1307
|
+
`[sigil] <${name}> was compiled as static and skipped during hydration, but rendering it created ${probe.live} live effect(s) and ${probe.listeners} event listener(s). ` +
|
|
1308
|
+
`The compiler's analysis missed something; add static={false} to this use to hydrate it, and run \`sigil explain\` to see how it was judged.`,
|
|
1309
|
+
);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/**
|
|
1314
|
+
* Hydrate a keyed list. SSR renders the rows as flat siblings between a
|
|
1315
|
+
* `<!--g-->` / `<!--/g-->` pair.
|
|
1316
|
+
*
|
|
1317
|
+
* Each row is hydrated by running its own row body (`createNode`) against
|
|
1318
|
+
* that row's server markup, so listeners, effects and bindings inside rows
|
|
1319
|
+
* attach to the existing nodes. While the rows hydrate, the pool holds only
|
|
1320
|
+
* the list's own segment: a row the server didn't render must create a fresh
|
|
1321
|
+
* node, never claim some element that happens to follow the list. Afterwards
|
|
1322
|
+
* the segment is gone from the pool for good, so rows created by later
|
|
1323
|
+
* updates can't claim a node that is already in use.
|
|
1324
|
+
*/
|
|
1325
|
+
export function hydrateKeyedList<T>(
|
|
1326
|
+
nodesVar: ChildNode[],
|
|
1327
|
+
signalItems: T[],
|
|
1328
|
+
getKey: (item: T, index: number) => unknown,
|
|
1329
|
+
createNode: (item: T, index: number) => Node,
|
|
1330
|
+
parent?: Element,
|
|
1331
|
+
usesIndex = false,
|
|
1332
|
+
): { anchor: Comment; keyMap: Map<unknown, Node> } {
|
|
1333
|
+
const keyMap = new Map<unknown, Node>();
|
|
1334
|
+
const isMarker = (n: ChildNode, data: string) =>
|
|
1335
|
+
n instanceof Comment && n.data === data;
|
|
1336
|
+
const start = nodesVar.findIndex((n) => isMarker(n, "g"));
|
|
1337
|
+
|
|
1338
|
+
if (start === -1) {
|
|
1339
|
+
// no server markup (client navigation): build the list fresh
|
|
1340
|
+
const anchor = claimComment(nodesVar, "g", parent);
|
|
1341
|
+
reconcile(anchor, keyMap, signalItems, getKey, createNode, usesIndex);
|
|
1342
|
+
return { anchor, keyMap };
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
const anchor = nodesVar[start] as Comment;
|
|
1346
|
+
// the matching close: a row that is a fragment can hold slots of its own
|
|
1347
|
+
let end = matchingClose(nodesVar, start);
|
|
1348
|
+
if (end === -1) end = nodesVar.length;
|
|
1349
|
+
const all = nodesVar.splice(
|
|
1350
|
+
0,
|
|
1351
|
+
nodesVar.length,
|
|
1352
|
+
...nodesVar.slice(start + 1, end),
|
|
1353
|
+
);
|
|
1354
|
+
for (let i = 0; i < signalItems.length; i++) {
|
|
1355
|
+
const item = signalItems[i] as T;
|
|
1356
|
+
const node = createNode(item, i);
|
|
1357
|
+
lastItemFor.set(node, item);
|
|
1358
|
+
lastIndexFor.set(node, i);
|
|
1359
|
+
keyMap.set(getKey(item, i), node);
|
|
1360
|
+
}
|
|
1361
|
+
// rows the server rendered but the client has no item for
|
|
1362
|
+
for (const leftover of nodesVar) leftover.remove();
|
|
1363
|
+
(all[end] as ChildNode | undefined)?.remove();
|
|
1364
|
+
nodesVar.splice(
|
|
1365
|
+
0,
|
|
1366
|
+
nodesVar.length,
|
|
1367
|
+
...all.slice(0, start),
|
|
1368
|
+
...all.slice(end + 1),
|
|
1369
|
+
);
|
|
1370
|
+
|
|
1371
|
+
reconcile(anchor, keyMap, signalItems, getKey, createNode, usesIndex);
|
|
1372
|
+
return { anchor, keyMap };
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
export function insert(
|
|
1376
|
+
parent: Element | DocumentFragment,
|
|
1377
|
+
value: unknown,
|
|
1378
|
+
): void {
|
|
1379
|
+
if (value === false || value === null || value === undefined) return;
|
|
1380
|
+
if (typeof value === "function") {
|
|
1381
|
+
insert(parent, value());
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1384
|
+
if (Array.isArray(value)) {
|
|
1385
|
+
for (const item of value) insert(parent, item);
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
1388
|
+
if (value instanceof Node) {
|
|
1389
|
+
parent.appendChild(value);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
parent.append(String(value));
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
/**
|
|
1396
|
+
* Hydrate a `<!--g-->…<!--/g-->` slot.
|
|
1397
|
+
*
|
|
1398
|
+
* Consumes the delimiter pair and the SSR content between them from `nodes`,
|
|
1399
|
+
* then runs `value` against that content as its hydration pool. Component
|
|
1400
|
+
* children are compiled to thunks precisely so this call — not the caller's
|
|
1401
|
+
* pool — decides what they claim: `<Wrap><Item/></Wrap>` puts `Item`'s SSR
|
|
1402
|
+
* markup inside `Wrap`'s root, which the caller cannot see.
|
|
1403
|
+
*
|
|
1404
|
+
* SSR left no slot (SPA navigation renders into empty elements): fall back to
|
|
1405
|
+
* a fresh `insert`.
|
|
1406
|
+
*/
|
|
1407
|
+
export function hydrateSlot(
|
|
1408
|
+
nodes: ChildNode[],
|
|
1409
|
+
parent: Element | DocumentFragment,
|
|
1410
|
+
value: unknown,
|
|
1411
|
+
): void {
|
|
1412
|
+
let open = -1;
|
|
1413
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1414
|
+
const node = nodes[i];
|
|
1415
|
+
if (node instanceof Comment && node.data === "g") {
|
|
1416
|
+
open = i;
|
|
1417
|
+
break;
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
if (open === -1) {
|
|
1421
|
+
insert(parent, value);
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
// Slots nest (a component's {children} can hold a child's own slot), so
|
|
1425
|
+
// match the closing marker by depth rather than taking the first one.
|
|
1426
|
+
let close = open + 1;
|
|
1427
|
+
for (let depth = 0; close < nodes.length; close++) {
|
|
1428
|
+
const node = nodes[close];
|
|
1429
|
+
if (!(node instanceof Comment)) continue;
|
|
1430
|
+
if (node.data === "g") depth++;
|
|
1431
|
+
else if (node.data === "/g" && depth-- === 0) break;
|
|
1432
|
+
}
|
|
1433
|
+
const inner = nodes.slice(open + 1, close);
|
|
1434
|
+
// Drop the whole run so later siblings can't claim slot content.
|
|
1435
|
+
nodes.splice(open, close - open + 1);
|
|
1436
|
+
pushHydrationNodes(inner);
|
|
1437
|
+
try {
|
|
1438
|
+
attachHydrated(parent, value);
|
|
1439
|
+
} finally {
|
|
1440
|
+
popHydrationNodes();
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* A slot whose value can change: `{title}` or `{children}` in a component,
|
|
1446
|
+
* where the prop may be a string, nodes, or a thunk, and may be a getter over
|
|
1447
|
+
* state (see the compiler's reactive props).
|
|
1448
|
+
*
|
|
1449
|
+
* The first run is exactly `hydrateSlot` (or a fresh insert when there is no
|
|
1450
|
+
* server markup). After that, whenever `read` sees a new value, the content
|
|
1451
|
+
* between the slot's markers is replaced: a text node is updated in place,
|
|
1452
|
+
* anything else is rebuilt. Only `read` is tracked; building the content is
|
|
1453
|
+
* not, so a child component's own reads never re-run the slot.
|
|
1454
|
+
*/
|
|
1455
|
+
export function liveSlot(
|
|
1456
|
+
nodes: ChildNode[],
|
|
1457
|
+
parent: Element | DocumentFragment,
|
|
1458
|
+
read: () => unknown,
|
|
1459
|
+
): void {
|
|
1460
|
+
// Claim the slot's markers now, not in the effect's first run: effects
|
|
1461
|
+
// can be deferred while a page hydrates, and a sibling claimed after this
|
|
1462
|
+
// call must not find these markers still in the pool.
|
|
1463
|
+
let start: Comment | undefined;
|
|
1464
|
+
let end: Comment | undefined;
|
|
1465
|
+
let inner: ChildNode[] | null = null;
|
|
1466
|
+
const open = nodes.findIndex((n) => n instanceof Comment && n.data === "g");
|
|
1467
|
+
if (open !== -1) {
|
|
1468
|
+
let close = open + 1;
|
|
1469
|
+
for (let depth = 0; close < nodes.length; close++) {
|
|
1470
|
+
const node = nodes[close];
|
|
1471
|
+
if (!(node instanceof Comment)) continue;
|
|
1472
|
+
if (node.data === "g") depth++;
|
|
1473
|
+
else if (node.data === "/g" && depth-- === 0) break;
|
|
1474
|
+
}
|
|
1475
|
+
if (close < nodes.length) {
|
|
1476
|
+
start = nodes[open] as Comment;
|
|
1477
|
+
end = nodes[close] as Comment;
|
|
1478
|
+
inner = nodes.slice(open + 1, close);
|
|
1479
|
+
nodes.splice(open, close - open + 1);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
if (!start || !end) {
|
|
1483
|
+
start = document.createComment("g");
|
|
1484
|
+
end = document.createComment("/g");
|
|
1485
|
+
parent.appendChild(start);
|
|
1486
|
+
parent.appendChild(end);
|
|
1487
|
+
}
|
|
1488
|
+
let first = true;
|
|
1489
|
+
createEffect(() => {
|
|
1490
|
+
const value = read();
|
|
1491
|
+
untrack(() => {
|
|
1492
|
+
if (first) {
|
|
1493
|
+
first = false;
|
|
1494
|
+
if (inner) {
|
|
1495
|
+
pushHydrationNodes(inner);
|
|
1496
|
+
try {
|
|
1497
|
+
attachHydrated(parent, value);
|
|
1498
|
+
} finally {
|
|
1499
|
+
popHydrationNodes();
|
|
1500
|
+
}
|
|
1501
|
+
} else {
|
|
1502
|
+
mountBefore(end!, value);
|
|
1503
|
+
}
|
|
1504
|
+
return;
|
|
1505
|
+
}
|
|
1506
|
+
const between: ChildNode[] = [];
|
|
1507
|
+
for (let n = start!.nextSibling; n && n !== end; n = n.nextSibling) {
|
|
1508
|
+
between.push(n);
|
|
1509
|
+
}
|
|
1510
|
+
const primitive =
|
|
1511
|
+
value === null ||
|
|
1512
|
+
value === undefined ||
|
|
1513
|
+
typeof value === "string" ||
|
|
1514
|
+
typeof value === "number" ||
|
|
1515
|
+
typeof value === "boolean";
|
|
1516
|
+
if (primitive && between.length === 1 && between[0] instanceof Text) {
|
|
1517
|
+
between[0].data =
|
|
1518
|
+
value === null || value === undefined || value === false
|
|
1519
|
+
? ""
|
|
1520
|
+
: String(value);
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1523
|
+
for (const n of between) n.remove();
|
|
1524
|
+
mountBefore(end!, value);
|
|
1525
|
+
});
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
function mountBefore(marker: ChildNode, value: unknown): void {
|
|
1530
|
+
if (value === null || value === undefined || value === false) return;
|
|
1531
|
+
if (typeof value === "function") {
|
|
1532
|
+
// a deferred child built now has no server markup to adopt
|
|
1533
|
+
pushHydrationNodes([]);
|
|
1534
|
+
try {
|
|
1535
|
+
mountBefore(marker, (value as () => unknown)());
|
|
1536
|
+
} finally {
|
|
1537
|
+
popHydrationNodes();
|
|
1538
|
+
}
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
if (Array.isArray(value)) {
|
|
1542
|
+
for (const item of value) mountBefore(marker, item);
|
|
1543
|
+
return;
|
|
1544
|
+
}
|
|
1545
|
+
marker.before(
|
|
1546
|
+
value instanceof Node ? value : document.createTextNode(String(value)),
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
/**
|
|
1551
|
+
* Realize a slot value that already has SSR markup in the DOM: run thunks,
|
|
1552
|
+
* skip primitives (their text is already rendered), and append only nodes
|
|
1553
|
+
* that hydration created fresh instead of claiming.
|
|
1554
|
+
*/
|
|
1555
|
+
function attachHydrated(
|
|
1556
|
+
parent: Element | DocumentFragment,
|
|
1557
|
+
value: unknown,
|
|
1558
|
+
): void {
|
|
1559
|
+
if (typeof value === "function") {
|
|
1560
|
+
attachHydrated(parent, (value as () => unknown)());
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
if (Array.isArray(value)) {
|
|
1564
|
+
for (const item of value) attachHydrated(parent, item);
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
if (value instanceof Node) {
|
|
1568
|
+
// During hydration the parent's SSR-rendered children are already in
|
|
1569
|
+
// the DOM. A freshly-created JSX Node for the same slot is a duplicate
|
|
1570
|
+
// — appending it doubles the content (e.g. a `<span>` rendered both
|
|
1571
|
+
// server-side and as a JSX value prop). Keep the SSR node, drop the clone.
|
|
1572
|
+
// A node that already sits in the DOM was claimed from SSR markup:
|
|
1573
|
+
// leave it where it is.
|
|
1574
|
+
if (parent.childNodes.length > 0 || value.parentNode) return;
|
|
1575
|
+
parent.appendChild(value);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
/**
|
|
1580
|
+
* Apply a use: directive to an element.
|
|
1581
|
+
* The directive function receives (element, params) and may return a cleanup function.
|
|
1582
|
+
* Cleanup runs when the enclosing $effect re-runs or the component is destroyed.
|
|
1583
|
+
*
|
|
1584
|
+
* It runs as an effect, so it waits with the others while hydration or a
|
|
1585
|
+
* client navigation holds them back: the element is in the document by the
|
|
1586
|
+
* time the directive sees it. Untracked, so a signal it happens to read does
|
|
1587
|
+
* not set it up a second time.
|
|
1588
|
+
*/
|
|
1589
|
+
export type Directive = (
|
|
1590
|
+
node: Element,
|
|
1591
|
+
params: unknown,
|
|
1592
|
+
) => undefined | (() => void);
|
|
1593
|
+
|
|
1594
|
+
export function applyDirective(
|
|
1595
|
+
node: Element,
|
|
1596
|
+
directive: Directive | [Directive, unknown],
|
|
1597
|
+
): void {
|
|
1598
|
+
const [fn, params] = Array.isArray(directive)
|
|
1599
|
+
? directive
|
|
1600
|
+
: [directive, undefined];
|
|
1601
|
+
createEffect(() => {
|
|
1602
|
+
const cleanup = untrack(() => fn(node, params));
|
|
1603
|
+
return typeof cleanup === "function" ? cleanup : undefined;
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
export type ContextKey<T> = symbol & { __type: T };
|
|
1608
|
+
|
|
1609
|
+
const clientContextStore = new Map<symbol, unknown>();
|
|
1610
|
+
|
|
1611
|
+
let storeProvider: (() => Map<symbol, unknown>) | null = null;
|
|
1612
|
+
export function __setStoreProvider(fn: () => Map<symbol, unknown>) {
|
|
1613
|
+
storeProvider = fn;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
function getStore(): Map<symbol, unknown> {
|
|
1617
|
+
return storeProvider?.() ?? clientContextStore;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
export function createContext<T>(): ContextKey<T> {
|
|
1621
|
+
return Symbol() as ContextKey<T>;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
/**
|
|
1625
|
+
* Set context for the calling component and everything it renders. Outside
|
|
1626
|
+
* any component (app bootstrap, the server's per-request setup) it goes to
|
|
1627
|
+
* the root store, which every component falls back to.
|
|
1628
|
+
*/
|
|
1629
|
+
export function setContext<T>(key: ContextKey<T>, value: T): void {
|
|
1630
|
+
if (currentOwner) {
|
|
1631
|
+
(currentOwner.context ??= new Map()).set(key as unknown as symbol, value);
|
|
1632
|
+
} else {
|
|
1633
|
+
getStore().set(key as unknown as symbol, value);
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
/**
|
|
1638
|
+
* The nearest value set by this component or one of its ancestors, else the
|
|
1639
|
+
* root store's. Call it while the component runs (or in an effect it
|
|
1640
|
+
* creates): an event handler runs outside the tree and sees only the root.
|
|
1641
|
+
*/
|
|
1642
|
+
export function getContext<T>(key: ContextKey<T>): T {
|
|
1643
|
+
const k = key as unknown as symbol;
|
|
1644
|
+
for (let o = currentOwner; o; o = o.parent) {
|
|
1645
|
+
if (o.context?.has(k)) return o.context.get(k) as T;
|
|
1646
|
+
}
|
|
1647
|
+
return getStore().get(k) as T;
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/** Whether getContext(key) would find a value. */
|
|
1651
|
+
export function hasContext(key: ContextKey<unknown>): boolean {
|
|
1652
|
+
const k = key as unknown as symbol;
|
|
1653
|
+
for (let o = currentOwner; o; o = o.parent) {
|
|
1654
|
+
if (o.context?.has(k)) return true;
|
|
1655
|
+
}
|
|
1656
|
+
return getStore().has(k);
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
const _hydrationStack: ChildNode[][] = [];
|
|
1660
|
+
|
|
1661
|
+
export function pushHydrationNodes(nodes: ChildNode[]): void {
|
|
1662
|
+
_hydrationStack.push(nodes);
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
export function popHydrationNodes(): void {
|
|
1666
|
+
_hydrationStack.pop();
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
export function getHydrationNodes(): ChildNode[] {
|
|
1670
|
+
return _hydrationStack[_hydrationStack.length - 1] ?? [];
|
|
1671
|
+
}
|