@krate/runtime 0.5.0-alpha.3 → 0.5.0-beta.1
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/dist/action-context.d.ts +14 -3
- package/dist/action-context.js +70 -4
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +101 -5
- package/dist/actions.test.js +87 -1
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +314 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +233 -0
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +75 -2
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +69 -3
- package/dist/effects.js +74 -3
- package/dist/error-overlay.js +4 -4
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +65 -2
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +239 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/portal.d.ts +5 -1
- package/dist/portal.js +15 -4
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +437 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1617 -648
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +194 -0
- package/dist/server-renderer.js +556 -132
- package/dist/server.d.ts +24 -0
- package/dist/server.js +247 -11
- package/dist/server.test.js +20 -0
- package/dist/signal.d.ts +41 -4
- package/dist/signal.js +254 -24
- package/dist/slots.d.ts +5 -1
- package/dist/slots.js +40 -3
- package/package.json +6 -2
package/dist/signal.js
CHANGED
|
@@ -24,14 +24,68 @@ const allEffects = new Set();
|
|
|
24
24
|
// gives component-level cleanup semantics: a component's top-level onCleanup
|
|
25
25
|
// registers once when its hydration IIFE runs and is invoked on unmount.
|
|
26
26
|
const rootCleanups = [];
|
|
27
|
+
// Route scopes (SPA layout persistence). Hydration code of a layout runs
|
|
28
|
+
// between krateEnterScope(id) and krateLeaveScope(), so every effect, cleanup
|
|
29
|
+
// and component owner it creates is tagged with the layout's id. On a soft
|
|
30
|
+
// navigation that keeps the layout mounted, the router disposes everything
|
|
31
|
+
// except the kept scopes (krateDisposeExcept) and the new page's hydration
|
|
32
|
+
// script skips the kept layouts' code (krateEnterScope returns false).
|
|
33
|
+
let routeScope = null;
|
|
34
|
+
let keptScopes = new Set();
|
|
35
|
+
// Scope of each owner key used by ownerEffects/ownerCleanups.
|
|
36
|
+
const ownerScopes = new Map();
|
|
37
|
+
/**
|
|
38
|
+
* Enters the route scope `id` for the hydration code that follows. Returns
|
|
39
|
+
* false when the SPA router kept that layout mounted: its bindings and
|
|
40
|
+
* effects are still live and must not be created twice.
|
|
41
|
+
*/
|
|
42
|
+
export function krateEnterScope(id) {
|
|
43
|
+
if (keptScopes.has(id))
|
|
44
|
+
return false;
|
|
45
|
+
routeScope = id;
|
|
46
|
+
currentOwner = null;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/** Leaves the current route scope (see krateEnterScope). */
|
|
50
|
+
export function krateLeaveScope() {
|
|
51
|
+
routeScope = null;
|
|
52
|
+
currentOwner = null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The route scope new effects and owners are tagged with (internal): the
|
|
56
|
+
* running effect's scope, else the scope whose hydration code is running,
|
|
57
|
+
* else the current component owner's scope (code running later, e.g. an
|
|
58
|
+
* event handler of a layout component).
|
|
59
|
+
*/
|
|
60
|
+
export function krateCurrentScope() {
|
|
61
|
+
const top = context[context.length - 1];
|
|
62
|
+
if (top)
|
|
63
|
+
return top.scope;
|
|
64
|
+
if (routeScope !== null)
|
|
65
|
+
return routeScope;
|
|
66
|
+
if (currentOwner !== null && ownerScopes.has(currentOwner))
|
|
67
|
+
return ownerScopes.get(currentOwner) ?? null;
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/** Sets the layout scopes the router kept mounted for the next hydration (internal). */
|
|
71
|
+
export function krateKeepScopes(ids) {
|
|
72
|
+
keptScopes = new Set(ids);
|
|
73
|
+
}
|
|
74
|
+
/** Records the route scope of an owner key (internal; see effects.ts). */
|
|
75
|
+
export function krateTagOwner(owner, scope) {
|
|
76
|
+
if (owner !== null && !ownerScopes.has(owner))
|
|
77
|
+
ownerScopes.set(owner, scope);
|
|
78
|
+
}
|
|
27
79
|
// Pending effects scheduled to re-run in the next microtask. Writes batch
|
|
28
80
|
// subscriber re-runs into a single flush so N writes in a loop (or one write
|
|
29
81
|
// fanning out) produce one pass instead of N synchronous effect runs.
|
|
30
82
|
const pending = [];
|
|
31
83
|
let flushScheduled = false;
|
|
32
84
|
function newEffectState(fn) {
|
|
33
|
-
const
|
|
85
|
+
const scope = krateCurrentScope();
|
|
86
|
+
const effect = { fn, cleanups: [], disposed: false, sources: new Set(), running: false, rerun: false, queued: false, owner: currentOwner, boundaryOwner: ownerHooks ? ownerHooks.current() : null, scope };
|
|
34
87
|
if (currentOwner !== null) {
|
|
88
|
+
krateTagOwner(currentOwner, scope);
|
|
35
89
|
let set = ownerEffects.get(currentOwner);
|
|
36
90
|
if (!set)
|
|
37
91
|
ownerEffects.set(currentOwner, (set = new Set()));
|
|
@@ -58,6 +112,7 @@ export function krateDisposeOwner(owner) {
|
|
|
58
112
|
}
|
|
59
113
|
const cleanups = ownerCleanups.get(owner);
|
|
60
114
|
ownerCleanups.delete(owner);
|
|
115
|
+
ownerScopes.delete(owner);
|
|
61
116
|
if (cleanups) {
|
|
62
117
|
for (const c of cleanups) {
|
|
63
118
|
try {
|
|
@@ -236,7 +291,11 @@ export function krateOnBeforeFlush(fn) {
|
|
|
236
291
|
export function krateOnAfterFlush(fn) {
|
|
237
292
|
afterFlushHooks.push(fn);
|
|
238
293
|
}
|
|
239
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* Registers fn to run at the start of disposeAll (internal). For a partial
|
|
296
|
+
* teardown (krateDisposeExcept / krateDisposeScopes) `gone` reports which
|
|
297
|
+
* route scopes are torn down; it is null for a full teardown.
|
|
298
|
+
*/
|
|
240
299
|
export function krateOnDispose(fn) {
|
|
241
300
|
disposeHooks.push(fn);
|
|
242
301
|
}
|
|
@@ -299,10 +358,94 @@ function disposeEffect(effect) {
|
|
|
299
358
|
effect.cleanups.length = 0;
|
|
300
359
|
}
|
|
301
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Partial teardown for a soft navigation that keeps some layouts mounted:
|
|
363
|
+
* disposes every effect, cleanup and component owner whose route scope is
|
|
364
|
+
* not in `keep`. With an empty `keep` this is disposeAll().
|
|
365
|
+
*/
|
|
366
|
+
export function krateDisposeExcept(keep) {
|
|
367
|
+
if (keep.length === 0) {
|
|
368
|
+
disposeAll();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const k = new Set(keep);
|
|
372
|
+
disposeMatching((scope) => !(scope != null && k.has(scope)), true);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Disposes only the effects, cleanups and component owners of the given
|
|
376
|
+
* route scopes (a parallel-route slot whose content is replaced).
|
|
377
|
+
*/
|
|
378
|
+
export function krateDisposeScopes(ids) {
|
|
379
|
+
if (ids.length === 0)
|
|
380
|
+
return;
|
|
381
|
+
const k = new Set(ids);
|
|
382
|
+
disposeMatching((scope) => scope != null && k.has(scope), false);
|
|
383
|
+
}
|
|
384
|
+
/** The route scope encoded in a layout/slot id ("_lHASH_x" -> "_lHASH_"), else null. */
|
|
385
|
+
export function krateScopeOfId(id) {
|
|
386
|
+
if (id.charCodeAt(0) !== 95 /* _ */)
|
|
387
|
+
return null;
|
|
388
|
+
const end = id.indexOf('_', 1);
|
|
389
|
+
return end > 0 ? id.slice(0, end + 1) : null;
|
|
390
|
+
}
|
|
391
|
+
function disposeMatching(gone, pageTeardown) {
|
|
392
|
+
for (const hook of disposeHooks) {
|
|
393
|
+
try {
|
|
394
|
+
hook(gone);
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
// Unmount errors must not prevent the rest of the teardown.
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
for (const effect of Array.from(allEffects)) {
|
|
401
|
+
if (gone(effect.scope)) {
|
|
402
|
+
disposeEffect(effect);
|
|
403
|
+
allEffects.delete(effect);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const kept = [];
|
|
407
|
+
for (const cleanup of rootCleanups.splice(0)) {
|
|
408
|
+
if (!gone(cleanup.scope)) {
|
|
409
|
+
kept.push(cleanup);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
cleanup.fn();
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
// Cleanup errors must not prevent remaining cleanups or DOM swap.
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
rootCleanups.push(...kept);
|
|
420
|
+
const owners = new Set([...ownerCleanups.keys(), ...ownerEffects.keys()]);
|
|
421
|
+
for (const owner of owners) {
|
|
422
|
+
if (gone(ownerScopes.get(owner) ?? null))
|
|
423
|
+
krateDisposeOwner(owner);
|
|
424
|
+
}
|
|
425
|
+
if (!pageTeardown)
|
|
426
|
+
return;
|
|
427
|
+
// Islands are only mounted by page code (layouts with islands are never kept).
|
|
428
|
+
const g = globalThis;
|
|
429
|
+
const islands = g.__krate_island_cleanups;
|
|
430
|
+
if (Array.isArray(islands)) {
|
|
431
|
+
g.__krate_island_cleanups = [];
|
|
432
|
+
for (const cleanup of islands) {
|
|
433
|
+
try {
|
|
434
|
+
cleanup();
|
|
435
|
+
}
|
|
436
|
+
catch {
|
|
437
|
+
// Island unmount errors must not prevent the SPA swap.
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
routeScope = null;
|
|
442
|
+
resetContexts();
|
|
443
|
+
}
|
|
302
444
|
export function disposeAll() {
|
|
445
|
+
routeScope = null;
|
|
303
446
|
for (const hook of disposeHooks) {
|
|
304
447
|
try {
|
|
305
|
-
hook();
|
|
448
|
+
hook(null);
|
|
306
449
|
}
|
|
307
450
|
catch {
|
|
308
451
|
// Unmount errors must not prevent the rest of the teardown.
|
|
@@ -314,7 +457,7 @@ export function disposeAll() {
|
|
|
314
457
|
allEffects.clear();
|
|
315
458
|
for (const cleanup of rootCleanups) {
|
|
316
459
|
try {
|
|
317
|
-
cleanup();
|
|
460
|
+
cleanup.fn();
|
|
318
461
|
}
|
|
319
462
|
catch {
|
|
320
463
|
// Cleanup errors must not prevent remaining cleanups or DOM swap.
|
|
@@ -325,6 +468,7 @@ export function disposeAll() {
|
|
|
325
468
|
krateDisposeOwner(owner);
|
|
326
469
|
}
|
|
327
470
|
ownerEffects.clear();
|
|
471
|
+
ownerScopes.clear();
|
|
328
472
|
changedCells.clear();
|
|
329
473
|
historyCells.clear();
|
|
330
474
|
snapshotEpoch = null;
|
|
@@ -718,8 +862,10 @@ function flushEffectsInner() {
|
|
|
718
862
|
// microtask can run. Otherwise code running between passes (an action's
|
|
719
863
|
// settle callback) would read React state that is written but not yet
|
|
720
864
|
// committed.
|
|
721
|
-
|
|
865
|
+
let pass = 0;
|
|
866
|
+
for (; pending.length > 0 && pass < MAX_UPDATE_PASSES; pass++) {
|
|
722
867
|
const effects = pending.splice(0);
|
|
868
|
+
lastPassEffects = effects;
|
|
723
869
|
for (const effect of effects) {
|
|
724
870
|
if (effect.disposed) {
|
|
725
871
|
continue;
|
|
@@ -733,21 +879,23 @@ function flushEffectsInner() {
|
|
|
733
879
|
}
|
|
734
880
|
}
|
|
735
881
|
}
|
|
882
|
+
if (pending.length > 0)
|
|
883
|
+
abortUpdateLoop();
|
|
736
884
|
if (pending.length === 0) {
|
|
737
885
|
// The cascade is done: a flush the passes scheduled has nothing to do.
|
|
738
886
|
flushMode = 0;
|
|
739
887
|
flushScheduled = false;
|
|
740
888
|
flushToken++;
|
|
741
889
|
}
|
|
742
|
-
// A cascade longer than the pass cap continues in the scheduled flush; the
|
|
743
|
-
// commit runs once it settles, so it sees the final DOM.
|
|
744
890
|
if (pending.length === 0) {
|
|
745
891
|
// Settle hooks (the class component render phase) may write signals;
|
|
746
892
|
// flush those in this same pass so the commit sees every DOM write.
|
|
747
893
|
for (const hook of settleHooks) {
|
|
748
894
|
hook();
|
|
749
895
|
}
|
|
750
|
-
if (pending.length > 0
|
|
896
|
+
if (pending.length > 0) {
|
|
897
|
+
if (settleDepth >= 50)
|
|
898
|
+
abortUpdateLoop();
|
|
751
899
|
settleDepth++;
|
|
752
900
|
try {
|
|
753
901
|
flushEffects();
|
|
@@ -760,6 +908,34 @@ function flushEffectsInner() {
|
|
|
760
908
|
runAfterFlush();
|
|
761
909
|
}
|
|
762
910
|
}
|
|
911
|
+
// A flush chain that needs more passes than this is an update loop (two
|
|
912
|
+
// effects feeding each other), not a cascade that settles.
|
|
913
|
+
const MAX_UPDATE_PASSES = 100;
|
|
914
|
+
let lastPassEffects = [];
|
|
915
|
+
// Drops the pending queue and reports the loop as React does, so the page
|
|
916
|
+
// stays responsive instead of re-flushing forever.
|
|
917
|
+
function abortUpdateLoop() {
|
|
918
|
+
const looping = pending.splice(0);
|
|
919
|
+
for (const effect of looping)
|
|
920
|
+
effect.queued = false;
|
|
921
|
+
flushScheduled = false;
|
|
922
|
+
flushMode = 0;
|
|
923
|
+
flushToken++;
|
|
924
|
+
const culprits = looping.length > 0 ? looping : lastPassEffects;
|
|
925
|
+
lastPassEffects = [];
|
|
926
|
+
const who = Array.from(new Set(culprits.map((e) => e.fn.name || 'anonymous effect')))
|
|
927
|
+
.slice(0, 3)
|
|
928
|
+
.join(', ');
|
|
929
|
+
const err = new Error('Maximum update depth exceeded. An effect keeps writing state that re-runs itself or another effect (' +
|
|
930
|
+
who +
|
|
931
|
+
'). Make the write conditional or remove the dependency that changes on every run.');
|
|
932
|
+
// The nearest error boundary of a looping effect handles it, as React does.
|
|
933
|
+
for (const effect of culprits) {
|
|
934
|
+
if (effect.boundaryOwner && ownerHooks && ownerHooks.error(effect.boundaryOwner, err))
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
throw err;
|
|
938
|
+
}
|
|
763
939
|
const settleHooks = [];
|
|
764
940
|
let settleDepth = 0;
|
|
765
941
|
/**
|
|
@@ -804,7 +980,9 @@ export function krateFlushSync() {
|
|
|
804
980
|
}
|
|
805
981
|
// flushEffects runs the after-flush hooks itself once nothing is pending.
|
|
806
982
|
let guard = 0;
|
|
807
|
-
while (pending.length > 0
|
|
983
|
+
while (pending.length > 0) {
|
|
984
|
+
if (guard++ >= MAX_UPDATE_PASSES)
|
|
985
|
+
abortUpdateLoop();
|
|
808
986
|
flushEffects();
|
|
809
987
|
}
|
|
810
988
|
}
|
|
@@ -823,6 +1001,12 @@ function runEffect(effect) {
|
|
|
823
1001
|
for (const cleanup of effect.cleanups.splice(0)) {
|
|
824
1002
|
cleanup();
|
|
825
1003
|
}
|
|
1004
|
+
// Re-track from scratch so a dependency the last run did not read stops
|
|
1005
|
+
// re-running the effect.
|
|
1006
|
+
for (const set of effect.sources) {
|
|
1007
|
+
set.delete(effect);
|
|
1008
|
+
}
|
|
1009
|
+
effect.sources.clear();
|
|
826
1010
|
context.push(effect);
|
|
827
1011
|
pushed = true;
|
|
828
1012
|
// Support Solid-style `return () => {...}` cleanup functions. Many
|
|
@@ -866,7 +1050,11 @@ export function createEffect(fn) {
|
|
|
866
1050
|
ownerHooks.cleanup(effect.boundaryOwner, dispose);
|
|
867
1051
|
}
|
|
868
1052
|
runEffect(effect);
|
|
869
|
-
|
|
1053
|
+
// A nested effect belongs to the running parent: it is disposed when the
|
|
1054
|
+
// parent re-runs or is disposed.
|
|
1055
|
+
if (owner && !owner.disposed)
|
|
1056
|
+
owner.cleanups.push(dispose);
|
|
1057
|
+
else if (owner && owner.root)
|
|
870
1058
|
addOwnerCleanup(owner, dispose);
|
|
871
1059
|
return dispose;
|
|
872
1060
|
}
|
|
@@ -885,13 +1073,14 @@ export function onCleanup(fn) {
|
|
|
885
1073
|
// No active effect: a component-level cleanup. It runs when its owner
|
|
886
1074
|
// unmounts, or on disposeAll() (route change / page teardown).
|
|
887
1075
|
if (currentOwner !== null) {
|
|
1076
|
+
krateTagOwner(currentOwner, krateCurrentScope());
|
|
888
1077
|
let list = ownerCleanups.get(currentOwner);
|
|
889
1078
|
if (!list)
|
|
890
1079
|
ownerCleanups.set(currentOwner, (list = []));
|
|
891
1080
|
list.push(fn);
|
|
892
1081
|
}
|
|
893
1082
|
else {
|
|
894
|
-
rootCleanups.push(fn);
|
|
1083
|
+
rootCleanups.push({ fn, scope: krateCurrentScope() });
|
|
895
1084
|
}
|
|
896
1085
|
}
|
|
897
1086
|
}
|
|
@@ -984,29 +1173,70 @@ function flushMounts() {
|
|
|
984
1173
|
// when the recomputed value is unchanged, avoiding thundering re-renders.
|
|
985
1174
|
export function createMemo(fn) {
|
|
986
1175
|
const [get, set] = createSignal(undefined);
|
|
987
|
-
|
|
988
|
-
|
|
1176
|
+
let stale = true;
|
|
1177
|
+
// The node tracks fn's sources and only marks the memo stale; the value is
|
|
1178
|
+
// recomputed on read, so a read right after a write is current.
|
|
1179
|
+
const node = newEffectState(() => { });
|
|
1180
|
+
allEffects.add(node);
|
|
1181
|
+
const recompute = () => {
|
|
1182
|
+
for (const cleanup of node.cleanups.splice(0))
|
|
1183
|
+
cleanup();
|
|
1184
|
+
for (const source of node.sources)
|
|
1185
|
+
source.delete(node);
|
|
1186
|
+
node.sources.clear();
|
|
1187
|
+
stale = false;
|
|
1188
|
+
context.push(node);
|
|
1189
|
+
let next;
|
|
1190
|
+
try {
|
|
1191
|
+
next = fn();
|
|
1192
|
+
}
|
|
1193
|
+
catch (err) {
|
|
1194
|
+
stale = true;
|
|
1195
|
+
throw err;
|
|
1196
|
+
}
|
|
1197
|
+
finally {
|
|
1198
|
+
context.pop();
|
|
1199
|
+
}
|
|
989
1200
|
set(next);
|
|
1201
|
+
};
|
|
1202
|
+
// Settles the memo in the next flush even when nothing reads it, so
|
|
1203
|
+
// subscribers of an unread memo still hear about a changed value.
|
|
1204
|
+
const checker = newEffectState(() => {
|
|
1205
|
+
if (stale && !node.disposed)
|
|
1206
|
+
recompute();
|
|
990
1207
|
});
|
|
991
|
-
allEffects.add(
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1208
|
+
allEffects.add(checker);
|
|
1209
|
+
node.mark = () => {
|
|
1210
|
+
stale = true;
|
|
1211
|
+
if (!checker.queued && !checker.disposed) {
|
|
1212
|
+
checker.queued = true;
|
|
1213
|
+
pending.push(checker);
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
runEffect(checker);
|
|
1217
|
+
const read = () => {
|
|
1218
|
+
if (stale && !node.disposed)
|
|
1219
|
+
recompute();
|
|
1220
|
+
return get();
|
|
1221
|
+
};
|
|
1222
|
+
// Registered on the parent context so the memo is disposed when the parent
|
|
1223
|
+
// re-runs or is disposed.
|
|
996
1224
|
const parent = currentEffect();
|
|
997
1225
|
const disposeMemo = () => {
|
|
998
|
-
if (!
|
|
999
|
-
disposeEffect(
|
|
1000
|
-
allEffects.delete(
|
|
1226
|
+
if (!node.disposed) {
|
|
1227
|
+
disposeEffect(node);
|
|
1228
|
+
allEffects.delete(node);
|
|
1229
|
+
disposeEffect(checker);
|
|
1230
|
+
allEffects.delete(checker);
|
|
1001
1231
|
}
|
|
1002
1232
|
};
|
|
1003
1233
|
if (parent) {
|
|
1004
1234
|
parent.cleanups.push(disposeMemo);
|
|
1005
1235
|
}
|
|
1006
|
-
else if (
|
|
1007
|
-
ownerHooks.cleanup(
|
|
1236
|
+
else if (checker.boundaryOwner && ownerHooks) {
|
|
1237
|
+
ownerHooks.cleanup(checker.boundaryOwner, disposeMemo);
|
|
1008
1238
|
}
|
|
1009
|
-
return
|
|
1239
|
+
return read;
|
|
1010
1240
|
}
|
|
1011
1241
|
/**
|
|
1012
1242
|
* Reducer state primitive: a `createSignal` whose setter applies a reducer.
|
package/dist/slots.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export * from './portal.js';
|
|
2
2
|
/** Fragments holding currently hidden conditional branches. */
|
|
3
3
|
export declare function krateCondDetached(): DocumentFragment[];
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Forgets hidden branches of the previous page (called on slot rescans).
|
|
6
|
+
* Branches whose conditional is still in the document (a layout the SPA
|
|
7
|
+
* router kept mounted), directly or inside another kept hidden branch, stay.
|
|
8
|
+
*/
|
|
5
9
|
export declare function krateCondReset(): void;
|
|
6
10
|
/**
|
|
7
11
|
* Shows the consequent (`show` true) or alternate branch of the conditional
|
package/dist/slots.js
CHANGED
|
@@ -16,13 +16,38 @@ export * from './portal.js';
|
|
|
16
16
|
// Branches swapped out of the document. Hydration lookups (findSlot) search
|
|
17
17
|
// them too, so a binding registered after a swap still finds its node.
|
|
18
18
|
const detached = new Set();
|
|
19
|
+
// Start marker of the conditional each detached fragment belongs to.
|
|
20
|
+
const detachedStart = new WeakMap();
|
|
19
21
|
/** Fragments holding currently hidden conditional branches. */
|
|
20
22
|
export function krateCondDetached() {
|
|
21
23
|
return Array.from(detached);
|
|
22
24
|
}
|
|
23
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Forgets hidden branches of the previous page (called on slot rescans).
|
|
27
|
+
* Branches whose conditional is still in the document (a layout the SPA
|
|
28
|
+
* router kept mounted), directly or inside another kept hidden branch, stay.
|
|
29
|
+
*/
|
|
24
30
|
export function krateCondReset() {
|
|
25
|
-
|
|
31
|
+
const keep = new Set();
|
|
32
|
+
let grew = true;
|
|
33
|
+
while (grew) {
|
|
34
|
+
grew = false;
|
|
35
|
+
for (const frag of detached) {
|
|
36
|
+
if (keep.has(frag))
|
|
37
|
+
continue;
|
|
38
|
+
const start = detachedStart.get(frag);
|
|
39
|
+
if (!start)
|
|
40
|
+
continue;
|
|
41
|
+
const home = start.getRootNode();
|
|
42
|
+
if (start.isConnected || keep.has(home)) {
|
|
43
|
+
keep.add(frag);
|
|
44
|
+
grew = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const frag of Array.from(detached))
|
|
49
|
+
if (!keep.has(frag))
|
|
50
|
+
detached.delete(frag);
|
|
26
51
|
}
|
|
27
52
|
function findCondEnd(start) {
|
|
28
53
|
const want = '/' + (start.nodeValue || '');
|
|
@@ -73,8 +98,19 @@ export function krateCondSwap(start, show, tpl) {
|
|
|
73
98
|
let incoming = st.other;
|
|
74
99
|
if (!incoming) {
|
|
75
100
|
incoming = document.createDocumentFragment();
|
|
76
|
-
if (st.tpl)
|
|
101
|
+
if (st.tpl) {
|
|
77
102
|
incoming.appendChild(st.tpl.content);
|
|
103
|
+
// An SVG branch is wrapped in an <svg> so it parses in the SVG
|
|
104
|
+
// namespace; unwrap it.
|
|
105
|
+
if (st.tpl.getAttribute('data-kns') === 'svg') {
|
|
106
|
+
const wrap = incoming.firstChild;
|
|
107
|
+
if (wrap && wrap.nodeType === 1) {
|
|
108
|
+
while (wrap.firstChild)
|
|
109
|
+
incoming.insertBefore(wrap.firstChild, wrap);
|
|
110
|
+
incoming.removeChild(wrap);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
78
114
|
}
|
|
79
115
|
detached.delete(incoming);
|
|
80
116
|
// React focuses an autoFocus element when it mounts (and renders no
|
|
@@ -86,6 +122,7 @@ export function krateCondSwap(start, show, tpl) {
|
|
|
86
122
|
kratePortalsEnter(portals);
|
|
87
123
|
st.other = out;
|
|
88
124
|
detached.add(out);
|
|
125
|
+
detachedStart.set(out, start);
|
|
89
126
|
st.shown = want;
|
|
90
127
|
krateRefsEnter();
|
|
91
128
|
if (auto) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krate/runtime",
|
|
3
|
-
"version": "0.5.0-
|
|
4
|
-
"description": "Client-side runtime for Krate
|
|
3
|
+
"version": "0.5.0-beta.1",
|
|
4
|
+
"description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"homepage": "https://krate.js.org",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"types": "./dist/server.d.ts",
|
|
35
35
|
"default": "./dist/server.js"
|
|
36
36
|
},
|
|
37
|
+
"./cache": {
|
|
38
|
+
"types": "./dist/cache.d.ts",
|
|
39
|
+
"default": "./dist/cache.js"
|
|
40
|
+
},
|
|
37
41
|
"./server/jsx-runtime": {
|
|
38
42
|
"types": "./dist/server-jsx-runtime.d.ts",
|
|
39
43
|
"default": "./dist/server-jsx-runtime.js"
|