@tschk/moonshine-dom 0.0.0-stage → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +398 -2
- package/package.json +57 -4
- package/src/attributes.ts +20 -0
- package/src/binding.ts +380 -0
- package/src/cleanup-order.ts +16 -0
- package/src/component-instance.ts +290 -0
- package/src/component.ts +176 -0
- package/src/control.ts +367 -0
- package/src/custom-element.ts +54 -0
- package/src/dom.ts +851 -0
- package/src/error.ts +59 -0
- package/src/framework-lazy.ts +20 -0
- package/src/framework-native.ts +134 -0
- package/src/framework-source.ts +28 -0
- package/src/html.ts +54 -0
- package/src/jsx-types.d.ts +19 -0
- package/src/reactive.ts +673 -0
- package/src/render-scheduler.ts +17 -0
- package/src/resource.ts +189 -0
- package/src/rotating-text.ts +38 -0
- package/src/ssr-attempt.ts +29 -0
- package/src/ssr-owner.ts +31 -0
- package/src/ssr.ts +617 -0
- package/src/store.ts +496 -0
- package/src/style.ts +30 -0
- package/src/template-retention.ts +4 -0
package/src/reactive.ts
ADDED
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
import { cleanupOrderKey, type CleanupPolicy } from "./cleanup-order";
|
|
2
|
+
export { cleanupOrderKey };
|
|
3
|
+
export type { CleanupPolicy };
|
|
4
|
+
import { renderSchedulerKey, type RenderScheduler } from "./render-scheduler";
|
|
5
|
+
/** Independent reactive primitives; compiled Solid DOM output is not supported. */
|
|
6
|
+
import { isTracking } from "@tschk/moonshine/tracking";
|
|
7
|
+
import {
|
|
8
|
+
batch as moonshineBatch,
|
|
9
|
+
createMemo as moonshineMemo,
|
|
10
|
+
createSignal as moonshineSignal,
|
|
11
|
+
untrack,
|
|
12
|
+
} from "@tschk/moonshine";
|
|
13
|
+
|
|
14
|
+
/** Internal scoped historical evaluation capability, never a live-owner substitute. */
|
|
15
|
+
export const cleanupComputationKey = /* @__PURE__ */ Symbol(
|
|
16
|
+
"moonshine.cleanup.computation",
|
|
17
|
+
);
|
|
18
|
+
export function assertNotCleanupComputation(): void {
|
|
19
|
+
if (owner?.context?.has(cleanupComputationKey))
|
|
20
|
+
throw new Error(
|
|
21
|
+
"effect_in_teardown: Cannot create owned work during cleanup computation",
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Accessor<T> = () => T;
|
|
26
|
+
export type Setter<T> = (value: T | ((previous: T) => T)) => T;
|
|
27
|
+
export type Owner = {
|
|
28
|
+
parent: Owner | null;
|
|
29
|
+
children: Set<Owner>;
|
|
30
|
+
cleanups: Array<() => void>;
|
|
31
|
+
disposed: boolean;
|
|
32
|
+
disposing?: boolean;
|
|
33
|
+
beforeDispose?: Array<() => void>;
|
|
34
|
+
afterDispose?: Array<() => void>;
|
|
35
|
+
context?: Map<symbol, unknown>;
|
|
36
|
+
stop?: () => void;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
let owner: Owner | null = null;
|
|
40
|
+
let rootDepth = 0;
|
|
41
|
+
let updateDepth = 0;
|
|
42
|
+
let cleanupDepth = 0;
|
|
43
|
+
let flushing = false;
|
|
44
|
+
let callbackDepth = 0;
|
|
45
|
+
let startupDepth = 0;
|
|
46
|
+
let pendingMicrotask = false;
|
|
47
|
+
let routedErrors = 0;
|
|
48
|
+
function deferPending(): void {
|
|
49
|
+
if (pendingMicrotask) return;
|
|
50
|
+
pendingMicrotask = true;
|
|
51
|
+
queueMicrotask(() => {
|
|
52
|
+
pendingMicrotask = false;
|
|
53
|
+
flushPending();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function invokeCallback<T>(fn: () => T): T {
|
|
57
|
+
callbackDepth++;
|
|
58
|
+
try {
|
|
59
|
+
return fn();
|
|
60
|
+
} finally {
|
|
61
|
+
callbackDepth--;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function subscribeTracked(tracked: {
|
|
65
|
+
subscribe(fn: () => void): () => void;
|
|
66
|
+
}): () => void {
|
|
67
|
+
startupDepth++;
|
|
68
|
+
try {
|
|
69
|
+
return tracked.subscribe(() => {});
|
|
70
|
+
} finally {
|
|
71
|
+
startupDepth--;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const pending: Array<() => void> = [];
|
|
75
|
+
|
|
76
|
+
function flushPending(): void {
|
|
77
|
+
if (
|
|
78
|
+
!pending.length ||
|
|
79
|
+
rootDepth ||
|
|
80
|
+
updateDepth ||
|
|
81
|
+
cleanupDepth ||
|
|
82
|
+
flushing ||
|
|
83
|
+
flushingCleanupFinalizers
|
|
84
|
+
)
|
|
85
|
+
return;
|
|
86
|
+
if (callbackDepth || startupDepth || isTracking()) {
|
|
87
|
+
deferPending();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
flushing = true;
|
|
91
|
+
let failed = false;
|
|
92
|
+
let failure: unknown;
|
|
93
|
+
try {
|
|
94
|
+
while (pending.length) {
|
|
95
|
+
for (const start of pending.splice(0)) {
|
|
96
|
+
try {
|
|
97
|
+
start();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (!failed) failure = error;
|
|
100
|
+
failed = true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} finally {
|
|
105
|
+
flushing = false;
|
|
106
|
+
}
|
|
107
|
+
if (failed) throw failure;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function runUpdates<T>(fn: () => T): T {
|
|
111
|
+
updateDepth++;
|
|
112
|
+
let result!: T;
|
|
113
|
+
let failed = false;
|
|
114
|
+
let failure: unknown;
|
|
115
|
+
try {
|
|
116
|
+
result = fn();
|
|
117
|
+
} catch (error) {
|
|
118
|
+
failed = true;
|
|
119
|
+
failure = error;
|
|
120
|
+
}
|
|
121
|
+
updateDepth--;
|
|
122
|
+
try {
|
|
123
|
+
flushPending();
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (!failed) failure = error;
|
|
126
|
+
failed = true;
|
|
127
|
+
}
|
|
128
|
+
if (failed) throw failure;
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const errorHandlerKey = /* @__PURE__ */ Symbol();
|
|
133
|
+
function routeHandler(scope: Owner): ((error: unknown) => boolean) | undefined {
|
|
134
|
+
return scope.context?.get(errorHandlerKey) as
|
|
135
|
+
((error: unknown) => boolean) | undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Internal queue seam for the opt-in error boundary implementation. */
|
|
139
|
+
export function queueErrorHandler(fn: () => void): void {
|
|
140
|
+
routedErrors++;
|
|
141
|
+
pending.push(() => {
|
|
142
|
+
routedErrors--;
|
|
143
|
+
fn();
|
|
144
|
+
});
|
|
145
|
+
if (!rootDepth && !updateDepth && !cleanupDepth && !flushing) deferPending();
|
|
146
|
+
}
|
|
147
|
+
export { createOwner as createErrorOwner };
|
|
148
|
+
export { catchError } from "./error";
|
|
149
|
+
|
|
150
|
+
function createOwner(): Owner {
|
|
151
|
+
assertNotCleanupComputation();
|
|
152
|
+
if (owner?.disposed)
|
|
153
|
+
throw new Error("Cannot attach work to a disposed native owner");
|
|
154
|
+
const scope: Owner = {
|
|
155
|
+
parent: owner,
|
|
156
|
+
children: new Set(),
|
|
157
|
+
cleanups: [],
|
|
158
|
+
disposed: false,
|
|
159
|
+
context: owner?.context,
|
|
160
|
+
};
|
|
161
|
+
owner?.children.add(scope);
|
|
162
|
+
return scope;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// One lazy notification for the optional physical-cleanup transaction implementation.
|
|
166
|
+
let cleanupFinalizerFlush: (() => void) | undefined;
|
|
167
|
+
let flushingCleanupFinalizers = false;
|
|
168
|
+
export function requestCleanupFinalizerFlush(flush: () => void): void {
|
|
169
|
+
if (cleanupFinalizerFlush && cleanupFinalizerFlush !== flush)
|
|
170
|
+
throw new Error("Conflicting native cleanup finalizer implementations");
|
|
171
|
+
cleanupFinalizerFlush = flush;
|
|
172
|
+
}
|
|
173
|
+
function flushCleanupFinalizers(): void {
|
|
174
|
+
if (cleanupDepth || flushingCleanupFinalizers) return;
|
|
175
|
+
flushingCleanupFinalizers = true;
|
|
176
|
+
let first: unknown,
|
|
177
|
+
failed = false;
|
|
178
|
+
let waves = 0;
|
|
179
|
+
try {
|
|
180
|
+
while (cleanupFinalizerFlush) {
|
|
181
|
+
if (++waves > 1000) {
|
|
182
|
+
cleanupFinalizerFlush = undefined;
|
|
183
|
+
if (!failed)
|
|
184
|
+
first = new Error("Native cleanup finalizer update limit exceeded");
|
|
185
|
+
failed = true;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
const flush = cleanupFinalizerFlush;
|
|
189
|
+
cleanupFinalizerFlush = undefined;
|
|
190
|
+
try {
|
|
191
|
+
flush();
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (!failed) first = error;
|
|
194
|
+
failed = true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} finally {
|
|
198
|
+
flushingCleanupFinalizers = false;
|
|
199
|
+
}
|
|
200
|
+
if (failed) throw first;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Internal bracket for opted physical cleanup outside an owner disposal. */
|
|
204
|
+
export function withCleanupTransaction(fn: () => void): void {
|
|
205
|
+
let failed = false;
|
|
206
|
+
let failure: unknown;
|
|
207
|
+
cleanupDepth++;
|
|
208
|
+
try {
|
|
209
|
+
fn();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
failed = true;
|
|
212
|
+
failure = error;
|
|
213
|
+
} finally {
|
|
214
|
+
cleanupDepth--;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
flushCleanupFinalizers();
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (!failed) failure = error;
|
|
220
|
+
failed = true;
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
flushPending();
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (!failed) failure = error;
|
|
226
|
+
failed = true;
|
|
227
|
+
}
|
|
228
|
+
if (failed) throw failure;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Read-only seam for an optional cleanup policy's fixed physical finalizers. */
|
|
232
|
+
export function cleanupTransactionActive(): boolean {
|
|
233
|
+
return cleanupDepth > 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function clean(scope: Owner): void {
|
|
237
|
+
if (scope.children.size === 0 && scope.cleanups.length === 0) return;
|
|
238
|
+
cleanupDepth++;
|
|
239
|
+
const policy = scope.context?.get(cleanupOrderKey) as
|
|
240
|
+
CleanupPolicy | undefined;
|
|
241
|
+
let failure: unknown;
|
|
242
|
+
let failed = false;
|
|
243
|
+
try {
|
|
244
|
+
const children = Array.from(
|
|
245
|
+
scope.children,
|
|
246
|
+
(child) => () => dispose(child),
|
|
247
|
+
);
|
|
248
|
+
const cleanups = scope.cleanups.splice(0);
|
|
249
|
+
if (policy?.reverse) {
|
|
250
|
+
children.reverse();
|
|
251
|
+
cleanups.reverse();
|
|
252
|
+
}
|
|
253
|
+
for (const callback of [...children, ...cleanups]) {
|
|
254
|
+
try {
|
|
255
|
+
untrack(callback);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (!failed) failure = error;
|
|
258
|
+
failed = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
} finally {
|
|
262
|
+
cleanupDepth--;
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
flushCleanupFinalizers();
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (!failed) failure = error;
|
|
268
|
+
failed = true;
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
flushPending();
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (!failed) failure = error;
|
|
274
|
+
failed = true;
|
|
275
|
+
}
|
|
276
|
+
if (failed) throw failure;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function dispose(scope: Owner): void {
|
|
280
|
+
if (scope.disposed || scope.disposing) return;
|
|
281
|
+
scope.disposing = true;
|
|
282
|
+
cleanupDepth++;
|
|
283
|
+
let failed = false;
|
|
284
|
+
let failure: unknown;
|
|
285
|
+
try {
|
|
286
|
+
if (scope.beforeDispose) {
|
|
287
|
+
for (const callback of scope.beforeDispose.splice(0)) {
|
|
288
|
+
try {
|
|
289
|
+
runWithOwner(scope, () => untrack(callback));
|
|
290
|
+
} catch (error) {
|
|
291
|
+
if (!failed) failure = error;
|
|
292
|
+
failed = true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
scope.disposed = true;
|
|
297
|
+
scope.parent?.children.delete(scope);
|
|
298
|
+
try {
|
|
299
|
+
clean(scope);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (!failed) failure = error;
|
|
302
|
+
failed = true;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
scope.stop?.();
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (!failed) failure = error;
|
|
308
|
+
failed = true;
|
|
309
|
+
}
|
|
310
|
+
if (scope.afterDispose) {
|
|
311
|
+
for (const callback of scope.afterDispose.splice(0))
|
|
312
|
+
pending.push(() => runWithOwner(scope, () => untrack(callback)));
|
|
313
|
+
}
|
|
314
|
+
} finally {
|
|
315
|
+
// Scoped cleanup computations retain their capability until their finally exits.
|
|
316
|
+
if (!scope.context?.has(cleanupComputationKey)) scope.context = undefined;
|
|
317
|
+
scope.disposing = false;
|
|
318
|
+
cleanupDepth--;
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
flushCleanupFinalizers();
|
|
322
|
+
} catch (error) {
|
|
323
|
+
if (!failed) failure = error;
|
|
324
|
+
failed = true;
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
flushPending();
|
|
328
|
+
} catch (error) {
|
|
329
|
+
if (!failed) failure = error;
|
|
330
|
+
failed = true;
|
|
331
|
+
}
|
|
332
|
+
if (failed) throw failure;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Schedule after the current native root/update and complete cleanup commit. */
|
|
336
|
+
export function queuePostCommit(fn: () => void): void {
|
|
337
|
+
pending.push(fn);
|
|
338
|
+
if (!rootDepth && !updateDepth && !cleanupDepth && !flushing)
|
|
339
|
+
queueMicrotask(flushPending);
|
|
340
|
+
}
|
|
341
|
+
function lifecycleOwner(hook: string): Owner {
|
|
342
|
+
assertNotCleanupComputation();
|
|
343
|
+
if (
|
|
344
|
+
!owner ||
|
|
345
|
+
owner.disposed ||
|
|
346
|
+
(owner.disposing && hook !== "onAfterDispose")
|
|
347
|
+
)
|
|
348
|
+
throw new Error(`${hook} requires a live native owner`);
|
|
349
|
+
return owner;
|
|
350
|
+
}
|
|
351
|
+
/** Final disposal only; executes untracked before owned work is stopped. */
|
|
352
|
+
export function onBeforeDispose(fn: () => void): void {
|
|
353
|
+
(lifecycleOwner("onBeforeDispose").beforeDispose ??= []).push(fn);
|
|
354
|
+
}
|
|
355
|
+
/** Final disposal only; child-first after cleanup; before-disposal hooks may register it. */
|
|
356
|
+
export function onAfterDispose(fn: () => void): void {
|
|
357
|
+
(lifecycleOwner("onAfterDispose").afterDispose ??= []).push(fn);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function getOwner(): Owner | null {
|
|
361
|
+
return owner;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function runWithOwner<T>(scope: Owner | null, fn: () => T): T {
|
|
365
|
+
const previous = owner;
|
|
366
|
+
owner = scope;
|
|
367
|
+
try {
|
|
368
|
+
return fn();
|
|
369
|
+
} finally {
|
|
370
|
+
owner = previous;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Attached component setup: isolate dependencies and dispose failed owned work. */
|
|
375
|
+
export function createComponentScope<T>(fn: () => T): T {
|
|
376
|
+
return invokeCallback(() => {
|
|
377
|
+
const scope = createOwner();
|
|
378
|
+
try {
|
|
379
|
+
return runWithOwner(scope, () => untrack(fn));
|
|
380
|
+
} catch (error) {
|
|
381
|
+
const target = routeHandler(scope);
|
|
382
|
+
try {
|
|
383
|
+
dispose(scope);
|
|
384
|
+
} catch {
|
|
385
|
+
/* Preserve setup failure. */
|
|
386
|
+
}
|
|
387
|
+
if (target?.(error)) return undefined as T;
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function createRoot<T>(fn: (dispose: () => void) => T): T {
|
|
394
|
+
assertNotCleanupComputation();
|
|
395
|
+
// Successfully created roots require explicit disposal.
|
|
396
|
+
const context = owner?.context;
|
|
397
|
+
const scope = runWithOwner(null, createOwner);
|
|
398
|
+
scope.context = context;
|
|
399
|
+
rootDepth++;
|
|
400
|
+
let result!: T;
|
|
401
|
+
let failure: unknown;
|
|
402
|
+
let failed = false;
|
|
403
|
+
try {
|
|
404
|
+
result = runWithOwner(scope, () => untrack(() => fn(() => dispose(scope))));
|
|
405
|
+
} catch (error) {
|
|
406
|
+
failure = error;
|
|
407
|
+
failed = true;
|
|
408
|
+
try {
|
|
409
|
+
dispose(scope);
|
|
410
|
+
} catch {
|
|
411
|
+
// Preserve the callback failure after attempting every owned cleanup.
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
rootDepth--;
|
|
415
|
+
try {
|
|
416
|
+
flushPending();
|
|
417
|
+
} catch (error) {
|
|
418
|
+
if (!failed) failure = error;
|
|
419
|
+
failed = true;
|
|
420
|
+
}
|
|
421
|
+
if (failed) throw failure;
|
|
422
|
+
return result;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function onCleanup<T extends () => void>(fn: T): T {
|
|
426
|
+
assertNotCleanupComputation();
|
|
427
|
+
if (owner && !owner.disposed) owner.cleanups.push(fn);
|
|
428
|
+
return fn;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export function createSignal<T>(
|
|
432
|
+
initial: T,
|
|
433
|
+
options: { equals?: false | ((previous: T, next: T) => boolean) } = {},
|
|
434
|
+
): [Accessor<T>, Setter<T>] {
|
|
435
|
+
let value = initial;
|
|
436
|
+
const version = moonshineSignal(0);
|
|
437
|
+
const equals =
|
|
438
|
+
options.equals === undefined
|
|
439
|
+
? (previous: T, next: T) => previous === next
|
|
440
|
+
: options.equals;
|
|
441
|
+
return [
|
|
442
|
+
() => {
|
|
443
|
+
version();
|
|
444
|
+
return value;
|
|
445
|
+
},
|
|
446
|
+
(next) =>
|
|
447
|
+
runUpdates(() => {
|
|
448
|
+
const resolved =
|
|
449
|
+
typeof next === "function"
|
|
450
|
+
? (next as (previous: T) => T)(value)
|
|
451
|
+
: next;
|
|
452
|
+
if (equals && equals(value, resolved)) return resolved;
|
|
453
|
+
value = resolved;
|
|
454
|
+
version.set(version.peek() + 1);
|
|
455
|
+
return value;
|
|
456
|
+
}),
|
|
457
|
+
];
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** Preserve the callback result, which the signal kernel's batch discards. */
|
|
461
|
+
export function batch<T>(fn: () => T): T {
|
|
462
|
+
return runUpdates(() => {
|
|
463
|
+
let result!: T;
|
|
464
|
+
moonshineBatch(() => {
|
|
465
|
+
result = fn();
|
|
466
|
+
});
|
|
467
|
+
return result;
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function computation<T>(
|
|
472
|
+
fn: (previous: T) => T,
|
|
473
|
+
initial: T,
|
|
474
|
+
deferred: boolean,
|
|
475
|
+
): void {
|
|
476
|
+
const scope = createOwner();
|
|
477
|
+
let previous = initial;
|
|
478
|
+
const start = () => {
|
|
479
|
+
if (scope.disposed) return;
|
|
480
|
+
const tracked = moonshineMemo(() =>
|
|
481
|
+
invokeCallback(() => {
|
|
482
|
+
if (scope.disposed) return;
|
|
483
|
+
untrack(() => clean(scope));
|
|
484
|
+
try {
|
|
485
|
+
previous = runWithOwner(scope, () => fn(previous));
|
|
486
|
+
} catch (error) {
|
|
487
|
+
try {
|
|
488
|
+
clean(scope);
|
|
489
|
+
} catch {
|
|
490
|
+
// Preserve the computation failure after attempting every cleanup.
|
|
491
|
+
}
|
|
492
|
+
if (!routeHandler(scope)?.(error)) throw error;
|
|
493
|
+
}
|
|
494
|
+
}),
|
|
495
|
+
);
|
|
496
|
+
try {
|
|
497
|
+
scope.stop = subscribeTracked(tracked);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
try {
|
|
500
|
+
dispose(scope);
|
|
501
|
+
} catch {
|
|
502
|
+
// Preserve the computation failure when cleanup also fails.
|
|
503
|
+
}
|
|
504
|
+
throw error;
|
|
505
|
+
}
|
|
506
|
+
if (scope.disposed) scope.stop();
|
|
507
|
+
if (routedErrors) flushPending();
|
|
508
|
+
};
|
|
509
|
+
if (!deferred) start();
|
|
510
|
+
else if (rootDepth > 0 || updateDepth > 0 || cleanupDepth > 0 || flushing)
|
|
511
|
+
pending.push(start);
|
|
512
|
+
else queueMicrotask(start);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export function createEffect<T>(fn: (previous: T) => T, initial: T): void;
|
|
516
|
+
export function createEffect(fn: (previous: undefined) => unknown): void;
|
|
517
|
+
export function createEffect<T>(fn: (previous: T) => T, initial?: T): void {
|
|
518
|
+
computation(fn, initial as T, true);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export function createComputed<T>(fn: (previous: T) => T, initial: T): void;
|
|
522
|
+
export function createComputed(fn: (previous: undefined) => unknown): void;
|
|
523
|
+
export function createComputed<T>(fn: (previous: T) => T, initial?: T): void {
|
|
524
|
+
computation(fn, initial as T, false);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export function getRenderScheduler(): RenderScheduler | undefined {
|
|
528
|
+
return owner?.context?.get(renderSchedulerKey) as RenderScheduler | undefined;
|
|
529
|
+
}
|
|
530
|
+
export function createRenderEffect<T>(
|
|
531
|
+
fn: (previous: T) => T,
|
|
532
|
+
initial?: T,
|
|
533
|
+
): void {
|
|
534
|
+
const scheduler = getRenderScheduler();
|
|
535
|
+
if (scheduler) scheduler.effect(fn, initial as T);
|
|
536
|
+
else computation(fn, initial as T, false);
|
|
537
|
+
}
|
|
538
|
+
export const createRenderMemo: typeof createMemo = ((
|
|
539
|
+
fn: any,
|
|
540
|
+
initial: any,
|
|
541
|
+
options: any,
|
|
542
|
+
) => {
|
|
543
|
+
const scheduler = getRenderScheduler();
|
|
544
|
+
return scheduler
|
|
545
|
+
? scheduler.memo(fn, initial, options)
|
|
546
|
+
: createMemo(fn, initial, options);
|
|
547
|
+
}) as typeof createMemo;
|
|
548
|
+
|
|
549
|
+
export function createMemo<T>(
|
|
550
|
+
fn: (previous: T) => T,
|
|
551
|
+
initial: T,
|
|
552
|
+
options?: { equals?: false | ((previous: T, next: T) => boolean) },
|
|
553
|
+
): Accessor<T>;
|
|
554
|
+
export function createMemo<T>(
|
|
555
|
+
fn: (previous: T | undefined) => T,
|
|
556
|
+
initial?: undefined,
|
|
557
|
+
options?: { equals?: false | ((previous: T, next: T) => boolean) },
|
|
558
|
+
): Accessor<T>;
|
|
559
|
+
export function createMemo<T>(
|
|
560
|
+
fn: (previous: T) => T,
|
|
561
|
+
initial?: T,
|
|
562
|
+
options: { equals?: false | ((previous: T, next: T) => boolean) } = {},
|
|
563
|
+
): Accessor<T> {
|
|
564
|
+
const scope = createOwner();
|
|
565
|
+
const active = moonshineSignal(true);
|
|
566
|
+
let current = { value: initial as T };
|
|
567
|
+
let initialized = false;
|
|
568
|
+
const equals =
|
|
569
|
+
options.equals === undefined
|
|
570
|
+
? (previous: T, next: T) => previous === next
|
|
571
|
+
: options.equals;
|
|
572
|
+
const tracked = moonshineMemo(() =>
|
|
573
|
+
invokeCallback(() => {
|
|
574
|
+
if (!active() || scope.disposed) return current;
|
|
575
|
+
untrack(() => clean(scope));
|
|
576
|
+
let next: T;
|
|
577
|
+
try {
|
|
578
|
+
next = runWithOwner(scope, () => fn(current.value));
|
|
579
|
+
} catch (error) {
|
|
580
|
+
try {
|
|
581
|
+
clean(scope);
|
|
582
|
+
} catch {
|
|
583
|
+
// Preserve the memo failure after attempting every owned cleanup.
|
|
584
|
+
}
|
|
585
|
+
if (routeHandler(scope)?.(error)) return current;
|
|
586
|
+
throw error;
|
|
587
|
+
}
|
|
588
|
+
if (!initialized || !equals || !equals(current.value, next)) {
|
|
589
|
+
current = { value: next };
|
|
590
|
+
}
|
|
591
|
+
initialized = true;
|
|
592
|
+
return current;
|
|
593
|
+
}),
|
|
594
|
+
);
|
|
595
|
+
let stop: () => void;
|
|
596
|
+
try {
|
|
597
|
+
stop = subscribeTracked(tracked);
|
|
598
|
+
} catch (error) {
|
|
599
|
+
try {
|
|
600
|
+
dispose(scope);
|
|
601
|
+
} catch {
|
|
602
|
+
// Preserve the initial memo failure when cleanup also fails.
|
|
603
|
+
}
|
|
604
|
+
throw error;
|
|
605
|
+
}
|
|
606
|
+
scope.stop = () => {
|
|
607
|
+
stop();
|
|
608
|
+
active.set(false);
|
|
609
|
+
tracked.peek();
|
|
610
|
+
};
|
|
611
|
+
if (scope.disposed) scope.stop();
|
|
612
|
+
if (routedErrors) flushPending();
|
|
613
|
+
return () => (scope.disposed ? current.value : tracked().value);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export function onMount(fn: () => void): void {
|
|
617
|
+
createEffect(() => untrack(fn));
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
export { untrack };
|
|
621
|
+
|
|
622
|
+
type OnInputs<T extends readonly Accessor<unknown>[]> = {
|
|
623
|
+
[K in keyof T]: ReturnType<T[K]>;
|
|
624
|
+
};
|
|
625
|
+
type OnCallback<Input, Value> = (
|
|
626
|
+
input: Input,
|
|
627
|
+
previousInput: Input | undefined,
|
|
628
|
+
previousValue: NoInfer<Value> | undefined,
|
|
629
|
+
) => Value;
|
|
630
|
+
export function on<Input, Value>(
|
|
631
|
+
dependencies: Accessor<Input>,
|
|
632
|
+
fn: OnCallback<Input, Value>,
|
|
633
|
+
options?: { defer?: false },
|
|
634
|
+
): (previousValue: Value | undefined) => Value;
|
|
635
|
+
export function on<const Inputs extends readonly Accessor<unknown>[], Value>(
|
|
636
|
+
dependencies: Inputs,
|
|
637
|
+
fn: OnCallback<OnInputs<Inputs>, Value>,
|
|
638
|
+
options?: { defer?: false },
|
|
639
|
+
): (previousValue: Value | undefined) => Value;
|
|
640
|
+
export function on<Input, Value>(
|
|
641
|
+
dependencies: Accessor<Input>,
|
|
642
|
+
fn: OnCallback<Input, Value>,
|
|
643
|
+
options: { defer?: boolean },
|
|
644
|
+
): (previousValue: Value | undefined) => Value | undefined;
|
|
645
|
+
export function on<const Inputs extends readonly Accessor<unknown>[], Value>(
|
|
646
|
+
dependencies: Inputs,
|
|
647
|
+
fn: OnCallback<OnInputs<Inputs>, Value>,
|
|
648
|
+
options: { defer?: boolean },
|
|
649
|
+
): (previousValue: Value | undefined) => Value | undefined;
|
|
650
|
+
/** Track only explicit inputs; callback reads and writes do not add dependencies. */
|
|
651
|
+
export function on(
|
|
652
|
+
dependencies: Accessor<unknown> | readonly Accessor<unknown>[],
|
|
653
|
+
fn: (input: any, previousInput: any, previousValue: any) => unknown,
|
|
654
|
+
options: { defer?: boolean } = {},
|
|
655
|
+
): (previousValue: unknown) => unknown {
|
|
656
|
+
let previousInput: unknown;
|
|
657
|
+
let deferred = options.defer;
|
|
658
|
+
return (previousValue) => {
|
|
659
|
+
const input =
|
|
660
|
+
typeof dependencies === "function"
|
|
661
|
+
? dependencies()
|
|
662
|
+
: dependencies.map((dependency) => dependency());
|
|
663
|
+
if (deferred) {
|
|
664
|
+
deferred = false;
|
|
665
|
+
return previousValue;
|
|
666
|
+
}
|
|
667
|
+
const value = untrack(() => fn(input, previousInput, previousValue));
|
|
668
|
+
previousInput = input;
|
|
669
|
+
return value;
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
export { templateRetentionKey } from "./template-retention";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Internal opt-in render/structural/post phase carrier inherited through owner context. */
|
|
2
|
+
import type { createMemo } from "./reactive";
|
|
3
|
+
export const renderSchedulerKey = /* @__PURE__ */ Symbol(
|
|
4
|
+
"native-render-scheduler",
|
|
5
|
+
);
|
|
6
|
+
export interface RenderScheduler {
|
|
7
|
+
effect<T>(fn: (previous: T) => T, initial: T): void;
|
|
8
|
+
memo: typeof createMemo;
|
|
9
|
+
/** Compiler reference jobs defer their reactive render work until current posts finish. */
|
|
10
|
+
post(fn: () => void, referencePublication?: true): void;
|
|
11
|
+
pre(fn: () => void | (() => void)): void;
|
|
12
|
+
component<T>(fn: () => T): T;
|
|
13
|
+
/** Called after a keyed control updates retained index accessors. */
|
|
14
|
+
reorder(): void;
|
|
15
|
+
/** The existing control index supplies live logical sibling order. */
|
|
16
|
+
branch<T>(fn: () => T, order?: () => number): T;
|
|
17
|
+
}
|