@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.2

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.
Files changed (165) hide show
  1. package/dist/action-context.d.ts +5 -1
  2. package/dist/action-context.js +33 -3
  3. package/dist/action-context.test.js +26 -1
  4. package/dist/actions.d.ts +12 -1
  5. package/dist/actions.js +84 -4
  6. package/dist/actions.test.js +61 -1
  7. package/dist/assets-static.d.ts +5 -0
  8. package/dist/assets-static.js +31 -0
  9. package/dist/boundary.d.ts +12 -0
  10. package/dist/boundary.js +53 -13
  11. package/dist/cache-adapter.d.ts +113 -0
  12. package/dist/cache-adapter.js +217 -0
  13. package/dist/cache-adapter.test.d.ts +1 -0
  14. package/dist/cache-adapter.test.js +244 -0
  15. package/dist/cache-redis.d.ts +27 -0
  16. package/dist/cache-redis.js +110 -0
  17. package/dist/cache-scope.d.ts +25 -9
  18. package/dist/cache-scope.js +64 -13
  19. package/dist/cache.d.ts +1 -0
  20. package/dist/cache.js +3 -3
  21. package/dist/cache.test.js +16 -0
  22. package/dist/class-component.js +6 -0
  23. package/dist/class-component.test.js +53 -2
  24. package/dist/class-helpers.d.ts +4 -4
  25. package/dist/class-helpers.js +357 -32
  26. package/dist/class-helpers.test.d.ts +1 -0
  27. package/dist/class-helpers.test.js +169 -0
  28. package/dist/context.d.ts +16 -2
  29. package/dist/context.js +44 -3
  30. package/dist/dev-overlay.js +85 -12
  31. package/dist/dev-overlay.test.js +22 -0
  32. package/dist/dom.d.ts +15 -0
  33. package/dist/dom.js +97 -3
  34. package/dist/effect-errors.test.d.ts +1 -0
  35. package/dist/effect-errors.test.js +163 -0
  36. package/dist/effects.d.ts +8 -0
  37. package/dist/effects.js +13 -0
  38. package/dist/error-overlay.js +7 -7
  39. package/dist/handler-html.d.ts +63 -0
  40. package/dist/handler-html.js +285 -0
  41. package/dist/handler-html.test.d.ts +1 -0
  42. package/dist/handler-html.test.js +256 -0
  43. package/dist/handler-routing.d.ts +83 -0
  44. package/dist/handler-routing.js +579 -0
  45. package/dist/handler-routing.test.d.ts +1 -0
  46. package/dist/handler-routing.test.js +99 -0
  47. package/dist/handler-segments.d.ts +43 -0
  48. package/dist/handler-segments.js +115 -0
  49. package/dist/handler-segments.test.d.ts +1 -0
  50. package/dist/handler-segments.test.js +217 -0
  51. package/dist/head-tags.d.ts +51 -0
  52. package/dist/head-tags.js +329 -0
  53. package/dist/head-tags.test.d.ts +1 -0
  54. package/dist/head-tags.test.js +131 -0
  55. package/dist/highlight-bind.d.ts +42 -0
  56. package/dist/highlight-bind.js +176 -0
  57. package/dist/highlight-bind.test.d.ts +1 -0
  58. package/dist/highlight-bind.test.js +124 -0
  59. package/dist/highlight.d.ts +93 -0
  60. package/dist/highlight.js +426 -0
  61. package/dist/highlight.test.d.ts +1 -0
  62. package/dist/highlight.test.js +56 -0
  63. package/dist/hints.d.ts +19 -0
  64. package/dist/hints.js +97 -0
  65. package/dist/hook-args.d.ts +7 -0
  66. package/dist/hook-args.js +10 -0
  67. package/dist/htmlspec-attributes.d.ts +1 -0
  68. package/dist/htmlspec-attributes.js +159 -0
  69. package/dist/index.d.ts +4 -0
  70. package/dist/index.js +3 -0
  71. package/dist/jsx-types.d.ts +11 -0
  72. package/dist/krate-core.js +2 -0
  73. package/dist/krate-dev.js +75 -12
  74. package/dist/krate-f-actions.js +2 -0
  75. package/dist/krate-f-classes.js +8 -0
  76. package/dist/krate-f-helpers.js +2 -0
  77. package/dist/krate-f-highlight.js +2 -0
  78. package/dist/krate-f-list.js +2 -0
  79. package/dist/krate-f-navigation.js +2 -0
  80. package/dist/krate-f-router-lite.js +2 -0
  81. package/dist/krate-f-router.js +2 -0
  82. package/dist/krate-features.json +493 -0
  83. package/dist/krate-hydrate-lite.js +5 -5
  84. package/dist/krate-hydrate.js +5 -5
  85. package/dist/krate-runtime.js +5 -5
  86. package/dist/list.js +4 -2
  87. package/dist/metadata-routes.d.ts +75 -0
  88. package/dist/metadata-routes.js +76 -0
  89. package/dist/metadata.d.ts +222 -0
  90. package/dist/metadata.js +452 -0
  91. package/dist/metadata.test.d.ts +1 -0
  92. package/dist/metadata.test.js +167 -0
  93. package/dist/navigation-server.d.ts +27 -0
  94. package/dist/navigation-server.js +90 -0
  95. package/dist/navigation.d.ts +37 -0
  96. package/dist/navigation.js +195 -0
  97. package/dist/navigation.test.d.ts +1 -0
  98. package/dist/navigation.test.js +130 -0
  99. package/dist/node-server.d.ts +22 -0
  100. package/dist/node-server.js +436 -0
  101. package/dist/node-server.test.d.ts +1 -0
  102. package/dist/node-server.test.js +492 -0
  103. package/dist/og-files.d.ts +2 -0
  104. package/dist/og-files.js +19 -0
  105. package/dist/og.d.ts +56 -0
  106. package/dist/og.js +156 -0
  107. package/dist/patterns.test.d.ts +1 -0
  108. package/dist/patterns.test.js +98 -0
  109. package/dist/react.d.ts +47 -0
  110. package/dist/react.js +59 -1
  111. package/dist/react19.test.d.ts +1 -0
  112. package/dist/react19.test.js +176 -0
  113. package/dist/reactivity-core.test.d.ts +1 -0
  114. package/dist/reactivity-core.test.js +152 -0
  115. package/dist/reconcile.js +4 -2
  116. package/dist/request-context.d.ts +23 -0
  117. package/dist/request-context.js +45 -0
  118. package/dist/resp.d.ts +55 -0
  119. package/dist/resp.js +239 -0
  120. package/dist/router-layout.test.js +70 -0
  121. package/dist/router-nav.test.js +65 -0
  122. package/dist/router-speculation.test.d.ts +1 -0
  123. package/dist/router-speculation.test.js +25 -0
  124. package/dist/router-styles.test.d.ts +1 -0
  125. package/dist/router-styles.test.js +98 -0
  126. package/dist/router.js +175 -36
  127. package/dist/segmented-lru.d.ts +19 -0
  128. package/dist/segmented-lru.js +87 -0
  129. package/dist/server-async.test.js +43 -1
  130. package/dist/server-handler.d.ts +165 -0
  131. package/dist/server-handler.js +1826 -0
  132. package/dist/server-handler.test.d.ts +1 -0
  133. package/dist/server-handler.test.js +522 -0
  134. package/dist/server-highlight.test.d.ts +1 -0
  135. package/dist/server-highlight.test.js +32 -0
  136. package/dist/server-renderer.js +165 -720
  137. package/dist/server-renderer.test.d.ts +1 -0
  138. package/dist/server-renderer.test.js +266 -0
  139. package/dist/server.d.ts +13 -1
  140. package/dist/server.js +131 -16
  141. package/dist/server.test.js +20 -0
  142. package/dist/sha256.d.ts +1 -0
  143. package/dist/sha256.js +66 -0
  144. package/dist/sha256.test.d.ts +1 -0
  145. package/dist/sha256.test.js +13 -0
  146. package/dist/shared-isr.d.ts +31 -0
  147. package/dist/shared-isr.js +142 -0
  148. package/dist/signal.d.ts +3 -2
  149. package/dist/signal.js +163 -23
  150. package/dist/slots.js +12 -1
  151. package/dist/ssr-core-behavior.test.d.ts +1 -0
  152. package/dist/ssr-core-behavior.test.js +620 -0
  153. package/dist/ssr-core.d.ts +175 -0
  154. package/dist/ssr-core.js +940 -0
  155. package/dist/ssr-core.test.d.ts +1 -0
  156. package/dist/ssr-core.test.js +324 -0
  157. package/dist/static-memory.d.ts +3 -0
  158. package/dist/static-memory.js +47 -0
  159. package/dist/static-memory.test.d.ts +1 -0
  160. package/dist/static-memory.test.js +26 -0
  161. package/dist/test-redis.d.ts +12 -0
  162. package/dist/test-redis.js +131 -0
  163. package/dist/use-id.d.ts +8 -0
  164. package/dist/use-id.js +40 -0
  165. package/package.json +30 -2
@@ -0,0 +1,31 @@
1
+ import { type CacheEntry, type IsrStore } from './ssr-core.js';
2
+ import { type CacheAdapter, type CacheGuard, type CacheSpec } from './cache-adapter.js';
3
+ /**
4
+ * The ISR page cache on a shared adapter. Entries live in the adapter, so every
5
+ * instance serves and invalidates the same pages. When the adapter is down the
6
+ * store falls back to process memory, and the page is rendered rather than failed.
7
+ */
8
+ export declare class SharedIsrStore implements IsrStore {
9
+ private readonly guard;
10
+ private readonly ttlSeconds;
11
+ private readonly memory;
12
+ constructor(guard: CacheGuard, ttlSeconds?: number, maxMemory?: number);
13
+ get(key: string): Promise<CacheEntry | undefined>;
14
+ set(key: string, entry: CacheEntry): Promise<void>;
15
+ deleteRoute(route: string): Promise<number>;
16
+ deleteVariant(route: string, params: Record<string, string>): Promise<number>;
17
+ deleteTags(tags: string[]): Promise<number>;
18
+ entriesForRoute(route: string): Promise<Array<[string, CacheEntry]>>;
19
+ }
20
+ /** Where an ISR store came from: the shared cache once its adapter is up. */
21
+ export interface IsrStoreSetup {
22
+ store: IsrStore;
23
+ /** Resolves with the adapter once it started (null when none is configured or it failed). */
24
+ ready: Promise<CacheAdapter | null>;
25
+ }
26
+ /**
27
+ * The ISR store for a cache spec: `fallback` (process memory) without an
28
+ * adapter or while it starts; the shared store after. A spec that fails to load
29
+ * logs and keeps `fallback`, so a bad cache setting never takes the site down.
30
+ */
31
+ export declare function isrStoreFor(specOrAdapter: CacheSpec | CacheAdapter | Promise<CacheAdapter> | undefined, fallback: IsrStore): IsrStoreSetup;
@@ -0,0 +1,142 @@
1
+ // The ISR page cache on a shared cache adapter (see cache-adapter.ts).
2
+ import { MemoryIsrStore, variantKey } from './ssr-core.js';
3
+ import { installCacheAdapter, loadCacheAdapter, tagName } from './cache-adapter.js';
4
+ const ISR_PREFIX = 'isr:';
5
+ /**
6
+ * The ISR page cache on a shared adapter. Entries live in the adapter, so every
7
+ * instance serves and invalidates the same pages. When the adapter is down the
8
+ * store falls back to process memory, and the page is rendered rather than failed.
9
+ */
10
+ export class SharedIsrStore {
11
+ constructor(guard, ttlSeconds = 7 * 24 * 3600, maxMemory = 512) {
12
+ this.guard = guard;
13
+ this.ttlSeconds = ttlSeconds;
14
+ this.memory = new MemoryIsrStore(maxMemory);
15
+ }
16
+ async get(key) {
17
+ const r = await this.guard.call((a) => a.get(ISR_PREFIX + key));
18
+ if (r.ok) {
19
+ if (r.value === null)
20
+ return undefined;
21
+ try {
22
+ return JSON.parse(r.value);
23
+ }
24
+ catch {
25
+ return undefined;
26
+ }
27
+ }
28
+ return this.memory.get(key);
29
+ }
30
+ async set(key, entry) {
31
+ const route = routeOfKey(key);
32
+ const tags = (entry.tags || []).map(tagName).concat(routeTag(route));
33
+ const r = await this.guard.call((a) => a.set(ISR_PREFIX + key, JSON.stringify(entry), { ttlSeconds: this.ttlSeconds, tags }));
34
+ // Without the shared store this instance still caches for itself.
35
+ if (!r.ok)
36
+ this.memory.set(key, entry);
37
+ }
38
+ async deleteRoute(route) {
39
+ let n = this.memory.deleteRoute(route);
40
+ const r = await this.guard.call((a) => a.deleteByTags([routeTag(route)]));
41
+ if (r.ok)
42
+ n += r.value;
43
+ return n;
44
+ }
45
+ async deleteVariant(route, params) {
46
+ let n = this.memory.deleteVariant(route, params);
47
+ const base = variantKey({ route, params });
48
+ const keys = await this.guard.call((a) => a.keysByTag(routeTag(route)));
49
+ if (keys.ok) {
50
+ const stale = keys.value.filter((k) => {
51
+ const bare = k.startsWith(ISR_PREFIX) ? k.slice(ISR_PREFIX.length) : k;
52
+ return bare === base || bare.startsWith(base + '#');
53
+ });
54
+ for (const k of stale) {
55
+ const d = await this.guard.call((a) => a.delete(k));
56
+ if (d.ok)
57
+ n++;
58
+ }
59
+ }
60
+ return n;
61
+ }
62
+ async deleteTags(tags) {
63
+ let n = this.memory.deleteTags(tags);
64
+ const r = await this.guard.call((a) => a.deleteByTags(tags.map(tagName)));
65
+ if (r.ok)
66
+ n += r.value;
67
+ return n;
68
+ }
69
+ async entriesForRoute(route) {
70
+ const keys = await this.guard.call((a) => a.keysByTag(routeTag(route)));
71
+ if (!keys.ok)
72
+ return this.memory.entriesForRoute(route);
73
+ const out = [];
74
+ for (const k of keys.value) {
75
+ if (!k.startsWith(ISR_PREFIX))
76
+ continue;
77
+ const r = await this.guard.call((a) => a.get(k));
78
+ if (!r.ok || r.value === null)
79
+ continue;
80
+ try {
81
+ out.push([k.slice(ISR_PREFIX.length), JSON.parse(r.value)]);
82
+ }
83
+ catch {
84
+ /* skip an unreadable entry */
85
+ }
86
+ }
87
+ return out;
88
+ }
89
+ }
90
+ function routeOfKey(key) {
91
+ return key.split('?')[0].split('#')[0];
92
+ }
93
+ function routeTag(route) {
94
+ return 'route:' + route;
95
+ }
96
+ /**
97
+ * The ISR store for a cache spec: `fallback` (process memory) without an
98
+ * adapter or while it starts; the shared store after. A spec that fails to load
99
+ * logs and keeps `fallback`, so a bad cache setting never takes the site down.
100
+ */
101
+ export function isrStoreFor(specOrAdapter, fallback) {
102
+ const promised = specOrAdapter && typeof specOrAdapter.then === 'function' ? specOrAdapter : undefined;
103
+ if (promised) {
104
+ let active = fallback;
105
+ const ready = promised.then((adapter) => {
106
+ const installed = installCacheAdapter(adapter, {});
107
+ active = new SharedIsrStore(installed.guard);
108
+ return adapter;
109
+ }, (err) => {
110
+ console.error('[krate] the cache adapter failed to start; using the in-process caches:', err);
111
+ return null;
112
+ });
113
+ return { store: delegating(() => active, ready), ready };
114
+ }
115
+ const given = specOrAdapter && typeof specOrAdapter.get === 'function' ? specOrAdapter : undefined;
116
+ const spec = given ? {} : specOrAdapter;
117
+ if (!given && (!spec || !spec.adapter || spec.adapter === 'memory'))
118
+ return { store: fallback, ready: Promise.resolve(null) };
119
+ let active = fallback;
120
+ const load = given ? Promise.resolve(given) : loadCacheAdapter(spec);
121
+ const ready = load.then((adapter) => {
122
+ if (!adapter)
123
+ return null;
124
+ const installed = installCacheAdapter(adapter, spec || {});
125
+ active = new SharedIsrStore(installed.guard, spec?.ttlSeconds, 512);
126
+ return adapter;
127
+ }, (err) => {
128
+ console.error('[krate] the cache adapter failed to start; using the in-process caches:', err);
129
+ return null;
130
+ });
131
+ return { store: delegating(() => active, ready), ready };
132
+ }
133
+ function delegating(current, ready) {
134
+ return {
135
+ get: async (key) => (await ready, current().get(key)),
136
+ set: async (key, entry) => (await ready, current().set(key, entry)),
137
+ deleteRoute: async (route) => (await ready, current().deleteRoute(route)),
138
+ deleteVariant: async (route, params) => (await ready, current().deleteVariant(route, params)),
139
+ deleteTags: async (tags) => (await ready, current().deleteTags(tags)),
140
+ entriesForRoute: async (route) => (await ready, current().entriesForRoute(route)),
141
+ };
142
+ }
package/dist/signal.d.ts CHANGED
@@ -151,9 +151,10 @@ export interface CSSARIAOptions {
151
151
  * Accessibility semantics to synthesize. Defaults are native (radios,
152
152
  * checkboxes) and ship no JS. Presets that need synthesized state
153
153
  * (`tabs`, `listbox`, `accordion`, `disclosure`, `dialog`, `menu`,
154
- * `popover`) inject a tiny ARIA synchroniser automatically.
154
+ * `popover`, `nav`) inject a tiny keyboard and ARIA enhancer automatically:
155
+ * Enter/Space/Escape/arrow keys, focus move/trap/restore, aria-expanded.
155
156
  */
156
- as?: 'radiogroup' | 'tabs' | 'tablist' | 'listbox' | 'menu' | 'menubar' | 'accordion' | 'disclosure' | 'dialog' | 'modal' | 'popover' | 'switch' | 'checkbox';
157
+ as?: 'radiogroup' | 'tabs' | 'tablist' | 'listbox' | 'menu' | 'menubar' | 'nav' | 'accordion' | 'disclosure' | 'dialog' | 'modal' | 'popover' | 'switch' | 'checkbox';
157
158
  /** Accessible label for the scope container. */
158
159
  label?: string;
159
160
  /** Raw ARIA overrides (`{ role, 'aria-controls', panelRole }`); `false` opts out of all ARIA. */
package/dist/signal.js CHANGED
@@ -854,6 +854,21 @@ function flushEffectsInner() {
854
854
  flushScheduled = false;
855
855
  flushMode = 0;
856
856
  flushToken++;
857
+ // One effect throwing must not strand the effects queued behind it: their
858
+ // `queued` flags would stay set and they could never be queued again. Errors
859
+ // are collected, the flush (settle hooks included) finishes, and the first
860
+ // one is rethrown so it still surfaces; later ones are logged.
861
+ let failed = false;
862
+ let firstError;
863
+ const fail = (err) => {
864
+ if (!failed) {
865
+ failed = true;
866
+ firstError = err;
867
+ }
868
+ else if (typeof console !== 'undefined') {
869
+ console.error(err);
870
+ }
871
+ };
857
872
  for (const hook of beforeFlushHooks) {
858
873
  hook();
859
874
  }
@@ -862,47 +877,119 @@ function flushEffectsInner() {
862
877
  // microtask can run. Otherwise code running between passes (an action's
863
878
  // settle callback) would read React state that is written but not yet
864
879
  // committed.
865
- for (let pass = 0; pending.length > 0 && pass < 100; pass++) {
880
+ let pass = 0;
881
+ for (; pending.length > 0 && pass < MAX_UPDATE_PASSES; pass++) {
866
882
  const effects = pending.splice(0);
883
+ lastPassEffects = effects;
867
884
  for (const effect of effects) {
868
885
  if (effect.disposed) {
869
886
  continue;
870
887
  }
871
888
  effect.queued = false;
872
889
  effect.rerun = false;
873
- runEffect(effect);
890
+ try {
891
+ runEffect(effect);
892
+ }
893
+ catch (err) {
894
+ fail(err);
895
+ }
874
896
  if (effect.rerun && !effect.disposed) {
875
897
  effect.rerun = false;
876
- runEffect(effect);
898
+ try {
899
+ runEffect(effect);
900
+ }
901
+ catch (err) {
902
+ fail(err);
903
+ }
877
904
  }
878
905
  }
879
906
  }
907
+ if (pending.length > 0) {
908
+ try {
909
+ abortUpdateLoop();
910
+ }
911
+ catch (err) {
912
+ fail(err);
913
+ }
914
+ }
880
915
  if (pending.length === 0) {
881
916
  // The cascade is done: a flush the passes scheduled has nothing to do.
882
917
  flushMode = 0;
883
918
  flushScheduled = false;
884
919
  flushToken++;
885
920
  }
886
- // A cascade longer than the pass cap continues in the scheduled flush; the
887
- // commit runs once it settles, so it sees the final DOM.
888
921
  if (pending.length === 0) {
889
922
  // Settle hooks (the class component render phase) may write signals;
890
923
  // flush those in this same pass so the commit sees every DOM write.
891
924
  for (const hook of settleHooks) {
892
- hook();
925
+ try {
926
+ hook();
927
+ }
928
+ catch (err) {
929
+ fail(err);
930
+ }
893
931
  }
894
- if (pending.length > 0 && settleDepth < 50) {
932
+ if (pending.length > 0) {
933
+ if (settleDepth >= 50) {
934
+ try {
935
+ abortUpdateLoop();
936
+ }
937
+ catch (err) {
938
+ fail(err);
939
+ throw firstError;
940
+ }
941
+ }
895
942
  settleDepth++;
896
943
  try {
897
944
  flushEffects();
898
945
  }
946
+ catch (err) {
947
+ fail(err);
948
+ }
899
949
  finally {
900
950
  settleDepth--;
901
951
  }
952
+ if (failed)
953
+ throw firstError;
902
954
  return;
903
955
  }
904
- runAfterFlush();
956
+ try {
957
+ runAfterFlush();
958
+ }
959
+ catch (err) {
960
+ fail(err);
961
+ }
962
+ }
963
+ if (failed)
964
+ throw firstError;
965
+ }
966
+ // A flush chain that needs more passes than this is an update loop (two
967
+ // effects feeding each other), not a cascade that settles.
968
+ const MAX_UPDATE_PASSES = 100;
969
+ let lastPassEffects = [];
970
+ // Drops the pending queue and reports the loop as React does, so the page
971
+ // stays responsive instead of re-flushing forever.
972
+ function abortUpdateLoop() {
973
+ const looping = pending.splice(0);
974
+ for (const effect of looping)
975
+ effect.queued = false;
976
+ flushScheduled = false;
977
+ flushMode = 0;
978
+ flushToken++;
979
+ const culprits = looping.length > 0 ? looping : lastPassEffects;
980
+ lastPassEffects = [];
981
+ const who = Array.from(new Set(culprits.map((e) => e.fn.name || 'anonymous effect')))
982
+ .slice(0, 3)
983
+ .join(', ');
984
+ const err = new Error('Maximum update depth exceeded. An effect keeps writing state that re-runs itself or another effect (' +
985
+ who +
986
+ '). Make the write conditional or remove the dependency that changes on every run.');
987
+ // The nearest error boundary of a looping effect handles it, as React does.
988
+ for (const effect of culprits) {
989
+ if (effect.boundaryOwner && ownerHooks && ownerHooks.error(effect.boundaryOwner, err))
990
+ return;
905
991
  }
992
+ throw err;
906
993
  }
907
994
  const settleHooks = [];
908
995
  let settleDepth = 0;
@@ -948,7 +1035,9 @@ export function krateFlushSync() {
948
1035
  }
949
1036
  // flushEffects runs the after-flush hooks itself once nothing is pending.
950
1037
  let guard = 0;
951
- while (pending.length > 0 && guard++ < 100) {
1038
+ while (pending.length > 0) {
1039
+ if (guard++ >= MAX_UPDATE_PASSES)
1040
+ abortUpdateLoop();
952
1041
  flushEffects();
953
1042
  }
954
1043
  }
@@ -967,6 +1056,12 @@ function runEffect(effect) {
967
1056
  for (const cleanup of effect.cleanups.splice(0)) {
968
1057
  cleanup();
969
1058
  }
1059
+ // Re-track from scratch so a dependency the last run did not read stops
1060
+ // re-running the effect.
1061
+ for (const set of effect.sources) {
1062
+ set.delete(effect);
1063
+ }
1064
+ effect.sources.clear();
970
1065
  context.push(effect);
971
1066
  pushed = true;
972
1067
  // Support Solid-style `return () => {...}` cleanup functions. Many
@@ -1010,7 +1105,11 @@ export function createEffect(fn) {
1010
1105
  ownerHooks.cleanup(effect.boundaryOwner, dispose);
1011
1106
  }
1012
1107
  runEffect(effect);
1013
- if (owner && owner.root)
1108
+ // A nested effect belongs to the running parent: it is disposed when the
1109
+ // parent re-runs or is disposed.
1110
+ if (owner && !owner.disposed)
1111
+ owner.cleanups.push(dispose);
1112
+ else if (owner && owner.root)
1014
1113
  addOwnerCleanup(owner, dispose);
1015
1114
  return dispose;
1016
1115
  }
@@ -1129,29 +1228,70 @@ function flushMounts() {
1129
1228
  // when the recomputed value is unchanged, avoiding thundering re-renders.
1130
1229
  export function createMemo(fn) {
1131
1230
  const [get, set] = createSignal(undefined);
1132
- const effect = newEffectState(() => {
1133
- const next = fn();
1231
+ let stale = true;
1232
+ // The node tracks fn's sources and only marks the memo stale; the value is
1233
+ // recomputed on read, so a read right after a write is current.
1234
+ const node = newEffectState(() => { });
1235
+ allEffects.add(node);
1236
+ const recompute = () => {
1237
+ for (const cleanup of node.cleanups.splice(0))
1238
+ cleanup();
1239
+ for (const source of node.sources)
1240
+ source.delete(node);
1241
+ node.sources.clear();
1242
+ stale = false;
1243
+ context.push(node);
1244
+ let next;
1245
+ try {
1246
+ next = fn();
1247
+ }
1248
+ catch (err) {
1249
+ stale = true;
1250
+ throw err;
1251
+ }
1252
+ finally {
1253
+ context.pop();
1254
+ }
1134
1255
  set(next);
1256
+ };
1257
+ // Settles the memo in the next flush even when nothing reads it, so
1258
+ // subscribers of an unread memo still hear about a changed value.
1259
+ const checker = newEffectState(() => {
1260
+ if (stale && !node.disposed)
1261
+ recompute();
1135
1262
  });
1136
- allEffects.add(effect);
1137
- // Run the effect to establish subscriptions (pushes to context briefly for tracking)
1138
- runEffect(effect);
1139
- // Register cleanup on the parent context so this memo effect is disposed
1140
- // when the parent re-runs or is disposed.
1263
+ allEffects.add(checker);
1264
+ node.mark = () => {
1265
+ stale = true;
1266
+ if (!checker.queued && !checker.disposed) {
1267
+ checker.queued = true;
1268
+ pending.push(checker);
1269
+ }
1270
+ };
1271
+ runEffect(checker);
1272
+ const read = () => {
1273
+ if (stale && !node.disposed)
1274
+ recompute();
1275
+ return get();
1276
+ };
1277
+ // Registered on the parent context so the memo is disposed when the parent
1278
+ // re-runs or is disposed.
1141
1279
  const parent = currentEffect();
1142
1280
  const disposeMemo = () => {
1143
- if (!effect.disposed) {
1144
- disposeEffect(effect);
1145
- allEffects.delete(effect);
1281
+ if (!node.disposed) {
1282
+ disposeEffect(node);
1283
+ allEffects.delete(node);
1284
+ disposeEffect(checker);
1285
+ allEffects.delete(checker);
1146
1286
  }
1147
1287
  };
1148
1288
  if (parent) {
1149
1289
  parent.cleanups.push(disposeMemo);
1150
1290
  }
1151
- else if (effect.boundaryOwner && ownerHooks) {
1152
- ownerHooks.cleanup(effect.boundaryOwner, disposeMemo);
1291
+ else if (checker.boundaryOwner && ownerHooks) {
1292
+ ownerHooks.cleanup(checker.boundaryOwner, disposeMemo);
1153
1293
  }
1154
- return get;
1294
+ return read;
1155
1295
  }
1156
1296
  /**
1157
1297
  * Reducer state primitive: a `createSignal` whose setter applies a reducer.
package/dist/slots.js CHANGED
@@ -98,8 +98,19 @@ export function krateCondSwap(start, show, tpl) {
98
98
  let incoming = st.other;
99
99
  if (!incoming) {
100
100
  incoming = document.createDocumentFragment();
101
- if (st.tpl)
101
+ if (st.tpl) {
102
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
+ }
103
114
  }
104
115
  detached.delete(incoming);
105
116
  // React focuses an autoFocus element when it mounts (and renders no
@@ -0,0 +1 @@
1
+ export {};