@krate/runtime 0.5.0-alpha.4 → 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.
Files changed (57) 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 +83 -4
  6. package/dist/actions.test.js +61 -1
  7. package/dist/cache-scope.js +4 -1
  8. package/dist/cache.test.js +16 -0
  9. package/dist/class-helpers.d.ts +4 -4
  10. package/dist/class-helpers.js +357 -32
  11. package/dist/class-helpers.test.d.ts +1 -0
  12. package/dist/class-helpers.test.js +169 -0
  13. package/dist/context.d.ts +16 -2
  14. package/dist/context.js +44 -3
  15. package/dist/dev-overlay.js +75 -2
  16. package/dist/dev-overlay.test.js +22 -0
  17. package/dist/dom.d.ts +7 -0
  18. package/dist/dom.js +69 -3
  19. package/dist/error-overlay.js +4 -4
  20. package/dist/hook-args.d.ts +7 -0
  21. package/dist/hook-args.js +10 -0
  22. package/dist/htmlspec-attributes.d.ts +1 -0
  23. package/dist/htmlspec-attributes.js +159 -0
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +1 -0
  26. package/dist/krate-core.js +2 -0
  27. package/dist/krate-dev.js +65 -2
  28. package/dist/krate-f-actions.js +2 -0
  29. package/dist/krate-f-classes.js +8 -0
  30. package/dist/krate-f-helpers.js +2 -0
  31. package/dist/krate-f-list.js +2 -0
  32. package/dist/krate-f-router-lite.js +2 -0
  33. package/dist/krate-f-router.js +2 -0
  34. package/dist/krate-features.json +239 -0
  35. package/dist/krate-hydrate-lite.js +5 -5
  36. package/dist/krate-hydrate.js +5 -5
  37. package/dist/krate-runtime.js +5 -5
  38. package/dist/patterns.test.d.ts +1 -0
  39. package/dist/patterns.test.js +98 -0
  40. package/dist/reactivity-core.test.d.ts +1 -0
  41. package/dist/reactivity-core.test.js +152 -0
  42. package/dist/reconcile.js +4 -2
  43. package/dist/router-nav.test.js +38 -0
  44. package/dist/router-speculation.test.d.ts +1 -0
  45. package/dist/router-speculation.test.js +25 -0
  46. package/dist/router-styles.test.d.ts +1 -0
  47. package/dist/router-styles.test.js +98 -0
  48. package/dist/router.js +116 -32
  49. package/dist/server-async.test.js +20 -0
  50. package/dist/server-renderer.js +109 -22
  51. package/dist/server.d.ts +1 -0
  52. package/dist/server.js +90 -11
  53. package/dist/server.test.js +20 -0
  54. package/dist/signal.d.ts +3 -2
  55. package/dist/signal.js +104 -19
  56. package/dist/slots.js +12 -1
  57. package/package.json +2 -2
package/dist/signal.js CHANGED
@@ -862,8 +862,10 @@ function flushEffectsInner() {
862
862
  // microtask can run. Otherwise code running between passes (an action's
863
863
  // settle callback) would read React state that is written but not yet
864
864
  // committed.
865
- for (let pass = 0; pending.length > 0 && pass < 100; pass++) {
865
+ let pass = 0;
866
+ for (; pending.length > 0 && pass < MAX_UPDATE_PASSES; pass++) {
866
867
  const effects = pending.splice(0);
868
+ lastPassEffects = effects;
867
869
  for (const effect of effects) {
868
870
  if (effect.disposed) {
869
871
  continue;
@@ -877,21 +879,23 @@ function flushEffectsInner() {
877
879
  }
878
880
  }
879
881
  }
882
+ if (pending.length > 0)
883
+ abortUpdateLoop();
880
884
  if (pending.length === 0) {
881
885
  // The cascade is done: a flush the passes scheduled has nothing to do.
882
886
  flushMode = 0;
883
887
  flushScheduled = false;
884
888
  flushToken++;
885
889
  }
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
890
  if (pending.length === 0) {
889
891
  // Settle hooks (the class component render phase) may write signals;
890
892
  // flush those in this same pass so the commit sees every DOM write.
891
893
  for (const hook of settleHooks) {
892
894
  hook();
893
895
  }
894
- if (pending.length > 0 && settleDepth < 50) {
896
+ if (pending.length > 0) {
897
+ if (settleDepth >= 50)
898
+ abortUpdateLoop();
895
899
  settleDepth++;
896
900
  try {
897
901
  flushEffects();
@@ -904,6 +908,34 @@ function flushEffectsInner() {
904
908
  runAfterFlush();
905
909
  }
906
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
+ }
907
939
  const settleHooks = [];
908
940
  let settleDepth = 0;
909
941
  /**
@@ -948,7 +980,9 @@ export function krateFlushSync() {
948
980
  }
949
981
  // flushEffects runs the after-flush hooks itself once nothing is pending.
950
982
  let guard = 0;
951
- while (pending.length > 0 && guard++ < 100) {
983
+ while (pending.length > 0) {
984
+ if (guard++ >= MAX_UPDATE_PASSES)
985
+ abortUpdateLoop();
952
986
  flushEffects();
953
987
  }
954
988
  }
@@ -967,6 +1001,12 @@ function runEffect(effect) {
967
1001
  for (const cleanup of effect.cleanups.splice(0)) {
968
1002
  cleanup();
969
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();
970
1010
  context.push(effect);
971
1011
  pushed = true;
972
1012
  // Support Solid-style `return () => {...}` cleanup functions. Many
@@ -1010,7 +1050,11 @@ export function createEffect(fn) {
1010
1050
  ownerHooks.cleanup(effect.boundaryOwner, dispose);
1011
1051
  }
1012
1052
  runEffect(effect);
1013
- if (owner && owner.root)
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)
1014
1058
  addOwnerCleanup(owner, dispose);
1015
1059
  return dispose;
1016
1060
  }
@@ -1129,29 +1173,70 @@ function flushMounts() {
1129
1173
  // when the recomputed value is unchanged, avoiding thundering re-renders.
1130
1174
  export function createMemo(fn) {
1131
1175
  const [get, set] = createSignal(undefined);
1132
- const effect = newEffectState(() => {
1133
- const next = fn();
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
+ }
1134
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();
1135
1207
  });
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.
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.
1141
1224
  const parent = currentEffect();
1142
1225
  const disposeMemo = () => {
1143
- if (!effect.disposed) {
1144
- disposeEffect(effect);
1145
- allEffects.delete(effect);
1226
+ if (!node.disposed) {
1227
+ disposeEffect(node);
1228
+ allEffects.delete(node);
1229
+ disposeEffect(checker);
1230
+ allEffects.delete(checker);
1146
1231
  }
1147
1232
  };
1148
1233
  if (parent) {
1149
1234
  parent.cleanups.push(disposeMemo);
1150
1235
  }
1151
- else if (effect.boundaryOwner && ownerHooks) {
1152
- ownerHooks.cleanup(effect.boundaryOwner, disposeMemo);
1236
+ else if (checker.boundaryOwner && ownerHooks) {
1237
+ ownerHooks.cleanup(checker.boundaryOwner, disposeMemo);
1153
1238
  }
1154
- return get;
1239
+ return read;
1155
1240
  }
1156
1241
  /**
1157
1242
  * 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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-alpha.4",
4
- "description": "Client-side runtime for Krate — signals, hydration, SPA router, resources, and context.",
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",