@torpor/view 1.3.0 → 1.3.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/index.mjs CHANGED
@@ -1243,22 +1243,76 @@ function isServerComponent(component) {
1243
1243
  return component.constructor?.name === "AsyncFunction";
1244
1244
  }
1245
1245
  //#endregion
1246
+ //#region src/render/regionTreeRegistry.ts
1247
+ /**
1248
+ * Region trees created by `mount`/`hydrate`, keyed by the container they
1249
+ * were created into.
1250
+ *
1251
+ * Root-level calls (the usual `mount`/`hydrate` bootstrap) register the root
1252
+ * region, so `unmount` disposes exactly what was registered. Nested calls —
1253
+ * an embed hydrating server-rendered markup inside a live page — register a
1254
+ * region tree of their own, so their teardown (`unmount(parent)`) disposes
1255
+ * only the embed and leaves the host's render context alone. Without this,
1256
+ * hydrating an embed would replace the host's `rootRegion`/`previousRegion`
1257
+ * and the embed's teardown would clear the host's root.
1258
+ */
1259
+ const trees = /* @__PURE__ */ new Map();
1260
+ function registerRegionTree(parent, region) {
1261
+ trees.set(parent, region);
1262
+ }
1263
+ function registeredRegionTree(parent) {
1264
+ return trees.get(parent);
1265
+ }
1266
+ function unregisterRegionTree(parent) {
1267
+ trees.delete(parent);
1268
+ }
1269
+ /** Entries for trees whose container lives inside `parent` (nested embeds). */
1270
+ function containedTrees(parent) {
1271
+ const found = [];
1272
+ for (const entry of trees) if (entry[0] !== parent && parent.contains(entry[0])) found.push(entry);
1273
+ return found;
1274
+ }
1275
+ //#endregion
1246
1276
  //#region src/render/hydrate.ts
1247
1277
  /**
1248
1278
  * Hydrates a component into an existing DOM tree
1279
+ *
1280
+ * When a root region is already live (an embed hydrating its server-rendered
1281
+ * markup inside a mounted page), the component hydrates against a region
1282
+ * tree of its own and the surrounding render context is restored afterwards
1283
+ * — hydration must not replace the host's `rootRegion`/`previousRegion`.
1284
+ * Tearing the embed down later (`unmount(parent)`) then disposes only the
1285
+ * embed's tree.
1286
+ *
1249
1287
  * @param parent The parent node
1250
1288
  * @param component The component to mount
1251
1289
  * @param props An object containing component props
1252
1290
  */
1253
1291
  function hydrate(parent, component, props, slots) {
1254
1292
  if (isServerComponent(component)) throw new Error("The component was compiled for SSR (it renders to an HTML string) and cannot be hydrated in the browser. Import the client build instead, e.g. with the '?client' import query");
1293
+ const saved = context.rootRegion !== null ? {
1294
+ activeRegion: context.activeRegion,
1295
+ hydrationNode: context.hydrationNode,
1296
+ previousRegion: context.previousRegion,
1297
+ rootRegion: context.rootRegion
1298
+ } : null;
1255
1299
  context.hydrationNode = parent.firstChild;
1256
- context.rootRegion = newRegion(devContext.enabled ? "Root" : void 0);
1257
- context.previousRegion = context.rootRegion;
1258
- pushRegion(context.rootRegion);
1300
+ const root = newRegion(devContext.enabled ? "Root" : void 0);
1301
+ context.rootRegion = root;
1302
+ context.previousRegion = root;
1303
+ pushRegion(root);
1259
1304
  component(parent, null, props, void 0, slots);
1260
1305
  context.hydrationNode = null;
1261
1306
  flushMountEffects();
1307
+ if (saved === null) {
1308
+ registerRegionTree(parent, root);
1309
+ return;
1310
+ }
1311
+ context.hydrationNode = saved.hydrationNode;
1312
+ context.rootRegion = saved.rootRegion;
1313
+ context.previousRegion = saved.previousRegion;
1314
+ context.activeRegion = saved.activeRegion;
1315
+ registerRegionTree(parent, root);
1262
1316
  }
1263
1317
  //#endregion
1264
1318
  //#region src/render/mount.ts
@@ -1271,7 +1325,13 @@ function hydrate(parent, component, props, slots) {
1271
1325
  * handlers. (`hydrate` mounts into server-rendered markup, which is already
1272
1326
  * connected.)
1273
1327
  *
1274
- * @param parent The node to mount the component into
1328
+ * When a root region is already live (an embed mounting into an element
1329
+ * inside a mounted page), the component mounts against a region tree of its
1330
+ * own and the surrounding render context is left untouched — so tearing the
1331
+ * embed down later (`unmount(parent)`) disposes only the embed.
1332
+ *
1333
+ * @param parent The node to mount into. Like `hydrate`, it must have no
1334
+ * child elements
1275
1335
  * @param component The component to mount
1276
1336
  * @param props An object containing component props
1277
1337
  */
@@ -1279,33 +1339,59 @@ function mount(parent, component, props, slots) {
1279
1339
  if (isServerComponent(component)) throw new Error("The component was compiled for SSR (it renders to an HTML string) and cannot be mounted in the browser. Import the client build instead, e.g. with the '?client' import query");
1280
1340
  if (parent.childElementCount > 0) throw new Error("The parent node must have no child elements");
1281
1341
  while (parent.firstChild !== null) parent.firstChild.remove();
1282
- if (context.rootRegion === null) {
1283
- context.rootRegion = newRegion(devContext.enabled ? "Root" : void 0);
1284
- context.previousRegion = context.rootRegion;
1285
- pushRegion(context.rootRegion);
1286
- }
1342
+ const saved = context.rootRegion !== null ? {
1343
+ activeRegion: context.activeRegion,
1344
+ previousRegion: context.previousRegion,
1345
+ rootRegion: context.rootRegion
1346
+ } : null;
1347
+ const root = newRegion(devContext.enabled ? "Root" : void 0);
1348
+ context.rootRegion = root;
1349
+ context.previousRegion = root;
1350
+ pushRegion(root);
1287
1351
  component(parent, null, props, void 0, slots);
1352
+ if (saved !== null) {
1353
+ context.rootRegion = saved.rootRegion;
1354
+ context.previousRegion = saved.previousRegion;
1355
+ context.activeRegion = saved.activeRegion;
1356
+ }
1357
+ registerRegionTree(parent, root);
1288
1358
  }
1289
1359
  //#endregion
1290
1360
  //#region src/render/unmount.ts
1291
1361
  /**
1292
- * Tears down the current UI: disposes the root region tree (running effect
1293
- * cleanups and detaching subscriptions), resets the render context, and
1294
- * removes any remaining child nodes from `parent`.
1362
+ * Tears down the component tree that was mounted/hydrated into `parent`:
1363
+ * disposes its region tree (running effect cleanups and detaching
1364
+ * subscriptions), removes any remaining child nodes from `parent`, and —
1365
+ * when `parent` holds the root tree — resets the render context.
1366
+ *
1367
+ * When `parent` holds a nested tree (an embed inside a live page), only the
1368
+ * embed is disposed and the host's render context is left untouched. Any
1369
+ * embeds living inside a root container are disposed along with it.
1295
1370
  *
1296
1371
  * Call this before `mount`ing a fresh component tree into the same container
1297
1372
  * (e.g. when client-side navigating to a route whose layout chain differs
1298
1373
  * from the previous one). `mount` refuses to mount into a non-empty parent
1299
- * and reuses an existing root region, so both must be cleared first.
1374
+ * and expects no leftover tree, so both must be cleared first.
1300
1375
  *
1301
1376
  * @param parent The container that the previous UI was mounted into
1302
1377
  */
1303
1378
  function unmount(parent) {
1304
- if (context.rootRegion !== null) {
1305
- clearRegion(context.rootRegion);
1306
- context.rootRegion = null;
1307
- context.previousRegion = null;
1308
- context.activeRegion = null;
1379
+ const owned = registeredRegionTree(parent);
1380
+ for (const [nestedParent, nestedTree] of containedTrees(parent)) {
1381
+ clearRegion(nestedTree);
1382
+ unregisterRegionTree(nestedParent);
1383
+ }
1384
+ if (owned !== void 0 && owned !== context.rootRegion) {
1385
+ clearRegion(owned);
1386
+ unregisterRegionTree(parent);
1387
+ } else {
1388
+ if (context.rootRegion !== null) {
1389
+ clearRegion(context.rootRegion);
1390
+ context.rootRegion = null;
1391
+ context.previousRegion = null;
1392
+ context.activeRegion = null;
1393
+ }
1394
+ if (owned !== void 0) unregisterRegionTree(parent);
1309
1395
  }
1310
1396
  while (parent.firstChild !== null) parent.firstChild.remove();
1311
1397
  }
@@ -1681,7 +1767,24 @@ function runControl(region, anchor, create, name) {
1681
1767
  }
1682
1768
  //#endregion
1683
1769
  //#region src/render/runControlBranch.ts
1684
- function runControlBranch(region, oldIndex, index) {
1770
+ /**
1771
+ * Runs one branch of a control block (`@if`/`@switch`/`@replace`, and the
1772
+ * runtime-driven `@await`/`@try` groups). Returns the region to pop after
1773
+ * the branch's content has rendered, or `null` when the branch should be
1774
+ * skipped (same index as the previous render and not forced to recreate).
1775
+ *
1776
+ * The branch region is created and pushed here — the single point of branch
1777
+ * bookkeeping, so emitters (and the runtime-driven groups) don't repeat the
1778
+ * region/push/pop dance and can't get it wrong (e.g. forgetting that the
1779
+ * clear above can release the region `context.previousRegion` points at).
1780
+ *
1781
+ * @param region The control's own region
1782
+ * @param oldIndex The branch index of the previous render (callers pass a
1783
+ * sentinel like `0` → `-1` to force a clear-and-rerender)
1784
+ * @param index The branch index of this render
1785
+ * @param name Dev-only name for the pushed branch region
1786
+ */
1787
+ function runControlBranch(region, oldIndex, index, name) {
1685
1788
  const recreate = region.recreate;
1686
1789
  if (recreate) region.recreate = false;
1687
1790
  const anchor = region.ssrBranchAnchor;
@@ -1700,12 +1803,24 @@ function runControlBranch(region, oldIndex, index) {
1700
1803
  if (oldIndex === index) {
1701
1804
  if (recreate) {
1702
1805
  if (region.nextRegion !== null && region.nextRegion.depth > region.depth) clearRegion(region.nextRegion);
1703
- return true;
1806
+ return pushBranchRegion(region, name);
1704
1807
  }
1705
- return false;
1808
+ return null;
1706
1809
  }
1707
1810
  if (!firstRender && region.nextRegion !== null && region.nextRegion.depth > region.depth) clearRegion(region.nextRegion);
1708
- return true;
1811
+ return pushBranchRegion(region, name);
1812
+ }
1813
+ /**
1814
+ * Creates and pushes the region the branch's content renders into.
1815
+ *
1816
+ * The clear above can release the region `context.previousRegion` points at
1817
+ * (the previous branch subtree's last region), so re-anchor it to the control
1818
+ * region before linking — otherwise the fresh branch would chain off a
1819
+ * released region instead of the control's.
1820
+ */
1821
+ function pushBranchRegion(region, name) {
1822
+ context.previousRegion = region;
1823
+ return pushRegion(newRegion(devContext.enabled ? name : void 0), true);
1709
1824
  }
1710
1825
  /**
1711
1826
  * Clears a server-rendered control branch that the client didn't pick, so the
@@ -2729,9 +2844,8 @@ function runAwait(region, anchor, renderContent, renderWith, name) {
2729
2844
  const oldRegion = pushRegion(region, first);
2730
2845
  first = false;
2731
2846
  const renderBranch = (targetIndex, fn) => {
2732
- runControlBranch(region, index, targetIndex);
2733
- context.previousRegion = region;
2734
- const oldR = pushRegion(newRegion(targetIndex === 0 ? "await_content" : "await_with"), true);
2847
+ const oldR = runControlBranch(region, index, targetIndex, targetIndex === 0 ? "await_content" : "await_with");
2848
+ if (oldR === null) return;
2735
2849
  try {
2736
2850
  fn(anchor);
2737
2851
  } finally {
@@ -2764,7 +2878,8 @@ function runAwait(region, anchor, renderContent, renderWith, name) {
2764
2878
  }
2765
2879
  if (boundary.suspended && renderWith !== null) renderBranch(1, renderWith);
2766
2880
  else if (boundary.suspended && aborted) {
2767
- runControlBranch(region, index, 1);
2881
+ const oldR = runControlBranch(region, index, 1, "await_with");
2882
+ if (oldR !== null) popRegion(oldR);
2768
2883
  index = 1;
2769
2884
  } else if (!boundary.suspended) hasContent = true;
2770
2885
  };
@@ -2781,7 +2896,8 @@ function runAwait(region, anchor, renderContent, renderWith, name) {
2781
2896
  else if (!hasContent) {
2782
2897
  if (renderWith !== null) renderBranch(1, renderWith);
2783
2898
  else {
2784
- runControlBranch(region, index, 1);
2899
+ const oldR = runControlBranch(region, index, 1, "await_with");
2900
+ if (oldR !== null) popRegion(oldR);
2785
2901
  index = 1;
2786
2902
  }
2787
2903
  }
@@ -2866,9 +2982,8 @@ function runTry(region, anchor, renderTry, renderCatch, name) {
2866
2982
  const oldRegion = pushRegion(region, first);
2867
2983
  first = false;
2868
2984
  const renderBranch = (targetIndex, fn) => {
2869
- runControlBranch(region, index, targetIndex);
2870
- context.previousRegion = region;
2871
- const oldR = pushRegion(newRegion(targetIndex === 0 ? "try" : "catch"), true);
2985
+ const oldR = runControlBranch(region, index, targetIndex, targetIndex === 0 ? "try" : "catch");
2986
+ if (oldR === null) return;
2872
2987
  fn(anchor);
2873
2988
  popRegion(oldR);
2874
2989
  index = targetIndex;