marko 6.0.0-next.3.38 → 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.
package/dist/debug/dom.js CHANGED
@@ -70,7 +70,7 @@ __export(dom_exports, {
70
70
  props: () => props,
71
71
  register: () => register,
72
72
  registerBoundSignal: () => registerBoundSignal,
73
- registerSubscriber: () => registerSubscriber,
73
+ registerDynamicClosure: () => registerDynamicClosure,
74
74
  resetAbortSignal: () => resetAbortSignal,
75
75
  run: () => run,
76
76
  setTagVar: () => setTagVar,
@@ -130,49 +130,6 @@ function forTo(to, from, step, cb) {
130
130
  }
131
131
  }
132
132
 
133
- // src/dom/schedule.ts
134
- var runTask;
135
- var port2 = /* @__PURE__ */ (() => {
136
- const { port1, port2: port22 } = new MessageChannel();
137
- port1.onmessage = () => {
138
- isScheduled = false;
139
- if (true) {
140
- const run2 = runTask;
141
- runTask = void 0;
142
- run2();
143
- } else {
144
- run();
145
- }
146
- };
147
- return port22;
148
- })();
149
- var isScheduled;
150
- function schedule() {
151
- if (!isScheduled) {
152
- if (true) {
153
- if (console.createTask) {
154
- const task = console.createTask("queue");
155
- runTask = () => task.run(run);
156
- } else {
157
- runTask = run;
158
- }
159
- }
160
- isScheduled = true;
161
- queueMicrotask(flushAndWaitFrame);
162
- }
163
- }
164
- function flushAndWaitFrame() {
165
- if (true) {
166
- runTask();
167
- } else {
168
- run();
169
- }
170
- requestAnimationFrame(triggerMacroTask);
171
- }
172
- function triggerMacroTask() {
173
- port2.postMessage(0);
174
- }
175
-
176
133
  // src/common/helpers.ts
177
134
  function classValue(value2) {
178
135
  return toDelimitedString(value2, " ", stringifyClassObject);
@@ -229,35 +186,24 @@ function normalizeDynamicRenderer(value2) {
229
186
  }
230
187
 
231
188
  // src/dom/event.ts
232
- var elementHandlersByEvent = /* @__PURE__ */ new Map();
233
189
  var defaultDelegator = createDelegator();
234
190
  function on(element, type, handler) {
235
- let handlersByElement = elementHandlersByEvent.get(type);
236
- if (!handlersByElement) {
237
- elementHandlersByEvent.set(type, handlersByElement = /* @__PURE__ */ new WeakMap());
238
- }
239
- if (!handlersByElement.has(element)) {
191
+ if (element["$" + type] === void 0) {
240
192
  defaultDelegator(element, type, handleDelegated);
241
193
  }
242
- handlersByElement.set(element, handler || null);
194
+ element["$" + type] = handler || null;
243
195
  }
244
196
  function createDelegator() {
245
197
  const kEvents = Symbol();
246
198
  return function ensureDelegated(node, type, handler) {
247
- const root = node.getRootNode();
248
- (root[kEvents] ||= {})[type] ||= (root.addEventListener(type, handler, true), 1);
199
+ ((node = node.getRootNode())[kEvents] ||= {})[type] ||= (node.addEventListener(type, handler, true), 1);
249
200
  };
250
201
  }
251
202
  function handleDelegated(ev) {
252
203
  let target = !rendering && ev.target;
253
- if (target) {
254
- const handlersByElement = elementHandlersByEvent.get(ev.type);
255
- handlersByElement.get(target)?.(ev, target);
256
- if (ev.bubbles) {
257
- while ((target = target.parentNode) && !ev.cancelBubble) {
258
- handlersByElement.get(target)?.(ev, target);
259
- }
260
- }
204
+ while (target) {
205
+ target["$" + ev.type]?.(ev, target);
206
+ target = ev.bubbles && !ev.cancelBubble && target.parentNode;
261
207
  }
262
208
  }
263
209
 
@@ -503,10 +449,6 @@ function init(runtimeId = DEFAULT_RUNTIME_ID) {
503
449
  });
504
450
  }
505
451
  }
506
- function registerSubscriber(id, signal) {
507
- register(id, signal.___subscribe);
508
- return signal;
509
- }
510
452
  function nodeRef(id, key) {
511
453
  return register(id, (scope) => () => scope[key]);
512
454
  }
@@ -638,7 +580,7 @@ function controllable_select_value_effect(scope, nodeAccessor) {
638
580
  }
639
581
  }
640
582
  };
641
- if (!controllableHandlers.has(el)) {
583
+ if (!el._) {
642
584
  new MutationObserver(() => {
643
585
  const value2 = scope[nodeAccessor + ":" /* ControlledValue */];
644
586
  if (Array.isArray(value2) ? value2.length !== el.selectedOptions.length || value2.some((value3, i) => value3 != el.selectedOptions[i].value) : el.value != value2) {
@@ -733,9 +675,8 @@ function setCheckboxValue(scope, nodeAccessor, type, checked, checkedChange) {
733
675
  }
734
676
  }
735
677
  var controllableDelegate = createDelegator();
736
- var controllableHandlers = /* @__PURE__ */ new WeakMap();
737
678
  function syncControllable(el, event, hasChanged, onChange) {
738
- if (!controllableHandlers.has(el)) {
679
+ if (!el._) {
739
680
  controllableDelegate(el, event, handleChange);
740
681
  if (el.form) {
741
682
  controllableDelegate(el.form, "reset", handleFormReset);
@@ -744,17 +685,16 @@ function syncControllable(el, event, hasChanged, onChange) {
744
685
  queueMicrotask(onChange);
745
686
  }
746
687
  }
747
- controllableHandlers.set(el, onChange);
688
+ el._ = onChange;
748
689
  }
749
690
  function handleChange(ev) {
750
- controllableHandlers.get(ev.target)?.(ev);
691
+ ev.target._?.(ev);
751
692
  }
752
693
  function handleFormReset(ev) {
753
694
  const handlers = [];
754
695
  for (const el of ev.target.elements) {
755
- const handler = controllableHandlers.get(el);
756
- if (handler && hasFormElementChanged(el)) {
757
- handlers.push(handler);
696
+ if (el._ && hasFormElementChanged(el)) {
697
+ handlers.push(el._);
758
698
  }
759
699
  }
760
700
  requestAnimationFrame(() => {
@@ -1057,9 +997,10 @@ function toInsertNode(startNode, endNode) {
1057
997
  // src/dom/scope.ts
1058
998
  var pendingScopes = [];
1059
999
  var debugID = 0;
1060
- function createScope($global) {
1000
+ function createScope($global, closestBranch) {
1061
1001
  const scope = {
1062
1002
  ___pending: 1,
1003
+ ___closestBranch: closestBranch,
1063
1004
  $global
1064
1005
  };
1065
1006
  if (true) {
@@ -1074,11 +1015,6 @@ function finishPendingScopes() {
1074
1015
  }
1075
1016
  pendingScopes = [];
1076
1017
  }
1077
- var emptyBranch = createScope({});
1078
- function getEmptyBranch(marker) {
1079
- emptyBranch.___startNode = emptyBranch.___endNode = marker;
1080
- return emptyBranch;
1081
- }
1082
1018
  function destroyBranch(branch) {
1083
1019
  branch.___parentBranch?.___branchScopes?.delete(branch);
1084
1020
  destroyNestedBranches(branch);
@@ -1105,135 +1041,494 @@ function insertBranchBefore(branch, parentNode, nextSibling) {
1105
1041
  );
1106
1042
  }
1107
1043
 
1108
- // src/dom/reconcile.ts
1109
- var WRONG_POS = 2147483647;
1110
- function reconcile(parent, oldBranches, newBranches, afterReference) {
1111
- let oldStart = 0;
1112
- let newStart = 0;
1113
- let oldEnd = oldBranches.length - 1;
1114
- let newEnd = newBranches.length - 1;
1115
- let oldStartBranch = oldBranches[oldStart];
1116
- let newStartBranch = newBranches[newStart];
1117
- let oldEndBranch = oldBranches[oldEnd];
1118
- let newEndBranch = newBranches[newEnd];
1119
- let i;
1120
- let j;
1121
- let k;
1122
- let nextSibling;
1123
- let oldBranch;
1124
- let newBranch;
1125
- outer: {
1126
- while (oldStartBranch === newStartBranch) {
1127
- ++oldStart;
1128
- ++newStart;
1129
- if (oldStart > oldEnd || newStart > newEnd) {
1130
- break outer;
1131
- }
1132
- oldStartBranch = oldBranches[oldStart];
1133
- newStartBranch = newBranches[newStart];
1044
+ // src/dom/schedule.ts
1045
+ var runTask;
1046
+ var port2 = /* @__PURE__ */ (() => {
1047
+ const { port1, port2: port22 } = new MessageChannel();
1048
+ port1.onmessage = () => {
1049
+ isScheduled = false;
1050
+ if (true) {
1051
+ const run2 = runTask;
1052
+ runTask = void 0;
1053
+ run2();
1054
+ } else {
1055
+ run();
1134
1056
  }
1135
- while (oldEndBranch === newEndBranch) {
1136
- --oldEnd;
1137
- --newEnd;
1138
- if (oldStart > oldEnd || newStart > newEnd) {
1139
- break outer;
1057
+ };
1058
+ return port22;
1059
+ })();
1060
+ var isScheduled;
1061
+ function schedule() {
1062
+ if (!isScheduled) {
1063
+ if (true) {
1064
+ if (console.createTask) {
1065
+ const task = console.createTask("queue");
1066
+ runTask = () => task.run(run);
1067
+ } else {
1068
+ runTask = run;
1140
1069
  }
1141
- oldEndBranch = oldBranches[oldEnd];
1142
- newEndBranch = newBranches[newEnd];
1143
1070
  }
1071
+ isScheduled = true;
1072
+ queueMicrotask(flushAndWaitFrame);
1144
1073
  }
1145
- if (oldStart > oldEnd) {
1146
- if (newStart <= newEnd) {
1147
- k = newEnd + 1;
1148
- nextSibling = k < newBranches.length ? newBranches[k].___startNode : afterReference;
1149
- do {
1150
- insertBranchBefore(newBranches[newStart++], parent, nextSibling);
1151
- } while (newStart <= newEnd);
1152
- }
1153
- } else if (newStart > newEnd) {
1154
- do {
1155
- removeAndDestroyBranch(oldBranches[oldStart++]);
1156
- } while (oldStart <= oldEnd);
1074
+ }
1075
+ function flushAndWaitFrame() {
1076
+ if (true) {
1077
+ runTask();
1157
1078
  } else {
1158
- const oldLength = oldEnd - oldStart + 1;
1159
- const newLength = newEnd - newStart + 1;
1160
- const aNullable = oldBranches;
1161
- const sources = new Array(newLength);
1162
- for (i = 0; i < newLength; ++i) {
1163
- sources[i] = -1;
1164
- }
1165
- let pos = 0;
1166
- let synced = 0;
1167
- const keyIndex = /* @__PURE__ */ new Map();
1168
- for (j = newStart; j <= newEnd; ++j) {
1169
- keyIndex.set(newBranches[j], j);
1079
+ run();
1080
+ }
1081
+ requestAnimationFrame(triggerMacroTask);
1082
+ }
1083
+ function triggerMacroTask() {
1084
+ port2.postMessage(0);
1085
+ }
1086
+
1087
+ // src/dom/signals.ts
1088
+ var MARK = true ? Symbol("mark") : {};
1089
+ var CLEAN = true ? Symbol("clean") : {};
1090
+ var DIRTY = true ? Symbol("dirty") : {};
1091
+ function state(valueAccessor, fn, getIntersection) {
1092
+ const valueSignal = value(valueAccessor, fn, getIntersection);
1093
+ const markAccessor = valueAccessor + "#" /* Mark */;
1094
+ const valueChangeAccessor = valueAccessor + "@" /* TagVariableChange */;
1095
+ return (scope, valueOrOp, valueChange) => {
1096
+ if (rendering) {
1097
+ const valueIsOp = valueOrOp === MARK || valueOrOp === CLEAN || valueOrOp === DIRTY;
1098
+ valueSignal(
1099
+ scope,
1100
+ valueIsOp || (scope[valueChangeAccessor] = valueChange) || scope[markAccessor] === void 0 ? valueOrOp : CLEAN
1101
+ );
1102
+ } else if (scope[valueChangeAccessor]) {
1103
+ scope[valueChangeAccessor](valueOrOp);
1104
+ } else {
1105
+ schedule();
1106
+ queueSource(scope, valueSignal, valueOrOp);
1170
1107
  }
1171
- for (i = oldStart; i <= oldEnd && synced < newLength; ++i) {
1172
- oldBranch = oldBranches[i];
1173
- j = keyIndex.get(oldBranch);
1174
- if (j !== void 0) {
1175
- pos = pos > j ? WRONG_POS : j;
1176
- ++synced;
1177
- newBranch = newBranches[j];
1178
- sources[j - newStart] = i;
1179
- aNullable[i] = null;
1108
+ return valueOrOp;
1109
+ };
1110
+ }
1111
+ function value(valueAccessor, fn, getIntersection) {
1112
+ const markAccessor = valueAccessor + "#" /* Mark */;
1113
+ let intersection2;
1114
+ return (scope, valueOrOp) => {
1115
+ if (valueOrOp === MARK) {
1116
+ if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1117
+ getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1118
+ }
1119
+ } else if (valueOrOp !== DIRTY) {
1120
+ const existing = scope[markAccessor] !== void 0;
1121
+ if ((scope[markAccessor] ||= 1) === 1) {
1122
+ if (valueOrOp === CLEAN || existing && scope[valueAccessor] === valueOrOp) {
1123
+ getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1124
+ } else {
1125
+ scope[valueAccessor] = valueOrOp;
1126
+ fn && fn(scope, valueOrOp);
1127
+ getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1128
+ }
1180
1129
  }
1130
+ scope[markAccessor]--;
1181
1131
  }
1182
- if (oldLength === oldBranches.length && synced === 0) {
1183
- for (; newStart < newLength; ++newStart) {
1184
- insertBranchBefore(newBranches[newStart], parent, afterReference);
1132
+ };
1133
+ }
1134
+ var accessorId = 0;
1135
+ function intersection(count, fn, getIntersection) {
1136
+ const dirtyAccessor = "?" /* Dynamic */ + accessorId++;
1137
+ const markAccessor = dirtyAccessor + "#" /* Mark */;
1138
+ let intersection2;
1139
+ return (scope, op) => {
1140
+ if (op === MARK) {
1141
+ if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1142
+ getIntersection && (intersection2 ||= getIntersection())(scope, MARK);
1185
1143
  }
1186
- for (; oldStart < oldLength; ++oldStart) {
1187
- removeAndDestroyBranch(oldBranches[oldStart]);
1144
+ } else if (scope[markAccessor] === void 0) {
1145
+ scope[markAccessor] = count - 1;
1146
+ scope[dirtyAccessor] = true;
1147
+ } else if (--scope[markAccessor] === 0) {
1148
+ if (op === DIRTY || scope[dirtyAccessor]) {
1149
+ scope[dirtyAccessor] = false;
1150
+ fn(scope);
1151
+ getIntersection && (intersection2 ||= getIntersection())(scope, DIRTY);
1152
+ } else {
1153
+ getIntersection && (intersection2 ||= getIntersection())(scope, CLEAN);
1188
1154
  }
1189
1155
  } else {
1190
- i = oldLength - synced;
1191
- while (i > 0) {
1192
- oldBranch = aNullable[oldStart++];
1193
- if (oldBranch !== null) {
1194
- removeAndDestroyBranch(oldBranch);
1195
- i--;
1196
- }
1197
- }
1198
- if (pos === WRONG_POS) {
1199
- const seq = longestIncreasingSubsequence(sources);
1200
- j = seq.length - 1;
1201
- k = newBranches.length;
1202
- for (i = newLength - 1; i >= 0; --i) {
1203
- if (sources[i] === -1) {
1204
- pos = i + newStart;
1205
- newBranch = newBranches[pos++];
1206
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1207
- insertBranchBefore(newBranch, parent, nextSibling);
1208
- } else {
1209
- if (j < 0 || i !== seq[j]) {
1210
- pos = i + newStart;
1211
- newBranch = newBranches[pos++];
1212
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1213
- insertBranchBefore(newBranch, parent, nextSibling);
1214
- } else {
1215
- --j;
1216
- }
1217
- }
1218
- }
1219
- } else if (synced !== newLength) {
1220
- k = newBranches.length;
1221
- for (i = newLength - 1; i >= 0; --i) {
1222
- if (sources[i] === -1) {
1223
- pos = i + newStart;
1224
- newBranch = newBranches[pos++];
1225
- nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1226
- insertBranchBefore(newBranch, parent, nextSibling);
1227
- }
1228
- }
1229
- }
1156
+ scope[dirtyAccessor] ||= op === DIRTY;
1230
1157
  }
1231
- }
1158
+ };
1232
1159
  }
1233
- function longestIncreasingSubsequence(a) {
1160
+ function loopClosure(valueAccessor, ownerLoopNodeAccessor, fn, getIntersection) {
1161
+ const childSignal = closure(valueAccessor, fn, getIntersection);
1162
+ const loopScopeAccessor = ownerLoopNodeAccessor + "!" /* LoopScopeArray */;
1163
+ const loopScopeMapAccessor = ownerLoopNodeAccessor + "(" /* LoopScopeMap */;
1164
+ const ownerSignal = (ownerScope) => {
1165
+ for (const scope of ownerScope[loopScopeAccessor] || ownerScope[loopScopeMapAccessor]?.values() || []) {
1166
+ if (!scope.___pending) {
1167
+ queueSource(scope, childSignal);
1168
+ }
1169
+ }
1170
+ };
1171
+ ownerSignal._ = childSignal;
1172
+ return ownerSignal;
1173
+ }
1174
+ function conditionalClosure(valueAccessor, ownerConditionalNodeAccessor, branch, fn, getIntersection) {
1175
+ const childSignal = closure(valueAccessor, fn, getIntersection);
1176
+ const scopeAccessor = ownerConditionalNodeAccessor + "!" /* ConditionalScope */;
1177
+ const branchAccessor = ownerConditionalNodeAccessor + "(" /* ConditionalRenderer */;
1178
+ const ownerSignal = (scope) => {
1179
+ const ifScope = scope[scopeAccessor];
1180
+ if (ifScope && !ifScope.___pending && scope[branchAccessor] === branch) {
1181
+ queueSource(ifScope, childSignal);
1182
+ }
1183
+ };
1184
+ ownerSignal._ = childSignal;
1185
+ return ownerSignal;
1186
+ }
1187
+ function dynamicClosure(valueAccessor, fn, getIntersection, getOwnerScope) {
1188
+ const subscribersAccessor = "?" /* Dynamic */ + accessorId++;
1189
+ const childSignal = closure(
1190
+ valueAccessor,
1191
+ fn,
1192
+ getIntersection,
1193
+ getOwnerScope
1194
+ );
1195
+ const ownerSignal = (ownerScope) => {
1196
+ const subscribers = ownerScope[subscribersAccessor];
1197
+ if (subscribers) {
1198
+ for (const subscriber of subscribers) {
1199
+ if (!subscriber.___pending) {
1200
+ queueSource(subscriber, childSignal);
1201
+ }
1202
+ }
1203
+ }
1204
+ };
1205
+ const subscribe = (scope) => {
1206
+ const owner = getOwnerScope ? getOwnerScope(scope) : scope._;
1207
+ const subscribers = owner[subscribersAccessor] ||= /* @__PURE__ */ new Set();
1208
+ if (!subscribers.has(scope)) {
1209
+ subscribers.add(scope);
1210
+ getAbortSignal(scope, -1).addEventListener(
1211
+ "abort",
1212
+ () => subscribers.delete(scope)
1213
+ );
1214
+ }
1215
+ };
1216
+ ownerSignal.___subscribe = subscribe;
1217
+ ownerSignal._ = (scope) => {
1218
+ childSignal(scope);
1219
+ subscribe(scope);
1220
+ };
1221
+ return ownerSignal;
1222
+ }
1223
+ function registerDynamicClosure(id, valueAccessor, fn, getIntersection, getOwnerScope) {
1224
+ const signal = dynamicClosure(
1225
+ valueAccessor,
1226
+ fn,
1227
+ getIntersection,
1228
+ getOwnerScope
1229
+ );
1230
+ register(id, signal.___subscribe);
1231
+ return signal;
1232
+ }
1233
+ function closure(valueAccessor, fn, getIntersection, getOwnerScope) {
1234
+ let intersection2;
1235
+ return (scope, op) => {
1236
+ op || fn && fn(
1237
+ scope,
1238
+ (getOwnerScope ? getOwnerScope(scope) : scope._)[valueAccessor]
1239
+ );
1240
+ getIntersection && (intersection2 ||= getIntersection())(scope, op ? MARK : DIRTY);
1241
+ };
1242
+ }
1243
+ function setTagVar(scope, childAccessor, tagVarSignal2) {
1244
+ scope[childAccessor]["/" /* TagVariable */] = (valueOrOp) => tagVarSignal2(scope, valueOrOp);
1245
+ }
1246
+ var tagVarSignal = (scope, valueOrOp) => scope["/" /* TagVariable */]?.(valueOrOp);
1247
+ function setTagVarChange(scope, changeHandler) {
1248
+ scope["@" /* TagVariableChange */] = changeHandler;
1249
+ }
1250
+ var tagVarSignalChange = (scope, value2) => scope["@" /* TagVariableChange */]?.(value2);
1251
+ var tagIdsByGlobal = /* @__PURE__ */ new WeakMap();
1252
+ function nextTagId({ $global }) {
1253
+ const id = tagIdsByGlobal.get($global) || 0;
1254
+ tagIdsByGlobal.set($global, id + 1);
1255
+ return "c" + $global.runtimeId + $global.renderId + id.toString(36);
1256
+ }
1257
+ function inChild(childAccessor, signal) {
1258
+ return (scope, valueOrOp) => {
1259
+ signal(scope[childAccessor], valueOrOp);
1260
+ };
1261
+ }
1262
+ function intersections(signals) {
1263
+ return (scope, op) => {
1264
+ for (const signal of signals) {
1265
+ signal(scope, op);
1266
+ }
1267
+ };
1268
+ }
1269
+ function effect(id, fn) {
1270
+ register(id, fn);
1271
+ return (scope) => {
1272
+ queueEffect(scope, fn);
1273
+ };
1274
+ }
1275
+
1276
+ // src/dom/queue.ts
1277
+ var pendingRenders = [];
1278
+ var pendingEffects = [];
1279
+ var rendering = false;
1280
+ function queueSource(scope, signal, value2) {
1281
+ const prevRendering = rendering;
1282
+ rendering = true;
1283
+ signal(scope, MARK);
1284
+ rendering = prevRendering;
1285
+ queueRender(scope, signal, value2);
1286
+ }
1287
+ function queueRender(scope, signal, value2) {
1288
+ let i = pendingRenders.length;
1289
+ const render = {
1290
+ ___scope: scope,
1291
+ ___signal: signal,
1292
+ ___value: value2,
1293
+ ___depth: scope.___closestBranch?.___branchDepth || 0,
1294
+ ___index: i
1295
+ };
1296
+ pendingRenders.push(render);
1297
+ while (i) {
1298
+ const parentIndex = i - 1 >> 1;
1299
+ const parent = pendingRenders[parentIndex];
1300
+ if (comparePendingRenders(render, parent) >= 0) break;
1301
+ pendingRenders[i] = parent;
1302
+ i = parentIndex;
1303
+ }
1304
+ pendingRenders[i] = render;
1305
+ }
1306
+ function queueEffect(scope, fn) {
1307
+ pendingEffects.push(scope, fn);
1308
+ }
1309
+ function run() {
1310
+ const effects = pendingEffects;
1311
+ try {
1312
+ rendering = true;
1313
+ runRenders();
1314
+ } finally {
1315
+ pendingRenders = [];
1316
+ pendingEffects = [];
1317
+ rendering = false;
1318
+ }
1319
+ runEffects(effects);
1320
+ }
1321
+ function prepareEffects(fn) {
1322
+ const prevRenders = pendingRenders;
1323
+ const prevEffects = pendingEffects;
1324
+ const preparedEffects = pendingEffects = [];
1325
+ pendingRenders = [];
1326
+ try {
1327
+ rendering = true;
1328
+ fn();
1329
+ runRenders();
1330
+ } finally {
1331
+ rendering = false;
1332
+ pendingRenders = prevRenders;
1333
+ pendingEffects = prevEffects;
1334
+ }
1335
+ return preparedEffects;
1336
+ }
1337
+ function runEffects(effects) {
1338
+ for (let i = 0; i < effects.length; i += 2 /* Total */) {
1339
+ const scope = effects[i];
1340
+ const fn = effects[i + 1];
1341
+ fn(scope, scope);
1342
+ }
1343
+ }
1344
+ function runRenders() {
1345
+ while (pendingRenders.length) {
1346
+ const render = pendingRenders[0];
1347
+ const next = pendingRenders.pop();
1348
+ if (render !== next) {
1349
+ let i = 0;
1350
+ const mid = pendingRenders.length >> 1;
1351
+ const item = pendingRenders[0] = next;
1352
+ while (i < mid) {
1353
+ let bestChild = (i << 1) + 1;
1354
+ const right = bestChild + 1;
1355
+ if (right < pendingRenders.length && comparePendingRenders(
1356
+ pendingRenders[right],
1357
+ pendingRenders[bestChild]
1358
+ ) < 0) {
1359
+ bestChild = right;
1360
+ }
1361
+ if (comparePendingRenders(pendingRenders[bestChild], item) >= 0) {
1362
+ break;
1363
+ } else {
1364
+ pendingRenders[i] = pendingRenders[bestChild];
1365
+ i = bestChild;
1366
+ }
1367
+ }
1368
+ pendingRenders[i] = item;
1369
+ }
1370
+ if (!render.___scope.___closestBranch?.___destroyed) {
1371
+ render.___signal(render.___scope, render.___value);
1372
+ }
1373
+ }
1374
+ finishPendingScopes();
1375
+ }
1376
+ function comparePendingRenders(a, b) {
1377
+ return a.___depth - b.___depth || a.___index - b.___index;
1378
+ }
1379
+
1380
+ // src/dom/abort-signal.ts
1381
+ function resetAbortSignal(scope, id) {
1382
+ const ctrl = scope.___abortControllers?.[id];
1383
+ if (ctrl) {
1384
+ queueEffect(ctrl, abort);
1385
+ scope.___abortControllers[id] = void 0;
1386
+ }
1387
+ }
1388
+ function getAbortSignal(scope, id) {
1389
+ if (scope.___closestBranch) {
1390
+ (scope.___closestBranch.___abortScopes ||= /* @__PURE__ */ new Set()).add(scope);
1391
+ }
1392
+ return ((scope.___abortControllers ||= {})[id] ||= new AbortController()).signal;
1393
+ }
1394
+ function abort(ctrl) {
1395
+ ctrl.abort();
1396
+ }
1397
+
1398
+ // src/common/compat-meta.ts
1399
+ var prefix = true ? "$compat_" : "$C_";
1400
+ var RENDERER_REGISTER_ID = prefix + (true ? "renderer" : "r");
1401
+ var SET_SCOPE_REGISTER_ID = prefix + (true ? "setScope" : "s");
1402
+ var RENDER_BODY_ID = prefix + (true ? "renderBody" : "b");
1403
+
1404
+ // src/dom/reconcile.ts
1405
+ var WRONG_POS = 2147483647;
1406
+ function reconcile(parent, oldBranches, newBranches, afterReference) {
1407
+ let oldStart = 0;
1408
+ let newStart = 0;
1409
+ let oldEnd = oldBranches.length - 1;
1410
+ let newEnd = newBranches.length - 1;
1411
+ let oldStartBranch = oldBranches[oldStart];
1412
+ let newStartBranch = newBranches[newStart];
1413
+ let oldEndBranch = oldBranches[oldEnd];
1414
+ let newEndBranch = newBranches[newEnd];
1415
+ let i;
1416
+ let j;
1417
+ let k;
1418
+ let nextSibling;
1419
+ let oldBranch;
1420
+ let newBranch;
1421
+ outer: {
1422
+ while (oldStartBranch === newStartBranch) {
1423
+ ++oldStart;
1424
+ ++newStart;
1425
+ if (oldStart > oldEnd || newStart > newEnd) {
1426
+ break outer;
1427
+ }
1428
+ oldStartBranch = oldBranches[oldStart];
1429
+ newStartBranch = newBranches[newStart];
1430
+ }
1431
+ while (oldEndBranch === newEndBranch) {
1432
+ --oldEnd;
1433
+ --newEnd;
1434
+ if (oldStart > oldEnd || newStart > newEnd) {
1435
+ break outer;
1436
+ }
1437
+ oldEndBranch = oldBranches[oldEnd];
1438
+ newEndBranch = newBranches[newEnd];
1439
+ }
1440
+ }
1441
+ if (oldStart > oldEnd) {
1442
+ if (newStart <= newEnd) {
1443
+ k = newEnd + 1;
1444
+ nextSibling = k < newBranches.length ? newBranches[k].___startNode : afterReference;
1445
+ do {
1446
+ insertBranchBefore(newBranches[newStart++], parent, nextSibling);
1447
+ } while (newStart <= newEnd);
1448
+ }
1449
+ } else if (newStart > newEnd) {
1450
+ do {
1451
+ removeAndDestroyBranch(oldBranches[oldStart++]);
1452
+ } while (oldStart <= oldEnd);
1453
+ } else {
1454
+ const oldLength = oldEnd - oldStart + 1;
1455
+ const newLength = newEnd - newStart + 1;
1456
+ const aNullable = oldBranches;
1457
+ const sources = new Array(newLength);
1458
+ for (i = 0; i < newLength; ++i) {
1459
+ sources[i] = -1;
1460
+ }
1461
+ let pos = 0;
1462
+ let synced = 0;
1463
+ const keyIndex = /* @__PURE__ */ new Map();
1464
+ for (j = newStart; j <= newEnd; ++j) {
1465
+ keyIndex.set(newBranches[j], j);
1466
+ }
1467
+ for (i = oldStart; i <= oldEnd && synced < newLength; ++i) {
1468
+ oldBranch = oldBranches[i];
1469
+ j = keyIndex.get(oldBranch);
1470
+ if (j !== void 0) {
1471
+ pos = pos > j ? WRONG_POS : j;
1472
+ ++synced;
1473
+ newBranch = newBranches[j];
1474
+ sources[j - newStart] = i;
1475
+ aNullable[i] = null;
1476
+ }
1477
+ }
1478
+ if (oldLength === oldBranches.length && synced === 0) {
1479
+ for (; newStart < newLength; ++newStart) {
1480
+ insertBranchBefore(newBranches[newStart], parent, afterReference);
1481
+ }
1482
+ for (; oldStart < oldLength; ++oldStart) {
1483
+ removeAndDestroyBranch(oldBranches[oldStart]);
1484
+ }
1485
+ } else {
1486
+ i = oldLength - synced;
1487
+ while (i > 0) {
1488
+ oldBranch = aNullable[oldStart++];
1489
+ if (oldBranch !== null) {
1490
+ removeAndDestroyBranch(oldBranch);
1491
+ i--;
1492
+ }
1493
+ }
1494
+ if (pos === WRONG_POS) {
1495
+ const seq = longestIncreasingSubsequence(sources);
1496
+ j = seq.length - 1;
1497
+ k = newBranches.length;
1498
+ for (i = newLength - 1; i >= 0; --i) {
1499
+ if (sources[i] === -1) {
1500
+ pos = i + newStart;
1501
+ newBranch = newBranches[pos++];
1502
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1503
+ insertBranchBefore(newBranch, parent, nextSibling);
1504
+ } else {
1505
+ if (j < 0 || i !== seq[j]) {
1506
+ pos = i + newStart;
1507
+ newBranch = newBranches[pos++];
1508
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1509
+ insertBranchBefore(newBranch, parent, nextSibling);
1510
+ } else {
1511
+ --j;
1512
+ }
1513
+ }
1514
+ }
1515
+ } else if (synced !== newLength) {
1516
+ k = newBranches.length;
1517
+ for (i = newLength - 1; i >= 0; --i) {
1518
+ if (sources[i] === -1) {
1519
+ pos = i + newStart;
1520
+ newBranch = newBranches[pos++];
1521
+ nextSibling = pos < k ? newBranches[pos].___startNode : afterReference;
1522
+ insertBranchBefore(newBranch, parent, nextSibling);
1523
+ }
1524
+ }
1525
+ }
1526
+ }
1527
+ }
1528
+ }
1529
+ function longestIncreasingSubsequence(a) {
1234
1530
  const p = a.slice();
1235
- const result = [];
1236
- result.push(0);
1531
+ const result = [0];
1237
1532
  let u;
1238
1533
  let v;
1239
1534
  for (let i = 0, il = a.length; i < il; ++i) {
@@ -1281,55 +1576,54 @@ function trimWalkString(walkString) {
1281
1576
  }
1282
1577
  function walk(startNode, walkCodes, branch) {
1283
1578
  walker.currentNode = startNode;
1284
- walkInternal(walkCodes, branch, 0);
1285
- walker.currentNode = document;
1579
+ walkInternal(0, walkCodes, branch);
1286
1580
  }
1287
- function walkInternal(walkCodes, scope, currentWalkIndex) {
1581
+ function walkInternal(currentWalkIndex, walkCodes, scope) {
1288
1582
  let value2;
1289
1583
  let storedMultiplier = 0;
1290
1584
  let currentMultiplier = 0;
1291
1585
  let currentScopeIndex = 0;
1292
- while (value2 = walkCodes.charCodeAt(currentWalkIndex++)) {
1586
+ for (; currentWalkIndex < walkCodes.length; ) {
1587
+ value2 = walkCodes.charCodeAt(currentWalkIndex++);
1293
1588
  currentMultiplier = storedMultiplier;
1294
1589
  storedMultiplier = 0;
1295
- if (value2 >= 117 /* Multiplier */) {
1296
- storedMultiplier = currentMultiplier * 10 /* Multiplier */ + value2 - 117 /* Multiplier */;
1297
- } else if (value2 >= 107 /* Out */) {
1298
- value2 = 10 /* Out */ * currentMultiplier + value2 - 107 /* Out */;
1590
+ if (value2 === 32 /* Get */) {
1591
+ scope[true ? getDebugKey(currentScopeIndex++, walker.currentNode) : currentScopeIndex++] = walker.currentNode;
1592
+ } else if (value2 === 37 /* Replace */) {
1593
+ walker.currentNode.replaceWith(
1594
+ walker.currentNode = scope[true ? getDebugKey(currentScopeIndex++, "#text") : currentScopeIndex++] = new Text()
1595
+ );
1596
+ } else if (value2 === 38 /* EndChild */) {
1597
+ return currentWalkIndex;
1598
+ } else if (value2 === 47 /* BeginChild */) {
1599
+ currentWalkIndex = walkInternal(
1600
+ currentWalkIndex,
1601
+ walkCodes,
1602
+ scope[true ? getDebugKey(currentScopeIndex++, "#childScope") : currentScopeIndex++] = createScope(scope.$global, scope.___closestBranch)
1603
+ );
1604
+ } else if (value2 < 91 /* NextEnd */ + 1) {
1605
+ value2 = 20 /* Next */ * currentMultiplier + value2 - 67 /* Next */;
1299
1606
  while (value2--) {
1300
- walker.parentNode();
1607
+ walker.nextNode();
1301
1608
  }
1302
- walker.nextSibling();
1303
- } else if (value2 >= 97 /* Over */) {
1609
+ } else if (value2 < 106 /* OverEnd */ + 1) {
1304
1610
  value2 = 10 /* Over */ * currentMultiplier + value2 - 97 /* Over */;
1305
1611
  while (value2--) {
1306
1612
  walker.nextSibling();
1307
1613
  }
1308
- } else if (value2 >= 67 /* Next */) {
1309
- value2 = 20 /* Next */ * currentMultiplier + value2 - 67 /* Next */;
1614
+ } else if (value2 < 116 /* OutEnd */ + 1) {
1615
+ value2 = 10 /* Out */ * currentMultiplier + value2 - 107 /* Out */;
1310
1616
  while (value2--) {
1311
- walker.nextNode();
1617
+ walker.parentNode();
1312
1618
  }
1313
- } else if (value2 === 47 /* BeginChild */) {
1314
- const childScope = scope[true ? getDebugKey(currentScopeIndex++, "#childScope") : currentScopeIndex++] = createScope(scope.$global);
1315
- childScope.___closestBranch = scope.___closestBranch;
1316
- currentWalkIndex = walkInternal(walkCodes, childScope, currentWalkIndex);
1317
- } else if (value2 === 38 /* EndChild */) {
1318
- return currentWalkIndex;
1319
- } else if (value2 === 32 /* Get */) {
1320
- scope[true ? getDebugKey(currentScopeIndex++, walker.currentNode) : currentScopeIndex++] = walker.currentNode;
1619
+ walker.nextSibling();
1321
1620
  } else {
1322
- const newNode = scope[true ? getDebugKey(currentScopeIndex++, "#text") : currentScopeIndex++] = new Text();
1323
- const current = walker.currentNode;
1324
- const parentNode = current.parentNode;
1325
- if (value2 !== 37 /* Replace */) {
1621
+ if (value2 < 117 /* Multiplier */ || value2 > 126 /* MultiplierEnd */) {
1326
1622
  throw new Error(`Unknown walk code: ${value2}`);
1327
1623
  }
1328
- parentNode.replaceChild(newNode, current);
1329
- walker.currentNode = newNode;
1624
+ storedMultiplier = currentMultiplier * 10 /* Multiplier */ + value2 - 117 /* Multiplier */;
1330
1625
  }
1331
1626
  }
1332
- return currentWalkIndex;
1333
1627
  }
1334
1628
  function getDebugKey(index, node) {
1335
1629
  if (typeof node === "string") {
@@ -1390,9 +1684,14 @@ function createBranch($global, ownerScope, parentScope) {
1390
1684
  function initBranch(renderer, branch, parentNode) {
1391
1685
  const clone = renderer.___clone(parentNode.namespaceURI);
1392
1686
  const cloneParent = clone.parentNode;
1393
- walk(cloneParent?.firstChild || clone, renderer.___walks, branch);
1394
- branch.___startNode = cloneParent?.firstChild || clone;
1395
- branch.___endNode = cloneParent?.lastChild || clone;
1687
+ if (cloneParent) {
1688
+ walk(cloneParent.firstChild, renderer.___walks, branch);
1689
+ branch.___startNode = cloneParent.firstChild;
1690
+ branch.___endNode = cloneParent.lastChild;
1691
+ } else {
1692
+ walk(clone, renderer.___walks, branch);
1693
+ branch.___startNode = branch.___endNode = clone;
1694
+ }
1396
1695
  if (renderer.___setup) {
1397
1696
  queueRender(branch, renderer.___setup);
1398
1697
  }
@@ -1415,7 +1714,7 @@ function dynamicTagAttrs(nodeAccessor, getContent, inputIsArgs) {
1415
1714
  "A dynamic tag rendering a `<textarea>` cannot have `content` and must use the `value` attribute instead."
1416
1715
  );
1417
1716
  }
1418
- setConditionalRendererOnlyChild(
1717
+ setConditionalRenderer(
1419
1718
  childScope,
1420
1719
  nodeAccessor2,
1421
1720
  content,
@@ -1436,541 +1735,197 @@ function dynamicTagAttrs(nodeAccessor, getContent, inputIsArgs) {
1436
1735
  }
1437
1736
  };
1438
1737
  }
1439
- function createRendererWithOwner(template, rawWalks, setup, getArgs) {
1440
- let args;
1441
- const id = true ? Symbol("Marko Renderer") : {};
1442
- const walks = rawWalks ? /* @__PURE__ */ trimWalkString(rawWalks) : " ";
1443
- return (owner) => {
1444
- return {
1445
- ___id: id,
1446
- ___template: template,
1447
- ___walks: walks,
1448
- ___setup: setup,
1449
- ___clone: _clone,
1450
- ___owner: owner,
1451
- get ___args() {
1452
- return args ||= getArgs?.();
1453
- }
1454
- };
1455
- };
1456
- }
1457
- function createRenderer(template, walks, setup, getArgs) {
1458
- return createRendererWithOwner(template, walks, setup, getArgs)();
1459
- }
1460
- function _clone(ns) {
1461
- return ((cloneCache[ns] ||= {})[this.___template] ||= createCloneableHTML(
1462
- this.___template,
1463
- ns
1464
- ))();
1465
- }
1466
- var cloneCache = {};
1467
- function createCloneableHTML(html2, ns) {
1468
- const { firstChild, lastChild } = parseHTML(html2, ns);
1469
- const parent = document.createElementNS(ns, "t");
1470
- insertChildNodes(parent, null, firstChild, lastChild);
1471
- return firstChild === lastChild && firstChild.nodeType < 8 /* Comment */ ? () => firstChild.cloneNode(true) : () => parent.cloneNode(true).firstChild;
1472
- }
1473
-
1474
- // src/dom/control-flow.ts
1475
- function conditional(nodeAccessor, ...branches) {
1476
- const branchAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1477
- return (scope, newBranchIndexOrOp) => {
1478
- if (newBranchIndexOrOp !== scope[branchAccessor] && newBranchIndexOrOp !== DIRTY && newBranchIndexOrOp !== MARK && newBranchIndexOrOp !== CLEAN) {
1479
- (scope[nodeAccessor].nodeType > 1 /* Element */ ? setConditionalRenderer : setConditionalRendererOnlyChild)(
1480
- scope,
1481
- nodeAccessor,
1482
- branches[scope[branchAccessor] = newBranchIndexOrOp],
1483
- createBranchScopeWithRenderer
1484
- );
1485
- }
1486
- };
1487
- }
1488
- function patchDynamicTag(fn) {
1489
- dynamicTag = fn(dynamicTag);
1490
- }
1491
- var dynamicTag = function dynamicTag2(nodeAccessor, fn, getIntersection) {
1492
- const rendererAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1493
- let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1494
- return (scope, newRendererOrOp) => {
1495
- if (newRendererOrOp === DIRTY) return;
1496
- const currentRenderer = scope[rendererAccessor];
1497
- let op = newRendererOrOp;
1498
- if (newRendererOrOp !== MARK && newRendererOrOp !== CLEAN) {
1499
- const normalizedRenderer = normalizeDynamicRenderer(newRendererOrOp);
1500
- if (isDifferentRenderer(normalizedRenderer, currentRenderer)) {
1501
- scope[rendererAccessor] = normalizedRenderer;
1502
- setConditionalRenderer(
1503
- scope,
1504
- nodeAccessor,
1505
- normalizedRenderer,
1506
- createBranchScopeWithTagNameOrRenderer
1507
- );
1508
- fn && fn(scope);
1509
- op = DIRTY;
1510
- } else {
1511
- op = CLEAN;
1512
- }
1513
- }
1514
- intersection2?.(scope, op);
1515
- };
1516
- };
1517
- function setConditionalRenderer(scope, nodeAccessor, newRenderer, createBranch2) {
1518
- const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */] || getEmptyBranch(scope[nodeAccessor]);
1519
- const newBranch = newRenderer ? createBranch2(
1520
- newRenderer,
1521
- scope.$global,
1522
- scope,
1523
- prevBranch.___endNode.parentNode
1524
- ) : getEmptyBranch(scope[nodeAccessor]);
1525
- if (prevBranch !== newBranch) {
1526
- insertBranchBefore(
1527
- newBranch,
1528
- prevBranch.___endNode.parentNode,
1529
- prevBranch.___endNode.nextSibling
1530
- );
1531
- removeAndDestroyBranch(prevBranch);
1532
- scope[nodeAccessor + "!" /* ConditionalScope */] = newRenderer && newBranch;
1533
- }
1534
- }
1535
- function setConditionalRendererOnlyChild(scope, nodeAccessor, newRenderer, createBranch2) {
1536
- const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */];
1537
- const referenceNode = scope[nodeAccessor];
1538
- const newBranch = newRenderer && createBranch2(newRenderer, scope.$global, scope, referenceNode);
1539
- referenceNode.textContent = "";
1540
- if (newBranch) {
1541
- insertBranchBefore(newBranch, referenceNode, null);
1542
- }
1543
- prevBranch && destroyBranch(prevBranch);
1544
- scope[nodeAccessor + "!" /* ConditionalScope */] = newBranch;
1545
- }
1546
- var emptyMarkerMap = /* @__PURE__ */ new Map([
1547
- [Symbol(), /* @__PURE__ */ getEmptyBranch(0)]
1548
- ]);
1549
- var emptyMarkerArray = [
1550
- /* @__PURE__ */ getEmptyBranch(0)
1551
- ];
1552
- var emptyMap = /* @__PURE__ */ new Map();
1553
- var emptyArray = [];
1554
- function loopOf(nodeAccessor, renderer) {
1555
- return loop(
1556
- nodeAccessor,
1557
- renderer,
1558
- ([all, by = bySecondArg], cb) => {
1559
- if (typeof by === "string") {
1560
- forOf(
1561
- all,
1562
- (item, i) => cb(item[by], [item, i])
1563
- );
1564
- } else {
1565
- forOf(all, (item, i) => cb(by(item, i), [item, i]));
1566
- }
1567
- }
1568
- );
1569
- }
1570
- function loopIn(nodeAccessor, renderer) {
1571
- return loop(
1572
- nodeAccessor,
1573
- renderer,
1574
- ([obj, by = byFirstArg], cb) => forIn(obj, (key, value2) => cb(by(key, value2), [key, value2]))
1575
- );
1576
- }
1577
- function loopTo(nodeAccessor, renderer) {
1578
- return loop(
1579
- nodeAccessor,
1580
- renderer,
1581
- ([to, from, step, by = byFirstArg], cb) => forTo(to, from, step, (v) => cb(by(v), [v]))
1582
- );
1583
- }
1584
- function loop(nodeAccessor, renderer, forEach) {
1585
- const loopScopeAccessor = nodeAccessor + "!" /* LoopScopeArray */;
1586
- const params = renderer.___args;
1587
- return (scope, valueOrOp) => {
1588
- if (valueOrOp === DIRTY) return;
1589
- if (valueOrOp === MARK || valueOrOp === CLEAN) {
1590
- const loopBranches = scope[loopScopeAccessor] ?? scope[nodeAccessor + "(" /* LoopScopeMap */]?.values() ?? [];
1591
- if (loopBranches !== emptyMarkerArray) {
1592
- for (const branch of loopBranches) {
1593
- params?.(branch, valueOrOp);
1594
- }
1595
- }
1596
- return;
1597
- }
1598
- const referenceNode = scope[nodeAccessor];
1599
- const referenceIsMarker = referenceNode.nodeType > 1 /* Element */;
1600
- const oldMap = scope[nodeAccessor + "(" /* LoopScopeMap */] || (referenceIsMarker ? emptyMarkerMap : emptyMap);
1601
- const oldArray = scope[nodeAccessor + "!" /* LoopScopeArray */] || Array.from(oldMap.values());
1602
- const parentNode = referenceIsMarker ? referenceNode.parentNode || oldArray[0].___startNode.parentNode : referenceNode;
1603
- let newMap;
1604
- let newArray;
1605
- let afterReference;
1606
- let needsReconciliation = true;
1607
- forEach(valueOrOp, (key, args) => {
1608
- let branch = oldMap.get(key);
1609
- if (!branch) {
1610
- branch = createBranchScopeWithRenderer(
1611
- renderer,
1612
- scope.$global,
1613
- scope,
1614
- parentNode
1615
- );
1616
- } else {
1617
- }
1618
- if (params) {
1619
- params(branch, args);
1620
- }
1621
- if (newMap) {
1622
- newMap.set(key, branch);
1623
- newArray.push(branch);
1624
- } else {
1625
- newMap = /* @__PURE__ */ new Map([[key, branch]]);
1626
- newArray = [branch];
1627
- }
1628
- });
1629
- if (!newMap) {
1630
- if (referenceIsMarker) {
1631
- newMap = emptyMarkerMap;
1632
- newArray = emptyMarkerArray;
1633
- getEmptyBranch(referenceNode);
1634
- } else {
1635
- oldArray.forEach(destroyBranch);
1636
- referenceNode.textContent = "";
1637
- newMap = emptyMap;
1638
- newArray = emptyArray;
1639
- needsReconciliation = false;
1640
- }
1641
- }
1642
- if (needsReconciliation) {
1643
- if (referenceIsMarker) {
1644
- if (oldMap === emptyMarkerMap) {
1645
- getEmptyBranch(referenceNode);
1646
- }
1647
- const oldLastChild = oldArray[oldArray.length - 1];
1648
- afterReference = oldLastChild.___endNode.nextSibling;
1649
- } else {
1650
- afterReference = null;
1651
- }
1652
- reconcile(parentNode, oldArray, newArray, afterReference);
1653
- }
1654
- scope[nodeAccessor + "(" /* LoopScopeMap */] = newMap;
1655
- scope[nodeAccessor + "!" /* LoopScopeArray */] = newArray;
1656
- };
1657
- }
1658
- function bySecondArg(_item, index) {
1659
- return index;
1660
- }
1661
- function byFirstArg(name) {
1662
- return name;
1663
- }
1664
- function isDifferentRenderer(a, b) {
1665
- return a !== b && (a?.___id || 0) !== b?.___id;
1666
- }
1667
-
1668
- // src/dom/signals.ts
1669
- var MARK = true ? Symbol("mark") : {};
1670
- var CLEAN = true ? Symbol("clean") : {};
1671
- var DIRTY = true ? Symbol("dirty") : {};
1672
- function state(valueAccessor, fn, getIntersection) {
1673
- const valueSignal = value(valueAccessor, fn, getIntersection);
1674
- const markAccessor = valueAccessor + "#" /* Mark */;
1675
- const valueChangeAccessor = valueAccessor + "@" /* TagVariableChange */;
1676
- return (scope, valueOrOp, valueChange) => {
1677
- if (rendering) {
1678
- const valueIsOp = valueOrOp === MARK || valueOrOp === CLEAN || valueOrOp === DIRTY;
1679
- valueSignal(
1680
- scope,
1681
- valueIsOp || (scope[valueChangeAccessor] = valueChange) || scope[markAccessor] === void 0 ? valueOrOp : CLEAN
1682
- );
1683
- } else if (scope[valueChangeAccessor]) {
1684
- scope[valueChangeAccessor](valueOrOp);
1685
- } else {
1686
- queueSource(scope, valueSignal, valueOrOp);
1687
- }
1688
- return valueOrOp;
1689
- };
1690
- }
1691
- function value(valueAccessor, fn, getIntersection) {
1692
- const markAccessor = valueAccessor + "#" /* Mark */;
1693
- let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1694
- return (scope, valueOrOp) => {
1695
- if (valueOrOp === MARK) {
1696
- if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1697
- intersection2?.(scope, MARK);
1698
- }
1699
- } else if (valueOrOp !== DIRTY) {
1700
- const existing = scope[markAccessor] !== void 0;
1701
- if ((scope[markAccessor] ||= 1) === 1) {
1702
- if (valueOrOp === CLEAN || existing && scope[valueAccessor] === valueOrOp) {
1703
- intersection2?.(scope, CLEAN);
1704
- } else {
1705
- scope[valueAccessor] = valueOrOp;
1706
- fn && fn(scope, valueOrOp);
1707
- intersection2?.(scope, DIRTY);
1708
- }
1709
- }
1710
- scope[markAccessor]--;
1711
- }
1712
- };
1713
- }
1714
- var accessorId = 0;
1715
- function intersection(count, fn, getIntersection) {
1716
- const dirtyAccessor = "?" /* Dynamic */ + accessorId++;
1717
- const markAccessor = dirtyAccessor + "#" /* Mark */;
1718
- let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1719
- return (scope, op) => {
1720
- if (op === MARK) {
1721
- if ((scope[markAccessor] = (scope[markAccessor] ?? 0) + 1) === 1) {
1722
- intersection2?.(scope, MARK);
1723
- }
1724
- } else if (scope[markAccessor] === void 0) {
1725
- scope[markAccessor] = count - 1;
1726
- scope[dirtyAccessor] = true;
1727
- } else if (--scope[markAccessor] === 0) {
1728
- if (op === DIRTY || scope[dirtyAccessor]) {
1729
- scope[dirtyAccessor] = false;
1730
- fn(scope, 0);
1731
- intersection2?.(scope, DIRTY);
1732
- } else {
1733
- intersection2?.(scope, CLEAN);
1734
- }
1735
- } else {
1736
- scope[dirtyAccessor] ||= op === DIRTY;
1737
- }
1738
- };
1739
- }
1740
- function closure(fn, getIntersection) {
1741
- let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1742
- return (scope, valueOrOp) => {
1743
- if (valueOrOp === MARK) {
1744
- intersection2?.(scope, MARK);
1745
- } else {
1746
- fn && fn(scope, valueOrOp);
1747
- intersection2?.(scope, DIRTY);
1748
- }
1749
- };
1750
- }
1751
- function loopClosure(ownerLoopNodeAccessor, fn, getIntersection) {
1752
- const signal = closure(fn, getIntersection);
1753
- const loopScopeAccessor = ownerLoopNodeAccessor + "!" /* LoopScopeArray */;
1754
- const loopScopeMapAccessor = ownerLoopNodeAccessor + "(" /* LoopScopeMap */;
1755
- const helperSignal = (ownerScope, value2) => {
1756
- const loopScopes = ownerScope[loopScopeAccessor] ?? ownerScope[loopScopeMapAccessor]?.values() ?? [];
1757
- if (loopScopes !== emptyMarkerArray) {
1758
- for (const scope of loopScopes) {
1759
- if (!scope.___pending) {
1760
- queueSource(scope, signal, value2);
1761
- }
1762
- }
1763
- }
1764
- };
1765
- helperSignal._ = signal;
1766
- return helperSignal;
1767
- }
1768
- function conditionalClosure(ownerConditionalNodeAccessor, branch, fn, getIntersection) {
1769
- const signal = closure(fn, getIntersection);
1770
- const scopeAccessor = ownerConditionalNodeAccessor + "!" /* ConditionalScope */;
1771
- const branchAccessor = ownerConditionalNodeAccessor + "(" /* ConditionalRenderer */;
1772
- const helperSignal = (scope, value2) => {
1773
- const ifScope = scope[scopeAccessor];
1774
- if (ifScope && !ifScope.___pending && scope[branchAccessor] === branch) {
1775
- queueSource(ifScope, signal, value2);
1776
- }
1777
- };
1778
- helperSignal._ = signal;
1779
- return helperSignal;
1780
- }
1781
- var defaultGetOwnerScope = (scope) => scope._;
1782
- function dynamicClosure(fn, getOwnerScope = defaultGetOwnerScope, getIntersection) {
1783
- const ownerSubscribersAccessor = "?" /* Dynamic */ + accessorId++;
1784
- const _signal = closure(fn, getIntersection);
1785
- const helperSignal = (ownerScope, value2) => {
1786
- const subscribers = ownerScope[ownerSubscribersAccessor];
1787
- if (subscribers) {
1788
- for (const subscriber of subscribers) {
1789
- if (!subscriber.___pending) {
1790
- queueSource(subscriber, _signal, value2);
1791
- }
1792
- }
1793
- }
1794
- };
1795
- const setupSignal = (scope, value2) => {
1796
- _signal(scope, value2);
1797
- subscribe(scope);
1798
- };
1799
- const subscribe = (scope) => {
1800
- (getOwnerScope(scope)[ownerSubscribersAccessor] ||= /* @__PURE__ */ new Set()).add(scope);
1801
- getAbortSignal(scope, -1).addEventListener("abort", () => {
1802
- getOwnerScope(scope)[ownerSubscribersAccessor].delete(scope);
1803
- });
1804
- };
1805
- helperSignal._ = setupSignal;
1806
- helperSignal.___subscribe = subscribe;
1807
- return helperSignal;
1808
- }
1809
- function setTagVar(scope, childAccessor, tagVarSignal2) {
1810
- scope[childAccessor]["/" /* TagVariable */] = (valueOrOp) => tagVarSignal2(scope, valueOrOp);
1738
+ function createRendererWithOwner(template, rawWalks, setup, getArgs) {
1739
+ let args;
1740
+ const id = true ? Symbol("Marko Renderer") : {};
1741
+ const walks = rawWalks ? /* @__PURE__ */ trimWalkString(rawWalks) : " ";
1742
+ return (owner) => {
1743
+ return {
1744
+ ___id: id,
1745
+ ___template: template,
1746
+ ___walks: walks,
1747
+ ___setup: setup,
1748
+ ___clone: _clone,
1749
+ ___owner: owner,
1750
+ get ___args() {
1751
+ return args ||= getArgs?.();
1752
+ }
1753
+ };
1754
+ };
1811
1755
  }
1812
- var tagVarSignal = (scope, valueOrOp) => scope["/" /* TagVariable */]?.(valueOrOp);
1813
- function setTagVarChange(scope, changeHandler) {
1814
- scope["@" /* TagVariableChange */] = changeHandler;
1756
+ function createRenderer(template, walks, setup, getArgs) {
1757
+ return createRendererWithOwner(template, walks, setup, getArgs)();
1815
1758
  }
1816
- var tagVarSignalChange = (scope, value2) => scope["@" /* TagVariableChange */]?.(value2);
1817
- var tagIdsByGlobal = /* @__PURE__ */ new WeakMap();
1818
- function nextTagId({ $global }) {
1819
- const id = tagIdsByGlobal.get($global) || 0;
1820
- tagIdsByGlobal.set($global, id + 1);
1821
- return "c" + $global.runtimeId + $global.renderId + id.toString(36);
1759
+ function _clone(ns) {
1760
+ return ((cloneCache[ns] ||= {})[this.___template] ||= createCloneableHTML(
1761
+ this.___template,
1762
+ ns
1763
+ ))();
1822
1764
  }
1823
- function inChild(childAccessor, signal) {
1824
- return (scope, valueOrOp) => {
1825
- signal(scope[childAccessor], valueOrOp);
1826
- };
1765
+ var cloneCache = {};
1766
+ function createCloneableHTML(html2, ns) {
1767
+ const { firstChild, lastChild } = parseHTML(html2, ns);
1768
+ const parent = document.createElementNS(ns, "t");
1769
+ insertChildNodes(parent, null, firstChild, lastChild);
1770
+ return firstChild === lastChild && firstChild.nodeType < 8 /* Comment */ ? () => firstChild.cloneNode(true) : () => parent.cloneNode(true).firstChild;
1827
1771
  }
1828
- function intersections(signals) {
1829
- return (scope, op) => {
1830
- for (const signal of signals) {
1831
- signal(scope, op);
1772
+
1773
+ // src/dom/control-flow.ts
1774
+ function conditional(nodeAccessor, ...branches) {
1775
+ const branchAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1776
+ return (scope, newBranchIndexOrOp) => {
1777
+ if (newBranchIndexOrOp !== scope[branchAccessor] && newBranchIndexOrOp !== DIRTY && newBranchIndexOrOp !== MARK && newBranchIndexOrOp !== CLEAN) {
1778
+ setConditionalRenderer(
1779
+ scope,
1780
+ nodeAccessor,
1781
+ branches[scope[branchAccessor] = newBranchIndexOrOp],
1782
+ createBranchScopeWithRenderer
1783
+ );
1832
1784
  }
1833
1785
  };
1834
1786
  }
1835
- function effect(id, fn) {
1836
- register(id, fn);
1837
- return (scope) => {
1838
- queueEffect(scope, fn);
1839
- };
1840
- }
1841
-
1842
- // src/dom/queue.ts
1843
- var pendingRenders = [];
1844
- var pendingEffects = [];
1845
- var rendering = false;
1846
- function queueSource(scope, signal, value2) {
1847
- schedule();
1848
- const prevRendering = rendering;
1849
- rendering = true;
1850
- signal(scope, MARK);
1851
- rendering = prevRendering;
1852
- queueRender(scope, signal, value2);
1853
- return value2;
1787
+ function patchDynamicTag(fn) {
1788
+ dynamicTag = fn(dynamicTag);
1854
1789
  }
1855
- function queueRender(scope, signal, value2) {
1856
- let i = pendingRenders.length;
1857
- const render = {
1858
- ___scope: scope,
1859
- ___signal: signal,
1860
- ___value: value2,
1861
- ___index: i
1790
+ var dynamicTag = function dynamicTag2(nodeAccessor, fn, getIntersection) {
1791
+ const rendererAccessor = nodeAccessor + "(" /* ConditionalRenderer */;
1792
+ let intersection2 = getIntersection && ((scope, op) => (intersection2 = getIntersection())(scope, op));
1793
+ return (scope, newRendererOrOp) => {
1794
+ if (newRendererOrOp === DIRTY) return;
1795
+ const currentRenderer = scope[rendererAccessor];
1796
+ let op = newRendererOrOp;
1797
+ if (newRendererOrOp !== MARK && newRendererOrOp !== CLEAN) {
1798
+ if (isDifferentRenderer(
1799
+ currentRenderer,
1800
+ scope[rendererAccessor] = normalizeDynamicRenderer(newRendererOrOp)
1801
+ )) {
1802
+ setConditionalRenderer(
1803
+ scope,
1804
+ nodeAccessor,
1805
+ scope[rendererAccessor],
1806
+ createBranchScopeWithTagNameOrRenderer
1807
+ );
1808
+ fn && fn(scope);
1809
+ op = DIRTY;
1810
+ } else {
1811
+ op = CLEAN;
1812
+ }
1813
+ }
1814
+ intersection2?.(scope, op);
1862
1815
  };
1863
- pendingRenders.push(render);
1864
- while (i) {
1865
- const parentIndex = i - 1 >> 1;
1866
- const parent = pendingRenders[parentIndex];
1867
- if (comparePendingRenders(render, parent) >= 0) break;
1868
- pendingRenders[i] = parent;
1869
- i = parentIndex;
1816
+ };
1817
+ function setConditionalRenderer(scope, nodeAccessor, newRenderer, createBranch2) {
1818
+ const referenceNode = scope[nodeAccessor];
1819
+ const prevBranch = scope[nodeAccessor + "!" /* ConditionalScope */];
1820
+ const parentNode = referenceNode.nodeType > 1 /* Element */ ? (prevBranch?.___startNode || referenceNode).parentNode : referenceNode;
1821
+ const newBranch = scope[nodeAccessor + "!" /* ConditionalScope */] = newRenderer && createBranch2(newRenderer, scope.$global, scope, parentNode);
1822
+ if (referenceNode === parentNode) {
1823
+ if (prevBranch) {
1824
+ destroyBranch(prevBranch);
1825
+ referenceNode.textContent = "";
1826
+ }
1827
+ if (newBranch) {
1828
+ insertBranchBefore(newBranch, parentNode, null);
1829
+ }
1830
+ } else if (prevBranch) {
1831
+ if (newBranch) {
1832
+ insertBranchBefore(newBranch, parentNode, prevBranch.___startNode);
1833
+ } else {
1834
+ parentNode.insertBefore(referenceNode, prevBranch.___startNode);
1835
+ }
1836
+ removeAndDestroyBranch(prevBranch);
1837
+ } else if (newBranch) {
1838
+ insertBranchBefore(newBranch, parentNode, referenceNode);
1839
+ referenceNode.remove();
1870
1840
  }
1871
- pendingRenders[i] = render;
1872
- }
1873
- function queueEffect(scope, fn) {
1874
- pendingEffects.push(scope, fn);
1875
1841
  }
1876
- function run() {
1877
- const effects = pendingEffects;
1878
- try {
1879
- rendering = true;
1880
- runRenders();
1881
- } finally {
1882
- pendingRenders = [];
1883
- pendingEffects = [];
1884
- rendering = false;
1885
- }
1886
- runEffects(effects);
1842
+ function loopOf(nodeAccessor, renderer) {
1843
+ return loop(
1844
+ nodeAccessor,
1845
+ renderer,
1846
+ ([all, by = bySecondArg], cb) => {
1847
+ if (typeof by === "string") {
1848
+ forOf(
1849
+ all,
1850
+ (item, i) => cb(item[by], [item, i])
1851
+ );
1852
+ } else {
1853
+ forOf(all, (item, i) => cb(by(item, i), [item, i]));
1854
+ }
1855
+ }
1856
+ );
1887
1857
  }
1888
- function prepareEffects(fn) {
1889
- const prevRenders = pendingRenders;
1890
- const prevEffects = pendingEffects;
1891
- const preparedEffects = pendingEffects = [];
1892
- pendingRenders = [];
1893
- try {
1894
- rendering = true;
1895
- fn();
1896
- runRenders();
1897
- } finally {
1898
- rendering = false;
1899
- pendingRenders = prevRenders;
1900
- pendingEffects = prevEffects;
1901
- }
1902
- return preparedEffects;
1858
+ function loopIn(nodeAccessor, renderer) {
1859
+ return loop(
1860
+ nodeAccessor,
1861
+ renderer,
1862
+ ([obj, by = byFirstArg], cb) => forIn(obj, (key, value2) => cb(by(key, value2), [key, value2]))
1863
+ );
1903
1864
  }
1904
- function runEffects(effects = pendingEffects) {
1905
- for (let i = 0; i < effects.length; i += 2 /* Total */) {
1906
- const scope = effects[i];
1907
- const fn = effects[i + 1];
1908
- fn(scope, scope);
1909
- }
1865
+ function loopTo(nodeAccessor, renderer) {
1866
+ return loop(
1867
+ nodeAccessor,
1868
+ renderer,
1869
+ ([to, from, step, by = byFirstArg], cb) => forTo(to, from, step, (v) => cb(by(v), [v]))
1870
+ );
1910
1871
  }
1911
- function runRenders() {
1912
- while (pendingRenders.length) {
1913
- const render = pendingRenders[0];
1914
- const next = pendingRenders.pop();
1915
- if (render !== next) {
1916
- let i = 0;
1917
- const mid = pendingRenders.length >> 1;
1918
- const item = pendingRenders[0] = next;
1919
- while (i < mid) {
1920
- let bestChild = (i << 1) + 1;
1921
- const right = bestChild + 1;
1922
- if (right < pendingRenders.length && comparePendingRenders(
1923
- pendingRenders[right],
1924
- pendingRenders[bestChild]
1925
- ) < 0) {
1926
- bestChild = right;
1872
+ function loop(nodeAccessor, renderer, forEach) {
1873
+ const loopScopeAccessor = nodeAccessor + "!" /* LoopScopeArray */;
1874
+ const params = renderer.___args;
1875
+ return (scope, valueOrOp) => {
1876
+ if (valueOrOp === DIRTY) {
1877
+ } else if (valueOrOp === MARK || valueOrOp === CLEAN) {
1878
+ if (params) {
1879
+ for (const branch of scope[loopScopeAccessor] || scope[nodeAccessor + "(" /* LoopScopeMap */]?.values() || []) {
1880
+ params(branch, valueOrOp);
1927
1881
  }
1928
- if (comparePendingRenders(pendingRenders[bestChild], item) >= 0) {
1929
- break;
1930
- } else {
1931
- pendingRenders[i] = pendingRenders[bestChild];
1932
- i = bestChild;
1882
+ }
1883
+ } else {
1884
+ const referenceNode = scope[nodeAccessor];
1885
+ const oldMap = scope[nodeAccessor + "(" /* LoopScopeMap */];
1886
+ const oldArray = oldMap ? scope[nodeAccessor + "!" /* LoopScopeArray */] || [
1887
+ ...oldMap.values()
1888
+ ] : [];
1889
+ const parentNode = referenceNode.nodeType > 1 /* Element */ ? referenceNode.parentNode || oldArray[0].___startNode.parentNode : referenceNode;
1890
+ const newMap = scope[nodeAccessor + "(" /* LoopScopeMap */] = /* @__PURE__ */ new Map();
1891
+ const newArray = scope[nodeAccessor + "!" /* LoopScopeArray */] = [];
1892
+ forEach(valueOrOp, (key, args) => {
1893
+ const branch = oldMap?.get(key) || createBranchScopeWithRenderer(
1894
+ renderer,
1895
+ scope.$global,
1896
+ scope,
1897
+ parentNode
1898
+ );
1899
+ params?.(branch, args);
1900
+ newMap.set(key, branch);
1901
+ newArray.push(branch);
1902
+ });
1903
+ let afterReference = null;
1904
+ if (referenceNode !== parentNode) {
1905
+ if (oldArray.length) {
1906
+ afterReference = oldArray[oldArray.length - 1].___endNode.nextSibling;
1907
+ if (!newArray.length) {
1908
+ parentNode.insertBefore(referenceNode, afterReference);
1909
+ }
1910
+ } else if (newArray.length) {
1911
+ afterReference = referenceNode.nextSibling;
1912
+ referenceNode.remove();
1933
1913
  }
1934
1914
  }
1935
- pendingRenders[i] = item;
1936
- }
1937
- if (!render.___scope.___closestBranch?.___destroyed) {
1938
- render.___signal(render.___scope, render.___value);
1915
+ reconcile(parentNode, oldArray, newArray, afterReference);
1939
1916
  }
1940
- }
1941
- finishPendingScopes();
1942
- }
1943
- function comparePendingRenders(a, b) {
1944
- return getBranchDepth(a) - getBranchDepth(b) || a.___index - b.___index;
1945
- }
1946
- function getBranchDepth(render) {
1947
- return render.___scope.___closestBranch?.___branchDepth || 0;
1917
+ };
1948
1918
  }
1949
-
1950
- // src/dom/abort-signal.ts
1951
- function resetAbortSignal(scope, id) {
1952
- const ctrl = scope.___abortControllers?.[id];
1953
- if (ctrl) {
1954
- queueEffect(ctrl, abort);
1955
- scope.___abortControllers[id] = void 0;
1956
- }
1919
+ function bySecondArg(_item, index) {
1920
+ return index;
1957
1921
  }
1958
- function getAbortSignal(scope, id) {
1959
- if (scope.___closestBranch) {
1960
- (scope.___closestBranch.___abortScopes ||= /* @__PURE__ */ new Set()).add(scope);
1961
- }
1962
- return ((scope.___abortControllers ||= {})[id] ||= new AbortController()).signal;
1922
+ function byFirstArg(name) {
1923
+ return name;
1963
1924
  }
1964
- function abort(ctrl) {
1965
- ctrl.abort();
1925
+ function isDifferentRenderer(a, b) {
1926
+ return a !== b && (a?.___id || 0) !== b?.___id;
1966
1927
  }
1967
1928
 
1968
- // src/common/compat-meta.ts
1969
- var prefix = true ? "$compat_" : "$C_";
1970
- var RENDERER_REGISTER_ID = prefix + (true ? "renderer" : "r");
1971
- var SET_SCOPE_REGISTER_ID = prefix + (true ? "setScope" : "s");
1972
- var RENDER_BODY_ID = prefix + (true ? "renderBody" : "b");
1973
-
1974
1929
  // src/dom/compat.ts
1975
1930
  var classIdToBranch = /* @__PURE__ */ new Map();
1976
1931
  var compat = {
@@ -2119,7 +2074,7 @@ function mount(input = {}, reference, position) {
2119
2074
  );
2120
2075
  runEffects(effects);
2121
2076
  return {
2122
- update: (newInput) => {
2077
+ update(newInput) {
2123
2078
  if (args) {
2124
2079
  runEffects(
2125
2080
  prepareEffects(() => {
@@ -2129,7 +2084,7 @@ function mount(input = {}, reference, position) {
2129
2084
  );
2130
2085
  }
2131
2086
  },
2132
- destroy: () => {
2087
+ destroy() {
2133
2088
  removeAndDestroyBranch(branch);
2134
2089
  }
2135
2090
  };