marko 6.0.0-next.3.39 → 6.0.0-next.3.40

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