@torpor/view 1.2.1 → 1.2.3

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/index.mjs CHANGED
@@ -906,6 +906,7 @@ function pushRegion(region, toParent = false) {
906
906
  function fillLayoutSlot(component, slot, parent, anchor, $props, $context) {
907
907
  const region = newRegion(devContext.enabled ? "Layout slot" : void 0);
908
908
  const oldRegion = pushRegion(region, true);
909
+ region.depth = region.previousRegion === null ? 0 : region.previousRegion.depth + 1;
909
910
  component(parent, anchor, $props, $context, { _: slot });
910
911
  popRegion(oldRegion);
911
912
  return region;
@@ -1029,6 +1030,12 @@ function hydrate(parent, component, props, slots) {
1029
1030
  /**
1030
1031
  * Mounts a component into a DOM node
1031
1032
  *
1033
+ * The parent should be connected to the document: delegated events (click,
1034
+ * input, keydown, ...) are handled by a single listener per event type on
1035
+ * `document`, so events raised inside a detached container never reach their
1036
+ * handlers. (`hydrate` mounts into server-rendered markup, which is already
1037
+ * connected.)
1038
+ *
1032
1039
  * @param parent The node to mount the component into
1033
1040
  * @param component The component to mount
1034
1041
  * @param props An object containing component props
@@ -1084,6 +1091,51 @@ function newListItem(data, key, name) {
1084
1091
  };
1085
1092
  }
1086
1093
  //#endregion
1094
+ //#region src/render/branchIndex.ts
1095
+ /**
1096
+ * During hydration, the server writes the index of the branch it rendered for
1097
+ * a control statement (`@if`/`@switch`) into the block's leading branch-break
1098
+ * marker (`<!^2>`). `nodeAnchor` reads that marker while consuming the block's
1099
+ * markers and stashes the index here, keyed by the anchor node it returns —
1100
+ * the same pattern as `firstInsideNode`, since the marker is gone by the time
1101
+ * the index is needed. `runControl` picks the index up from the anchor so
1102
+ * `runControlBranch` can compare it with the branch the client picks: server
1103
+ * and client conditions can legitimately differ (browser-only state, effects
1104
+ * that no-op on the server), and a branch the server rendered but the client
1105
+ * didn't pick must be recreated rather than adopted.
1106
+ */
1107
+ const branchIndexes = /* @__PURE__ */ new WeakMap();
1108
+ function setBranchIndex(anchor, index) {
1109
+ branchIndexes.set(anchor, index);
1110
+ }
1111
+ /**
1112
+ * Takes (and clears) the server-rendered branch index stashed for the given
1113
+ * anchor node, or `undefined` if the anchor wasn't hydrated through a
1114
+ * branch-indexed hydration block.
1115
+ */
1116
+ function takeBranchIndex(anchor) {
1117
+ const index = branchIndexes.get(anchor);
1118
+ if (index !== void 0) branchIndexes.delete(anchor);
1119
+ return index;
1120
+ }
1121
+ const HYDRATION_BREAK_REGEX = /^\^(?:\d+)?$/;
1122
+ /**
1123
+ * Whether a comment node's data is a branch-break marker (`^`, or `^<index>`
1124
+ * when the server recorded the branch it rendered).
1125
+ */
1126
+ function isHydrationBreak(data) {
1127
+ return HYDRATION_BREAK_REGEX.test(data);
1128
+ }
1129
+ /**
1130
+ * The branch index encoded in a `^<index>` branch-break marker, or `undefined`
1131
+ * for a plain `^` marker (e.g. from `@for`, which has no branch index).
1132
+ */
1133
+ function hydrationBreakIndex(data) {
1134
+ if (!data.startsWith("^") || data.length === 1) return;
1135
+ const index = Number(data.slice(1));
1136
+ return Number.isNaN(index) ? void 0 : index;
1137
+ }
1138
+ //#endregion
1087
1139
  //#region src/render/isCommentNode.ts
1088
1140
  function isCommentNode(node) {
1089
1141
  return node.nodeType === 8;
@@ -1104,7 +1156,7 @@ function isTextNode(node) {
1104
1156
  * @returns The next node.
1105
1157
  */
1106
1158
  function nodeCheckHydrationBreak(node) {
1107
- if (node !== null && isCommentNode(node) && node.data === "^") {
1159
+ if (node !== null && isCommentNode(node) && isHydrationBreak(node.data)) {
1108
1160
  let comment = node;
1109
1161
  node = node.nextSibling;
1110
1162
  comment.remove();
@@ -1155,9 +1207,14 @@ function nodeAnchor(node) {
1155
1207
  if (context.hydrationNode !== null) {
1156
1208
  if (isCommentNode(node) && node.data === "[") {
1157
1209
  const startParent = node.parentNode;
1210
+ let branchIndex = void 0;
1211
+ let peek = node.nextSibling;
1212
+ while (peek !== null && isTextNode(peek) && (peek.textContent ?? "").trim() === "") peek = peek.nextSibling;
1213
+ if (peek !== null && isCommentNode(peek) && isHydrationBreak(peek.data)) branchIndex = hydrationBreakIndex(peek.data);
1158
1214
  let currentNode = nodeNext(node);
1159
1215
  node.remove();
1160
- while (currentNode !== null && isCommentNode(currentNode) && currentNode.data === "^") {
1216
+ while (currentNode !== null && isCommentNode(currentNode) && isHydrationBreak(currentNode.data)) {
1217
+ if (branchIndex === void 0) branchIndex = hydrationBreakIndex(currentNode.data);
1161
1218
  const next = currentNode.nextSibling;
1162
1219
  currentNode.remove();
1163
1220
  currentNode = next;
@@ -1178,6 +1235,7 @@ function nodeAnchor(node) {
1178
1235
  const region = context.activeRegion;
1179
1236
  if (region.startNode === null) region.startNode = firstContent ?? endNode;
1180
1237
  setFirstInsideNode(endNode, firstContent);
1238
+ if (branchIndex !== void 0) setBranchIndex(endNode, branchIndex);
1181
1239
  return endNode;
1182
1240
  }
1183
1241
  }
@@ -1231,7 +1289,7 @@ function walkHydrationRoot() {
1231
1289
  let rootNode = context.hydrationNode;
1232
1290
  while (rootNode !== null && isSkippable(rootNode)) {
1233
1291
  const next = rootNode.nextSibling;
1234
- if (isCommentNode(rootNode) && rootNode.data === "^") rootNode.remove();
1292
+ if (isCommentNode(rootNode) && isHydrationBreak(rootNode.data)) rootNode.remove();
1235
1293
  rootNode = next;
1236
1294
  context.hydrationNode = rootNode;
1237
1295
  }
@@ -1246,7 +1304,7 @@ function walkHydrationRoot() {
1246
1304
  }
1247
1305
  function isSkippable(node) {
1248
1306
  if (isTextNode(node) && (node.textContent ?? "").trim() === "") return true;
1249
- if (isCommentNode(node)) return node.data === "^" || node.data === "";
1307
+ if (isCommentNode(node)) return isHydrationBreak(node.data) || node.data === "";
1250
1308
  return false;
1251
1309
  }
1252
1310
  //#endregion
@@ -1427,6 +1485,11 @@ function widenAncestorsAtAnchor(region, anchor) {
1427
1485
  */
1428
1486
  function runControl(region, anchor, create, name) {
1429
1487
  let first = true;
1488
+ if (context.hydrationNode !== null && anchor !== null) {
1489
+ region.ssrBranchAnchor = anchor;
1490
+ const ssrBranchIndex = takeBranchIndex(anchor);
1491
+ if (ssrBranchIndex !== void 0) region.ssrBranchIndex = ssrBranchIndex;
1492
+ }
1430
1493
  const gen = region.generation = (region.generation ?? 0) + 1;
1431
1494
  $run(function runControl() {
1432
1495
  if (region.generation !== gen) return;
@@ -1436,7 +1499,10 @@ function runControl(region, anchor, create, name) {
1436
1499
  create(anchor);
1437
1500
  popRegion(oldRegion);
1438
1501
  widenAncestorsAtAnchor(region, anchor);
1439
- if (context.hydrationNode !== null && anchor !== null) context.hydrationNode = anchor;
1502
+ if (anchor !== null && (context.hydrationNode !== null || region.ssrBranchRecreated === true)) {
1503
+ region.ssrBranchRecreated = void 0;
1504
+ context.hydrationNode = anchor;
1505
+ }
1440
1506
  }, name);
1441
1507
  }
1442
1508
  //#endregion
@@ -1444,6 +1510,17 @@ function runControl(region, anchor, create, name) {
1444
1510
  function runControlBranch(region, oldIndex, index) {
1445
1511
  const recreate = region.recreate;
1446
1512
  if (recreate) region.recreate = false;
1513
+ const anchor = region.ssrBranchAnchor;
1514
+ if (anchor !== void 0) {
1515
+ region.ssrBranchAnchor = void 0;
1516
+ const ssrBranchIndex = region.ssrBranchIndex;
1517
+ region.ssrBranchIndex = void 0;
1518
+ const hydrationNode = context.hydrationNode;
1519
+ if (hydrationNode !== null && (ssrBranchIndex !== void 0 && ssrBranchIndex !== index || ssrBranchIndex === void 0 && hydrationNode === anchor)) {
1520
+ region.ssrBranchRecreated = true;
1521
+ clearServerBranch(region, hydrationNode, anchor);
1522
+ }
1523
+ }
1447
1524
  const firstRender = region.hasRenderedBranch !== true;
1448
1525
  region.hasRenderedBranch = true;
1449
1526
  if (oldIndex === index) {
@@ -1456,6 +1533,38 @@ function runControlBranch(region, oldIndex, index) {
1456
1533
  if (!firstRender && region.nextRegion !== null && region.nextRegion.depth > region.depth) clearRegion(region.nextRegion);
1457
1534
  return true;
1458
1535
  }
1536
+ /**
1537
+ * Clears a server-rendered control branch that the client didn't pick, so the
1538
+ * client's branch renders fresh DOM (hydration is disabled for the render,
1539
+ * which makes `addFragment` insert instead of adopt) and the parent resumes
1540
+ * hydrating at the control's anchor afterwards (see `runControl`).
1541
+ *
1542
+ * No client regions or effects exist for the server's markup — it was never
1543
+ * rendered through the client pipeline — so the nodes can simply be removed.
1544
+ * The nodes span from the first node inside the hydration block (the
1545
+ * hydration cursor) up to (not including) the control's anchor.
1546
+ *
1547
+ * Ancestor regions whose window starts at the removed node are reset to the
1548
+ * anchor: the same shape a mount-time block whose region owns only the anchor
1549
+ * has. `widenAncestorsAtAnchor` widens those to the fresh branch's first node
1550
+ * once the branch has rendered.
1551
+ */
1552
+ function clearServerBranch(region, removedFirst, anchor) {
1553
+ let ancestor = region.previousRegion;
1554
+ let ancestorDepth = region.depth;
1555
+ while (ancestor !== null && ancestor.depth < ancestorDepth) {
1556
+ if (ancestor.startNode === removedFirst) ancestor.startNode = anchor;
1557
+ ancestorDepth = ancestor.depth;
1558
+ ancestor = ancestor.previousRegion;
1559
+ }
1560
+ context.hydrationNode = null;
1561
+ let node = removedFirst;
1562
+ while (node !== null && node !== anchor) {
1563
+ const next = node.nextSibling;
1564
+ node.remove();
1565
+ node = next;
1566
+ }
1567
+ }
1459
1568
  //#endregion
1460
1569
  //#region src/render/checkListKeys.ts
1461
1570
  const warnedByRegion = /* @__PURE__ */ new WeakMap();
@@ -1503,6 +1612,24 @@ function formatKey(key) {
1503
1612
  }
1504
1613
  }
1505
1614
  //#endregion
1615
+ //#region src/render/AwaitSuspend.ts
1616
+ /**
1617
+ * Thrown when content rendered speculatively inside an `@await` boundary
1618
+ * dereferences (or calls) a first-load pending `$async` value.
1619
+ *
1620
+ * `suspendRead` normally returns `staleValue` so refresh suspends can keep
1621
+ * stale content mounted. On a first load there is no stale value, so returning
1622
+ * `undefined` would let user code throw a `TypeError` before the boundary can
1623
+ * switch to its `with` branch. Throwing this sentinel aborts the speculative
1624
+ * render instead; `runAwait` catches it and renders the fallback.
1625
+ */
1626
+ var AwaitSuspend = class extends Error {
1627
+ constructor() {
1628
+ super("Suspended async value");
1629
+ this.name = "AwaitSuspend";
1630
+ }
1631
+ };
1632
+ //#endregion
1506
1633
  //#region src/watch/deepWrap.ts
1507
1634
  /**
1508
1635
  * Wraps a value with `$watch` if it is a plain object (or Date/Map/Set) that
@@ -1622,10 +1749,13 @@ function trackProxySignal(data, key) {
1622
1749
  * (the computed has resolved before) returns the previously resolved value
1623
1750
  * held in `staleValue`, so readers keep displaying the old content instead of
1624
1751
  * a placeholder — an `@await` boundary thus keeps its content mounted
1625
- * rather than flashing the `with` branch. On a first load (`staleValue` is
1626
- * undefined) returns `undefined`, which the boundary replaces with its
1627
- * `with` branch. In peek mode (used by `$pending`) always returns `undefined`
1628
- * — the value isn't needed, only the subscription.
1752
+ * rather than flashing the `with` branch. On a first load (or a retry after a
1753
+ * rejection, both of which have no stale value), inside an `@await` boundary,
1754
+ * throws `AwaitSuspend` to abort the speculative content render so the
1755
+ * boundary can show its `with` branch — a direct `undefined` return would
1756
+ * make user code that dereferences the value throw a `TypeError` first. In
1757
+ * peek mode (used by `$pending`) always returns `undefined` — the value isn't
1758
+ * needed, only the subscription.
1629
1759
  */
1630
1760
  function suspendRead(signal) {
1631
1761
  trackSignal(signal);
@@ -1650,6 +1780,7 @@ function suspendRead(signal) {
1650
1780
  trackSignal(signal);
1651
1781
  context.activeTarget = oldActive;
1652
1782
  }
1783
+ if (!signal.hasResolved || signal.lastErrored) throw new AwaitSuspend();
1653
1784
  }
1654
1785
  return signal.staleValue;
1655
1786
  }
@@ -2337,7 +2468,11 @@ const SERVER_VALUES_PREFIX = "t-await:";
2337
2468
  /**
2338
2469
  * Renders an `@await` boundary. On the first run, content is rendered
2339
2470
  * speculatively; if any read inside suspends (`didSuspend`), the boundary
2340
- * discards the partial render and shows the `with` branch instead.
2471
+ * discards the partial render and shows the `with` branch instead. If the
2472
+ * content dereferences a first-load pending value before the boundary can
2473
+ * check, `suspendRead` throws `AwaitSuspend`; it is caught here so the partial
2474
+ * render is cleared and the fallback is shown instead of surfacing a
2475
+ * `TypeError`.
2341
2476
  *
2342
2477
  * Hydration of a server-resolved boundary (ASYNC.md → "Server rendering"): when the server
2343
2478
  * shipped resolved content, its values ride in a payload comment after the
@@ -2400,8 +2535,11 @@ function runAwait(region, anchor, renderContent, renderWith, name) {
2400
2535
  runControlBranch(region, index, targetIndex);
2401
2536
  context.previousRegion = region;
2402
2537
  const oldR = pushRegion(newRegion(targetIndex === 0 ? "await_content" : "await_with"), true);
2403
- fn(anchor);
2404
- popRegion(oldR);
2538
+ try {
2539
+ fn(anchor);
2540
+ } finally {
2541
+ popRegion(oldR);
2542
+ }
2405
2543
  index = targetIndex;
2406
2544
  };
2407
2545
  const attemptContent = () => {
@@ -2415,12 +2553,23 @@ function runAwait(region, anchor, renderContent, renderWith, name) {
2415
2553
  index: 0
2416
2554
  };
2417
2555
  else if (savedHydration !== null) context.hydrationNode = null;
2418
- renderBranch(0, renderContent);
2419
- context.hydrationNode = savedHydration;
2420
- context.serverValues = savedServerValues;
2421
- context.awaitBoundary = oldBoundary;
2556
+ let aborted = false;
2557
+ try {
2558
+ renderBranch(0, renderContent);
2559
+ } catch (error) {
2560
+ if (!(error instanceof AwaitSuspend)) throw error;
2561
+ aborted = true;
2562
+ index = 0;
2563
+ } finally {
2564
+ context.hydrationNode = savedHydration;
2565
+ context.serverValues = savedServerValues;
2566
+ context.awaitBoundary = oldBoundary;
2567
+ }
2422
2568
  if (boundary.suspended && renderWith !== null) renderBranch(1, renderWith);
2423
- else if (!boundary.suspended) hasContent = true;
2569
+ else if (boundary.suspended && aborted) {
2570
+ runControlBranch(region, index, 1);
2571
+ index = 1;
2572
+ } else if (!boundary.suspended) hasContent = true;
2424
2573
  };
2425
2574
  if (theEffect === null) attemptContent();
2426
2575
  else {
@@ -2578,8 +2727,23 @@ function headElement(tagName, selector) {
2578
2727
  //#endregion
2579
2728
  //#region src/render/setAttribute.ts
2580
2729
  function setAttribute(el, name, value) {
2581
- if (value === false || value === void 0 || value === null) el.removeAttribute(name);
2582
- else el.setAttribute(name, value);
2730
+ if (value === false || value === void 0 || value === null) {
2731
+ el.removeAttribute(name);
2732
+ if (name === "value") setValueProperty(el, "");
2733
+ return;
2734
+ }
2735
+ if (name === "value") {
2736
+ if (setValueProperty(el, String(value))) return;
2737
+ }
2738
+ el.setAttribute(name, value);
2739
+ }
2740
+ /** Sets the `value` property on a form control; returns false for other elements. */
2741
+ function setValueProperty(el, value) {
2742
+ if (el.tagName === "INPUT" || el.tagName === "SELECT" || el.tagName === "TEXTAREA") {
2743
+ el.value = value;
2744
+ return true;
2745
+ }
2746
+ return false;
2583
2747
  }
2584
2748
  //#endregion
2585
2749
  //#region src/render/setDynamicElement.ts