marko 6.0.0-next.3.39 → 6.0.0-next.3.41

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.
@@ -100,35 +100,24 @@ function normalizeDynamicRenderer(value2) {
100
100
  }
101
101
 
102
102
  // src/dom/event.ts
103
- var elementHandlersByEvent = /* @__PURE__ */ new Map();
104
103
  var defaultDelegator = createDelegator();
105
104
  function on(element, type, handler) {
106
- let handlersByElement = elementHandlersByEvent.get(type);
107
- if (!handlersByElement) {
108
- elementHandlersByEvent.set(type, handlersByElement = /* @__PURE__ */ new WeakMap());
109
- }
110
- if (!handlersByElement.has(element)) {
105
+ if (element["$" + type] === void 0) {
111
106
  defaultDelegator(element, type, handleDelegated);
112
107
  }
113
- handlersByElement.set(element, handler || null);
108
+ element["$" + type] = handler || null;
114
109
  }
115
110
  function createDelegator() {
116
111
  const kEvents = Symbol();
117
112
  return function ensureDelegated(node, type, handler) {
118
- const root = node.getRootNode();
119
- (root[kEvents] ||= {})[type] ||= (root.addEventListener(type, handler, true), 1);
113
+ ((node = node.getRootNode())[kEvents] ||= {})[type] ||= (node.addEventListener(type, handler, true), 1);
120
114
  };
121
115
  }
122
116
  function handleDelegated(ev) {
123
117
  let target = !rendering && ev.target;
124
- if (target) {
125
- const handlersByElement = elementHandlersByEvent.get(ev.type);
126
- handlersByElement.get(target)?.(ev, target);
127
- if (ev.bubbles) {
128
- while ((target = target.parentNode) && !ev.cancelBubble) {
129
- handlersByElement.get(target)?.(ev, target);
130
- }
131
- }
118
+ while (target) {
119
+ target["$" + ev.type]?.(ev, target);
120
+ target = ev.bubbles && !ev.cancelBubble && target.parentNode;
132
121
  }
133
122
  }
134
123
 
@@ -425,7 +414,17 @@ function controllable_input_checkedValue_effect(scope, nodeAccessor) {
425
414
  );
426
415
  run();
427
416
  if (scope[nodeAccessor + "=" /* ControlledType */] === 6 /* Pending */) {
428
- el.checked = !el.checked;
417
+ if (el.name && el.type[0] === "r") {
418
+ for (const radio of el.getRootNode().querySelectorAll(
419
+ `[type=radio][name=${CSS.escape(el.name)}]`
420
+ )) {
421
+ if (radio.form === el.form) {
422
+ radio.checked = Array.isArray(oldValue) ? oldValue.includes(radio.value) : oldValue === radio.value;
423
+ }
424
+ }
425
+ } else {
426
+ el.checked = !el.checked;
427
+ }
429
428
  }
430
429
  }
431
430
  });
@@ -505,7 +504,7 @@ function controllable_select_value_effect(scope, nodeAccessor) {
505
504
  }
506
505
  }
507
506
  };
508
- if (!controllableHandlers.has(el)) {
507
+ if (!el._) {
509
508
  new MutationObserver(() => {
510
509
  const value2 = scope[nodeAccessor + ":" /* ControlledValue */];
511
510
  if (Array.isArray(value2) ? value2.length !== el.selectedOptions.length || value2.some((value3, i) => value3 != el.selectedOptions[i].value) : el.value != value2) {
@@ -600,9 +599,8 @@ function setCheckboxValue(scope, nodeAccessor, type, checked, checkedChange) {
600
599
  }
601
600
  }
602
601
  var controllableDelegate = createDelegator();
603
- var controllableHandlers = /* @__PURE__ */ new WeakMap();
604
602
  function syncControllable(el, event, hasChanged, onChange) {
605
- if (!controllableHandlers.has(el)) {
603
+ if (!el._) {
606
604
  controllableDelegate(el, event, handleChange);
607
605
  if (el.form) {
608
606
  controllableDelegate(el.form, "reset", handleFormReset);
@@ -611,17 +609,16 @@ function syncControllable(el, event, hasChanged, onChange) {
611
609
  queueMicrotask(onChange);
612
610
  }
613
611
  }
614
- controllableHandlers.set(el, onChange);
612
+ el._ = onChange;
615
613
  }
616
614
  function handleChange(ev) {
617
- controllableHandlers.get(ev.target)?.(ev);
615
+ ev.target._?.(ev);
618
616
  }
619
617
  function handleFormReset(ev) {
620
618
  const handlers = [];
621
619
  for (const el of ev.target.elements) {
622
- const handler = controllableHandlers.get(el);
623
- if (handler && hasFormElementChanged(el)) {
624
- handlers.push(handler);
620
+ if (el._ && hasFormElementChanged(el)) {
621
+ handlers.push(el._);
625
622
  }
626
623
  }
627
624
  requestAnimationFrame(() => {
@@ -942,11 +939,6 @@ function finishPendingScopes() {
942
939
  }
943
940
  pendingScopes = [];
944
941
  }
945
- var emptyBranch = createScope({});
946
- function getEmptyBranch(marker) {
947
- emptyBranch.___startNode = emptyBranch.___endNode = marker;
948
- return emptyBranch;
949
- }
950
942
  function destroyBranch(branch) {
951
943
  branch.___parentBranch?.___branchScopes?.delete(branch);
952
944
  destroyNestedBranches(branch);
@@ -973,936 +965,891 @@ function insertBranchBefore(branch, parentNode, nextSibling) {
973
965
  );
974
966
  }
975
967
 
976
- // src/dom/reconcile.ts
977
- var WRONG_POS = 2147483647;
978
- function reconcile(parent, oldBranches, newBranches, afterReference) {
979
- let oldStart = 0;
980
- let newStart = 0;
981
- let oldEnd = oldBranches.length - 1;
982
- let newEnd = newBranches.length - 1;
983
- let oldStartBranch = oldBranches[oldStart];
984
- let newStartBranch = newBranches[newStart];
985
- let oldEndBranch = oldBranches[oldEnd];
986
- let newEndBranch = newBranches[newEnd];
987
- let i;
988
- let j;
989
- let k;
990
- let nextSibling;
991
- let oldBranch;
992
- let newBranch;
993
- outer: {
994
- while (oldStartBranch === newStartBranch) {
995
- ++oldStart;
996
- ++newStart;
997
- if (oldStart > oldEnd || newStart > newEnd) {
998
- break outer;
999
- }
1000
- oldStartBranch = oldBranches[oldStart];
1001
- newStartBranch = newBranches[newStart];
968
+ // src/dom/schedule.ts
969
+ var runTask;
970
+ var port2 = /* @__PURE__ */ (() => {
971
+ const { port1, port2: port22 } = new MessageChannel();
972
+ port1.onmessage = () => {
973
+ isScheduled = false;
974
+ if (true) {
975
+ const run2 = runTask;
976
+ runTask = void 0;
977
+ run2();
978
+ } else {
979
+ run();
1002
980
  }
1003
- while (oldEndBranch === newEndBranch) {
1004
- --oldEnd;
1005
- --newEnd;
1006
- if (oldStart > oldEnd || newStart > newEnd) {
1007
- break outer;
981
+ };
982
+ return port22;
983
+ })();
984
+ var isScheduled;
985
+ function schedule() {
986
+ if (!isScheduled) {
987
+ if (true) {
988
+ if (console.createTask) {
989
+ const task = console.createTask("queue");
990
+ runTask = () => task.run(run);
991
+ } else {
992
+ runTask = run;
1008
993
  }
1009
- oldEndBranch = oldBranches[oldEnd];
1010
- newEndBranch = newBranches[newEnd];
1011
994
  }
995
+ isScheduled = true;
996
+ queueMicrotask(flushAndWaitFrame);
1012
997
  }
1013
- if (oldStart > oldEnd) {
1014
- if (newStart <= newEnd) {
1015
- k = newEnd + 1;
1016
- nextSibling = k < newBranches.length ? newBranches[k].___startNode : afterReference;
1017
- do {
1018
- insertBranchBefore(newBranches[newStart++], parent, nextSibling);
1019
- } while (newStart <= newEnd);
1020
- }
1021
- } else if (newStart > newEnd) {
1022
- do {
1023
- removeAndDestroyBranch(oldBranches[oldStart++]);
1024
- } while (oldStart <= oldEnd);
998
+ }
999
+ function flushAndWaitFrame() {
1000
+ if (true) {
1001
+ runTask();
1025
1002
  } else {
1026
- const oldLength = oldEnd - oldStart + 1;
1027
- const newLength = newEnd - newStart + 1;
1028
- const aNullable = oldBranches;
1029
- const sources = new Array(newLength);
1030
- for (i = 0; i < newLength; ++i) {
1031
- sources[i] = -1;
1032
- }
1033
- let pos = 0;
1034
- let synced = 0;
1035
- const keyIndex = /* @__PURE__ */ new Map();
1036
- for (j = newStart; j <= newEnd; ++j) {
1037
- keyIndex.set(newBranches[j], j);
1038
- }
1039
- for (i = oldStart; i <= oldEnd && synced < newLength; ++i) {
1040
- oldBranch = oldBranches[i];
1041
- j = keyIndex.get(oldBranch);
1042
- if (j !== void 0) {
1043
- pos = pos > j ? WRONG_POS : j;
1044
- ++synced;
1045
- newBranch = newBranches[j];
1046
- sources[j - newStart] = i;
1047
- aNullable[i] = null;
1048
- }
1049
- }
1050
- if (oldLength === oldBranches.length && synced === 0) {
1051
- for (; newStart < newLength; ++newStart) {
1052
- insertBranchBefore(newBranches[newStart], parent, afterReference);
1053
- }
1054
- for (; oldStart < oldLength; ++oldStart) {
1055
- removeAndDestroyBranch(oldBranches[oldStart]);
1056
- }
1003
+ run();
1004
+ }
1005
+ requestAnimationFrame(triggerMacroTask);
1006
+ }
1007
+ function triggerMacroTask() {
1008
+ port2.postMessage(0);
1009
+ }
1010
+
1011
+ // src/dom/signals.ts
1012
+ var MARK = true ? Symbol("mark") : {};
1013
+ var CLEAN = true ? Symbol("clean") : {};
1014
+ var DIRTY = true ? Symbol("dirty") : {};
1015
+ function state(valueAccessor, fn, getIntersection) {
1016
+ const valueSignal = value(valueAccessor, fn, getIntersection);
1017
+ const markAccessor = valueAccessor + "#" /* Mark */;
1018
+ const valueChangeAccessor = valueAccessor + "@" /* TagVariableChange */;
1019
+ return (scope, valueOrOp, valueChange) => {
1020
+ if (rendering) {
1021
+ const valueIsOp = valueOrOp === MARK || valueOrOp === CLEAN || valueOrOp === DIRTY;
1022
+ valueSignal(
1023
+ scope,
1024
+ valueIsOp || (scope[valueChangeAccessor] = valueChange) || scope[markAccessor] === void 0 ? valueOrOp : CLEAN
1025
+ );
1026
+ } else if (scope[valueChangeAccessor]) {
1027
+ scope[valueChangeAccessor](valueOrOp);
1057
1028
  } else {
1058
- i = oldLength - synced;
1059
- while (i > 0) {
1060
- oldBranch = aNullable[oldStart++];
1061
- if (oldBranch !== null) {
1062
- removeAndDestroyBranch(oldBranch);
1063
- i--;
1064
- }
1029
+ schedule();
1030
+ queueSource(scope, valueSignal, valueOrOp);
1031
+ }
1032
+ return valueOrOp;
1033
+ };
1034
+ }
1035
+ function value(valueAccessor, fn, getIntersection) {
1036
+ const markAccessor = valueAccessor + "#" /* Mark */;
1037
+ let intersection2;
1038
+ return (scope, valueOrOp) => {
1039
+ if (valueOrOp === MARK) {
1040
+ if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1041
+ getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1065
1042
  }
1066
- if (pos === WRONG_POS) {
1067
- const seq = longestIncreasingSubsequence(sources);
1068
- j = seq.length - 1;
1069
- k = newBranches.length;
1070
- for (i = newLength - 1; i >= 0; --i) {
1071
- if (sources[i] === -1) {
1072
- pos = i + newStart;
1073
- newBranch = newBranches[pos++];
1074
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1075
- insertBranchBefore(newBranch, parent, nextSibling);
1076
- } else {
1077
- if (j < 0 || i !== seq[j]) {
1078
- pos = i + newStart;
1079
- newBranch = newBranches[pos++];
1080
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1081
- insertBranchBefore(newBranch, parent, nextSibling);
1082
- } else {
1083
- --j;
1084
- }
1085
- }
1086
- }
1087
- } else if (synced !== newLength) {
1088
- k = newBranches.length;
1089
- for (i = newLength - 1; i >= 0; --i) {
1090
- if (sources[i] === -1) {
1091
- pos = i + newStart;
1092
- newBranch = newBranches[pos++];
1093
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1094
- insertBranchBefore(newBranch, parent, nextSibling);
1095
- }
1043
+ } else if (valueOrOp !== DIRTY) {
1044
+ const existing = scope[markAccessor] !== void 0;
1045
+ if ((scope[markAccessor] ||= 1) === 1) {
1046
+ if (valueOrOp === CLEAN || existing && scope[valueAccessor] === valueOrOp) {
1047
+ getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1048
+ } else {
1049
+ scope[valueAccessor] = valueOrOp;
1050
+ fn && fn(scope, valueOrOp);
1051
+ getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1096
1052
  }
1097
1053
  }
1054
+ scope[markAccessor]--;
1098
1055
  }
1099
- }
1056
+ };
1100
1057
  }
1101
- function longestIncreasingSubsequence(a) {
1102
- const p = a.slice();
1103
- const result = [];
1104
- result.push(0);
1105
- let u;
1106
- let v;
1107
- for (let i = 0, il = a.length; i < il; ++i) {
1108
- if (a[i] === -1) {
1109
- continue;
1110
- }
1111
- const j = result[result.length - 1];
1112
- if (a[j] < a[i]) {
1113
- p[i] = j;
1114
- result.push(i);
1115
- continue;
1116
- }
1117
- u = 0;
1118
- v = result.length - 1;
1119
- while (u < v) {
1120
- const c = (u + v) / 2 | 0;
1121
- if (a[result[c]] < a[i]) {
1122
- u = c + 1;
1058
+ var accessorId = 0;
1059
+ function intersection(count, fn, getIntersection) {
1060
+ const dirtyAccessor = "?" /* Dynamic */ + accessorId++;
1061
+ const markAccessor = dirtyAccessor + "#" /* Mark */;
1062
+ let intersection2;
1063
+ return (scope, op) => {
1064
+ if (op === MARK) {
1065
+ if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1066
+ getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1067
+ }
1068
+ } else if (scope[markAccessor] === void 0) {
1069
+ scope[markAccessor] = count - 1;
1070
+ scope[dirtyAccessor] = true;
1071
+ } else if (--scope[markAccessor] === 0) {
1072
+ if (op === DIRTY || scope[dirtyAccessor]) {
1073
+ scope[dirtyAccessor] = false;
1074
+ fn(scope);
1075
+ getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1123
1076
  } else {
1124
- v = c;
1077
+ getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1125
1078
  }
1079
+ } else {
1080
+ scope[dirtyAccessor] ||= op === DIRTY;
1126
1081
  }
1127
- if (a[i] < a[result[u]]) {
1128
- if (u > 0) {
1129
- p[i] = result[u - 1];
1082
+ };
1083
+ }
1084
+ function loopClosure(valueAccessor, ownerLoopNodeAccessor, fn, getIntersection) {
1085
+ const childSignal = closure(valueAccessor, fn, getIntersection);
1086
+ const loopScopeAccessor = ownerLoopNodeAccessor + "!" /* LoopScopeArray */;
1087
+ const loopScopeMapAccessor = ownerLoopNodeAccessor + "(" /* LoopScopeMap */;
1088
+ const ownerSignal = (ownerScope) => {
1089
+ for (const scope of ownerScope[loopScopeAccessor] || ownerScope[loopScopeMapAccessor]?.values() || []) {
1090
+ if (!scope.___pending) {
1091
+ queueSource(scope, childSignal);
1130
1092
  }
1131
- result[u] = i;
1132
1093
  }
1133
- }
1134
- u = result.length;
1135
- v = result[u - 1];
1136
- while (u-- > 0) {
1137
- result[u] = v;
1138
- v = p[v];
1139
- }
1140
- return result;
1141
- }
1142
-
1143
- // src/dom/walker.ts
1144
- var walker = /* @__PURE__ */ document.createTreeWalker(document);
1145
- function trimWalkString(walkString) {
1146
- let end = walkString.length;
1147
- while (walkString.charCodeAt(--end) > 47 /* BeginChild */) ;
1148
- return walkString.slice(0, end + 1);
1094
+ };
1095
+ ownerSignal._ = childSignal;
1096
+ return ownerSignal;
1149
1097
  }
1150
- function walk(startNode, walkCodes, branch) {
1151
- walker.currentNode = startNode;
1152
- walkInternal(0, walkCodes, branch);
1153
- walker.currentNode = document;
1098
+ function conditionalClosure(valueAccessor, ownerConditionalNodeAccessor, branch, fn, getIntersection) {
1099
+ const childSignal = closure(valueAccessor, fn, getIntersection);
1100
+ const scopeAccessor = ownerConditionalNodeAccessor + "!" /* ConditionalScope */;
1101
+ const branchAccessor = ownerConditionalNodeAccessor + "(" /* ConditionalRenderer */;
1102
+ const ownerSignal = (scope) => {
1103
+ const ifScope = scope[scopeAccessor];
1104
+ if (ifScope && !ifScope.___pending && scope[branchAccessor] === branch) {
1105
+ queueSource(ifScope, childSignal);
1106
+ }
1107
+ };
1108
+ ownerSignal._ = childSignal;
1109
+ return ownerSignal;
1154
1110
  }
1155
- function walkInternal(currentWalkIndex, walkCodes, scope) {
1156
- let value2;
1157
- let storedMultiplier = 0;
1158
- let currentMultiplier = 0;
1159
- let currentScopeIndex = 0;
1160
- while (value2 = walkCodes.charCodeAt(currentWalkIndex++)) {
1161
- currentMultiplier = storedMultiplier;
1162
- storedMultiplier = 0;
1163
- if (value2 >= 117 /* Multiplier */) {
1164
- storedMultiplier = currentMultiplier * 10 /* Multiplier */ + value2 - 117 /* Multiplier */;
1165
- } else if (value2 >= 107 /* Out */) {
1166
- value2 = 10 /* Out */ * currentMultiplier + value2 - 107 /* Out */;
1167
- while (value2--) {
1168
- walker.parentNode();
1169
- }
1170
- walker.nextSibling();
1171
- } else if (value2 >= 97 /* Over */) {
1172
- value2 = 10 /* Over */ * currentMultiplier + value2 - 97 /* Over */;
1173
- while (value2--) {
1174
- walker.nextSibling();
1175
- }
1176
- } else if (value2 >= 67 /* Next */) {
1177
- value2 = 20 /* Next */ * currentMultiplier + value2 - 67 /* Next */;
1178
- while (value2--) {
1179
- walker.nextNode();
1111
+ function dynamicClosure(valueAccessor, fn, getIntersection, getOwnerScope) {
1112
+ const subscribersAccessor = "?" /* Dynamic */ + accessorId++;
1113
+ const childSignal = closure(
1114
+ valueAccessor,
1115
+ fn,
1116
+ getIntersection,
1117
+ getOwnerScope
1118
+ );
1119
+ const ownerSignal = (ownerScope) => {
1120
+ const subscribers = ownerScope[subscribersAccessor];
1121
+ if (subscribers) {
1122
+ for (const subscriber of subscribers) {
1123
+ if (!subscriber.___pending) {
1124
+ queueSource(subscriber, childSignal);
1125
+ }
1180
1126
  }
1181
- } else if (value2 === 47 /* BeginChild */) {
1182
- currentWalkIndex = walkInternal(
1183
- currentWalkIndex,
1184
- walkCodes,
1185
- scope[true ? getDebugKey(currentScopeIndex++, "#childScope") : currentScopeIndex++] = createScope(scope.$global, scope.___closestBranch)
1127
+ }
1128
+ };
1129
+ const subscribe = (scope) => {
1130
+ const owner = getOwnerScope ? getOwnerScope(scope) : scope._;
1131
+ const subscribers = owner[subscribersAccessor] ||= /* @__PURE__ */ new Set();
1132
+ if (!subscribers.has(scope)) {
1133
+ subscribers.add(scope);
1134
+ getAbortSignal(scope, -1).addEventListener(
1135
+ "abort",
1136
+ () => subscribers.delete(scope)
1186
1137
  );
1187
- } else if (value2 === 38 /* EndChild */) {
1188
- return currentWalkIndex;
1189
- } else if (value2 === 32 /* Get */) {
1190
- scope[true ? getDebugKey(currentScopeIndex++, walker.currentNode) : currentScopeIndex++] = walker.currentNode;
1191
- } else {
1192
- const newNode = scope[true ? getDebugKey(currentScopeIndex++, "#text") : currentScopeIndex++] = new Text();
1193
- const current = walker.currentNode;
1194
- const parentNode = current.parentNode;
1195
- if (value2 !== 37 /* Replace */) {
1196
- throw new Error(`Unknown walk code: ${value2}`);
1197
- }
1198
- parentNode.replaceChild(newNode, current);
1199
- walker.currentNode = newNode;
1200
1138
  }
1201
- }
1202
- return currentWalkIndex;
1203
- }
1204
- function getDebugKey(index, node) {
1205
- if (typeof node === "string") {
1206
- return `${node}/${index}`;
1207
- } else if (node.nodeType === 3 /* Text */) {
1208
- return `#text/${index}`;
1209
- } else if (node.nodeType === 8 /* Comment */) {
1210
- return `#comment/${index}`;
1211
- } else if (node.nodeType === 1 /* Element */) {
1212
- return `#${node.tagName.toLowerCase()}/${index}`;
1213
- }
1214
- return index;
1139
+ };
1140
+ ownerSignal.___subscribe = subscribe;
1141
+ ownerSignal._ = (scope) => {
1142
+ childSignal(scope);
1143
+ subscribe(scope);
1144
+ };
1145
+ return ownerSignal;
1215
1146
  }
1216
-
1217
- // src/dom/renderer.ts
1218
- function createBranchScopeWithRenderer(renderer, $global, parentScope, parentNode) {
1219
- const branch = createBranch(
1220
- $global,
1221
- renderer.___owner || parentScope,
1222
- parentScope
1147
+ function registerDynamicClosure(id, valueAccessor, fn, getIntersection, getOwnerScope) {
1148
+ const signal = dynamicClosure(
1149
+ valueAccessor,
1150
+ fn,
1151
+ getIntersection,
1152
+ getOwnerScope
1223
1153
  );
1224
- if (true) {
1225
- branch.___renderer = renderer;
1226
- }
1227
- initBranch(renderer, branch, parentNode);
1228
- return branch;
1154
+ register(id, signal.___subscribe);
1155
+ return signal;
1229
1156
  }
1230
- function createBranchScopeWithTagNameOrRenderer(tagNameOrRenderer, $global, parentScope, parentNode) {
1231
- if (typeof tagNameOrRenderer !== "string") {
1232
- return createBranchScopeWithRenderer(
1233
- tagNameOrRenderer,
1234
- $global,
1235
- parentScope,
1236
- parentNode
1157
+ function closure(valueAccessor, fn, getIntersection, getOwnerScope) {
1158
+ let intersection2;
1159
+ return (scope, op) => {
1160
+ op || fn && fn(
1161
+ scope,
1162
+ (getOwnerScope ? getOwnerScope(scope) : scope._)[valueAccessor]
1237
1163
  );
1164
+ getIntersection && (intersection2 ||= getIntersection())(scope, op ? MARK : DIRTY);
1165
+ };
1166
+ }
1167
+ function setTagVar(scope, childAccessor, tagVarSignal2) {
1168
+ scope[childAccessor]["/" /* TagVariable */] = (valueOrOp) => tagVarSignal2(scope, valueOrOp);
1169
+ }
1170
+ var tagVarSignal = (scope, valueOrOp) => scope["/" /* TagVariable */]?.(valueOrOp);
1171
+ function setTagVarChange(scope, changeHandler) {
1172
+ scope["@" /* TagVariableChange */] = changeHandler;
1173
+ }
1174
+ var tagVarSignalChange = (scope, value2) => scope["@" /* TagVariableChange */]?.(value2);
1175
+ var tagIdsByGlobal = /* @__PURE__ */ new WeakMap();
1176
+ function nextTagId({ $global }) {
1177
+ const id = tagIdsByGlobal.get($global) || 0;
1178
+ tagIdsByGlobal.set($global, id + 1);
1179
+ return "c" + $global.runtimeId + $global.renderId + id.toString(36);
1180
+ }
1181
+ function inChild(childAccessor, signal) {
1182
+ return (scope, valueOrOp) => {
1183
+ signal(scope[childAccessor], valueOrOp);
1184
+ };
1185
+ }
1186
+ function intersections(signals) {
1187
+ return (scope, op) => {
1188
+ for (const signal of signals) {
1189
+ signal(scope, op);
1190
+ }
1191
+ };
1192
+ }
1193
+ function effect(id, fn) {
1194
+ register(id, fn);
1195
+ return (scope) => {
1196
+ queueEffect(scope, fn);
1197
+ };
1198
+ }
1199
+
1200
+ // src/dom/queue.ts
1201
+ var pendingRenders = [];
1202
+ var pendingEffects = [];
1203
+ var rendering = false;
1204
+ function queueSource(scope, signal, value2) {
1205
+ const prevRendering = rendering;
1206
+ rendering = true;
1207
+ signal(scope, MARK);
1208
+ rendering = prevRendering;
1209
+ queueRender(scope, signal, value2);
1210
+ }
1211
+ function queueRender(scope, signal, value2) {
1212
+ let i = pendingRenders.length;
1213
+ const render = {
1214
+ ___scope: scope,
1215
+ ___signal: signal,
1216
+ ___value: value2,
1217
+ ___depth: scope.___closestBranch?.___branchDepth || 0,
1218
+ ___index: i
1219
+ };
1220
+ pendingRenders.push(render);
1221
+ while (i) {
1222
+ const parentIndex = i - 1 >> 1;
1223
+ const parent = pendingRenders[parentIndex];
1224
+ if (comparePendingRenders(render, parent) >= 0) break;
1225
+ pendingRenders[i] = parent;
1226
+ i = parentIndex;
1238
1227
  }
1239
- const branch = createBranch($global, parentScope, parentScope);
1240
- branch[true ? `#${tagNameOrRenderer}/0` : 0] = branch.___startNode = branch.___endNode = document.createElementNS(
1241
- tagNameOrRenderer === "svg" ? "http://www.w3.org/2000/svg" : tagNameOrRenderer === "math" ? "http://www.w3.org/1998/Math/MathML" : parentNode.namespaceURI,
1242
- tagNameOrRenderer
1243
- );
1244
- return branch;
1228
+ pendingRenders[i] = render;
1245
1229
  }
1246
- function createBranch($global, ownerScope, parentScope) {
1247
- const branch = createScope($global);
1248
- const parentBranch = parentScope.___closestBranch;
1249
- branch._ = ownerScope;
1250
- branch.___closestBranch = branch;
1251
- if (parentBranch) {
1252
- branch.___branchDepth = parentBranch.___branchDepth + 1;
1253
- branch.___parentBranch = parentBranch;
1254
- (parentBranch.___branchScopes ||= /* @__PURE__ */ new Set()).add(branch);
1255
- } else {
1256
- branch.___branchDepth = 1;
1230
+ function queueEffect(scope, fn) {
1231
+ pendingEffects.push(scope, fn);
1232
+ }
1233
+ function run() {
1234
+ const effects = pendingEffects;
1235
+ try {
1236
+ rendering = true;
1237
+ runRenders();
1238
+ } finally {
1239
+ pendingRenders = [];
1240
+ pendingEffects = [];
1241
+ rendering = false;
1257
1242
  }
1258
- return branch;
1243
+ runEffects(effects);
1259
1244
  }
1260
- function initBranch(renderer, branch, parentNode) {
1261
- const clone = renderer.___clone(parentNode.namespaceURI);
1262
- const cloneParent = clone.parentNode;
1263
- if (cloneParent) {
1264
- walk(cloneParent.firstChild, renderer.___walks, branch);
1265
- branch.___startNode = cloneParent.firstChild;
1266
- branch.___endNode = cloneParent.lastChild;
1267
- } else {
1268
- walk(clone, renderer.___walks, branch);
1269
- branch.___startNode = branch.___endNode = clone;
1245
+ function prepareEffects(fn) {
1246
+ const prevRenders = pendingRenders;
1247
+ const prevEffects = pendingEffects;
1248
+ const preparedEffects = pendingEffects = [];
1249
+ pendingRenders = [];
1250
+ try {
1251
+ rendering = true;
1252
+ fn();
1253
+ runRenders();
1254
+ } finally {
1255
+ rendering = false;
1256
+ pendingRenders = prevRenders;
1257
+ pendingEffects = prevEffects;
1270
1258
  }
1271
- if (renderer.___setup) {
1272
- queueRender(branch, renderer.___setup);
1259
+ return preparedEffects;
1260
+ }
1261
+ function runEffects(effects) {
1262
+ for (let i = 0; i < effects.length; i += 2 /* Total */) {
1263
+ const scope = effects[i];
1264
+ const fn = effects[i + 1];
1265
+ fn(scope, scope);
1273
1266
  }
1274
1267
  }
1275
- function dynamicTagAttrs(nodeAccessor, getContent, inputIsArgs) {
1276
- return (scope, attrsOrOp) => {
1277
- const renderer = scope[nodeAccessor + "(" /* ConditionalRenderer */];
1278
- if (!renderer || attrsOrOp === DIRTY) {
1279
- return;
1280
- }
1281
- const childScope = scope[nodeAccessor + "!" /* ConditionalScope */];
1282
- if (attrsOrOp === MARK || attrsOrOp === CLEAN) {
1283
- return renderer.___args?.(childScope, attrsOrOp);
1284
- }
1285
- const content = getContent?.(scope);
1286
- if (typeof renderer === "string") {
1287
- const nodeAccessor2 = true ? `#${renderer}/0` : 0;
1288
- if (renderer === "textarea" && content) {
1289
- throw new Error(
1290
- "A dynamic tag rendering a `<textarea>` cannot have `content` and must use the `value` attribute instead."
1291
- );
1268
+ function runRenders() {
1269
+ while (pendingRenders.length) {
1270
+ const render = pendingRenders[0];
1271
+ const next = pendingRenders.pop();
1272
+ if (render !== next) {
1273
+ let i = 0;
1274
+ const mid = pendingRenders.length >> 1;
1275
+ const item = pendingRenders[0] = next;
1276
+ while (i < mid) {
1277
+ let bestChild = (i << 1) + 1;
1278
+ const right = bestChild + 1;
1279
+ if (right < pendingRenders.length && comparePendingRenders(
1280
+ pendingRenders[right],
1281
+ pendingRenders[bestChild]
1282
+ ) < 0) {
1283
+ bestChild = right;
1284
+ }
1285
+ if (comparePendingRenders(pendingRenders[bestChild], item) >= 0) {
1286
+ break;
1287
+ } else {
1288
+ pendingRenders[i] = pendingRenders[bestChild];
1289
+ i = bestChild;
1290
+ }
1292
1291
  }
1293
- setConditionalRendererOnlyChild(
1294
- childScope,
1295
- nodeAccessor2,
1296
- content,
1297
- createBranchScopeWithTagNameOrRenderer
1298
- );
1299
- attrs(childScope, nodeAccessor2, attrsOrOp());
1300
- } else if (renderer.___args) {
1301
- const attributes = attrsOrOp();
1302
- renderer.___args(
1303
- childScope,
1304
- inputIsArgs ? attributes : [
1305
- content ? {
1306
- ...attributes,
1307
- content
1308
- } : attributes
1309
- ]
1310
- );
1292
+ pendingRenders[i] = item;
1311
1293
  }
1312
- };
1294
+ if (!render.___scope.___closestBranch?.___destroyed) {
1295
+ render.___signal(render.___scope, render.___value);
1296
+ }
1297
+ }
1298
+ finishPendingScopes();
1313
1299
  }
1314
- function createRendererWithOwner(template, rawWalks, setup, getArgs) {
1315
- let args;
1316
- const id = true ? Symbol("Marko Renderer") : {};
1317
- const walks = rawWalks ? /* @__PURE__ */ trimWalkString(rawWalks) : " ";
1318
- return (owner) => {
1319
- return {
1320
- ___id: id,
1321
- ___template: template,
1322
- ___walks: walks,
1323
- ___setup: setup,
1324
- ___clone: _clone,
1325
- ___owner: owner,
1326
- get ___args() {
1327
- return args ||= getArgs?.();
1328
- }
1329
- };
1330
- };
1300
+ function comparePendingRenders(a, b) {
1301
+ return a.___depth - b.___depth || a.___index - b.___index;
1331
1302
  }
1332
- function createRenderer(template, walks, setup, getArgs) {
1333
- return createRendererWithOwner(template, walks, setup, getArgs)();
1303
+
1304
+ // src/dom/abort-signal.ts
1305
+ function resetAbortSignal(scope, id) {
1306
+ const ctrl = scope.___abortControllers?.[id];
1307
+ if (ctrl) {
1308
+ queueEffect(ctrl, abort);
1309
+ scope.___abortControllers[id] = void 0;
1310
+ }
1334
1311
  }
1335
- function _clone(ns) {
1336
- return ((cloneCache[ns] ||= {})[this.___template] ||= createCloneableHTML(
1337
- this.___template,
1338
- ns
1339
- ))();
1312
+ function getAbortSignal(scope, id) {
1313
+ if (scope.___closestBranch) {
1314
+ (scope.___closestBranch.___abortScopes ||= /* @__PURE__ */ new Set()).add(scope);
1315
+ }
1316
+ return ((scope.___abortControllers ||= {})[id] ||= new AbortController()).signal;
1340
1317
  }
1341
- var cloneCache = {};
1342
- function createCloneableHTML(html2, ns) {
1343
- const { firstChild, lastChild } = parseHTML(html2, ns);
1344
- const parent = document.createElementNS(ns, "t");
1345
- insertChildNodes(parent, null, firstChild, lastChild);
1346
- return firstChild === lastChild && firstChild.nodeType < 8 /* Comment */ ? () => firstChild.cloneNode(true) : () => parent.cloneNode(true).firstChild;
1318
+ function abort(ctrl) {
1319
+ ctrl.abort();
1347
1320
  }
1348
1321
 
1349
- // src/dom/control-flow.ts
1350
- function conditional(nodeAccessor, ...branches) {
1351
- const branchAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1352
- return (scope, newBranchIndexOrOp) => {
1353
- if (newBranchIndexOrOp !== scope[branchAccessor] && newBranchIndexOrOp !== DIRTY && newBranchIndexOrOp !== MARK && newBranchIndexOrOp !== CLEAN) {
1354
- (scope[nodeAccessor].nodeType > 1 /* Element */ ? setConditionalRenderer : setConditionalRendererOnlyChild)(
1355
- scope,
1356
- nodeAccessor,
1357
- branches[scope[branchAccessor] = newBranchIndexOrOp],
1358
- createBranchScopeWithRenderer
1359
- );
1360
- }
1361
- };
1362
- }
1363
- function patchDynamicTag(fn) {
1364
- dynamicTag = fn(dynamicTag);
1365
- }
1366
- var dynamicTag = function dynamicTag2(nodeAccessor, fn, getIntersection) {
1367
- const rendererAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1368
- let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1369
- return (scope, newRendererOrOp) => {
1370
- if (newRendererOrOp === DIRTY) return;
1371
- const currentRenderer = scope[rendererAccessor];
1372
- let op = newRendererOrOp;
1373
- if (newRendererOrOp !== MARK && newRendererOrOp !== CLEAN) {
1374
- const normalizedRenderer = normalizeDynamicRenderer(newRendererOrOp);
1375
- if (isDifferentRenderer(normalizedRenderer, currentRenderer)) {
1376
- scope[rendererAccessor] = normalizedRenderer;
1377
- setConditionalRenderer(
1378
- scope,
1379
- nodeAccessor,
1380
- normalizedRenderer,
1381
- createBranchScopeWithTagNameOrRenderer
1382
- );
1383
- fn && fn(scope);
1384
- op = DIRTY;
1385
- } else {
1386
- op = CLEAN;
1322
+ // src/common/compat-meta.ts
1323
+ var prefix = true ? "$compat_" : "$C_";
1324
+ var RENDERER_REGISTER_ID = prefix + (true ? "renderer" : "r");
1325
+ var SET_SCOPE_REGISTER_ID = prefix + (true ? "setScope" : "s");
1326
+ var RENDER_BODY_ID = prefix + (true ? "renderBody" : "b");
1327
+
1328
+ // src/dom/reconcile.ts
1329
+ var WRONG_POS = 2147483647;
1330
+ function reconcile(parent, oldBranches, newBranches, afterReference) {
1331
+ let oldStart = 0;
1332
+ let newStart = 0;
1333
+ let oldEnd = oldBranches.length - 1;
1334
+ let newEnd = newBranches.length - 1;
1335
+ let oldStartBranch = oldBranches[oldStart];
1336
+ let newStartBranch = newBranches[newStart];
1337
+ let oldEndBranch = oldBranches[oldEnd];
1338
+ let newEndBranch = newBranches[newEnd];
1339
+ let i;
1340
+ let j;
1341
+ let k;
1342
+ let nextSibling;
1343
+ let oldBranch;
1344
+ let newBranch;
1345
+ outer: {
1346
+ while (oldStartBranch === newStartBranch) {
1347
+ ++oldStart;
1348
+ ++newStart;
1349
+ if (oldStart > oldEnd || newStart > newEnd) {
1350
+ break outer;
1351
+ }
1352
+ oldStartBranch = oldBranches[oldStart];
1353
+ newStartBranch = newBranches[newStart];
1354
+ }
1355
+ while (oldEndBranch === newEndBranch) {
1356
+ --oldEnd;
1357
+ --newEnd;
1358
+ if (oldStart > oldEnd || newStart > newEnd) {
1359
+ break outer;
1387
1360
  }
1361
+ oldEndBranch = oldBranches[oldEnd];
1362
+ newEndBranch = newBranches[newEnd];
1388
1363
  }
1389
- intersection2?.(scope, op);
1390
- };
1391
- };
1392
- function setConditionalRenderer(scope, nodeAccessor, newRenderer, createBranch2) {
1393
- const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */] || getEmptyBranch(scope[nodeAccessor]);
1394
- const newBranch = newRenderer ? createBranch2(
1395
- newRenderer,
1396
- scope.$global,
1397
- scope,
1398
- prevBranch.___endNode.parentNode
1399
- ) : getEmptyBranch(scope[nodeAccessor]);
1400
- if (prevBranch !== newBranch) {
1401
- insertBranchBefore(
1402
- newBranch,
1403
- prevBranch.___endNode.parentNode,
1404
- prevBranch.___endNode.nextSibling
1405
- );
1406
- removeAndDestroyBranch(prevBranch);
1407
- scope[nodeAccessor + "!" /* ConditionalScope */] = newRenderer && newBranch;
1408
1364
  }
1409
- }
1410
- function setConditionalRendererOnlyChild(scope, nodeAccessor, newRenderer, createBranch2) {
1411
- const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */];
1412
- const referenceNode = scope[nodeAccessor];
1413
- const newBranch = newRenderer && createBranch2(newRenderer, scope.$global, scope, referenceNode);
1414
- referenceNode.textContent = "";
1415
- if (newBranch) {
1416
- insertBranchBefore(newBranch, referenceNode, null);
1417
- }
1418
- prevBranch && destroyBranch(prevBranch);
1419
- scope[nodeAccessor + "!" /* ConditionalScope */] = newBranch;
1420
- }
1421
- var emptyMarkerMap = /* @__PURE__ */ new Map([
1422
- [Symbol(), /* @__PURE__ */ getEmptyBranch(0)]
1423
- ]);
1424
- var emptyMarkerArray = [
1425
- /* @__PURE__ */ getEmptyBranch(0)
1426
- ];
1427
- var emptyMap = /* @__PURE__ */ new Map();
1428
- var emptyArray = [];
1429
- function loopOf(nodeAccessor, renderer) {
1430
- return loop(
1431
- nodeAccessor,
1432
- renderer,
1433
- ([all, by = bySecondArg], cb) => {
1434
- if (typeof by === "string") {
1435
- forOf(
1436
- all,
1437
- (item, i) => cb(item[by], [item, i])
1438
- );
1439
- } else {
1440
- forOf(all, (item, i) => cb(by(item, i), [item, i]));
1441
- }
1365
+ if (oldStart > oldEnd) {
1366
+ if (newStart <= newEnd) {
1367
+ k = newEnd + 1;
1368
+ nextSibling = k < newBranches.length ? newBranches[k].___startNode : afterReference;
1369
+ do {
1370
+ insertBranchBefore(newBranches[newStart++], parent, nextSibling);
1371
+ } while (newStart <= newEnd);
1442
1372
  }
1443
- );
1444
- }
1445
- function loopIn(nodeAccessor, renderer) {
1446
- return loop(
1447
- nodeAccessor,
1448
- renderer,
1449
- ([obj, by = byFirstArg], cb) => forIn(obj, (key, value2) => cb(by(key, value2), [key, value2]))
1450
- );
1451
- }
1452
- function loopTo(nodeAccessor, renderer) {
1453
- return loop(
1454
- nodeAccessor,
1455
- renderer,
1456
- ([to, from, step, by = byFirstArg], cb) => forTo(to, from, step, (v) => cb(by(v), [v]))
1457
- );
1458
- }
1459
- function loop(nodeAccessor, renderer, forEach) {
1460
- const loopScopeAccessor = nodeAccessor + "!" /* LoopScopeArray */;
1461
- const params = renderer.___args;
1462
- return (scope, valueOrOp) => {
1463
- if (valueOrOp === DIRTY) return;
1464
- if (valueOrOp === MARK || valueOrOp === CLEAN) {
1465
- const loopBranches = scope[loopScopeAccessor] ?? scope[nodeAccessor + "(" /* LoopScopeMap */]?.values() ?? [];
1466
- if (loopBranches !== emptyMarkerArray) {
1467
- for (const branch of loopBranches) {
1468
- params?.(branch, valueOrOp);
1469
- }
1373
+ } else if (newStart > newEnd) {
1374
+ do {
1375
+ removeAndDestroyBranch(oldBranches[oldStart++]);
1376
+ } while (oldStart <= oldEnd);
1377
+ } else {
1378
+ const oldLength = oldEnd - oldStart + 1;
1379
+ const newLength = newEnd - newStart + 1;
1380
+ const aNullable = oldBranches;
1381
+ const sources = new Array(newLength);
1382
+ for (i = 0; i < newLength; ++i) {
1383
+ sources[i] = -1;
1384
+ }
1385
+ let pos = 0;
1386
+ let synced = 0;
1387
+ const keyIndex = /* @__PURE__ */ new Map();
1388
+ for (j = newStart; j <= newEnd; ++j) {
1389
+ keyIndex.set(newBranches[j], j);
1390
+ }
1391
+ for (i = oldStart; i <= oldEnd && synced < newLength; ++i) {
1392
+ oldBranch = oldBranches[i];
1393
+ j = keyIndex.get(oldBranch);
1394
+ if (j !== void 0) {
1395
+ pos = pos > j ? WRONG_POS : j;
1396
+ ++synced;
1397
+ newBranch = newBranches[j];
1398
+ sources[j - newStart] = i;
1399
+ aNullable[i] = null;
1470
1400
  }
1471
- return;
1472
1401
  }
1473
- const referenceNode = scope[nodeAccessor];
1474
- const referenceIsMarker = referenceNode.nodeType > 1 /* Element */;
1475
- const oldMap = scope[nodeAccessor + "(" /* LoopScopeMap */] || (referenceIsMarker ? emptyMarkerMap : emptyMap);
1476
- const oldArray = scope[nodeAccessor + "!" /* LoopScopeArray */] || Array.from(oldMap.values());
1477
- const parentNode = referenceIsMarker ? referenceNode.parentNode || oldArray[0].___startNode.parentNode : referenceNode;
1478
- let newMap;
1479
- let newArray;
1480
- let afterReference;
1481
- let needsReconciliation = true;
1482
- forEach(valueOrOp, (key, args) => {
1483
- let branch = oldMap.get(key);
1484
- if (!branch) {
1485
- branch = createBranchScopeWithRenderer(
1486
- renderer,
1487
- scope.$global,
1488
- scope,
1489
- parentNode
1490
- );
1491
- } else {
1402
+ if (oldLength === oldBranches.length && synced === 0) {
1403
+ for (; newStart < newLength; ++newStart) {
1404
+ insertBranchBefore(newBranches[newStart], parent, afterReference);
1492
1405
  }
1493
- if (params) {
1494
- params(branch, args);
1406
+ for (; oldStart < oldLength; ++oldStart) {
1407
+ removeAndDestroyBranch(oldBranches[oldStart]);
1495
1408
  }
1496
- if (newMap) {
1497
- newMap.set(key, branch);
1498
- newArray.push(branch);
1499
- } else {
1500
- newMap = /* @__PURE__ */ new Map([[key, branch]]);
1501
- newArray = [branch];
1409
+ } else {
1410
+ i = oldLength - synced;
1411
+ while (i > 0) {
1412
+ oldBranch = aNullable[oldStart++];
1413
+ if (oldBranch !== null) {
1414
+ removeAndDestroyBranch(oldBranch);
1415
+ i--;
1416
+ }
1502
1417
  }
1503
- });
1504
- if (!newMap) {
1505
- if (referenceIsMarker) {
1506
- newMap = emptyMarkerMap;
1507
- newArray = emptyMarkerArray;
1508
- getEmptyBranch(referenceNode);
1509
- } else {
1510
- oldArray.forEach(destroyBranch);
1511
- referenceNode.textContent = "";
1512
- newMap = emptyMap;
1513
- newArray = emptyArray;
1514
- needsReconciliation = false;
1418
+ if (pos === WRONG_POS) {
1419
+ const seq = longestIncreasingSubsequence(sources);
1420
+ j = seq.length - 1;
1421
+ k = newBranches.length;
1422
+ for (i = newLength - 1; i >= 0; --i) {
1423
+ if (sources[i] === -1) {
1424
+ pos = i + newStart;
1425
+ newBranch = newBranches[pos++];
1426
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1427
+ insertBranchBefore(newBranch, parent, nextSibling);
1428
+ } else {
1429
+ if (j < 0 || i !== seq[j]) {
1430
+ pos = i + newStart;
1431
+ newBranch = newBranches[pos++];
1432
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1433
+ insertBranchBefore(newBranch, parent, nextSibling);
1434
+ } else {
1435
+ --j;
1436
+ }
1437
+ }
1438
+ }
1439
+ } else if (synced !== newLength) {
1440
+ k = newBranches.length;
1441
+ for (i = newLength - 1; i >= 0; --i) {
1442
+ if (sources[i] === -1) {
1443
+ pos = i + newStart;
1444
+ newBranch = newBranches[pos++];
1445
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1446
+ insertBranchBefore(newBranch, parent, nextSibling);
1447
+ }
1448
+ }
1515
1449
  }
1516
1450
  }
1517
- if (needsReconciliation) {
1518
- if (referenceIsMarker) {
1519
- if (oldMap === emptyMarkerMap) {
1520
- getEmptyBranch(referenceNode);
1521
- }
1522
- const oldLastChild = oldArray[oldArray.length - 1];
1523
- afterReference = oldLastChild.___endNode.nextSibling;
1451
+ }
1452
+ }
1453
+ function longestIncreasingSubsequence(a) {
1454
+ const p = a.slice();
1455
+ const result = [0];
1456
+ let u;
1457
+ let v;
1458
+ for (let i = 0, il = a.length; i < il; ++i) {
1459
+ if (a[i] === -1) {
1460
+ continue;
1461
+ }
1462
+ const j = result[result.length - 1];
1463
+ if (a[j] < a[i]) {
1464
+ p[i] = j;
1465
+ result.push(i);
1466
+ continue;
1467
+ }
1468
+ u = 0;
1469
+ v = result.length - 1;
1470
+ while (u < v) {
1471
+ const c = (u + v) / 2 | 0;
1472
+ if (a[result[c]] < a[i]) {
1473
+ u = c + 1;
1524
1474
  } else {
1525
- afterReference = null;
1475
+ v = c;
1526
1476
  }
1527
- reconcile(parentNode, oldArray, newArray, afterReference);
1528
1477
  }
1529
- scope[nodeAccessor + "(" /* LoopScopeMap */] = newMap;
1530
- scope[nodeAccessor + "!" /* LoopScopeArray */] = newArray;
1531
- };
1532
- }
1533
- function bySecondArg(_item, index) {
1534
- return index;
1478
+ if (a[i] < a[result[u]]) {
1479
+ if (u > 0) {
1480
+ p[i] = result[u - 1];
1481
+ }
1482
+ result[u] = i;
1483
+ }
1484
+ }
1485
+ u = result.length;
1486
+ v = result[u - 1];
1487
+ while (u-- > 0) {
1488
+ result[u] = v;
1489
+ v = p[v];
1490
+ }
1491
+ return result;
1535
1492
  }
1536
- function byFirstArg(name) {
1537
- return name;
1493
+
1494
+ // src/dom/walker.ts
1495
+ var walker = /* @__PURE__ */ document.createTreeWalker(document);
1496
+ function trimWalkString(walkString) {
1497
+ let end = walkString.length;
1498
+ while (walkString.charCodeAt(--end) > 47 /* BeginChild */) ;
1499
+ return walkString.slice(0, end + 1);
1538
1500
  }
1539
- function isDifferentRenderer(a, b) {
1540
- return a !== b && (a?.___id || 0) !== b?.___id;
1501
+ function walk(startNode, walkCodes, branch) {
1502
+ walker.currentNode = startNode;
1503
+ walkInternal(0, walkCodes, branch);
1541
1504
  }
1542
-
1543
- // src/dom/schedule.ts
1544
- var runTask;
1545
- var port2 = /* @__PURE__ */ (() => {
1546
- const { port1, port2: port22 } = new MessageChannel();
1547
- port1.onmessage = () => {
1548
- isScheduled = false;
1549
- if (true) {
1550
- const run2 = runTask;
1551
- runTask = void 0;
1552
- run2();
1553
- } else {
1554
- run();
1555
- }
1556
- };
1557
- return port22;
1558
- })();
1559
- var isScheduled;
1560
- function schedule() {
1561
- if (!isScheduled) {
1562
- if (true) {
1563
- if (console.createTask) {
1564
- const task = console.createTask("queue");
1565
- runTask = () => task.run(run);
1566
- } else {
1567
- runTask = run;
1505
+ function walkInternal(currentWalkIndex, walkCodes, scope) {
1506
+ let value2;
1507
+ let storedMultiplier = 0;
1508
+ let currentMultiplier = 0;
1509
+ let currentScopeIndex = 0;
1510
+ for (; currentWalkIndex < walkCodes.length; ) {
1511
+ value2 = walkCodes.charCodeAt(currentWalkIndex++);
1512
+ currentMultiplier = storedMultiplier;
1513
+ storedMultiplier = 0;
1514
+ if (value2 === 32 /* Get */) {
1515
+ scope[true ? getDebugKey(currentScopeIndex++, walker.currentNode) : currentScopeIndex++] = walker.currentNode;
1516
+ } else if (value2 === 37 /* Replace */) {
1517
+ walker.currentNode.replaceWith(
1518
+ walker.currentNode = scope[true ? getDebugKey(currentScopeIndex++, "#text") : currentScopeIndex++] = new Text()
1519
+ );
1520
+ } else if (value2 === 38 /* EndChild */) {
1521
+ return currentWalkIndex;
1522
+ } else if (value2 === 47 /* BeginChild */) {
1523
+ currentWalkIndex = walkInternal(
1524
+ currentWalkIndex,
1525
+ walkCodes,
1526
+ scope[true ? getDebugKey(currentScopeIndex++, "#childScope") : currentScopeIndex++] = createScope(scope.$global, scope.___closestBranch)
1527
+ );
1528
+ } else if (value2 < 91 /* NextEnd */ + 1) {
1529
+ value2 = 20 /* Next */ * currentMultiplier + value2 - 67 /* Next */;
1530
+ while (value2--) {
1531
+ walker.nextNode();
1532
+ }
1533
+ } else if (value2 < 106 /* OverEnd */ + 1) {
1534
+ value2 = 10 /* Over */ * currentMultiplier + value2 - 97 /* Over */;
1535
+ while (value2--) {
1536
+ walker.nextSibling();
1537
+ }
1538
+ } else if (value2 < 116 /* OutEnd */ + 1) {
1539
+ value2 = 10 /* Out */ * currentMultiplier + value2 - 107 /* Out */;
1540
+ while (value2--) {
1541
+ walker.parentNode();
1542
+ }
1543
+ walker.nextSibling();
1544
+ } else {
1545
+ if (value2 < 117 /* Multiplier */ || value2 > 126 /* MultiplierEnd */) {
1546
+ throw new Error(`Unknown walk code: ${value2}`);
1568
1547
  }
1548
+ storedMultiplier = currentMultiplier * 10 /* Multiplier */ + value2 - 117 /* Multiplier */;
1569
1549
  }
1570
- isScheduled = true;
1571
- queueMicrotask(flushAndWaitFrame);
1572
1550
  }
1573
1551
  }
1574
- function flushAndWaitFrame() {
1575
- if (true) {
1576
- runTask();
1577
- } else {
1578
- run();
1552
+ function getDebugKey(index, node) {
1553
+ if (typeof node === "string") {
1554
+ return `${node}/${index}`;
1555
+ } else if (node.nodeType === 3 /* Text */) {
1556
+ return `#text/${index}`;
1557
+ } else if (node.nodeType === 8 /* Comment */) {
1558
+ return `#comment/${index}`;
1559
+ } else if (node.nodeType === 1 /* Element */) {
1560
+ return `#${node.tagName.toLowerCase()}/${index}`;
1579
1561
  }
1580
- requestAnimationFrame(triggerMacroTask);
1581
- }
1582
- function triggerMacroTask() {
1583
- port2.postMessage(0);
1562
+ return index;
1584
1563
  }
1585
1564
 
1586
- // src/dom/signals.ts
1587
- var MARK = true ? Symbol("mark") : {};
1588
- var CLEAN = true ? Symbol("clean") : {};
1589
- var DIRTY = true ? Symbol("dirty") : {};
1590
- function state(valueAccessor, fn, getIntersection) {
1591
- const valueSignal = value(valueAccessor, fn, getIntersection);
1592
- const markAccessor = valueAccessor + "#" /* Mark */;
1593
- const valueChangeAccessor = valueAccessor + "@" /* TagVariableChange */;
1594
- return (scope, valueOrOp, valueChange) => {
1595
- if (rendering) {
1596
- const valueIsOp = valueOrOp === MARK || valueOrOp === CLEAN || valueOrOp === DIRTY;
1597
- valueSignal(
1598
- scope,
1599
- valueIsOp || (scope[valueChangeAccessor] = valueChange) || scope[markAccessor] === void 0 ? valueOrOp : CLEAN
1600
- );
1601
- } else if (scope[valueChangeAccessor]) {
1602
- scope[valueChangeAccessor](valueOrOp);
1603
- } else {
1604
- schedule();
1605
- queueSource(scope, valueSignal, valueOrOp);
1606
- }
1607
- return valueOrOp;
1608
- };
1565
+ // src/dom/renderer.ts
1566
+ function createBranchScopeWithRenderer(renderer, $global, parentScope, parentNode) {
1567
+ const branch = createBranch(
1568
+ $global,
1569
+ renderer.___owner || parentScope,
1570
+ parentScope
1571
+ );
1572
+ if (true) {
1573
+ branch.___renderer = renderer;
1574
+ }
1575
+ initBranch(renderer, branch, parentNode);
1576
+ return branch;
1609
1577
  }
1610
- function value(valueAccessor, fn, getIntersection) {
1611
- const markAccessor = valueAccessor + "#" /* Mark */;
1612
- let intersection2;
1613
- return (scope, valueOrOp) => {
1614
- if (valueOrOp === MARK) {
1615
- if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1616
- getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1617
- }
1618
- } else if (valueOrOp !== DIRTY) {
1619
- const existing = scope[markAccessor] !== void 0;
1620
- if ((scope[markAccessor] ||= 1) === 1) {
1621
- if (valueOrOp === CLEAN || existing && scope[valueAccessor] === valueOrOp) {
1622
- getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1623
- } else {
1624
- scope[valueAccessor] = valueOrOp;
1625
- fn && fn(scope, valueOrOp);
1626
- getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1627
- }
1628
- }
1629
- scope[markAccessor]--;
1630
- }
1631
- };
1578
+ function createBranchScopeWithTagNameOrRenderer(tagNameOrRenderer, $global, parentScope, parentNode) {
1579
+ if (typeof tagNameOrRenderer !== "string") {
1580
+ return createBranchScopeWithRenderer(
1581
+ tagNameOrRenderer,
1582
+ $global,
1583
+ parentScope,
1584
+ parentNode
1585
+ );
1586
+ }
1587
+ const branch = createBranch($global, parentScope, parentScope);
1588
+ branch[true ? `#${tagNameOrRenderer}/0` : 0] = branch.___startNode = branch.___endNode = document.createElementNS(
1589
+ tagNameOrRenderer === "svg" ? "http://www.w3.org/2000/svg" : tagNameOrRenderer === "math" ? "http://www.w3.org/1998/Math/MathML" : parentNode.namespaceURI,
1590
+ tagNameOrRenderer
1591
+ );
1592
+ return branch;
1632
1593
  }
1633
- var accessorId = 0;
1634
- function intersection(count, fn, getIntersection) {
1635
- const dirtyAccessor = "?" /* Dynamic */ + accessorId++;
1636
- const markAccessor = dirtyAccessor + "#" /* Mark */;
1637
- let intersection2;
1638
- return (scope, op) => {
1639
- if (op === MARK) {
1640
- if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1641
- getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1642
- }
1643
- } else if (scope[markAccessor] === void 0) {
1644
- scope[markAccessor] = count - 1;
1645
- scope[dirtyAccessor] = true;
1646
- } else if (--scope[markAccessor] === 0) {
1647
- if (op === DIRTY || scope[dirtyAccessor]) {
1648
- scope[dirtyAccessor] = false;
1649
- fn(scope);
1650
- getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1651
- } else {
1652
- getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1653
- }
1654
- } else {
1655
- scope[dirtyAccessor] ||= op === DIRTY;
1656
- }
1657
- };
1594
+ function createBranch($global, ownerScope, parentScope) {
1595
+ const branch = createScope($global);
1596
+ const parentBranch = parentScope.___closestBranch;
1597
+ branch._ = ownerScope;
1598
+ branch.___closestBranch = branch;
1599
+ if (parentBranch) {
1600
+ branch.___branchDepth = parentBranch.___branchDepth + 1;
1601
+ branch.___parentBranch = parentBranch;
1602
+ (parentBranch.___branchScopes ||= /* @__PURE__ */ new Set()).add(branch);
1603
+ } else {
1604
+ branch.___branchDepth = 1;
1605
+ }
1606
+ return branch;
1658
1607
  }
1659
- function loopClosure(valueAccessor, ownerLoopNodeAccessor, fn, getIntersection) {
1660
- const childSignal = closure(valueAccessor, fn, getIntersection);
1661
- const loopScopeAccessor = ownerLoopNodeAccessor + "!" /* LoopScopeArray */;
1662
- const loopScopeMapAccessor = ownerLoopNodeAccessor + "(" /* LoopScopeMap */;
1663
- const ownerSignal = (ownerScope) => {
1664
- const loopScopes = ownerScope[loopScopeAccessor] ?? ownerScope[loopScopeMapAccessor]?.values() ?? [];
1665
- if (loopScopes !== emptyMarkerArray) {
1666
- for (const scope of loopScopes) {
1667
- if (!scope.___pending) {
1668
- queueSource(scope, childSignal);
1669
- }
1670
- }
1671
- }
1672
- };
1673
- ownerSignal._ = childSignal;
1674
- return ownerSignal;
1608
+ function initBranch(renderer, branch, parentNode) {
1609
+ const clone = renderer.___clone(parentNode.namespaceURI);
1610
+ const cloneParent = clone.parentNode;
1611
+ if (cloneParent) {
1612
+ walk(cloneParent.firstChild, renderer.___walks, branch);
1613
+ branch.___startNode = cloneParent.firstChild;
1614
+ branch.___endNode = cloneParent.lastChild;
1615
+ } else {
1616
+ walk(clone, renderer.___walks, branch);
1617
+ branch.___startNode = branch.___endNode = clone;
1618
+ }
1619
+ if (renderer.___setup) {
1620
+ queueRender(branch, renderer.___setup);
1621
+ }
1675
1622
  }
1676
- function conditionalClosure(valueAccessor, ownerConditionalNodeAccessor, branch, fn, getIntersection) {
1677
- const childSignal = closure(valueAccessor, fn, getIntersection);
1678
- const scopeAccessor = ownerConditionalNodeAccessor + "!" /* ConditionalScope */;
1679
- const branchAccessor = ownerConditionalNodeAccessor + "(" /* ConditionalRenderer */;
1680
- const ownerSignal = (scope) => {
1681
- const ifScope = scope[scopeAccessor];
1682
- if (ifScope && !ifScope.___pending && scope[branchAccessor] === branch) {
1683
- queueSource(ifScope, childSignal);
1623
+ function dynamicTagAttrs(nodeAccessor, getContent, inputIsArgs) {
1624
+ return (scope, attrsOrOp) => {
1625
+ const renderer = scope[nodeAccessor + "(" /* ConditionalRenderer */];
1626
+ if (!renderer || attrsOrOp === DIRTY) {
1627
+ return;
1684
1628
  }
1685
- };
1686
- ownerSignal._ = childSignal;
1687
- return ownerSignal;
1688
- }
1689
- function dynamicClosure(valueAccessor, fn, getIntersection, getOwnerScope) {
1690
- const subscribersAccessor = "?" /* Dynamic */ + accessorId++;
1691
- const childSignal = closure(
1692
- valueAccessor,
1693
- fn,
1694
- getIntersection,
1695
- getOwnerScope
1696
- );
1697
- const ownerSignal = (ownerScope) => {
1698
- const subscribers = ownerScope[subscribersAccessor];
1699
- if (subscribers) {
1700
- for (const subscriber of subscribers) {
1701
- if (!subscriber.___pending) {
1702
- queueSource(subscriber, childSignal);
1703
- }
1704
- }
1629
+ const childScope = scope[nodeAccessor + "!" /* ConditionalScope */];
1630
+ if (attrsOrOp === MARK || attrsOrOp === CLEAN) {
1631
+ return renderer.___args?.(childScope, attrsOrOp);
1705
1632
  }
1706
- };
1707
- const subscribe = (scope) => {
1708
- const owner = getOwnerScope ? getOwnerScope(scope) : scope._;
1709
- const subscribers = owner[subscribersAccessor] ||= /* @__PURE__ */ new Set();
1710
- if (!subscribers.has(scope)) {
1711
- subscribers.add(scope);
1712
- getAbortSignal(scope, -1).addEventListener(
1713
- "abort",
1714
- () => owner[subscribersAccessor].delete(scope)
1633
+ const content = getContent?.(scope);
1634
+ if (typeof renderer === "string") {
1635
+ const nodeAccessor2 = true ? `#${renderer}/0` : 0;
1636
+ if (renderer === "textarea" && content) {
1637
+ throw new Error(
1638
+ "A dynamic tag rendering a `<textarea>` cannot have `content` and must use the `value` attribute instead."
1639
+ );
1640
+ }
1641
+ setConditionalRenderer(
1642
+ childScope,
1643
+ nodeAccessor2,
1644
+ content,
1645
+ createBranchScopeWithTagNameOrRenderer
1646
+ );
1647
+ attrs(childScope, nodeAccessor2, attrsOrOp());
1648
+ } else if (renderer.___args) {
1649
+ const attributes = attrsOrOp();
1650
+ renderer.___args(
1651
+ childScope,
1652
+ inputIsArgs ? attributes : [
1653
+ content ? {
1654
+ ...attributes,
1655
+ content
1656
+ } : attributes
1657
+ ]
1715
1658
  );
1716
1659
  }
1717
1660
  };
1718
- ownerSignal.___subscribe = subscribe;
1719
- ownerSignal._ = (scope) => {
1720
- childSignal(scope);
1721
- subscribe(scope);
1722
- };
1723
- return ownerSignal;
1724
- }
1725
- function registerDynamicClosure(id, valueAccessor, fn, getIntersection, getOwnerScope) {
1726
- const signal = dynamicClosure(
1727
- valueAccessor,
1728
- fn,
1729
- getIntersection,
1730
- getOwnerScope
1731
- );
1732
- register(id, signal.___subscribe);
1733
- return signal;
1734
1661
  }
1735
- function closure(valueAccessor, fn, getIntersection, getOwnerScope) {
1736
- let intersection2;
1737
- return (scope, op) => {
1738
- op || fn && fn(
1739
- scope,
1740
- (getOwnerScope ? getOwnerScope(scope) : scope._)[valueAccessor]
1741
- );
1742
- getIntersection && (intersection2 ||= getIntersection())(scope, op ? MARK : DIRTY);
1662
+ function createRendererWithOwner(template, rawWalks, setup, getArgs) {
1663
+ let args;
1664
+ const id = true ? Symbol("Marko Renderer") : {};
1665
+ const walks = rawWalks ? /* @__PURE__ */ trimWalkString(rawWalks) : " ";
1666
+ return (owner) => {
1667
+ return {
1668
+ ___id: id,
1669
+ ___template: template,
1670
+ ___walks: walks,
1671
+ ___setup: setup,
1672
+ ___clone: _clone,
1673
+ ___owner: owner,
1674
+ get ___args() {
1675
+ return args ||= getArgs?.();
1676
+ }
1677
+ };
1743
1678
  };
1744
1679
  }
1745
- function setTagVar(scope, childAccessor, tagVarSignal2) {
1746
- scope[childAccessor]["/" /* TagVariable */] = (valueOrOp) => tagVarSignal2(scope, valueOrOp);
1747
- }
1748
- var tagVarSignal = (scope, valueOrOp) => scope["/" /* TagVariable */]?.(valueOrOp);
1749
- function setTagVarChange(scope, changeHandler) {
1750
- scope["@" /* TagVariableChange */] = changeHandler;
1680
+ function createRenderer(template, walks, setup, getArgs) {
1681
+ return createRendererWithOwner(template, walks, setup, getArgs)();
1751
1682
  }
1752
- var tagVarSignalChange = (scope, value2) => scope["@" /* TagVariableChange */]?.(value2);
1753
- var tagIdsByGlobal = /* @__PURE__ */ new WeakMap();
1754
- function nextTagId({ $global }) {
1755
- const id = tagIdsByGlobal.get($global) || 0;
1756
- tagIdsByGlobal.set($global, id + 1);
1757
- return "c" + $global.runtimeId + $global.renderId + id.toString(36);
1683
+ function _clone(ns) {
1684
+ return ((cloneCache[ns] ||= {})[this.___template] ||= createCloneableHTML(
1685
+ this.___template,
1686
+ ns
1687
+ ))();
1758
1688
  }
1759
- function inChild(childAccessor, signal) {
1760
- return (scope, valueOrOp) => {
1761
- signal(scope[childAccessor], valueOrOp);
1762
- };
1689
+ var cloneCache = {};
1690
+ function createCloneableHTML(html2, ns) {
1691
+ const { firstChild, lastChild } = parseHTML(html2, ns);
1692
+ const parent = document.createElementNS(ns, "t");
1693
+ insertChildNodes(parent, null, firstChild, lastChild);
1694
+ return firstChild === lastChild && firstChild.nodeType < 8 /* Comment */ ? () => firstChild.cloneNode(true) : () => parent.cloneNode(true).firstChild;
1763
1695
  }
1764
- function intersections(signals) {
1765
- return (scope, op) => {
1766
- for (const signal of signals) {
1767
- signal(scope, op);
1696
+
1697
+ // src/dom/control-flow.ts
1698
+ function conditional(nodeAccessor, ...branches) {
1699
+ const branchAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1700
+ return (scope, newBranchIndexOrOp) => {
1701
+ if (newBranchIndexOrOp !== scope[branchAccessor] && newBranchIndexOrOp !== DIRTY && newBranchIndexOrOp !== MARK && newBranchIndexOrOp !== CLEAN) {
1702
+ setConditionalRenderer(
1703
+ scope,
1704
+ nodeAccessor,
1705
+ branches[scope[branchAccessor] = newBranchIndexOrOp],
1706
+ createBranchScopeWithRenderer
1707
+ );
1768
1708
  }
1769
1709
  };
1770
1710
  }
1771
- function effect(id, fn) {
1772
- register(id, fn);
1773
- return (scope) => {
1774
- queueEffect(scope, fn);
1775
- };
1776
- }
1777
-
1778
- // src/dom/queue.ts
1779
- var pendingRenders = [];
1780
- var pendingEffects = [];
1781
- var rendering = false;
1782
- function queueSource(scope, signal, value2) {
1783
- const prevRendering = rendering;
1784
- rendering = true;
1785
- signal(scope, MARK);
1786
- rendering = prevRendering;
1787
- queueRender(scope, signal, value2);
1711
+ function patchDynamicTag(fn) {
1712
+ dynamicTag = fn(dynamicTag);
1788
1713
  }
1789
- function queueRender(scope, signal, value2) {
1790
- let i = pendingRenders.length;
1791
- const render = {
1792
- ___scope: scope,
1793
- ___signal: signal,
1794
- ___value: value2,
1795
- ___depth: scope.___closestBranch?.___branchDepth || 0,
1796
- ___index: i
1714
+ var dynamicTag = function dynamicTag2(nodeAccessor, fn, getIntersection) {
1715
+ const rendererAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1716
+ let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1717
+ return (scope, newRendererOrOp) => {
1718
+ if (newRendererOrOp === DIRTY) return;
1719
+ const currentRenderer = scope[rendererAccessor];
1720
+ let op = newRendererOrOp;
1721
+ if (newRendererOrOp !== MARK && newRendererOrOp !== CLEAN) {
1722
+ if (isDifferentRenderer(
1723
+ currentRenderer,
1724
+ scope[rendererAccessor] = normalizeDynamicRenderer(newRendererOrOp)
1725
+ )) {
1726
+ setConditionalRenderer(
1727
+ scope,
1728
+ nodeAccessor,
1729
+ scope[rendererAccessor],
1730
+ createBranchScopeWithTagNameOrRenderer
1731
+ );
1732
+ fn && fn(scope);
1733
+ op = DIRTY;
1734
+ } else {
1735
+ op = CLEAN;
1736
+ }
1737
+ }
1738
+ intersection2?.(scope, op);
1797
1739
  };
1798
- pendingRenders.push(render);
1799
- while (i) {
1800
- const parentIndex = i - 1 >> 1;
1801
- const parent = pendingRenders[parentIndex];
1802
- if (comparePendingRenders(render, parent) >= 0) break;
1803
- pendingRenders[i] = parent;
1804
- i = parentIndex;
1740
+ };
1741
+ function setConditionalRenderer(scope, nodeAccessor, newRenderer, createBranch2) {
1742
+ const referenceNode = scope[nodeAccessor];
1743
+ const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */];
1744
+ const parentNode = referenceNode.nodeType > 1 /* Element */ ? (prevBranch?.___startNode || referenceNode).parentNode : referenceNode;
1745
+ const newBranch = scope[nodeAccessor + "!" /* ConditionalScope */] = newRenderer && createBranch2(newRenderer, scope.$global, scope, parentNode);
1746
+ if (referenceNode === parentNode) {
1747
+ if (prevBranch) {
1748
+ destroyBranch(prevBranch);
1749
+ referenceNode.textContent = "";
1750
+ }
1751
+ if (newBranch) {
1752
+ insertBranchBefore(newBranch, parentNode, null);
1753
+ }
1754
+ } else if (prevBranch) {
1755
+ if (newBranch) {
1756
+ insertBranchBefore(newBranch, parentNode, prevBranch.___startNode);
1757
+ } else {
1758
+ parentNode.insertBefore(referenceNode, prevBranch.___startNode);
1759
+ }
1760
+ removeAndDestroyBranch(prevBranch);
1761
+ } else if (newBranch) {
1762
+ insertBranchBefore(newBranch, parentNode, referenceNode);
1763
+ referenceNode.remove();
1805
1764
  }
1806
- pendingRenders[i] = render;
1807
1765
  }
1808
- function queueEffect(scope, fn) {
1809
- pendingEffects.push(scope, fn);
1810
- }
1811
- function run() {
1812
- const effects = pendingEffects;
1813
- try {
1814
- rendering = true;
1815
- runRenders();
1816
- } finally {
1817
- pendingRenders = [];
1818
- pendingEffects = [];
1819
- rendering = false;
1820
- }
1821
- runEffects(effects);
1766
+ function loopOf(nodeAccessor, renderer) {
1767
+ return loop(
1768
+ nodeAccessor,
1769
+ renderer,
1770
+ ([all, by = bySecondArg], cb) => {
1771
+ if (typeof by === "string") {
1772
+ forOf(
1773
+ all,
1774
+ (item, i) => cb(item[by], [item, i])
1775
+ );
1776
+ } else {
1777
+ forOf(all, (item, i) => cb(by(item, i), [item, i]));
1778
+ }
1779
+ }
1780
+ );
1822
1781
  }
1823
- function prepareEffects(fn) {
1824
- const prevRenders = pendingRenders;
1825
- const prevEffects = pendingEffects;
1826
- const preparedEffects = pendingEffects = [];
1827
- pendingRenders = [];
1828
- try {
1829
- rendering = true;
1830
- fn();
1831
- runRenders();
1832
- } finally {
1833
- rendering = false;
1834
- pendingRenders = prevRenders;
1835
- pendingEffects = prevEffects;
1836
- }
1837
- return preparedEffects;
1782
+ function loopIn(nodeAccessor, renderer) {
1783
+ return loop(
1784
+ nodeAccessor,
1785
+ renderer,
1786
+ ([obj, by = byFirstArg], cb) => forIn(obj, (key, value2) => cb(by(key, value2), [key, value2]))
1787
+ );
1838
1788
  }
1839
- function runEffects(effects) {
1840
- for (let i = 0; i < effects.length; i += 2 /* Total */) {
1841
- const scope = effects[i];
1842
- const fn = effects[i + 1];
1843
- fn(scope, scope);
1844
- }
1789
+ function loopTo(nodeAccessor, renderer) {
1790
+ return loop(
1791
+ nodeAccessor,
1792
+ renderer,
1793
+ ([to, from, step, by = byFirstArg], cb) => forTo(to, from, step, (v) => cb(by(v), [v]))
1794
+ );
1845
1795
  }
1846
- function runRenders() {
1847
- while (pendingRenders.length) {
1848
- const render = pendingRenders[0];
1849
- const next = pendingRenders.pop();
1850
- if (render !== next) {
1851
- let i = 0;
1852
- const mid = pendingRenders.length >> 1;
1853
- const item = pendingRenders[0] = next;
1854
- while (i < mid) {
1855
- let bestChild = (i << 1) + 1;
1856
- const right = bestChild + 1;
1857
- if (right < pendingRenders.length && comparePendingRenders(
1858
- pendingRenders[right],
1859
- pendingRenders[bestChild]
1860
- ) < 0) {
1861
- bestChild = right;
1796
+ function loop(nodeAccessor, renderer, forEach) {
1797
+ const loopScopeAccessor = nodeAccessor + "!" /* LoopScopeArray */;
1798
+ const params = renderer.___args;
1799
+ return (scope, valueOrOp) => {
1800
+ if (valueOrOp === DIRTY) {
1801
+ } else if (valueOrOp === MARK || valueOrOp === CLEAN) {
1802
+ if (params) {
1803
+ for (const branch of scope[loopScopeAccessor] || scope[nodeAccessor + "(" /* LoopScopeMap */]?.values() || []) {
1804
+ params(branch, valueOrOp);
1862
1805
  }
1863
- if (comparePendingRenders(pendingRenders[bestChild], item) >= 0) {
1864
- break;
1865
- } else {
1866
- pendingRenders[i] = pendingRenders[bestChild];
1867
- i = bestChild;
1806
+ }
1807
+ } else {
1808
+ const referenceNode = scope[nodeAccessor];
1809
+ const oldMap = scope[nodeAccessor + "(" /* LoopScopeMap */];
1810
+ const oldArray = oldMap ? scope[nodeAccessor + "!" /* LoopScopeArray */] || [
1811
+ ...oldMap.values()
1812
+ ] : [];
1813
+ const parentNode = referenceNode.nodeType > 1 /* Element */ ? referenceNode.parentNode || oldArray[0].___startNode.parentNode : referenceNode;
1814
+ const newMap = scope[nodeAccessor + "(" /* LoopScopeMap */] = /* @__PURE__ */ new Map();
1815
+ const newArray = scope[nodeAccessor + "!" /* LoopScopeArray */] = [];
1816
+ forEach(valueOrOp, (key, args) => {
1817
+ const branch = oldMap?.get(key) || createBranchScopeWithRenderer(
1818
+ renderer,
1819
+ scope.$global,
1820
+ scope,
1821
+ parentNode
1822
+ );
1823
+ params?.(branch, args);
1824
+ newMap.set(key, branch);
1825
+ newArray.push(branch);
1826
+ });
1827
+ let afterReference = null;
1828
+ if (referenceNode !== parentNode) {
1829
+ if (oldArray.length) {
1830
+ afterReference = oldArray[oldArray.length - 1].___endNode.nextSibling;
1831
+ if (!newArray.length) {
1832
+ parentNode.insertBefore(referenceNode, afterReference);
1833
+ }
1834
+ } else if (newArray.length) {
1835
+ afterReference = referenceNode.nextSibling;
1836
+ referenceNode.remove();
1868
1837
  }
1869
1838
  }
1870
- pendingRenders[i] = item;
1871
- }
1872
- if (!render.___scope.___closestBranch?.___destroyed) {
1873
- render.___signal(render.___scope, render.___value);
1839
+ reconcile(parentNode, oldArray, newArray, afterReference);
1874
1840
  }
1875
- }
1876
- finishPendingScopes();
1877
- }
1878
- function comparePendingRenders(a, b) {
1879
- return a.___depth - b.___depth || a.___index - b.___index;
1841
+ };
1880
1842
  }
1881
-
1882
- // src/dom/abort-signal.ts
1883
- function resetAbortSignal(scope, id) {
1884
- const ctrl = scope.___abortControllers?.[id];
1885
- if (ctrl) {
1886
- queueEffect(ctrl, abort);
1887
- scope.___abortControllers[id] = void 0;
1888
- }
1843
+ function bySecondArg(_item, index) {
1844
+ return index;
1889
1845
  }
1890
- function getAbortSignal(scope, id) {
1891
- if (scope.___closestBranch) {
1892
- (scope.___closestBranch.___abortScopes ||= /* @__PURE__ */ new Set()).add(scope);
1893
- }
1894
- return ((scope.___abortControllers ||= {})[id] ||= new AbortController()).signal;
1846
+ function byFirstArg(name) {
1847
+ return name;
1895
1848
  }
1896
- function abort(ctrl) {
1897
- ctrl.abort();
1849
+ function isDifferentRenderer(a, b) {
1850
+ return a !== b && (a?.___id || 0) !== b?.___id;
1898
1851
  }
1899
1852
 
1900
- // src/common/compat-meta.ts
1901
- var prefix = true ? "$compat_" : "$C_";
1902
- var RENDERER_REGISTER_ID = prefix + (true ? "renderer" : "r");
1903
- var SET_SCOPE_REGISTER_ID = prefix + (true ? "setScope" : "s");
1904
- var RENDER_BODY_ID = prefix + (true ? "renderBody" : "b");
1905
-
1906
1853
  // src/dom/compat.ts
1907
1854
  var classIdToBranch = /* @__PURE__ */ new Map();
1908
1855
  var compat = {