marko 6.4.0 → 6.4.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 (130) hide show
  1. package/cheatsheet.md +2 -2
  2. package/dist/common/constants/accessor-prefix.d.ts +1 -0
  3. package/dist/common/constants/accessor-prefix.debug.d.ts +1 -0
  4. package/dist/common/constants/reserved-id.d.ts +5 -0
  5. package/dist/common/errors.d.ts +2 -1
  6. package/dist/common/opt.d.ts +1 -0
  7. package/dist/common/types.d.ts +3 -0
  8. package/dist/debug/dom/catch.feat.js +1 -1
  9. package/dist/debug/dom/catch.feat.mjs +1 -1
  10. package/dist/debug/dom/controllable-input.feat.js +1 -1
  11. package/dist/debug/dom/controllable-input.feat.mjs +1 -1
  12. package/dist/debug/dom/controllable-open.feat.js +1 -1
  13. package/dist/debug/dom/controllable-open.feat.mjs +1 -1
  14. package/dist/debug/dom/controllable-select.feat.js +1 -1
  15. package/dist/debug/dom/controllable-select.feat.mjs +1 -1
  16. package/dist/debug/dom/controllable-textarea.feat.js +1 -1
  17. package/dist/debug/dom/controllable-textarea.feat.mjs +1 -1
  18. package/dist/debug/dom/controllable.feat.js +1 -1
  19. package/dist/debug/dom/controllable.feat.mjs +1 -1
  20. package/dist/debug/dom/dynamic-tag-script.feat.js +1 -1
  21. package/dist/debug/dom/dynamic-tag-script.feat.mjs +1 -1
  22. package/dist/debug/dom/dynamic-tag-var.feat.js +1 -1
  23. package/dist/debug/dom/dynamic-tag-var.feat.mjs +1 -1
  24. package/dist/debug/dom/lazy.feat.js +1 -1
  25. package/dist/debug/dom/lazy.feat.mjs +1 -1
  26. package/dist/debug/dom/placeholder.feat.js +1 -1
  27. package/dist/debug/dom/placeholder.feat.mjs +1 -1
  28. package/dist/debug/{dom-BRO5aArp.js → dom-BLbR0hEx.js} +62 -16
  29. package/dist/debug/{dom-Btj2QJhk.mjs → dom-Cii0IA92.mjs} +51 -17
  30. package/dist/debug/dom.js +7 -4
  31. package/dist/debug/dom.mjs +6 -5
  32. package/dist/debug/html.js +551 -292
  33. package/dist/debug/html.mjs +548 -290
  34. package/dist/dom/catch.feat.js +3 -3
  35. package/dist/dom/catch.feat.mjs +1 -1
  36. package/dist/dom/controllable-input.feat.js +1 -1
  37. package/dist/dom/controllable-input.feat.mjs +1 -1
  38. package/dist/dom/controllable-open.feat.js +1 -1
  39. package/dist/dom/controllable-open.feat.mjs +1 -1
  40. package/dist/dom/controllable-select.feat.js +1 -1
  41. package/dist/dom/controllable-select.feat.mjs +1 -1
  42. package/dist/dom/controllable-textarea.feat.js +1 -1
  43. package/dist/dom/controllable-textarea.feat.mjs +1 -1
  44. package/dist/dom/controllable.feat.js +1 -1
  45. package/dist/dom/controllable.feat.mjs +1 -1
  46. package/dist/dom/dom.d.ts +1 -0
  47. package/dist/dom/dynamic-tag-script.feat.js +2 -2
  48. package/dist/dom/dynamic-tag-script.feat.mjs +1 -1
  49. package/dist/dom/dynamic-tag-var.feat.js +1 -1
  50. package/dist/dom/dynamic-tag-var.feat.mjs +1 -1
  51. package/dist/dom/lazy.feat.js +1 -1
  52. package/dist/dom/lazy.feat.mjs +1 -1
  53. package/dist/dom/placeholder.feat.js +3 -3
  54. package/dist/dom/placeholder.feat.mjs +1 -1
  55. package/dist/dom/signals.d.ts +3 -2
  56. package/dist/{dom-BSsI9OPC.js → dom-B8UEYzOz.js} +112 -84
  57. package/dist/{dom-B-v8Gw6T.mjs → dom-BPjLe6YU.mjs} +23 -5
  58. package/dist/dom.d.ts +2 -2
  59. package/dist/dom.js +34 -33
  60. package/dist/dom.mjs +6 -5
  61. package/dist/html/assets.d.ts +12 -6
  62. package/dist/html/attrs.d.ts +3 -2
  63. package/dist/html/compat.d.ts +10 -2
  64. package/dist/html/dynamic-tag.d.ts +1 -1
  65. package/dist/html/inlined-runtimes.d.ts +1 -1
  66. package/dist/html/inlined-runtimes.debug.d.ts +1 -1
  67. package/dist/html/serializer.d.ts +2 -1
  68. package/dist/html/writer.d.ts +42 -22
  69. package/dist/html.d.ts +2 -2
  70. package/dist/html.js +400 -208
  71. package/dist/html.mjs +399 -207
  72. package/dist/translator/core/html-comment.d.ts +0 -7
  73. package/dist/translator/core/let.d.ts +0 -5
  74. package/dist/translator/core/show.d.ts +2 -5
  75. package/dist/translator/core/style.d.ts +6 -8
  76. package/dist/translator/index.d.ts +9 -0
  77. package/dist/translator/index.js +8429 -8100
  78. package/dist/translator/util/binding-has-prop.d.ts +1 -1
  79. package/dist/translator/util/binding-prop-tree.d.ts +4 -4
  80. package/dist/translator/util/bindings.d.ts +135 -0
  81. package/dist/translator/util/body-to-text-literal.d.ts +1 -2
  82. package/dist/translator/util/branch-tag.d.ts +7 -7
  83. package/dist/translator/util/constants/binding-type.d.ts +5 -6
  84. package/dist/translator/util/constants/slot-kind.d.ts +14 -0
  85. package/dist/translator/util/constants/structure-kind.d.ts +2 -1
  86. package/dist/translator/util/evaluate.d.ts +11 -10
  87. package/dist/translator/util/finalize-references.d.ts +4 -0
  88. package/dist/translator/util/for-selector.d.ts +1 -1
  89. package/dist/translator/util/generate-uid.d.ts +3 -1
  90. package/dist/translator/util/get-declared-binding-expression.d.ts +2 -2
  91. package/dist/translator/util/is-core-tag.d.ts +0 -3
  92. package/dist/translator/util/is-only-child-in-parent.d.ts +2 -7
  93. package/dist/translator/util/known-tag.d.ts +10 -8
  94. package/dist/translator/util/module-read.d.ts +21 -0
  95. package/dist/translator/util/read-replacement.d.ts +3 -0
  96. package/dist/translator/util/reasons.d.ts +32 -0
  97. package/dist/translator/util/references.d.ts +68 -166
  98. package/dist/translator/util/runtime.d.ts +2 -0
  99. package/dist/translator/util/scope-accessor.d.ts +14 -0
  100. package/dist/translator/util/scope-read.d.ts +1 -1
  101. package/dist/translator/util/sections.d.ts +52 -48
  102. package/dist/translator/util/set-tag-derived-from.d.ts +7 -0
  103. package/dist/translator/util/setup-work.d.ts +1 -2
  104. package/dist/translator/util/signals.d.ts +3 -5
  105. package/dist/translator/util/slots.d.ts +50 -0
  106. package/dist/translator/util/solve-reasons.d.ts +10 -0
  107. package/dist/translator/util/sources.d.ts +21 -0
  108. package/dist/translator/util/structure.d.ts +6 -2
  109. package/dist/translator/util/tag-facts.d.ts +21 -0
  110. package/dist/translator/util/tag-name-type.d.ts +4 -1
  111. package/dist/translator/util/translate-attrs.d.ts +2 -0
  112. package/dist/translator/util/translate-var.d.ts +3 -0
  113. package/dist/translator/util/write-guard.d.ts +12 -0
  114. package/dist/translator/util/writer.d.ts +3 -3
  115. package/dist/translator/visitors/export-declaration.d.ts +9 -0
  116. package/dist/translator/visitors/function.d.ts +5 -4
  117. package/dist/translator/visitors/import-declaration.d.ts +2 -2
  118. package/dist/translator/visitors/placeholder.d.ts +0 -3
  119. package/dist/translator/visitors/program/index.d.ts +13 -3
  120. package/dist/translator/visitors/referenced-identifier.d.ts +2 -1
  121. package/dist/translator/visitors/tag/custom-tag.d.ts +1 -1
  122. package/dist/translator/visitors/tag/dynamic-tag.d.ts +0 -3
  123. package/dist/translator/visitors/tag/index.d.ts +2 -1
  124. package/dist/translator/visitors/tag/native-tag.d.ts +26 -12
  125. package/package.json +3 -3
  126. package/dist/translator/util/insertion-context.d.ts +0 -1
  127. package/dist/translator/util/is-non-html-text.d.ts +0 -3
  128. package/dist/translator/util/serialize-guard.d.ts +0 -11
  129. package/dist/translator/util/serialize-reasons.d.ts +0 -37
  130. package/dist/translator/util/set-tag-sections-downstream.d.ts +0 -6
@@ -162,6 +162,16 @@ function assertValidAttrValue(name, value) {
162
162
  const unrenderable = describeUnrenderable(value);
163
163
  if (unrenderable) throw new Error(`The \`${name}\` attribute cannot be ${unrenderable}.`);
164
164
  }
165
+ function _assert_class_toggles(classes, toggles) {
166
+ {
167
+ const written = new Set(classes.split(" "));
168
+ for (const toggle of toggles) {
169
+ const names = new Set(toggle.split(" "));
170
+ for (const name of names) if (written.has(name)) throw new Error(`The toggled class \`${name}\` is also written by another part of the \`class\` value; a toggled class may appear only once.`);
171
+ for (const name of names) written.add(name);
172
+ }
173
+ }
174
+ }
165
175
  function assertValidTextValue(value) {
166
176
  const unrenderable = describeUnrenderable(value);
167
177
  if (unrenderable) throw new Error(`Text content cannot be ${unrenderable}.`);
@@ -218,18 +228,18 @@ function _hoist_read_error() {
218
228
  function _assert_hoist(value) {
219
229
  if (value !== void 0 && typeof value !== "function") throw new Error(`Hoisted values must be functions, received type "${typeof value}".`);
220
230
  }
221
- function assertExclusiveAttrs(attrs, onError = throwErr) {
231
+ function assertExclusiveAttrs(attrs, onError = throwErr, isSet = isTruthy) {
222
232
  if (attrs) {
223
233
  let exclusiveAttrs;
224
- if (attrs.checkedChange) (exclusiveAttrs ||= []).push("checkedChange");
234
+ if (isSet(attrs.checkedChange)) (exclusiveAttrs ||= []).push("checkedChange");
225
235
  if ("checkedValue" in attrs) {
226
236
  (exclusiveAttrs ||= []).push("checkedValue");
227
237
  if ("checked" in attrs) exclusiveAttrs.push("checked");
228
- } else if (attrs.checkedValueChange) {
238
+ } else if (isSet(attrs.checkedValueChange)) {
229
239
  (exclusiveAttrs ||= []).push("checkedValueChange");
230
240
  if ("checked" in attrs) exclusiveAttrs.push("checked");
231
241
  }
232
- if (attrs.valueChange) {
242
+ if (isSet(attrs.valueChange)) {
233
243
  (exclusiveAttrs ||= []).push("valueChange");
234
244
  if ("checked" in attrs && !exclusiveAttrs.includes("checked")) exclusiveAttrs.push("checked");
235
245
  }
@@ -245,6 +255,9 @@ function assertHandlerIsFunction(name, value) {
245
255
  function assertValidTagName(tagName) {
246
256
  if (!/^[a-z][a-z0-9._-]*$/i.test(tagName)) throw new Error(`Invalid tag name: "${tagName}". Tag names must start with a letter and contain only letters, numbers, periods, hyphens, and underscores.`);
247
257
  }
258
+ function isTruthy(value) {
259
+ return value;
260
+ }
248
261
  function throwErr(msg) {
249
262
  throw new Error(msg);
250
263
  }
@@ -291,11 +304,12 @@ function forUntil(until, from, step, cb) {
291
304
  }
292
305
  //#endregion
293
306
  //#region src/common/opt.ts
294
- function forEach(opt, cb) {
307
+ function reduce(opt, cb, acc) {
295
308
  if (opt) {
296
- if (Array.isArray(opt)) for (const item of opt) cb(item);
297
- else cb(opt);
309
+ if (Array.isArray(opt)) for (const item of opt) acc = cb(acc, item);
310
+ else acc = cb(acc, opt);
298
311
  }
312
+ return acc;
299
313
  }
300
314
  function push(opt, item) {
301
315
  return opt ? Array.isArray(opt) ? (opt.push(item), opt) : [opt, item] : item;
@@ -645,6 +659,17 @@ var Serializer = class {
645
659
  channel
646
660
  });
647
661
  }
662
+ dropCall(value, object, property) {
663
+ const { mutated } = this.#state;
664
+ for (let i = 0; i < mutated.length; i++) {
665
+ const mutation = mutated[i];
666
+ if (mutation.value === value && mutation.object === object && mutation.property === property) {
667
+ mutated.splice(i, 1);
668
+ return true;
669
+ }
670
+ }
671
+ return false;
672
+ }
648
673
  };
649
674
  function register(id, val, scope, locals) {
650
675
  REGISTRY.set(val, {
@@ -738,7 +763,7 @@ function writeAssigned(state) {
738
763
  if (writeProp(state, mutation.object, null, "")) {
739
764
  const objectRef = state.refs.get(mutation.object);
740
765
  if (objectRef && objectRef.scopeId === void 0) {
741
- if (!objectRef.id) {
766
+ if (!objectRef.id && !isAncestorChannelRef(state, objectRef)) {
742
767
  objectRef.id = nextRefAccess(state);
743
768
  state.buf[objectStartIndex] = "(" + objectRef.id + "=" + state.buf[objectStartIndex];
744
769
  state.buf.push(")");
@@ -751,7 +776,7 @@ function writeAssigned(state) {
751
776
  const valueStartIndex = state.buf.push(toAccess(toObjectKey(mutation.property)) + "(");
752
777
  if (mutation.value === void 0) {} else if (writeProp(state, mutation.value, null, "")) {
753
778
  const valueRef = typeof mutation.value === "string" ? state.strs.get(mutation.value) : state.refs.get(mutation.value);
754
- if (valueRef && !valueRef.id && valueRef.scopeId === void 0) {
779
+ if (valueRef && !valueRef.id && valueRef.scopeId === void 0 && !isAncestorChannelRef(state, valueRef)) {
755
780
  valueRef.id = mutation.valueId || nextRefAccess(state);
756
781
  state.buf[valueStartIndex] = valueRef.id + "=" + state.buf[valueStartIndex];
757
782
  }
@@ -768,7 +793,7 @@ function writeCallArg(state, val) {
768
793
  state.buf.push("$");
769
794
  } else if (writeProp(state, val, null, "")) {
770
795
  const ref = state.refs.get(val) || state.strs.get(val);
771
- if (ref && ref.id === null) assignId(state, ref);
796
+ if (ref) ensureId(state, ref);
772
797
  } else state.buf.push("void 0");
773
798
  }
774
799
  function hasChannelMutations(state) {
@@ -850,8 +875,7 @@ function writeRegistered(state, val, parent, accessor, registered) {
850
875
  state.buf.push(registered.access + "(_(" + scopeId + ")");
851
876
  for (const local of locals) {
852
877
  state.buf.push(",");
853
- if (local) writePlainObject(state, local, new Reference(ref, null, state.flushId, state.buf.length));
854
- else state.buf.push("0");
878
+ writePlainObject(state, local, newArgReference(state, ref, local));
855
879
  }
856
880
  } else state.buf.push("_(" + scopeId + "," + quoteRegisterId(registered.id));
857
881
  state.buf.push(")");
@@ -1160,6 +1184,17 @@ function writeArrayArg(state, ref, items, assignsPrefix, plainPrefix, needsId) {
1160
1184
  }
1161
1185
  state.buf.push(")");
1162
1186
  }
1187
+ function newArgReference(state, parent, val) {
1188
+ for (const key in val) {
1189
+ const member = val[key];
1190
+ if (isDedupedMember(member) && !(typeof member === "string" ? state.strs.has(member) : state.refs.has(member))) {
1191
+ const ref = new Reference(parent, null, state.flushId, null, nextRefAccess(state));
1192
+ state.buf.push(ref.id + "=");
1193
+ return ref;
1194
+ }
1195
+ }
1196
+ return new Reference(parent, null, state.flushId, state.buf.length);
1197
+ }
1163
1198
  function isDedupedMember(val) {
1164
1199
  switch (typeof val) {
1165
1200
  case "object": return val !== null && val[K_SCOPE_ID] === void 0;
@@ -1356,20 +1391,9 @@ function writeResponse(state, val, ref) {
1356
1391
  }
1357
1392
  function writeIntl(state, val, name, ref) {
1358
1393
  const { locale, ...options } = val.resolvedOptions();
1359
- let needsId = false;
1360
- for (const key in options) if (isDedupedMember(options[key])) {
1361
- needsId = true;
1362
- break;
1363
- }
1364
1394
  state.buf.push("new Intl." + name + "(" + quote(locale, 0) + ",");
1365
- let optionsRef;
1366
- if (needsId) {
1367
- optionsRef = new Reference(ref, null, state.flushId, null, nextRefAccess(state));
1368
- state.buf.push(optionsRef.id + "={");
1369
- } else {
1370
- optionsRef = new Reference(ref, null, state.flushId, state.buf.length);
1371
- state.buf.push("{");
1372
- }
1395
+ const optionsRef = newArgReference(state, ref, options);
1396
+ state.buf.push("{");
1373
1397
  writeObjectProps(state, options, optionsRef);
1374
1398
  state.buf.push("})");
1375
1399
  return true;
@@ -1393,7 +1417,7 @@ function writeReadableStream(state, val, ref) {
1393
1417
  const handle = newAsyncHandle(state, ref, iterId);
1394
1418
  const onFulfilled = ({ value, done }) => {
1395
1419
  if (done) writeAsyncCall(state, boundary, handle, "r", value, channel);
1396
- else if (!boundary.signal.aborted) {
1420
+ else if (!boundary.aborted) {
1397
1421
  reader.read().then(onFulfilled, onRejected);
1398
1422
  boundary.startAsync();
1399
1423
  writeAsyncCall(state, boundary, handle, "f", value, channel);
@@ -1440,13 +1464,10 @@ function writeGenerator(state, iter, ref) {
1440
1464
  state.buf.push("," + holder.id + "={}");
1441
1465
  writeProp(state, returnValue, holder, "v");
1442
1466
  } else if (returnValue !== void 0) {
1443
- const sepIndex = state.buf.push(",") - 1;
1467
+ state.buf.push(",");
1444
1468
  if (writeProp(state, returnValue, ref, "") && isDedupedMember(returnValue)) {
1445
1469
  const retRef = typeof returnValue === "string" ? state.strs.get(returnValue) : state.refs.get(returnValue);
1446
- if (retRef && !retRef.id && retRef.scopeId === void 0) {
1447
- retRef.id = nextRefAccess(state);
1448
- state.buf[sepIndex] = "," + retRef.id + "=";
1449
- }
1470
+ if (retRef) ensureId(state, retRef);
1450
1471
  }
1451
1472
  }
1452
1473
  state.buf.push(")");
@@ -1462,7 +1483,7 @@ function writeAsyncGenerator(state, iter, ref) {
1462
1483
  const handle = newAsyncHandle(state, ref, iterId);
1463
1484
  const onFulfilled = ({ value, done }) => {
1464
1485
  if (done) writeAsyncCall(state, boundary, handle, "r", value, channel);
1465
- else if (!boundary.signal.aborted) {
1486
+ else if (!boundary.aborted) {
1466
1487
  iter.next().then(onFulfilled, onRejected);
1467
1488
  boundary.startAsync();
1468
1489
  writeAsyncCall(state, boundary, handle, "f", value, channel);
@@ -1509,7 +1530,7 @@ function writeMaybeIterableProps(state, val, ref) {
1509
1530
  return sep;
1510
1531
  }
1511
1532
  function writeAsyncCall(state, boundary, handle, method, value, channel, valueId = null) {
1512
- if (boundary.signal.aborted) return;
1533
+ if (boundary.aborted) return;
1513
1534
  state.mutated.push({
1514
1535
  value,
1515
1536
  object: handle,
@@ -1567,6 +1588,7 @@ const accessorPrefixDescriptions = {
1567
1588
  ControlledType: "the controlled type",
1568
1589
  ControlledValue: "the controlled value",
1569
1590
  DynamicHTMLLastChild: "the dynamic html",
1591
+ DynamicHTMLValue: "the dynamic html value",
1570
1592
  EventAttributes: "the event handlers",
1571
1593
  IdFallback: "the generated id",
1572
1594
  KeyedScopes: "the keyed scopes",
@@ -1605,6 +1627,9 @@ function trackChannel(state, ref) {
1605
1627
  }
1606
1628
  return false;
1607
1629
  }
1630
+ function isAncestorChannelRef(state, ref) {
1631
+ return ref.channel?.readyId !== state.channel?.readyId;
1632
+ }
1608
1633
  function abortUnreachableChannel(state, val) {
1609
1634
  const err = new TypeError("Unable to serialize a value shared between independently lazy loaded content. Values shared this way must also be serialized by content that is not lazily loaded, or by a common parent.", { cause: val });
1610
1635
  err.stack = void 0;
@@ -1698,11 +1723,10 @@ function quote(str, startPos) {
1698
1723
  return "\"" + (lastPos === startPos ? str : result + str.slice(lastPos)) + "\"";
1699
1724
  }
1700
1725
  function writeRef(state, ref) {
1701
- if (ref.id === null && ref.scopeId === void 0 && ref.path === null && (ref.pos === null || ref.flushId !== state.flushId)) {
1726
+ if (ref.id === null && ref.scopeId === void 0 && ref.path === null && (ref.pos === null || ref.flushId !== state.flushId) && !isAncestorChannelRef(state, ref)) {
1702
1727
  ref.path = accessPath(state, ref);
1703
1728
  ref.pos = state.buf.length;
1704
1729
  ref.flushId = state.flushId;
1705
- ref.channel = state.channel;
1706
1730
  state.buf.push(ref.path);
1707
1731
  } else state.buf.push(ensureId(state, ref));
1708
1732
  }
@@ -1723,23 +1747,26 @@ function accessId(state, ref) {
1723
1747
  }
1724
1748
  function assignId(state, ref) {
1725
1749
  const { pos } = ref;
1726
- ref.id = nextRefAccess(state);
1727
1750
  if (pos !== null && ref.flushId === state.flushId) {
1751
+ ref.id = nextRefAccess(state);
1728
1752
  if (pos === 0) state.buf[0] = ref.id + "=" + state.buf[0];
1729
1753
  else state.buf[pos - 1] += ref.id + "=";
1730
1754
  return ref.id;
1731
1755
  }
1732
- ref.channel = state.channel;
1756
+ if (isAncestorChannelRef(state, ref)) return ref.path || accessPath(state, ref);
1757
+ ref.id = nextRefAccess(state);
1733
1758
  return ref.id + "=" + (ref.path || accessPath(state, ref));
1734
1759
  }
1735
1760
  function accessPath(state, ref) {
1736
1761
  let cur = ref;
1737
1762
  let accessPrevValue = "";
1738
1763
  do {
1764
+ if (cur.accessor === null) throw new Error("Unable to serialize a reused value: no access path reaches where it was first written, and it has no id.");
1739
1765
  accessPrevValue = toAccess(cur.accessor) + accessPrevValue;
1740
1766
  const parent = cur.parent;
1741
1767
  if (parent.id) {
1742
- if (trackChannel(state, parent) || !parent.parent) return parent.id + accessPrevValue;
1768
+ trackChannel(state, parent);
1769
+ return parent.id + accessPrevValue;
1743
1770
  }
1744
1771
  if (parent.flushId === state.flushId || parent.scopeId !== void 0) return accessId(state, parent) + accessPrevValue;
1745
1772
  cur = parent;
@@ -1967,9 +1994,9 @@ function _attrs(data, nodeAccessor, scopeId, tagName) {
1967
1994
  }
1968
1995
  return result;
1969
1996
  }
1970
- function _attrs_content(data, nodeAccessor, scopeId, tagName, serializeReason) {
1997
+ function _attrs_content(data, nodeAccessor, scopeId, tagName, markerGuard) {
1971
1998
  _html(`${_attrs(data, nodeAccessor, scopeId, tagName)}>`);
1972
- _attr_content(nodeAccessor, scopeId, data?.content, serializeReason);
1999
+ _attr_content(nodeAccessor, scopeId, data?.content, markerGuard);
1973
2000
  }
1974
2001
  function _attrs_partial(data, skip, nodeAccessor, scopeId, tagName) {
1975
2002
  const partial = {};
@@ -1983,9 +2010,9 @@ function _attrs_partial(data, skip, nodeAccessor, scopeId, tagName) {
1983
2010
  });
1984
2011
  return _attrs(partial, nodeAccessor, scopeId, tagName);
1985
2012
  }
1986
- function _attrs_partial_content(data, skip, nodeAccessor, scopeId, tagName, serializeReason) {
2013
+ function _attrs_partial_content(data, skip, nodeAccessor, scopeId, tagName, markerGuard) {
1987
2014
  _html(`${_attrs_partial(data, skip, nodeAccessor, scopeId, tagName)}>`);
1988
- _attr_content(nodeAccessor, scopeId, data?.content, serializeReason);
2015
+ _attr_content(nodeAccessor, scopeId, data?.content, markerGuard);
1989
2016
  }
1990
2017
  function writeControlledScope(type, scopeId, nodeAccessor, value, valueChange, serializeType) {
1991
2018
  var handlerName = [
@@ -2114,11 +2141,6 @@ const REORDER_RUNTIME_CODE = `((runtime) => {
2114
2141
 
2115
2142
  if (op == "#") {
2116
2143
  (placeholders[id] = placeholder).i++;
2117
- } else if (op == "!") {
2118
- if (runtime.l[id] && placeholders[id]) {
2119
- nextSibling = node.nextSibling;
2120
- onNextSibling = () => placeholders[id].c();
2121
- }
2122
2144
  } else if (node.tagName == "T" && (id = node.getAttribute(runtime.i))) {
2123
2145
  nextSibling = node.nextSibling;
2124
2146
  onNextSibling = () => {
@@ -2130,7 +2152,8 @@ const REORDER_RUNTIME_CODE = `((runtime) => {
2130
2152
  placeholders[id] ||
2131
2153
  (placeholderRoot = placeholders[id] =
2132
2154
  {
2133
- i: runtime.l[id] ? 1 : 2,
2155
+ // The writer streams a reorder only after its markers.
2156
+ i: 1,
2134
2157
  r: id,
2135
2158
  // Resume may still walk markers inside the dropped placeholder, so
2136
2159
  // park it in any detached parent (a bare <t> clone is the cheapest).
@@ -2238,8 +2261,17 @@ function _html(html) {
2238
2261
  function writeScript(script) {
2239
2262
  $chunk.writeScript(script);
2240
2263
  }
2241
- function _script(scopeId, registryId, serializeMarker) {
2242
- if (serializeMarker === 0 && ($chunk.serializeState.readyId || $chunk.context?.[kIsAsync])) _resume_branch(scopeId);
2264
+ function catchableBoundary() {
2265
+ const { boundary } = $chunk;
2266
+ return boundary.withinCatch ? boundary : null;
2267
+ }
2268
+ function mayDrop(written) {
2269
+ const { boundary } = $chunk;
2270
+ for (let cur = written; cur && !isWithin(boundary, cur); cur = cur.parent) if (cur.aborted || cur.count) return true;
2271
+ return false;
2272
+ }
2273
+ function _script(scopeId, registryId, markerGuard) {
2274
+ if (markerGuard === 0 && ($chunk.serializeState.readyId || $chunk.context?.[kIsAsync])) _resume_branch(scopeId);
2243
2275
  $chunk.boundary.state.needsMainRuntime = true;
2244
2276
  $chunk.writeEffect(scopeId, registryId);
2245
2277
  }
@@ -2287,8 +2319,8 @@ function _resume_branch(scopeId) {
2287
2319
  const branchId = $chunk.context?.[kBranchId];
2288
2320
  if (branchId !== void 0 && branchId !== scopeId) writeScope(scopeId, { [ClosestBranchId]: branchId });
2289
2321
  }
2290
- function _attr_content(nodeAccessor, scopeId, content, serializeReason) {
2291
- const shouldResume = serializeReason !== 0;
2322
+ function _attr_content(nodeAccessor, scopeId, content, markerGuard) {
2323
+ const shouldResume = markerGuard !== 0;
2292
2324
  const render = normalizeServerRender(content);
2293
2325
  const branchId = _peek_scope_id();
2294
2326
  if (render) {
@@ -2322,47 +2354,47 @@ function writeScopePassive(scopeId, partialScope) {
2322
2354
  passive[scopeId] = Object.assign(passive[scopeId] || {}, partialScope);
2323
2355
  return scope;
2324
2356
  }
2325
- function _show_start(display, mark) {
2357
+ function _show_start(display, markerGuard, branchExprGuard, parentEndTag) {
2326
2358
  if (display) {
2327
- if (mark) $chunk.writeHTML($chunk.boundary.state.mark("[", ""));
2359
+ if (resumesMarker(markerGuard, branchExprGuard, parentEndTag)) $chunk.writeHTML($chunk.boundary.state.mark("[", ""));
2328
2360
  } else $chunk.writeHTML("<t hidden>");
2329
2361
  }
2330
- function _show_end(scopeId, accessor, display, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2362
+ function _show_end(scopeId, accessor, display, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2331
2363
  const branchId = _scope_id();
2332
2364
  const wrap = !display;
2333
2365
  if (wrap) $chunk.writeHTML("</t>");
2334
- writeBranchEnd(scopeId, accessor, serializeStateful, serializeMarker, parentEndTag, wrap || singleNode ? 1 : void 0, " " + branchId);
2366
+ writeBranchEnd(scopeId, accessor, branchExprGuard, markerGuard, parentEndTag, wrap || singleNode ? 1 : void 0, " " + branchId);
2335
2367
  }
2336
- function _for_of(list, cb, by, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2368
+ function _for_of(list, cb, by, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2337
2369
  forBranches(by, (each) => each ? forOf(list, (item, index) => {
2338
2370
  const itemKey = forOfBy(by, item, index);
2339
2371
  each(itemKey, itemKey === index, () => cb(item, index));
2340
- }) : forOf(list, cb), scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode);
2372
+ }) : forOf(list, cb), scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode);
2341
2373
  }
2342
- function _for_in(obj, cb, by, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2374
+ function _for_in(obj, cb, by, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2343
2375
  forBranches(by, (each) => each ? forIn(obj, (key, value) => {
2344
2376
  each(forInBy(by, key, value), false, () => cb(key, value));
2345
- }) : forIn(obj, cb), scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode);
2377
+ }) : forIn(obj, cb), scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode);
2346
2378
  }
2347
- function _for_to(to, from, step, cb, by, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2379
+ function _for_to(to, from, step, cb, by, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2348
2380
  forBranches(by, (each) => {
2349
2381
  let index = 0;
2350
2382
  return each ? forTo(to, from, step, (value) => {
2351
2383
  const itemKey = forStepBy(by, value);
2352
2384
  each(itemKey, itemKey === index++, () => cb(value));
2353
2385
  }) : forTo(to, from, step, cb);
2354
- }, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode);
2386
+ }, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode);
2355
2387
  }
2356
- function _for_until(to, from, step, cb, by, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2388
+ function _for_until(to, from, step, cb, by, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2357
2389
  forBranches(by, (each) => {
2358
2390
  let index = 0;
2359
2391
  return each ? forUntil(to, from, step, (value) => {
2360
2392
  const itemKey = forStepBy(by, value);
2361
2393
  each(itemKey, itemKey === index++, () => cb(value));
2362
2394
  }) : forUntil(to, from, step, cb);
2363
- }, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode);
2395
+ }, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode);
2364
2396
  }
2365
- function forBranches(by, iterate, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2397
+ function forBranches(by, iterate, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2366
2398
  if (by) {
2367
2399
  const run = iterate;
2368
2400
  const seenKeys = /* @__PURE__ */ new Set();
@@ -2372,14 +2404,14 @@ function forBranches(by, iterate, scopeId, accessor, serializeBranch, serializeM
2372
2404
  else render();
2373
2405
  });
2374
2406
  }
2375
- if (serializeBranch === 0) {
2407
+ if (branchGuard === 0) {
2376
2408
  iterate(0);
2377
- writeBranchEnd(scopeId, accessor, serializeStateful, serializeMarker, parentEndTag, singleNode, "");
2409
+ writeBranchEnd(scopeId, accessor, branchExprGuard, markerGuard, parentEndTag, singleNode, "");
2378
2410
  return;
2379
2411
  }
2380
2412
  const { state } = $chunk.boundary;
2381
- const resumeKeys = serializeMarker !== 0;
2382
- const resumeMarker = resumeKeys && (!parentEndTag || serializeStateful !== 0);
2413
+ const resumeKeys = markerGuard !== 0;
2414
+ const resumeMarker = resumesMarker(markerGuard, branchExprGuard, parentEndTag);
2383
2415
  let flushBranchIds = "";
2384
2416
  let loopScopes;
2385
2417
  iterate((itemKey, sameAsIndex, render) => {
@@ -2398,11 +2430,11 @@ function forBranches(by, iterate, scopeId, accessor, serializeBranch, serializeM
2398
2430
  });
2399
2431
  });
2400
2432
  if (loopScopes) writeScope(scopeId, { [BranchScopes + accessor]: loopScopes });
2401
- writeBranchEnd(scopeId, accessor, serializeStateful, serializeMarker, parentEndTag, singleNode, singleNode ? flushBranchIds : flushBranchIds ? " " + flushBranchIds : "");
2433
+ writeBranchEnd(scopeId, accessor, branchExprGuard, markerGuard, parentEndTag, singleNode, singleNode ? flushBranchIds : flushBranchIds ? " " + flushBranchIds : "");
2402
2434
  }
2403
- function _if(cb, scopeId, accessor, serializeBranch, serializeMarker, serializeStateful, parentEndTag, singleNode) {
2404
- const resumeBranch = serializeBranch !== 0;
2405
- const resumeMarker = serializeMarker !== 0 && (!parentEndTag || serializeStateful !== 0);
2435
+ function _if(cb, scopeId, accessor, branchGuard, markerGuard, branchExprGuard, parentEndTag, singleNode) {
2436
+ const resumeBranch = branchGuard !== 0;
2437
+ const resumeMarker = resumesMarker(markerGuard, branchExprGuard, parentEndTag);
2406
2438
  const branchId = _peek_scope_id();
2407
2439
  const chunk = $chunk;
2408
2440
  const beforeBranch = resumeMarker && resumeBranch && !singleNode ? deferBranchStart(chunk) : void 0;
@@ -2413,7 +2445,7 @@ function _if(cb, scopeId, accessor, serializeBranch, serializeMarker, serializeS
2413
2445
  [ConditionalRenderer + accessor]: branchIndex || void 0,
2414
2446
  [BranchScopes + accessor]: resumeMarker ? void 0 : writeScope(branchId, {})
2415
2447
  });
2416
- writeBranchEnd(scopeId, accessor, serializeStateful, serializeMarker, parentEndTag, singleNode, shouldWriteBranch ? " " + branchId : "");
2448
+ writeBranchEnd(scopeId, accessor, branchExprGuard, markerGuard, parentEndTag, singleNode, shouldWriteBranch ? " " + branchId : "");
2417
2449
  }
2418
2450
  function deferBranchStart(chunk) {
2419
2451
  const beforeBranch = chunk.html;
@@ -2423,15 +2455,17 @@ function deferBranchStart(chunk) {
2423
2455
  function applyBranchStart(chunk, beforeBranch, rendered) {
2424
2456
  chunk.html = beforeBranch + (rendered ? chunk.boundary.state.mark("[", "") : "") + chunk.html;
2425
2457
  }
2426
- function writeBranchEnd(scopeId, accessor, serializeStateful, serializeMarker, parentEndTag, singleNode, branchIds) {
2458
+ function resumesMarker(markerGuard, branchExprGuard, parentEndTag) {
2459
+ return markerGuard !== 0 && (!parentEndTag || branchExprGuard !== 0);
2460
+ }
2461
+ function writeBranchEnd(scopeId, accessor, branchExprGuard, markerGuard, parentEndTag, singleNode, branchIds) {
2427
2462
  const endTag = parentEndTag || "";
2428
- if (serializeMarker !== 0) {
2429
- if (!parentEndTag || serializeStateful !== 0) {
2430
- const { state } = $chunk.boundary;
2431
- const mark = singleNode ? state.mark(parentEndTag ? "}" : "|", scopeId + " " + accessor + (branchIds || "")) : state.mark(parentEndTag ? ")" : "]", scopeId + " " + accessor + (branchIds || ""));
2432
- $chunk.writeHTML(mark + endTag);
2433
- } else $chunk.writeHTML(endTag + _el_resume(scopeId, accessor));
2434
- } else $chunk.writeHTML(endTag);
2463
+ if (resumesMarker(markerGuard, branchExprGuard, parentEndTag)) {
2464
+ const { state } = $chunk.boundary;
2465
+ const mark = singleNode ? state.mark(parentEndTag ? "}" : "|", scopeId + " " + accessor + (branchIds || "")) : state.mark(parentEndTag ? ")" : "]", scopeId + " " + accessor + (branchIds || ""));
2466
+ $chunk.writeHTML(mark + endTag);
2467
+ } else if (markerGuard !== 0) $chunk.writeHTML(endTag + _el_resume(scopeId, accessor));
2468
+ else $chunk.writeHTML(endTag);
2435
2469
  }
2436
2470
  let writeScope = (scopeId, partialScope) => {
2437
2471
  const { state } = $chunk.boundary;
@@ -2463,12 +2497,14 @@ function scopeWithId(state, scopeId) {
2463
2497
  if (!scope) scopes.set(scopeId, scope = { [K_SCOPE_ID]: scopeId });
2464
2498
  return scope;
2465
2499
  }
2466
- function _subscribe(subscribers, scope, resumeId, serializeMarker) {
2500
+ function _subscribe(subscribers, scope, resumeId, markerGuard) {
2467
2501
  if (subscribers) {
2468
- const { serializer } = $chunk.boundary.state;
2469
- if (!$chunk.serializeState.readyId && !serializer.written(subscribers)) subscribers.add(scope);
2470
- else if (resumeId) _script(scope[K_SCOPE_ID], resumeId, serializeMarker);
2471
- else serializer.writeCall(scope, subscribers, "add", $chunk.serializeState);
2502
+ const { boundary, serializeState } = $chunk;
2503
+ const { serializer } = boundary.state;
2504
+ if (!serializeState.readyId && !serializer.written(subscribers)) subscribers.add(scope);
2505
+ else if (resumeId) _script(scope[K_SCOPE_ID], resumeId, markerGuard);
2506
+ else serializer.writeCall(scope, subscribers, "add", serializeState);
2507
+ if (boundary.withinCatch) (boundary.subscribed ||= []).push(subscribers, scope, serializeState);
2472
2508
  }
2473
2509
  return scope;
2474
2510
  }
@@ -2476,19 +2512,19 @@ const CLIENT_ALL = 715827883;
2476
2512
  function maskGroup(mask, group) {
2477
2513
  return mask === 715827883 ? 1 : typeof mask === "number" ? group < 15 ? mask >>> 1 + 2 * group & 3 : 0 : mask[group] ?? 0;
2478
2514
  }
2479
- function _set_serialize_reason(reason) {
2480
- $chunk.boundary.state.serializeReason = reason;
2515
+ function _set_scope_reason(reason) {
2516
+ $chunk.boundary.state.scopeReason = reason;
2481
2517
  }
2482
2518
  function _scope_reason() {
2483
- const reason = $chunk.boundary.state.serializeReason;
2484
- $chunk.boundary.state.serializeReason = void 0;
2519
+ const reason = $chunk.boundary.state.scopeReason;
2520
+ $chunk.boundary.state.scopeReason = void 0;
2485
2521
  return reason;
2486
2522
  }
2487
- function _serialize_if(condition, key) {
2523
+ function _write_if(condition, key) {
2488
2524
  return condition && maskGroup(condition, key) ? 1 : void 0;
2489
2525
  }
2490
- function _serialize_guard(condition, key) {
2491
- return _serialize_if(condition, key) || 0;
2526
+ function _write_guard(condition, key) {
2527
+ return _write_if(condition, key) || 0;
2492
2528
  }
2493
2529
  function writeWaitReady(readyId, renderer, input) {
2494
2530
  const chunk = $chunk;
@@ -2501,6 +2537,7 @@ function writeWaitReady(readyId, renderer, input) {
2501
2537
  flushScopes: false
2502
2538
  });
2503
2539
  const bodyEnd = body.render(renderer, input);
2540
+ if (boundary.aborted) throw boundary.reason;
2504
2541
  if (body === bodyEnd) {
2505
2542
  chunk.writeHTML(body.html);
2506
2543
  body.deferOwnReady();
@@ -2510,8 +2547,8 @@ function writeWaitReady(readyId, renderer, input) {
2510
2547
  chunk.next = body;
2511
2548
  }
2512
2549
  }
2513
- function _await(scopeId, accessor, promise, content, serializeMarker) {
2514
- const resumeMarker = serializeMarker !== 0;
2550
+ function _await(scopeId, accessor, promise, content, markerGuard) {
2551
+ const resumeMarker = markerGuard !== 0;
2515
2552
  if (!isPromise(promise)) {
2516
2553
  if (resumeMarker) {
2517
2554
  const branchId = _peek_scope_id();
@@ -2531,7 +2568,8 @@ function _await(scopeId, accessor, promise, content, serializeMarker) {
2531
2568
  promise.then((value) => {
2532
2569
  if (chunk.async) {
2533
2570
  chunk.async = false;
2534
- if (!boundary.signal.aborted) {
2571
+ if (!boundary.aborted) {
2572
+ const { state } = boundary;
2535
2573
  chunk.render(() => {
2536
2574
  if (resumeMarker) {
2537
2575
  const branchId = _peek_scope_id();
@@ -2547,6 +2585,7 @@ function _await(scopeId, accessor, promise, content, serializeMarker) {
2547
2585
  async.end = $chunk;
2548
2586
  }
2549
2587
  });
2588
+ if (!chunk.async && boundary.pendingReorders?.delete(chunk)) state.reorder(chunk);
2550
2589
  boundary.endAsync();
2551
2590
  }
2552
2591
  }
@@ -2593,12 +2632,13 @@ function tryBoundary(content, catchContent, branchId, renderers) {
2593
2632
  const chunk = $chunk;
2594
2633
  const { boundary } = chunk;
2595
2634
  const { state } = boundary;
2596
- const catchBoundary = new Boundary(state, boundary.signal, boundary);
2635
+ const catchBoundary = new Boundary(state, void 0, boundary);
2636
+ if (catchContent) catchBoundary.withinCatch = true;
2597
2637
  const body = chunk.fork(catchBoundary, null);
2598
2638
  const bodyEnd = body.render(() => withBranchId(branchId, content));
2599
- if (catchBoundary.signal.aborted) {
2600
- if (catchContent) catchContent(catchBoundary.signal.reason);
2601
- else boundary.abort(catchBoundary.signal.reason);
2639
+ if (catchBoundary.aborted) {
2640
+ if (!catchContent) throw catchBoundary.reason;
2641
+ catchContent(catchBoundary.reason);
2602
2642
  return true;
2603
2643
  }
2604
2644
  if (body === bodyEnd) {
@@ -2610,49 +2650,69 @@ function tryBoundary(content, catchContent, branchId, renderers) {
2610
2650
  chunk.next = body;
2611
2651
  boundary.startAsync();
2612
2652
  const reorderId = catchContent ? state.nextReorderId() : "";
2613
- const endMarker = reorderId && state.mark("!", reorderId);
2614
2653
  if (reorderId) {
2615
- chunk.writeHTML(state.mark("!^", reorderId));
2616
- bodyEnd.writeHTML(endMarker);
2654
+ chunk.catchRange = {
2655
+ id: reorderId,
2656
+ end: bodyEnd
2657
+ };
2617
2658
  captureContext(chunk);
2618
2659
  }
2619
2660
  catchBoundary.onNext = () => {
2620
- if (boundary.signal.aborted) return;
2621
- if (catchBoundary.signal.aborted) {
2661
+ if (boundary.aborted) return;
2662
+ if (catchBoundary.aborted) {
2622
2663
  if (!reorderId) {
2623
- boundary.abort(catchBoundary.signal.reason);
2664
+ boundary.abort(catchBoundary.reason);
2624
2665
  return;
2625
2666
  }
2626
- if (!bodyEnd.consumed) {
2627
- let cur = body;
2628
- let writeMarker = true;
2629
- do {
2630
- const next = cur.next;
2631
- if (cur.boundary !== catchBoundary) cur.boundary.abort(catchBoundary.signal.reason);
2632
- if (writeMarker && !cur.consumed) {
2633
- writeMarker = false;
2634
- cur.async = false;
2635
- cur.next = bodyNext;
2636
- cur.needsWalk = true;
2637
- cur.html = endMarker;
2638
- cur.scripts = cur.effects = cur.lastEffect = "";
2639
- cur.placeholder = cur.reorderId = cur.deferredReady = null;
2640
- }
2641
- cur = next;
2642
- } while (cur !== bodyNext);
2643
- }
2667
+ const streamed = !chunk.catchRange;
2644
2668
  const catchChunk = chunk.fork(boundary, null);
2645
- catchChunk.reorderId = reorderId;
2646
- catchChunk.render(catchContent, catchBoundary.signal.reason);
2647
- state.reorder(catchChunk);
2669
+ chunk.catchRange = null;
2670
+ if (!renderersAtSettle) catchChunk.render(clearTryRenderers, branchId);
2671
+ const inOrder = catchChunk === catchChunk.render(catchContent, catchBoundary.reason);
2672
+ if (boundary.aborted) return;
2673
+ if (bodyEnd.consumed || !inOrder) catchChunk.reorderStart = state.mark("*", branchId + "");
2674
+ if (bodyEnd.consumed) {
2675
+ catchChunk.reorderId = reorderId;
2676
+ state.reorder(catchChunk);
2677
+ } else {
2678
+ let reorder = null;
2679
+ if (!inOrder) reorder = catchChunk;
2680
+ else if (streamed) reorder = chunk.fork(boundary, null);
2681
+ if (reorder) reorder.reorderId = reorderId;
2682
+ const endMarker = state.mark("!", reorderId);
2683
+ let cut = body;
2684
+ while (cut.consumed) cut = cut.next;
2685
+ cut.async = false;
2686
+ cut.next = inOrder ? catchChunk : bodyNext;
2687
+ cut.html = streamed ? endMarker : inOrder ? "" : state.mark("!^", reorderId) + endMarker;
2688
+ cut.scripts = cut.effects = cut.lastEffect = "";
2689
+ cut.placeholder = cut.reorderId = cut.catchRange = null;
2690
+ cut.deferredReorder = reorder;
2691
+ if (inOrder) catchChunk.next = bodyNext;
2692
+ }
2648
2693
  boundary.endAsync();
2649
2694
  } else if (!catchBoundary.count) {
2650
2695
  if (renderersAtSettle && catchBoundary.resumeWrites) bodyEnd.render(renderers);
2696
+ chunk.catchRange = null;
2651
2697
  boundary.endAsync();
2652
2698
  } else boundary.onNext();
2653
2699
  };
2654
2700
  return renderersAtSettle;
2655
2701
  }
2702
+ function unsubscribe(subscribed, serializer) {
2703
+ for (let i = 0; i < subscribed.length; i += 3) {
2704
+ const subscribers = subscribed[i];
2705
+ const scope = subscribed[i + 1];
2706
+ if (!serializer.written(subscribers)) subscribers.delete(scope);
2707
+ else if (!serializer.dropCall(scope, subscribers, "add")) serializer.writeCall(scope, subscribers, "delete", subscribed[i + 2]);
2708
+ }
2709
+ }
2710
+ function clearTryRenderers(branchId) {
2711
+ writeScope(branchId, {
2712
+ [CatchContent]: 0,
2713
+ [PlaceholderContent]: _scope_with_id(branchId)["#PlaceholderContent"] && 0
2714
+ });
2715
+ }
2656
2716
  function writeTryRenderers(branchId, catchContent, placeholderContent) {
2657
2717
  writeScope(branchId, {
2658
2718
  [CatchContent]: catchContent,
@@ -2692,7 +2752,7 @@ var State = class {
2692
2752
  flushScopes = false;
2693
2753
  writeScopes = {};
2694
2754
  readyIds = null;
2695
- serializeReason;
2755
+ scopeReason;
2696
2756
  $global;
2697
2757
  constructor($global) {
2698
2758
  this.$global = $global;
@@ -2753,38 +2813,57 @@ var State = class {
2753
2813
  return "<!--" + this.commentPrefix + code + str + "-->";
2754
2814
  }
2755
2815
  };
2756
- var Boundary = class extends AbortController {
2816
+ var Boundary = class {
2757
2817
  onNext = NOOP$2;
2758
2818
  count = 0;
2759
2819
  resumeWrites = 0;
2760
2820
  state;
2761
2821
  parent;
2822
+ aborted = false;
2823
+ reason;
2824
+ subscribed;
2825
+ pendingReorders;
2826
+ children;
2827
+ withinCatch = false;
2762
2828
  constructor(state, signal, parent) {
2763
- super();
2764
2829
  this.state = state;
2765
2830
  this.parent = parent;
2766
- this.signal.addEventListener("abort", () => {
2767
- this.count = 0;
2768
- this.state = new State(this.state.$global);
2769
- this.onNext();
2770
- });
2771
- if (signal) {
2831
+ if (parent) {
2832
+ this.withinCatch = parent.withinCatch;
2833
+ if (parent.aborted) this.abort(parent.reason);
2834
+ else (parent.children ||= []).push(this);
2835
+ } else if (signal) {
2772
2836
  if (signal.aborted) this.abort(signal.reason);
2773
2837
  else signal.addEventListener("abort", this);
2774
2838
  }
2775
2839
  }
2840
+ abort(reason) {
2841
+ if (this.aborted) return;
2842
+ const { state } = this;
2843
+ this.aborted = true;
2844
+ this.reason = reason;
2845
+ if (this.subscribed) unsubscribe(this.subscribed, state.serializer);
2846
+ if (this.pendingReorders) {
2847
+ for (const reorder of this.pendingReorders) state.reorder(reorder);
2848
+ this.pendingReorders = void 0;
2849
+ }
2850
+ this.count = 0;
2851
+ this.state = new State(state.$global);
2852
+ if (this.children) for (const child of this.children) child.abort(reason);
2853
+ this.onNext();
2854
+ }
2776
2855
  handleEvent(event) {
2777
2856
  this.abort(event.target.reason);
2778
2857
  }
2779
2858
  flush() {
2780
- if (!this.signal.aborted) flushSerializer(this, this.state);
2781
- return this.count ? 1 : this.signal.aborted ? 2 : 0;
2859
+ if (!this.aborted) flushSerializer(this, this.state);
2860
+ return this.count ? 1 : this.aborted ? 2 : 0;
2782
2861
  }
2783
2862
  startAsync() {
2784
- if (!this.signal.aborted) this.count++;
2863
+ if (!this.aborted) this.count++;
2785
2864
  }
2786
2865
  endAsync() {
2787
- if (!this.signal.aborted) {
2866
+ if (!this.aborted) {
2788
2867
  if (!this.count) throw new Error("A boundary ended more async work than it started.");
2789
2868
  this.count--;
2790
2869
  this.onNext();
@@ -2798,9 +2877,12 @@ var Chunk = class Chunk {
2798
2877
  lastEffect = "";
2799
2878
  async = false;
2800
2879
  consumed = false;
2801
- needsWalk = false;
2802
2880
  reorderId = null;
2881
+ reorderStart = "";
2803
2882
  deferredReady = null;
2883
+ heldEffects = null;
2884
+ deferredReorder = null;
2885
+ catchRange = null;
2804
2886
  placeholder = null;
2805
2887
  boundary;
2806
2888
  next;
@@ -2820,7 +2902,7 @@ var Chunk = class Chunk {
2820
2902
  }
2821
2903
  writeEffect(scopeId, registryId) {
2822
2904
  countResumeWrite(this);
2823
- if (this.lastEffect === registryId) this.effects += " " + scopeId;
2905
+ if (this.lastEffect === registryId) this.effects = concatEffects(this.effects, scopeId + "");
2824
2906
  else {
2825
2907
  this.lastEffect = registryId;
2826
2908
  this.effects = concatEffects(this.effects, registryId + " " + scopeId);
@@ -2831,12 +2913,16 @@ var Chunk = class Chunk {
2831
2913
  }
2832
2914
  append(chunk) {
2833
2915
  this.html += chunk.html;
2834
- this.needsWalk ||= chunk.needsWalk;
2835
2916
  this.effects = concatEffects(this.effects, chunk.effects);
2836
2917
  this.scripts = concatScripts(this.scripts, chunk.scripts);
2837
2918
  this.lastEffect = chunk.lastEffect || this.lastEffect;
2838
2919
  this.deferredReady = concat(this.deferredReady, chunk.takeDeferredReady());
2839
2920
  }
2921
+ takeHeldEffects() {
2922
+ const { heldEffects } = this;
2923
+ this.heldEffects = null;
2924
+ return heldEffects;
2925
+ }
2840
2926
  takeDeferredReady() {
2841
2927
  const { deferredReady } = this;
2842
2928
  this.deferredReady = null;
@@ -2851,64 +2937,123 @@ var Chunk = class Chunk {
2851
2937
  this.deferredReady = concat(deferred, this.deferredReady);
2852
2938
  }
2853
2939
  }
2854
- flushPlaceholder() {
2855
- const { placeholder } = this;
2856
- if (placeholder) {
2857
- this.placeholder = null;
2858
- const body = placeholder.body.consume();
2859
- if (body.async) {
2860
- const { state } = this.boundary;
2861
- const { branchId, scopeId, placeholderBranchId } = placeholder;
2862
- const reorderId = body.reorderId = branchId ? branchId + "" : state.nextReorderId();
2863
- this.writeHTML(state.mark("!^", reorderId));
2864
- const { effects } = this;
2865
- const beforeBranch = deferBranchStart(this);
2866
- if (this.render(() => withBranchId(placeholderBranchId, placeholder.render)) !== this) this.boundary.abort(/* @__PURE__ */ new Error("An @placeholder cannot contain async content."));
2867
- if (this.boundary.signal.aborted) return;
2868
- const stateful = this.effects !== effects;
2869
- applyBranchStart(this, beforeBranch, stateful);
2870
- if (stateful) {
2871
- this.render(() => writeScope(branchId, { [PlaceholderBranch]: scopeWithId(state, placeholderBranchId) }));
2872
- this.writeHTML(state.mark("]", scopeId + " " + (PlaceholderBranch + branchId) + " " + placeholderBranchId));
2873
- body.writeEffect(branchId, PLACEHOLDER_DISMISS_REGISTER_ID);
2940
+ renderPlaceholders(boundary) {
2941
+ const { state } = boundary;
2942
+ restart: for (;;) {
2943
+ let reorders = null;
2944
+ for (let cur = this; cur.next && !cur.async; cur = cur.next) {
2945
+ if (cur.renderPlaceholder(state)) {
2946
+ if (boundary.aborted) return;
2947
+ continue restart;
2874
2948
  }
2875
- this.writeHTML(state.mark("!", reorderId));
2876
- state.reorder(body);
2877
- } else {
2878
- body.next = this.next;
2879
- this.next = body;
2949
+ if (cur.deferredReorder) (reorders ||= []).push(cur.deferredReorder);
2950
+ }
2951
+ if (state.writeReorders) {
2952
+ for (const reorder of state.writeReorders) if ((reorders = renderReorderPlaceholders(reorder, reorders)) === false) {
2953
+ if (boundary.aborted) return;
2954
+ continue restart;
2955
+ }
2956
+ }
2957
+ for (let i = 0; reorders && i < reorders.length; i++) if ((reorders = renderReorderPlaceholders(reorders[i], reorders)) === false) {
2958
+ if (boundary.aborted) return;
2959
+ continue restart;
2880
2960
  }
2961
+ return;
2962
+ }
2963
+ }
2964
+ renderPlaceholder(state) {
2965
+ const { placeholder } = this;
2966
+ if (!placeholder) return false;
2967
+ this.placeholder = null;
2968
+ if (this.boundary.aborted) return false;
2969
+ const { body } = placeholder;
2970
+ let end = body;
2971
+ while (end.next && !end.async) end = end.next;
2972
+ if (!end.async) {
2973
+ end.next = this.next;
2974
+ this.next = body;
2975
+ return false;
2976
+ }
2977
+ const { branchId, scopeId, placeholderBranchId } = placeholder;
2978
+ const reorderId = body.reorderId = branchId + "";
2979
+ this.writeHTML(state.mark("!^", reorderId));
2980
+ const { effects } = this;
2981
+ const beforeBranch = deferBranchStart(this);
2982
+ if (this.render(() => withBranchId(placeholderBranchId, placeholder.render)) !== this) this.boundary.abort(/* @__PURE__ */ new Error("An @placeholder cannot contain async content."));
2983
+ if (this.boundary.aborted) return true;
2984
+ const stateful = this.effects !== effects;
2985
+ applyBranchStart(this, beforeBranch, stateful);
2986
+ if (stateful) {
2987
+ this.render(() => writeScope(branchId, { [PlaceholderBranch]: scopeWithId(state, placeholderBranchId) }));
2988
+ this.writeHTML(state.mark("]", scopeId + " " + (PlaceholderBranch + branchId) + " " + placeholderBranchId));
2989
+ dismissChunk(end, body).writeEffect(branchId, PLACEHOLDER_DISMISS_REGISTER_ID);
2990
+ }
2991
+ this.writeHTML(state.mark("!", reorderId));
2992
+ this.deferredReorder = body;
2993
+ return false;
2994
+ }
2995
+ markCatchRange() {
2996
+ const { catchRange } = this;
2997
+ if (catchRange) {
2998
+ const { state } = this.boundary;
2999
+ this.catchRange = null;
3000
+ this.writeHTML(state.mark("!^", catchRange.id));
3001
+ catchRange.end.writeHTML(state.mark("!", catchRange.id));
3002
+ }
3003
+ }
3004
+ queueDeferredReorder() {
3005
+ const { deferredReorder } = this;
3006
+ if (deferredReorder) {
3007
+ this.deferredReorder = null;
3008
+ deferredReorder.boundary.state.reorder(deferredReorder);
2881
3009
  }
2882
3010
  }
2883
- consume() {
3011
+ consume(boundary) {
3012
+ this.renderPlaceholders(boundary);
2884
3013
  let cur = this;
2885
3014
  let html = "";
2886
3015
  let effects = "";
2887
3016
  let scripts = "";
2888
- let lastEffect = "";
2889
- let needsWalk = false;
3017
+ let effectsEnd;
3018
+ let heldEffects = this.heldEffects;
2890
3019
  let deferredReady;
3020
+ this.heldEffects = null;
3021
+ if (this.serializeState.readyId && this.effects) followHeldEffects(this);
2891
3022
  while (cur.next && !cur.async) {
2892
- cur.flushPlaceholder();
2893
- needsWalk ||= cur.needsWalk;
3023
+ cur.markCatchRange();
3024
+ cur.queueDeferredReorder();
2894
3025
  html += cur.html;
2895
3026
  if (cur.serializeState.readyId) deferredReady = push(deferredReady, cur);
2896
3027
  else {
2897
- effects = concatEffects(effects, cur.effects);
3028
+ if (cur.effects) {
3029
+ if (effectsEnd && effectsEnd.boundary !== cur.boundary) {
3030
+ heldEffects = holdEffectsOn(heldEffects, effectsEnd, effects);
3031
+ effects = "";
3032
+ }
3033
+ effectsEnd = cur;
3034
+ effects = concatEffects(effects, cur.effects);
3035
+ }
2898
3036
  scripts = concatScripts(scripts, cur.scripts);
2899
- lastEffect = cur.lastEffect || lastEffect;
2900
3037
  }
2901
3038
  deferredReady = concat(deferredReady, cur.takeDeferredReady());
2902
3039
  cur.consumed = true;
2903
3040
  cur = cur.next;
2904
3041
  }
2905
3042
  cur.deferOwnReady();
3043
+ if (effects) {
3044
+ if (cur.serializeState.readyId) heldEffects = holdEffectsOn(heldEffects, effectsEnd, effects);
3045
+ else if (effectsEnd.boundary !== cur.boundary) {
3046
+ heldEffects = holdEffectsOn(heldEffects, effectsEnd, effects);
3047
+ if (isWithin(cur.boundary, effectsEnd.boundary)) cur.lastEffect ||= effectsEnd.lastEffect;
3048
+ } else {
3049
+ cur.effects = concatEffects(effects, cur.effects);
3050
+ cur.lastEffect ||= effectsEnd.lastEffect;
3051
+ }
3052
+ }
3053
+ cur.heldEffects = heldEffects;
2906
3054
  cur.deferredReady = concat(deferredReady, cur.deferredReady);
2907
- cur.needsWalk ||= needsWalk;
2908
3055
  cur.html = html + cur.html;
2909
- cur.effects = concatEffects(effects, cur.effects);
2910
3056
  cur.scripts = concatScripts(scripts, cur.scripts);
2911
- cur.lastEffect ||= lastEffect;
2912
3057
  return cur;
2913
3058
  }
2914
3059
  render(content, val) {
@@ -2928,10 +3073,12 @@ var Chunk = class Chunk {
2928
3073
  const { serializeState } = this;
2929
3074
  const { readyId } = serializeState;
2930
3075
  let scripts = "";
2931
- forEach(this.takeDeferredReady(), (chunk) => {
3076
+ const held = this.takeDeferredReady();
3077
+ if (held) for (const chunk of Array.isArray(held) ? held : [held]) {
3078
+ if (chunk.boundary.aborted) continue;
2932
3079
  scripts = concatScripts(scripts, chunk.flushReadyScripts(boundary, reservations, holdEffects));
2933
3080
  if (chunk.effects || chunk.deferredReady) this.deferredReady = push(this.deferredReady, chunk);
2934
- });
3081
+ }
2935
3082
  if (readyId && !this.async) {
2936
3083
  const { state } = boundary;
2937
3084
  flushSerializer(boundary, serializeState);
@@ -2959,16 +3106,18 @@ var Chunk = class Chunk {
2959
3106
  const { $global, runtimePrefix } = state;
2960
3107
  let needsWalk = state.walkOnNextFlush;
2961
3108
  if (needsWalk) state.walkOnNextFlush = false;
3109
+ flushSerializer(boundary, state);
2962
3110
  let readyResumeScripts = this.flushReadyScripts(boundary, void 0, this.async);
2963
- for (let channel; !boundary.signal.aborted && (channel = state.serializer.pendingReadyChannel());) {
3111
+ for (let channel; !boundary.aborted && (channel = state.serializer.pendingReadyChannel());) {
2964
3112
  const resumes = state.serializer.stringifyScopes([], boundary, channel);
2965
3113
  const deps = state.serializer.takeChannelDeps();
2966
3114
  state.needsMainRuntime = true;
2967
3115
  readyResumeScripts = concatScripts(readyResumeScripts, state.writeReady(channel.readyId, concatSequence(depsMarker(deps), resumes)));
2968
3116
  }
2969
3117
  if (readyResumeScripts) needsWalk = true;
2970
- const effects = this.async ? "" : this.effects;
3118
+ const effects = this.async ? "" : joinHeldEffects(this.takeHeldEffects(), this.effects);
2971
3119
  let { html, scripts } = this;
3120
+ if (html && state.hasReorderRuntime) html = state.mark("*", "") + html;
2972
3121
  if (state.needsMainRuntime && !state.hasMainRuntime) {
2973
3122
  state.hasMainRuntime = true;
2974
3123
  scripts = concatScripts(scripts, WALKER_RUNTIME_CODE + "(\"" + $global.runtimeId + "\")(\"" + $global.renderId + "\")");
@@ -2981,64 +3130,60 @@ var Chunk = class Chunk {
2981
3130
  let reordered = "";
2982
3131
  let needsResumeArray = false;
2983
3132
  if (state.writeReorders) {
2984
- let carried = null;
2985
3133
  for (const reorderedChunk of state.writeReorders) {
2986
- if (reorderedChunk.async && reorderedChunk.consumed) {
2987
- let aborted = reorderedChunk.boundary;
2988
- while (aborted && !aborted.signal.aborted) aborted = aborted.parent;
2989
- if (!aborted) {
2990
- (carried ||= []).push(reorderedChunk);
2991
- continue;
2992
- }
2993
- reorderedChunk.async = false;
2994
- }
2995
3134
  needsWalk = true;
2996
3135
  if (!state.hasReorderRuntime) {
2997
3136
  state.hasReorderRuntime = true;
2998
3137
  scripts = concatScripts(scripts, REORDER_RUNTIME_CODE + "(" + runtimePrefix + ")");
2999
3138
  }
3000
- const { reorderId } = reorderedChunk;
3139
+ const { reorderId, reorderStart } = reorderedChunk;
3001
3140
  const readyReservations = [];
3002
3141
  let reorderHTML = "";
3003
3142
  let reorderEffects = "";
3143
+ let effectsBoundary = reorderedChunk.boundary;
3004
3144
  let reorderScripts = "";
3005
- let cur = reorderedChunk;
3145
+ let cur = reorderedChunk.boundary.aborted ? null : reorderedChunk;
3006
3146
  reorderedChunk.reorderId = null;
3007
- for (;;) {
3008
- cur.flushPlaceholder();
3147
+ while (cur) {
3148
+ cur.markCatchRange();
3149
+ cur.queueDeferredReorder();
3009
3150
  cur.deferOwnReady();
3010
3151
  const { next } = cur;
3011
3152
  const readyResumeScripts = cur.flushReadyScripts(boundary, readyReservations, this.async);
3012
3153
  this.deferredReady = concat(this.deferredReady, cur.takeDeferredReady());
3013
3154
  cur.consumed = true;
3014
3155
  reorderHTML += cur.html;
3156
+ if (this.async && cur.effects && cur.boundary !== effectsBoundary) {
3157
+ if (reorderEffects) {
3158
+ holdReorderEffects(this, effectsBoundary, reorderEffects);
3159
+ reorderEffects = "";
3160
+ }
3161
+ effectsBoundary = cur.boundary;
3162
+ }
3015
3163
  reorderEffects = concatEffects(reorderEffects, cur.effects);
3016
3164
  reorderScripts = concatScripts(reorderScripts, concatScripts(readyResumeScripts, cur.scripts));
3017
3165
  if (cur.async) {
3018
3166
  reorderHTML += state.mark("#", cur.reorderId = state.nextReorderId());
3019
- state.reorder(cur);
3167
+ cur.reorderStart = reorderStart;
3168
+ (cur.boundary.pendingReorders ||= /* @__PURE__ */ new Set()).add(cur);
3020
3169
  cur.html = cur.effects = cur.scripts = cur.lastEffect = "";
3021
3170
  cur.next = null;
3022
3171
  }
3023
- if (next) cur = next;
3024
- else break;
3172
+ cur = next;
3025
3173
  }
3026
3174
  if (reorderEffects) {
3027
- if (this.async) {
3028
- this.effects = concatEffects(this.effects, reorderEffects);
3029
- this.lastEffect = "";
3030
- } else {
3175
+ if (this.async) holdReorderEffects(this, effectsBoundary, reorderEffects);
3176
+ else {
3031
3177
  needsResumeArray = true;
3032
3178
  reorderScripts = concatScripts(reorderScripts, "_.push(\"" + reorderEffects + "\")");
3033
3179
  }
3034
3180
  }
3035
3181
  for (const reservation of readyReservations) reordered = concatScripts(reordered, reservation);
3036
3182
  reordered = concatScripts(reordered, reorderScripts && runtimePrefix + ".j" + toAccess(reorderId) + "=_=>{" + reorderScripts + "}");
3037
- html += "<t hidden " + state.commentPrefix + "=" + reorderId + ">" + reorderHTML + "</t>";
3183
+ html += "<t hidden " + state.commentPrefix + "=" + reorderId + ">" + reorderStart + reorderHTML + "</t>";
3038
3184
  }
3039
- state.writeReorders = carried;
3185
+ state.writeReorders = null;
3040
3186
  }
3041
- flushSerializer(boundary, state);
3042
3187
  if (state.resumes || needsResumeArray && !state.hasWrittenResume) scripts = concatScripts(scripts, state.resumeScript(state.resumes));
3043
3188
  scripts = concatScripts(scripts, reordered);
3044
3189
  if (needsWalk) scripts = concatScripts(scripts, state.walkScript());
@@ -3050,16 +3195,54 @@ var Chunk = class Chunk {
3050
3195
  }
3051
3196
  flushHTML(boundary) {
3052
3197
  const { state } = boundary;
3053
- if (this.needsWalk) {
3054
- this.needsWalk = false;
3055
- state.walkOnNextFlush = true;
3056
- }
3057
3198
  this.flushScript(boundary);
3058
3199
  const { html, scripts } = this;
3059
3200
  this.html = this.scripts = "";
3060
3201
  return state.flushChunk(html, scripts, boundary.count);
3061
3202
  }
3062
3203
  };
3204
+ function renderReorderPlaceholders(reorder, reorders) {
3205
+ if (!reorder.boundary.aborted) for (let cur = reorder; cur; cur = cur.next) {
3206
+ if (cur.renderPlaceholder(reorder.boundary.state)) return false;
3207
+ if (cur.deferredReorder) (reorders ||= []).push(cur.deferredReorder);
3208
+ }
3209
+ return reorders;
3210
+ }
3211
+ function dismissChunk(end, body) {
3212
+ if (end.boundary !== body.boundary) while (end.next) end = end.next;
3213
+ return end;
3214
+ }
3215
+ function followHeldEffects(chunk) {
3216
+ const held = reduce(chunk.deferredReady, lastHeldOf, chunk);
3217
+ if (held !== chunk) {
3218
+ held.effects = concatEffects(held.effects, chunk.effects);
3219
+ chunk.effects = "";
3220
+ }
3221
+ }
3222
+ function lastHeldOf(held, cur) {
3223
+ return cur.serializeState === held.serializeState && cur.boundary === held.boundary ? cur : held;
3224
+ }
3225
+ function holdEffectsOn(heldEffects, chunk, effects) {
3226
+ chunk.effects = effects;
3227
+ return push(heldEffects, chunk);
3228
+ }
3229
+ function isWithin(boundary, outer) {
3230
+ while (boundary && boundary !== outer) boundary = boundary.parent;
3231
+ return !!boundary;
3232
+ }
3233
+ function holdReorderEffects(head, boundary, effects) {
3234
+ if (!head.serializeState.readyId) {
3235
+ if (head.effects) head.heldEffects = holdEffectsOn(head.heldEffects, head.fork(head.boundary, null), head.effects);
3236
+ head.effects = head.lastEffect = "";
3237
+ }
3238
+ head.heldEffects = holdEffectsOn(head.heldEffects, head.fork(boundary, null), effects);
3239
+ }
3240
+ function joinHeldEffects(heldEffects, effects) {
3241
+ return concatEffects(reduce(heldEffects, joinUncaught, ""), effects);
3242
+ }
3243
+ function joinUncaught(joined, held) {
3244
+ return held.boundary.aborted ? joined : concatEffects(joined, held.effects);
3245
+ }
3063
3246
  function flushSerializer(boundary, serializeState) {
3064
3247
  const { state } = boundary;
3065
3248
  const { serializer } = state;
@@ -3221,8 +3404,8 @@ function _escape_comment(val) {
3221
3404
  //#endregion
3222
3405
  //#region src/html/dynamic-tag.ts
3223
3406
  const voidElementsReg = /^(?:area|b(?:ase|r)|col|embed|hr|i(?:mg|nput)|link|meta|param|source|track|wbr)$/;
3224
- let _dynamic_tag = (scopeId, accessor, tag, inputOrArgs, content, inputIsArgs, serializeReason) => {
3225
- const shouldResume = serializeReason !== 0;
3407
+ let _dynamic_tag = (scopeId, accessor, tag, inputOrArgs, content, inputIsArgs, markerGuard) => {
3408
+ const shouldResume = markerGuard !== 0;
3226
3409
  const renderer = normalizeDynamicRenderer(tag);
3227
3410
  const state = getState();
3228
3411
  const branchId = _peek_scope_id();
@@ -3242,8 +3425,8 @@ let _dynamic_tag = (scopeId, accessor, tag, inputOrArgs, content, inputIsArgs, s
3242
3425
  _html(_attrs_textarea_value(branchId, `#${renderer.toLowerCase()}/0`, input));
3243
3426
  } else {
3244
3427
  if (renderContent && typeof renderContent !== "function") throw new Error(`Body content is not supported for the \`<${renderer}>\` tag.`);
3245
- if (renderer === "select" && ("value" in input || "valueChange" in input)) _attr_select_value(branchId, `#${renderer.toLowerCase()}/0`, input.value, input.valueChange, renderContent ? () => _dynamic_tag(branchId, `#${renderer.toLowerCase()}/0`, renderContent, void 0, 0, void 0, serializeReason) : void 0, 1);
3246
- else if (renderContent) _dynamic_tag(branchId, `#${renderer.toLowerCase()}/0`, renderContent, void 0, 0, void 0, serializeReason);
3428
+ if (renderer === "select" && ("value" in input || "valueChange" in input)) _attr_select_value(branchId, `#${renderer.toLowerCase()}/0`, input.value, input.valueChange, renderContent ? () => _dynamic_tag(branchId, `#${renderer.toLowerCase()}/0`, renderContent, void 0, 0, void 0, markerGuard) : void 0, 1);
3429
+ else if (renderContent) _dynamic_tag(branchId, `#${renderer.toLowerCase()}/0`, renderContent, void 0, 0, void 0, markerGuard);
3247
3430
  }
3248
3431
  _html(`</${renderer}>`);
3249
3432
  } else if (content) throw new Error(`Body content is not supported for the \`<${renderer}>\` tag.`);
@@ -3262,13 +3445,13 @@ let _dynamic_tag = (scopeId, accessor, tag, inputOrArgs, content, inputIsArgs, s
3262
3445
  const beforeBranch = shouldResume ? deferBranchStart(chunk) : void 0;
3263
3446
  const render = () => {
3264
3447
  if (renderer) try {
3265
- _set_serialize_reason(shouldResume && inputOrArgs !== void 0 ? CLIENT_ALL : 0);
3448
+ _set_scope_reason(shouldResume && inputOrArgs !== void 0 ? CLIENT_ALL : 0);
3266
3449
  return inputIsArgs ? renderer(...inputOrArgs) : renderer(content ? {
3267
3450
  ...inputOrArgs,
3268
3451
  content
3269
3452
  } : inputOrArgs);
3270
3453
  } finally {
3271
- _set_serialize_reason(void 0);
3454
+ _set_scope_reason(void 0);
3272
3455
  }
3273
3456
  else if (content) return content();
3274
3457
  };
@@ -3484,13 +3667,13 @@ var ServerRendered = class {
3484
3667
  switch (!boundary.count && boundary.flush()) {
3485
3668
  case 2:
3486
3669
  settle(boundary);
3487
- reject(boundary.signal.reason);
3670
+ reject(boundary.reason);
3488
3671
  break;
3489
3672
  case 0:
3490
- head = head.consume();
3491
- if (boundary.signal.aborted) break;
3673
+ head = head.consume(boundary);
3674
+ if (boundary.aborted) break;
3492
3675
  html += head.flushHTML(boundary);
3493
- if (!(boundary.count || boundary.signal.aborted)) {
3676
+ if (!(boundary.count || boundary.aborted)) {
3494
3677
  settle(boundary);
3495
3678
  resolve(html);
3496
3679
  }
@@ -3521,14 +3704,14 @@ var ServerRendered = class {
3521
3704
  if (status === 2) {
3522
3705
  if (!tick) offTick(onNext);
3523
3706
  settle(boundary);
3524
- onAbort(boundary.signal.reason);
3707
+ onAbort(boundary.reason);
3525
3708
  return;
3526
3709
  }
3527
3710
  if (write || status === 0) {
3528
- head = head.consume();
3529
- if (boundary.signal.aborted) continue;
3711
+ head = head.consume(boundary);
3712
+ if (boundary.aborted) continue;
3530
3713
  const html = head.flushHTML(boundary);
3531
- if (boundary.signal.aborted) continue;
3714
+ if (boundary.aborted) continue;
3532
3715
  if (html) onWrite(html);
3533
3716
  if (!boundary.count) {
3534
3717
  if (!tick) offTick(onNext);
@@ -3552,10 +3735,10 @@ var ServerRendered = class {
3552
3735
  this.#head = null;
3553
3736
  if (!head) throw new Error(CONSUMED_RESULT_MESSAGE);
3554
3737
  const { boundary } = head;
3555
- const html = boundary.flush() === 0 ? head.consume().flushHTML(boundary) : "";
3738
+ const html = boundary.flush() === 0 ? head.consume(boundary).flushHTML(boundary) : "";
3556
3739
  if (boundary.count) boundary.abort(/* @__PURE__ */ new Error("Cannot consume asynchronous render with 'toString'"));
3557
3740
  settle(boundary);
3558
- if (boundary.signal.aborted) throw boundary.signal.reason;
3741
+ if (boundary.aborted) throw boundary.reason;
3559
3742
  return html;
3560
3743
  }
3561
3744
  };
@@ -3567,19 +3750,18 @@ function NOOP$1() {}
3567
3750
  //#endregion
3568
3751
  //#region src/html/assets.ts
3569
3752
  const kAssets = Symbol();
3570
- const kBlockIndex = Symbol();
3571
- const kDeferIndex = Symbol();
3572
- let assetFlush;
3573
- function withLoadAssets(renderer, assetId, triggers) {
3753
+ const kHead = Symbol();
3754
+ function withLoadAssets(renderer, flush, assetId, triggers) {
3574
3755
  return Object.assign((input) => {
3575
- const g = $global();
3576
- addAsset(g, assetId, triggers);
3577
- _html(flush(g, ""));
3756
+ writeLoadAsset(assetId, flush, triggers);
3578
3757
  return writeWaitReady(assetId, renderer, input);
3579
3758
  }, renderer);
3580
3759
  }
3760
+ function writeLoadAsset(assetId, flush, triggers) {
3761
+ const g = $global();
3762
+ writeAsset(g, addAsset(g, assetId, flush, triggers));
3763
+ }
3581
3764
  function withPageAssets(template, runtime, assetId, runtimeId) {
3582
- assetFlush = runtime;
3583
3765
  return Object.assign((input) => {
3584
3766
  const g = $global();
3585
3767
  if (runtimeId) {
@@ -3588,57 +3770,78 @@ function withPageAssets(template, runtime, assetId, runtimeId) {
3588
3770
  g.runtimeId = runtimeId;
3589
3771
  } else if (g.runtimeId !== runtimeId) console.error(`A page entry compiled with runtimeId "${runtimeId}" is nested in a render using runtimeId "${g.runtimeId}", so its content cannot resume.`);
3590
3772
  }
3591
- addAsset(g, assetId);
3773
+ const asset = addAsset(g, assetId, runtime);
3592
3774
  if (g.__flush__) {
3593
- _html(flush(g, ""));
3775
+ writeAsset(g, asset);
3594
3776
  return writeWaitReady(assetId, template, input);
3595
3777
  }
3596
- g.__flush__ = flush;
3778
+ g.__flush__ = flushPage;
3597
3779
  return template(input);
3598
3780
  }, template);
3599
3781
  }
3600
3782
  function _flush_head() {
3601
3783
  const g = $global();
3602
- return g[kAssets] ? flush(g, "") : "";
3784
+ if (!g[kAssets]) return "";
3785
+ g[kHead] = true;
3786
+ return takeAssets(g, catchableBoundary());
3603
3787
  }
3604
- function flush(g, html) {
3605
- let result = "";
3606
- const assets = g[kAssets];
3607
- const { length } = assets;
3608
- let bi = g[kBlockIndex];
3609
- let di = g[kDeferIndex];
3610
- for (; bi < length; bi++) result += assetFlush(g, "block", assets[bi].id);
3611
- for (; di < length; di++) {
3612
- const { id, triggers } = assets[di];
3613
- const deferHTML = assetFlush(g, "defer", id);
3614
- if (triggers) {
3615
- if (deferHTML) writeTriggerScript(id, deferHTML, triggers);
3616
- } else result += deferHTML;
3617
- }
3618
- g[kBlockIndex] = bi;
3619
- g[kDeferIndex] = di;
3620
- return result + html;
3621
- }
3622
- function addAsset(g, id, triggers) {
3623
- const assets = g[kAssets];
3624
- if (!assets) {
3625
- g[kAssets] = [{
3626
- id,
3627
- triggers
3628
- }];
3629
- g[kBlockIndex] = g[kDeferIndex] = 0;
3630
- } else if (!assets.find((a) => a.id === id)) assets.push({
3788
+ function flushPage(g, html) {
3789
+ return takeAssets(g, null) + html;
3790
+ }
3791
+ function takeAssets(g, at) {
3792
+ let block = "";
3793
+ let defer = "";
3794
+ for (const asset of g[kAssets]) if (asset.htmlAt === void 0) {
3795
+ asset.htmlAt = at;
3796
+ block += blockHTML(g, asset);
3797
+ if (!asset.triggers) defer += deferHTML(g, asset);
3798
+ }
3799
+ return block + defer;
3800
+ }
3801
+ function writeAsset(g, asset) {
3802
+ if (needsWrite(asset.htmlAt)) {
3803
+ asset.htmlAt = void 0;
3804
+ if (g[kHead] || g.__flush__ !== flushPage) {
3805
+ const block = blockHTML(g, asset);
3806
+ _html(block + (asset.triggers ? "" : deferHTML(g, asset)));
3807
+ if (block && isInResumedBranch()) writeScript(`document.head.insertAdjacentHTML("beforeend",${_escape_script(JSON.stringify(block))})`);
3808
+ asset.htmlAt = catchableBoundary();
3809
+ }
3810
+ }
3811
+ if (asset.triggers && needsWrite(asset.scriptAt)) {
3812
+ const defer = deferHTML(g, asset);
3813
+ if (defer) writeScript(asset.script ||= triggerScript(asset.id, defer, asset.triggers));
3814
+ asset.scriptAt = catchableBoundary();
3815
+ }
3816
+ }
3817
+ function needsWrite(at) {
3818
+ return at === void 0 || at !== null && mayDrop(at);
3819
+ }
3820
+ function blockHTML(g, asset) {
3821
+ return asset.block ??= asset.flush(g, "block", asset.id);
3822
+ }
3823
+ function deferHTML(g, asset) {
3824
+ return asset.defer ??= asset.flush(g, "defer", asset.id);
3825
+ }
3826
+ function addAsset(g, id, flush, triggers) {
3827
+ const assets = g[kAssets] ||= [];
3828
+ let asset = assets.find((a) => a.id === id);
3829
+ if (!asset) assets.push(asset = {
3631
3830
  id,
3632
- triggers
3831
+ flush,
3832
+ triggers,
3833
+ block: void 0,
3834
+ defer: void 0,
3835
+ script: void 0,
3836
+ htmlAt: void 0,
3837
+ scriptAt: void 0
3633
3838
  });
3634
- else {
3635
- const existing = assets.find((a) => a.id === id);
3636
- if (JSON.stringify(existing.triggers) !== JSON.stringify(triggers)) console.error(`The lazy asset "${id}" is imported with different \`load\` triggers; an asset must use one consistent trigger.`);
3637
- }
3839
+ else if (JSON.stringify(asset.triggers) !== JSON.stringify(triggers)) console.error(`The lazy asset "${id}" is imported with different \`load\` triggers; an asset must use one consistent trigger.`);
3840
+ return asset;
3638
3841
  }
3639
- function writeTriggerScript(id, html, triggers) {
3842
+ function triggerScript(id, html, triggers) {
3640
3843
  const htmlStr = _escape_script(JSON.stringify(html));
3641
- const insert = `(d=new Range().createContextualFragment(h),d.querySelectorAll("script").forEach(s=>s.onerror=()=>console.error(${_escape_script(JSON.stringify(`The lazy module for "${id}" failed to load; its server-rendered content cannot become interactive.`))})),p.after(d))`;
3844
+ const insert = `(d=new Range().createContextualFragment(h),d.querySelectorAll("script").forEach(s=>s.onerror=()=>console.error(${_escape_script(JSON.stringify(`The lazy module for "${id}" failed to load; its server-rendered content cannot become interactive.`))})),document.head.append(d))`;
3642
3845
  const exprs = triggers.map((trigger) => {
3643
3846
  const options = trigger.options && toObjectExpression(trigger.options);
3644
3847
  switch (trigger.type) {
@@ -3648,7 +3851,7 @@ function writeTriggerScript(id, html, triggers) {
3648
3851
  default: return `(e=>e?.addEventListener("${trigger.type.slice(3)}",l,{once:1}))(${querySelectorOrLoad(trigger.selector)})`;
3649
3852
  }
3650
3853
  });
3651
- writeScript(`((p,h,d,l=$=>{d||${insert}})=>${exprs.length > 1 ? `{${exprs.join(";")}}` : exprs[0]})(document.currentScript,${htmlStr})`);
3854
+ return `((h,d,l=$=>{d||${insert}})=>${exprs.length > 1 ? `{${exprs.join(";")}}` : exprs[0]})(${htmlStr})`;
3652
3855
  }
3653
3856
  function querySelectorOrLoad(selector) {
3654
3857
  return `document.querySelector(${JSON.stringify(selector)})||${`(console.warn(${JSON.stringify(`A lazy load trigger could not find an element matching "${selector}". The module was loaded immediately.`)}),l())`}`;
@@ -3672,6 +3875,7 @@ const K_TAGS_API_STATE = Symbol();
3672
3875
  const COMPAT_REGISTRY = /* @__PURE__ */ new WeakMap();
3673
3876
  const compat = {
3674
3877
  $global,
3878
+ writeLoadAsset,
3675
3879
  fork: _await,
3676
3880
  write: _html,
3677
3881
  writeScript,
@@ -3695,7 +3899,10 @@ const compat = {
3695
3899
  onFlush(fn) {
3696
3900
  const { flushHTML } = Chunk.prototype;
3697
3901
  Chunk.prototype.flushHTML = function(boundary) {
3902
+ const { state } = boundary;
3903
+ const reorders = swapReorders(state, null);
3698
3904
  fn(this);
3905
+ swapReorders(state, reorders);
3699
3906
  return flushHTML.call(this, boundary);
3700
3907
  };
3701
3908
  },
@@ -3721,21 +3928,52 @@ const compat = {
3721
3928
  return compatRegistered;
3722
3929
  };
3723
3930
  },
3931
+ joinChunks(chunks) {
3932
+ let joined;
3933
+ for (const chunk of chunks) {
3934
+ if (chunk.boundary.aborted) continue;
3935
+ if (joined) joined.append(chunk);
3936
+ else joined = chunk;
3937
+ }
3938
+ return joined;
3939
+ },
3940
+ appendChunks(chunks) {
3941
+ const chunk = getChunk();
3942
+ for (const part of chunks) {
3943
+ chunk.append(part);
3944
+ if (part.effects) chunk.lastEffect = part.lastEffect;
3945
+ }
3946
+ },
3947
+ deferReorders(reorders) {
3948
+ const chunk = getChunk();
3949
+ for (let i = reorders.length; i--;) {
3950
+ const carrier = chunk.fork(chunk.boundary, chunk.next);
3951
+ carrier.deferredReorder = reorders[i];
3952
+ chunk.next = carrier;
3953
+ }
3954
+ },
3724
3955
  createChunk($global) {
3725
3956
  const state = this.ensureState($global);
3726
3957
  return new Chunk(new Boundary(state), null, null, state);
3727
3958
  },
3728
- flushScript($global, chunk) {
3729
- chunk ||= this.createChunk($global);
3959
+ flushScript(chunk, reorders) {
3730
3960
  const { boundary } = chunk;
3731
- const scripts = boundary.flush() === 0 ? chunk.flushScript(boundary).scripts : "";
3732
- if (boundary.signal.aborted) throw boundary.signal.reason;
3961
+ const { state } = boundary;
3962
+ let html = "";
3963
+ let scripts = "";
3964
+ if (reorders) for (const reorder of reorders) state.reorder(reorder);
3965
+ if (boundary.flush() === 0) ({html, scripts} = chunk.flushScript(boundary));
3966
+ if (boundary.aborted) throw boundary.reason;
3733
3967
  if (boundary.count) throw new Error("Cannot serialize promise across tags/class compat layer.");
3734
- return scripts;
3968
+ return {
3969
+ html,
3970
+ scripts
3971
+ };
3735
3972
  },
3736
- render(renderer, willRerender, classAPIOut, component, input, completeChunks, registerChildScope) {
3973
+ render(renderer, willRerender, classAPIOut, component, input, componentScopes, registerChildScope, parentChunk) {
3974
+ if (parentChunk?.boundary.aborted) return;
3737
3975
  const state = this.ensureState(classAPIOut.global);
3738
- const boundary = new Boundary(state);
3976
+ const boundary = new Boundary(state, void 0, parentChunk?.boundary);
3739
3977
  const context = getChunk()?.context ?? null;
3740
3978
  let head = new Chunk(boundary, new Chunk(boundary, null, context, state), context, state);
3741
3979
  let normalizedInput = input;
@@ -3743,7 +3981,8 @@ const compat = {
3743
3981
  normalizedInput = {};
3744
3982
  for (const key in input) normalizedInput[key === "renderBody" ? "content" : key] = input[key];
3745
3983
  }
3746
- head.render(() => {
3984
+ const componentScope = new Chunk(boundary, null, context, state);
3985
+ componentScope.render(() => {
3747
3986
  if (this.hasPendingClassFunctions(classAPIOut.global)) drainClassFunctions(classAPIOut.global, (hostId) => {
3748
3987
  const fnScopeId = _scope_id();
3749
3988
  const scope = writeScope(fnScopeId, {
@@ -3758,11 +3997,13 @@ const compat = {
3758
3997
  writeScope(scopeId, { m5c: component.id });
3759
3998
  _script(scopeId, SET_SCOPE_REGISTER_ID);
3760
3999
  }
3761
- _set_serialize_reason(willRerender ? CLIENT_ALL : 0);
4000
+ });
4001
+ head.render(() => {
4002
+ _set_scope_reason(willRerender ? CLIENT_ALL : 0);
3762
4003
  try {
3763
4004
  renderer(normalizedInput);
3764
4005
  } finally {
3765
- _set_serialize_reason(void 0);
4006
+ _set_scope_reason(void 0);
3766
4007
  }
3767
4008
  const asyncOut = classAPIOut.beginAsync({
3768
4009
  last: true,
@@ -3770,17 +4011,29 @@ const compat = {
3770
4011
  });
3771
4012
  classAPIOut.onLast((next) => {
3772
4013
  (boundary.onNext = () => {
3773
- if (boundary.signal.aborted) {
3774
- asyncOut.error(boundary.signal.reason);
4014
+ if (boundary.aborted) {
3775
4015
  boundary.onNext = NOOP;
4016
+ if (parentChunk?.boundary.aborted) {
4017
+ asyncOut.end();
4018
+ next();
4019
+ } else asyncOut.error(boundary.reason);
3776
4020
  } else if (!boundary.count) {
3777
4021
  boundary.onNext = NOOP;
3778
- head = head.consume();
4022
+ const queued = swapReorders(state, null);
4023
+ head = head.consume(boundary);
4024
+ const reorders = swapReorders(state, queued);
4025
+ if (reorders) asyncOut.writer.get("reorders").push(...reorders);
4026
+ const heldEffects = head.takeHeldEffects();
4027
+ if (heldEffects) {
4028
+ head.effects = joinHeldEffects(heldEffects, head.effects);
4029
+ head.lastEffect = "";
4030
+ }
3779
4031
  asyncOut.write(head.html);
3780
4032
  asyncOut.script(head.scripts);
3781
- asyncOut.end();
3782
4033
  head.html = head.scripts = "";
3783
- completeChunks.push(head);
4034
+ asyncOut.writer.get("chunks").push(head);
4035
+ asyncOut.end();
4036
+ componentScopes.push(componentScope);
3784
4037
  next();
3785
4038
  }
3786
4039
  })();
@@ -3818,6 +4071,11 @@ function drainClassFunctions($global, writeScope) {
3818
4071
  for (const [id, fn, hostId] of pending) register(id, fn, scopeByHost[hostId] ||= writeScope(hostId));
3819
4072
  pending.length = 0;
3820
4073
  }
4074
+ function swapReorders(state, reorders) {
4075
+ const { writeReorders } = state;
4076
+ state.writeReorders = reorders;
4077
+ return writeReorders;
4078
+ }
3821
4079
  function NOOP() {}
3822
4080
  //#endregion
3823
- export { $global, _assert_hoist, _attr, _attr_and, _attr_class, _attr_content, _attr_details_or_dialog_open as _attr_details_open, _attr_details_or_dialog_open as _attr_dialog_open, _attr_input_checked, _attr_input_checkedValue, _attr_input_value, _attr_nonce, _attr_nullish, _attr_option_value, _attr_or, _attr_select_value, _attr_style, _attr_textarea_value, _attrs, _attrs_content, _attrs_partial, _attrs_partial_content, _attrs_select_value, _attrs_textarea_value, _await, _content, _content_resume, _dynamic_tag, _el, _el_read_error, _el_resume, _escape, _escape_comment, _escape_script, _escape_style, _escape_style_value, _existing_scope, _flush_head, _for_in, _for_of, _for_to, _for_until, _hoist, _hoist_read_error, _html, _html_resume, _id, _if, _peek_scope_id, _resume, _resume_branch, _resume_locals, writeScope as _scope, _scope_id, _scope_reason, _scope_with_id, _script, _serialize_guard, _serialize_if, _set_serialize_reason, _show_end, _show_start, _style_html, _subscribe, _template, _text_resume, _textarea_value, _to_text, _trailers, _try, _unescaped, _var, attrTag, attrTags, compat, forIn, forOf, forTo, forUntil, withLoadAssets, withPageAssets };
4081
+ export { $global, _assert_class_toggles, _assert_hoist, _attr, _attr_and, _attr_class, _attr_content, _attr_details_or_dialog_open as _attr_details_open, _attr_details_or_dialog_open as _attr_dialog_open, _attr_input_checked, _attr_input_checkedValue, _attr_input_value, _attr_nonce, _attr_nullish, _attr_option_value, _attr_or, _attr_select_value, _attr_style, _attr_textarea_value, _attrs, _attrs_content, _attrs_partial, _attrs_partial_content, _attrs_select_value, _attrs_textarea_value, _await, _content, _content_resume, _dynamic_tag, _el, _el_read_error, _el_resume, _escape, _escape_comment, _escape_script, _escape_style, _escape_style_value, _existing_scope, _flush_head, _for_in, _for_of, _for_to, _for_until, _hoist, _hoist_read_error, _html, _html_resume, _id, _if, _peek_scope_id, _resume, _resume_branch, _resume_locals, writeScope as _scope, _scope_id, _scope_reason, _scope_with_id, _script, _set_scope_reason, _show_end, _show_start, _style_html, _subscribe, _template, _text_resume, _textarea_value, _to_text, _trailers, _try, _unescaped, _var, _write_guard, _write_if, attrTag, attrTags, compat, forIn, forOf, forTo, forUntil, withLoadAssets, withPageAssets };