@torpor/view 1.2.6 → 1.3.0

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 (38) hide show
  1. package/dist/{Effect-DazmAdyC.d.mts → Effect-99HYQdzZ.d.mts} +50 -33
  2. package/dist/Effect-99HYQdzZ.d.mts.map +1 -0
  3. package/dist/{Effect-Dewn7MLc.d.cts → Effect-Bf8fz_zC.d.cts} +50 -33
  4. package/dist/Effect-Bf8fz_zC.d.cts.map +1 -0
  5. package/dist/compile.cjs +1498 -101
  6. package/dist/compile.d.cts +268 -1
  7. package/dist/compile.d.cts.map +1 -1
  8. package/dist/compile.d.mts +268 -1
  9. package/dist/compile.d.mts.map +1 -1
  10. package/dist/compile.mjs +1495 -102
  11. package/dist/compile.mjs.map +1 -1
  12. package/dist/dev.d.cts +1 -1
  13. package/dist/dev.d.mts +1 -1
  14. package/dist/{fromWebSocket-DrN-IduP.cjs → fromWebSocket-Cdoghvmh.cjs} +15 -0
  15. package/dist/{fromWebSocket-CFGoMkV2.mjs → fromWebSocket-ERhd9KZR.mjs} +11 -2
  16. package/dist/fromWebSocket-ERhd9KZR.mjs.map +1 -0
  17. package/dist/index.cjs +121 -75
  18. package/dist/index.d.cts +12 -6
  19. package/dist/index.d.cts.map +1 -1
  20. package/dist/index.d.mts +12 -6
  21. package/dist/index.d.mts.map +1 -1
  22. package/dist/index.mjs +121 -75
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/runtime.iife.js +1 -0
  25. package/dist/slotAutoClose-BbKfzpkz.cjs +177 -0
  26. package/dist/slotAutoClose-CgDkf8fM.mjs +156 -0
  27. package/dist/slotAutoClose-CgDkf8fM.mjs.map +1 -0
  28. package/dist/ssr.cjs +29 -1
  29. package/dist/ssr.d.cts +32 -1
  30. package/dist/ssr.d.cts.map +1 -1
  31. package/dist/ssr.d.mts +32 -1
  32. package/dist/ssr.d.mts.map +1 -1
  33. package/dist/ssr.mjs +25 -2
  34. package/dist/ssr.mjs.map +1 -1
  35. package/package.json +8 -5
  36. package/dist/Effect-DazmAdyC.d.mts.map +0 -1
  37. package/dist/Effect-Dewn7MLc.d.cts.map +0 -1
  38. package/dist/fromWebSocket-CFGoMkV2.mjs.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as devContext } from "./devContext-ZNsPyOgB.mjs";
2
- import { a as buildStyles, c as proxyDataSymbol, i as formatText, n as fromServer, o as buildClasses, r as fromElement, s as $unwrap, t as fromWebSocket } from "./fromWebSocket-CFGoMkV2.mjs";
2
+ import { a as buildStyles, c as proxyDataSymbol, i as formatText, l as proxyKeysSymbol, n as fromServer, o as buildClasses, r as fromElement, s as $unwrap, t as fromWebSocket } from "./fromWebSocket-ERhd9KZR.mjs";
3
3
  //#region src/render/context.ts
4
4
  const CONTEXT_SYMBOL = Symbol.for("torp.Context");
5
5
  const context = globalThis[CONTEXT_SYMBOL] ??= {
@@ -115,6 +115,23 @@ function nodeNext(node, text = false) {
115
115
  return node.nextSibling;
116
116
  }
117
117
  //#endregion
118
+ //#region src/dev/devAssert.ts
119
+ /**
120
+ * Throws when `condition` is false — but only when dev mode is enabled (the
121
+ * dev-tools build calls `setDevMode`). Used for invariant checks on the
122
+ * runtime's implicit state machines (the batch counter, the signal update
123
+ * chain, the `$async` settle latch): violations mean the machine's
124
+ * bookkeeping is corrupt, and failing loudly in dev beats silently leaking
125
+ * subscriptions or dropping settles in ways that only show up as subtle UI
126
+ * glitches later. Production builds pay one boolean read per check.
127
+ *
128
+ * @param condition The invariant to hold.
129
+ * @param message What the violation means — the thrown error's message.
130
+ */
131
+ function devAssert(condition, message) {
132
+ if (devContext.enabled && !condition) throw new Error(message);
133
+ }
134
+ //#endregion
118
135
  //#region src/watch/clearSources.ts
119
136
  /**
120
137
  * Clear unused source subscriptions after each Computed or Effect has been run.
@@ -384,6 +401,7 @@ function triggerEffects() {
384
401
  //#endregion
385
402
  //#region src/watch/batchEnd.ts
386
403
  function batchEnd() {
404
+ devAssert(context.batch > 0, "batchEnd called without a matching batchStart");
387
405
  if (context.batch > 1) {
388
406
  context.batch--;
389
407
  return;
@@ -1516,15 +1534,23 @@ function nodeRoot(parent, text = false) {
1516
1534
  /**
1517
1535
  * Gets the sibling of a node, x nodes after it.
1518
1536
  *
1519
- * When hydrating, sets the hydration node.
1537
+ * When hydrating, sets the hydration node. `textMask` marks steps that
1538
+ * should land on a text node (bit i = the i-th step, counting from
1539
+ * `node`): such a step reuses the current node when it is already text
1540
+ * (text nodes get merged in HTML) and does not skip whitespace-only text
1541
+ * nodes — matching `nodeNext(node, true)`. Steps without their bit set
1542
+ * match `nodeNext(node, false)`.
1520
1543
  *
1521
1544
  * @param node The node.
1522
1545
  * @param count The number of nodes to skip.
1546
+ * @param textMask Bitmask of text steps.
1523
1547
  */
1524
- function nodeSkip(node, count) {
1548
+ function nodeSkip(node, count, textMask = 0) {
1525
1549
  let nextNode = node;
1526
- if (context.hydrationNode !== null) for (let i = 0; i < count; i++) nextNode = nodeCheckHydrationBreak(nextNode.nextSibling);
1527
- else for (let i = 0; i < count; i++) nextNode = nextNode.nextSibling;
1550
+ if (context.hydrationNode !== null) {
1551
+ if (textMask === 0) for (let i = 0; i < count; i++) nextNode = nodeCheckHydrationBreak(nextNode.nextSibling);
1552
+ else for (let i = 0; i < count; i++) nextNode = nodeNext(nextNode, (textMask & 1 << i) !== 0);
1553
+ } else for (let i = 0; i < count; i++) nextNode = nextNode.nextSibling;
1528
1554
  return nextNode;
1529
1555
  }
1530
1556
  //#endregion
@@ -1760,47 +1786,6 @@ function formatKey(key) {
1760
1786
  }
1761
1787
  }
1762
1788
  //#endregion
1763
- //#region src/render/AwaitSuspend.ts
1764
- /**
1765
- * Thrown when content rendered speculatively inside an `@await` boundary
1766
- * dereferences (or calls) a first-load pending `$async` value.
1767
- *
1768
- * `suspendRead` normally returns `staleValue` so refresh suspends can keep
1769
- * stale content mounted. On a first load there is no stale value, so returning
1770
- * `undefined` would let user code throw a `TypeError` before the boundary can
1771
- * switch to its `with` branch. Throwing this sentinel aborts the speculative
1772
- * render instead; `runAwait` catches it and renders the fallback.
1773
- */
1774
- var AwaitSuspend = class extends Error {
1775
- constructor() {
1776
- super("Suspended async value");
1777
- this.name = "AwaitSuspend";
1778
- }
1779
- };
1780
- //#endregion
1781
- //#region src/watch/deepWrap.ts
1782
- /**
1783
- * Wraps a value with `$watch` if it is a plain object (or Date/Map/Set) that
1784
- * is not already watched and is not a promise. Returns the value unchanged
1785
- * otherwise.
1786
- *
1787
- * Used by the `get` trap (wrap values on read) and the `set` trap (wrap
1788
- * values on write), so a value stored in watched state is reactive whether
1789
- * it's been read yet or not.
1790
- */
1791
- const proxyCache = /* @__PURE__ */ new WeakMap();
1792
- function deepWrap(value) {
1793
- if (value === void 0 || value === null || typeof value !== "object") return value;
1794
- if (value[proxyDataSymbol] !== void 0) return proxyCache.get(value) ?? value;
1795
- if (value.then !== void 0) return value;
1796
- if (value.nodeType !== void 0) return value;
1797
- const cached = proxyCache.get(value);
1798
- if (cached !== void 0) return cached;
1799
- const proxy = $watch(value);
1800
- proxyCache.set(value, proxy);
1801
- return proxy;
1802
- }
1803
- //#endregion
1804
1789
  //#region src/watch/propagateSignal.ts
1805
1790
  /**
1806
1791
  * Walk the target graph from a signal source (ProxySignal or Computed),
@@ -1858,6 +1843,7 @@ function propagateSignal(proxy, key) {
1858
1843
  if (signal !== void 0 && signal.firstTarget !== null) {
1859
1844
  batchStart();
1860
1845
  try {
1846
+ devAssert(context.firstSignalToUpdate === null === (context.lastSignalToUpdate === null), "Signal update chain head/tail out of sync");
1861
1847
  if (context.lastSignalToUpdate === null) {
1862
1848
  context.firstSignalToUpdate = signal;
1863
1849
  context.lastSignalToUpdate = signal;
@@ -1872,6 +1858,48 @@ function propagateSignal(proxy, key) {
1872
1858
  }
1873
1859
  }
1874
1860
  //#endregion
1861
+ //#region src/render/AwaitSuspend.ts
1862
+ /**
1863
+ * Thrown when content rendered speculatively inside an `@await` boundary
1864
+ * dereferences (or calls) a first-load pending `$async` value.
1865
+ *
1866
+ * `suspendRead` normally returns the retained settle value so refresh suspends
1867
+ * can keep
1868
+ * stale content mounted. On a first load there is no stale value, so returning
1869
+ * `undefined` would let user code throw a `TypeError` before the boundary can
1870
+ * switch to its `with` branch. Throwing this sentinel aborts the speculative
1871
+ * render instead; `runAwait` catches it and renders the fallback.
1872
+ */
1873
+ var AwaitSuspend = class extends Error {
1874
+ constructor() {
1875
+ super("Suspended async value");
1876
+ this.name = "AwaitSuspend";
1877
+ }
1878
+ };
1879
+ //#endregion
1880
+ //#region src/watch/deepWrap.ts
1881
+ /**
1882
+ * Wraps a value with `$watch` if it is a plain object (or Date/Map/Set) that
1883
+ * is not already watched and is not a promise. Returns the value unchanged
1884
+ * otherwise.
1885
+ *
1886
+ * Used by the `get` trap (wrap values on read) and the `set` trap (wrap
1887
+ * values on write), so a value stored in watched state is reactive whether
1888
+ * it's been read yet or not.
1889
+ */
1890
+ const proxyCache = /* @__PURE__ */ new WeakMap();
1891
+ function deepWrap(value) {
1892
+ if (value === void 0 || value === null || typeof value !== "object") return value;
1893
+ if (value[proxyDataSymbol] !== void 0) return proxyCache.get(value) ?? value;
1894
+ if (value.then !== void 0) return value;
1895
+ if (value.nodeType !== void 0) return value;
1896
+ const cached = proxyCache.get(value);
1897
+ if (cached !== void 0) return cached;
1898
+ const proxy = $watch(value);
1899
+ proxyCache.set(value, proxy);
1900
+ return proxy;
1901
+ }
1902
+ //#endregion
1875
1903
  //#region src/watch/trackProxySignal.ts
1876
1904
  function trackProxySignal(data, key) {
1877
1905
  let signal = data.signals.get(key);
@@ -1894,8 +1922,8 @@ function trackProxySignal(data, key) {
1894
1922
  * nearest `@await` boundary.
1895
1923
  *
1896
1924
  * Return value (ASYNC.md → "Stale-while-revalidate"): on a refresh suspend
1897
- * (the computed has resolved before) returns the previously resolved value
1898
- * held in `staleValue`, so readers keep displaying the old content instead of
1925
+ * (the computed has settled before) returns the previously resolved value
1926
+ * held in `settle`, so readers keep displaying the old content instead of
1899
1927
  * a placeholder — an `@await` boundary thus keeps its content mounted
1900
1928
  * rather than flashing the `with` branch. On a first load (or a retry after a
1901
1929
  * rejection, both of which have no stale value), inside an `@await` boundary,
@@ -1928,9 +1956,9 @@ function suspendRead(signal) {
1928
1956
  trackSignal(signal);
1929
1957
  context.activeTarget = oldActive;
1930
1958
  }
1931
- if (!signal.hasResolved || signal.lastErrored) throw new AwaitSuspend();
1959
+ if (signal.settle === void 0 || !signal.settle.ok) throw new AwaitSuspend();
1932
1960
  }
1933
- return signal.staleValue;
1961
+ return signal.settle !== void 0 && signal.settle.ok ? signal.settle.value : void 0;
1934
1962
  }
1935
1963
  function proxyGet(target, key, receiver) {
1936
1964
  let data = target[proxyDataSymbol];
@@ -2069,6 +2097,7 @@ function arrayHandle(data, target, key) {
2069
2097
  return function(...args) {
2070
2098
  const result = target[key].apply(target, args);
2071
2099
  propagateSignal(data, "length");
2100
+ if (key !== "sort" && key !== "reverse") propagateSignal(data, proxyKeysSymbol);
2072
2101
  return result;
2073
2102
  };
2074
2103
  }
@@ -2302,8 +2331,10 @@ function proxySet(target, key, value, receiver) {
2302
2331
  let data = target[proxyDataSymbol];
2303
2332
  devContext.signalSet(data, key);
2304
2333
  if (data.shallow !== true) value = deepWrap(value);
2334
+ const isNewKey = !Object.prototype.hasOwnProperty.call(target, key);
2305
2335
  Reflect.set(target, key, value, receiver);
2306
2336
  propagateSignal(data, key);
2337
+ if (isNewKey) propagateSignal(data, proxyKeysSymbol);
2307
2338
  if (data.isArray && typeof key !== "symbol" && !isNaN(+key)) propagateSignal(data, "length");
2308
2339
  }
2309
2340
  return true;
@@ -2312,7 +2343,21 @@ function proxySet(target, key, value, receiver) {
2312
2343
  //#region src/watch/$watch.ts
2313
2344
  const sharedHandler = {
2314
2345
  get: proxyGet,
2315
- set: proxySet
2346
+ set: proxySet,
2347
+ ownKeys: (target) => {
2348
+ const data = target[proxyDataSymbol];
2349
+ if (data !== void 0) trackProxySignal(data, proxyKeysSymbol);
2350
+ return Reflect.ownKeys(target);
2351
+ },
2352
+ deleteProperty: (target, key) => {
2353
+ const deleted = Reflect.deleteProperty(target, key);
2354
+ const data = target[proxyDataSymbol];
2355
+ if (deleted && data !== void 0) {
2356
+ propagateSignal(data, key);
2357
+ propagateSignal(data, proxyKeysSymbol);
2358
+ }
2359
+ return deleted;
2360
+ }
2316
2361
  };
2317
2362
  /**
2318
2363
  * Watches an object and runs effects when its properties are changed.
@@ -2471,13 +2516,13 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2471
2516
  let chainNeedsRelink = false;
2472
2517
  while (oldStart <= oldEnd && newStart <= newEnd) if (oldItems[oldStart].key === newSpecs[newStart].key) {
2473
2518
  const item = oldItems[oldStart];
2474
- update(item, newSpecs[newStart]);
2519
+ if (item.data !== newSpecs[newStart].data) update(item, newSpecs[newStart]);
2475
2520
  newItems[newStart] = item;
2476
2521
  oldStart++;
2477
2522
  newStart++;
2478
2523
  } else if (oldItems[oldEnd].key === newSpecs[newEnd].key) {
2479
2524
  const item = oldItems[oldEnd];
2480
- update(item, newSpecs[newEnd]);
2525
+ if (item.data !== newSpecs[newEnd].data) update(item, newSpecs[newEnd]);
2481
2526
  newItems[newEnd] = item;
2482
2527
  oldEnd--;
2483
2528
  newEnd--;
@@ -2485,7 +2530,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2485
2530
  const item = oldItems[oldStart];
2486
2531
  moveRegion(parent, item, oldItems[oldEnd].endNode?.nextSibling ?? anchor);
2487
2532
  chainNeedsRelink = true;
2488
- update(item, newSpecs[newEnd]);
2533
+ if (item.data !== newSpecs[newEnd].data) update(item, newSpecs[newEnd]);
2489
2534
  newItems[newEnd] = item;
2490
2535
  oldStart++;
2491
2536
  newEnd--;
@@ -2493,7 +2538,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2493
2538
  const item = oldItems[oldEnd];
2494
2539
  moveRegion(parent, item, oldItems[oldStart].startNode);
2495
2540
  chainNeedsRelink = true;
2496
- update(item, newSpecs[newStart]);
2541
+ if (item.data !== newSpecs[newStart].data) update(item, newSpecs[newStart]);
2497
2542
  newItems[newStart] = item;
2498
2543
  oldEnd--;
2499
2544
  newStart++;
@@ -2527,7 +2572,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2527
2572
  const newIdx = newKeyToIndex.get(oldItem.key);
2528
2573
  if (newIdx !== void 0) {
2529
2574
  newIndexToOld[newIdx - newStart] = i + 1;
2530
- update(oldItem, newSpecs[newIdx]);
2575
+ if (oldItem.data !== newSpecs[newIdx].data) update(oldItem, newSpecs[newIdx]);
2531
2576
  newItems[newIdx] = oldItem;
2532
2577
  } else clearRegion(oldItem);
2533
2578
  }
@@ -2645,7 +2690,7 @@ const SERVER_VALUES_PREFIX = "t-await:";
2645
2690
  * Stale-while-revalidate (ASYNC.md): once content has been produced
2646
2691
  * (`hasContent`), a subsequent suspend during a refresh does NOT switch to
2647
2692
  * the `with` branch — the boundary keeps the stale content mounted. `$async`
2648
- * retains the previous resolved value (`Computed.staleValue`) and
2693
+ * retains the previous resolved value (`Computed.settle.value`) and
2649
2694
  * `suspendRead` returns it, so child effects keep displaying the old value
2650
2695
  * until the new promise resolves and updates them in place. The `with` branch
2651
2696
  * is shown only on the first load, before content has ever rendered
@@ -2972,10 +3017,8 @@ function $async(fn, _options) {
2972
3017
  didError: false,
2973
3018
  didSuspend: false,
2974
3019
  generation: 0,
2975
- hasResolved: false,
2976
- lastErrored: false,
3020
+ settle: void 0,
2977
3021
  suspendQuiet: false,
2978
- staleValue: void 0,
2979
3022
  rollback: null
2980
3023
  };
2981
3024
  const serverValues = context.serverValues;
@@ -2987,25 +3030,29 @@ function $async(fn, _options) {
2987
3030
  if (value !== null && value !== void 0 && typeof value.then === "function") {
2988
3031
  const gen = ++computed.generation;
2989
3032
  computed.didSuspend = true;
2990
- computed.suspendQuiet = computed.hasResolved && !computed.recalc;
3033
+ computed.suspendQuiet = computed.settle !== void 0 && !computed.recalc;
2991
3034
  value.then((v) => {
2992
3035
  if (computed.generation !== gen) return;
3036
+ devAssert(computed.didSuspend, "$async: resolved while not suspended (settle bookkeeping is broken)");
2993
3037
  computed.value = v;
2994
3038
  computed.didSuspend = false;
2995
- computed.hasResolved = true;
2996
- computed.lastErrored = false;
2997
- computed.staleValue = v;
3039
+ computed.settle = {
3040
+ ok: true,
3041
+ value: v
3042
+ };
2998
3043
  batchStart();
2999
3044
  propagateFromSignal(computed);
3000
3045
  batchEnd();
3001
3046
  }, (e) => {
3002
3047
  if (computed.generation !== gen) return;
3048
+ devAssert(computed.didSuspend, "$async: rejected while not suspended (settle bookkeeping is broken)");
3003
3049
  computed.value = e;
3004
3050
  computed.didError = true;
3005
3051
  computed.didSuspend = false;
3006
- computed.hasResolved = true;
3007
- computed.lastErrored = true;
3008
- computed.staleValue = void 0;
3052
+ computed.settle = {
3053
+ ok: false,
3054
+ error: e
3055
+ };
3009
3056
  batchStart();
3010
3057
  propagateFromSignal(computed);
3011
3058
  batchEnd();
@@ -3019,10 +3066,11 @@ function $async(fn, _options) {
3019
3066
  computed.generation++;
3020
3067
  computed.didSuspend = false;
3021
3068
  computed.didError = false;
3022
- computed.hasResolved = true;
3023
- computed.lastErrored = false;
3024
3069
  computed.value = serverValues.values[serverValues.index++];
3025
- computed.staleValue = computed.value;
3070
+ computed.settle = {
3071
+ ok: true,
3072
+ value: computed.value
3073
+ };
3026
3074
  }
3027
3075
  return computed.value;
3028
3076
  }
@@ -3098,10 +3146,8 @@ function $cache(fn) {
3098
3146
  didError: false,
3099
3147
  didSuspend: false,
3100
3148
  generation: 0,
3101
- hasResolved: false,
3102
- lastErrored: false,
3149
+ settle: void 0,
3103
3150
  suspendQuiet: false,
3104
- staleValue: void 0,
3105
3151
  rollback: null
3106
3152
  };
3107
3153
  context.registerComputed(computed);
@@ -3158,7 +3204,7 @@ function $onmount(fn) {
3158
3204
  * re-asking the same question shouldn't ping the user. First loads and
3159
3205
  * dependency-change refreshes are loud (`true`), as is the default (loud)
3160
3206
  * `$refresh(fn)`. Quiet-ness is captured per computed at suspend time via
3161
- * `suspendQuiet` (computed in `$async`'s run from `hasResolved` and `recalc`).
3207
+ * `suspendQuiet` (computed in `$async`'s run from `settle` and `recalc`).
3162
3208
  *
3163
3209
  * @param fn A function that reads the reactive values to check.
3164
3210
  */
@@ -3186,7 +3232,7 @@ function $pending(fn) {
3186
3232
  * *loud*: `$pending(fn)` returns `true` while the re-fetch is in flight and
3187
3233
  * subscribers are notified at suspend *start*, so an inline "updating…"
3188
3234
  * indicator appears immediately. Readers keep displaying the previous
3189
- * resolved value (`Computed.staleValue`) until the new promise resolves and
3235
+ * resolved value (`Computed.settle.value`) until the new promise resolves and
3190
3236
  * propagates through the reactive graph; an `@await` boundary keeps its
3191
3237
  * content mounted instead of flashing fallback.
3192
3238
  *