marko 6.0.0-next.3.57 → 6.0.0-next.3.59

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.
@@ -158,168 +158,197 @@ var DEFAULT_RENDER_ID = "_";
158
158
 
159
159
  // src/dom/resume.ts
160
160
  var registeredValues = {};
161
- var Render = class {
162
- ___scopeStack = [];
163
- ___scopeLookup = {};
164
- ___serializeContext = {
165
- _: registeredValues
166
- };
167
- constructor(renders, runtimeId, renderId) {
168
- Object.assign(this, renders[renderId]);
169
- this.___renders = renders;
170
- this.___runtimeId = runtimeId;
171
- this.___renderId = renderId;
172
- this.___walk = this.w;
173
- this.___resume();
174
- this.w = () => {
175
- this.___walk();
176
- this.___resume();
177
- };
178
- }
179
- ___resume() {
180
- const serializeContext = this.___serializeContext;
181
- const scopeLookup = this.___scopeLookup;
182
- const visits = this.v;
183
- const branchIds = /* @__PURE__ */ new Set();
184
- const parentBranchIds = /* @__PURE__ */ new Map();
185
- if (visits.length) {
186
- const commentPrefix = this.i;
187
- const commentPrefixLen = commentPrefix.length;
188
- const closestBranchMarkers = /* @__PURE__ */ new Map();
189
- const visitNodes = new Set(visits);
190
- let lastEndNode;
191
- this.v = [];
192
- const branchEnd = (branchId, visit, reference) => {
193
- const branch = scopeLookup[branchId] ||= {};
194
- let endNode = reference;
195
- while (visitNodes.has(endNode = endNode.previousSibling)) ;
196
- if (endNode === lastEndNode) {
197
- endNode = reference.parentNode.insertBefore(new Text(), reference);
198
- }
199
- branch.___endNode = lastEndNode = endNode;
200
- branch.___startNode ||= endNode;
201
- for (const [markerScopeId, markerNode] of closestBranchMarkers) {
202
- if (branch.___startNode.compareDocumentPosition(markerNode) & 4 && reference.compareDocumentPosition(markerNode) & 2) {
203
- parentBranchIds.set(markerScopeId, branchId);
204
- closestBranchMarkers.delete(markerScopeId);
205
- }
206
- }
207
- branchIds.add(branchId);
208
- closestBranchMarkers.set(branchId, visit);
209
- return branch;
210
- };
211
- for (const visit of visits) {
212
- const commentText = visit.data;
213
- const dataIndex = commentText.indexOf(" ") + 1;
214
- const scopeId = commentText.slice(
215
- commentPrefixLen + 1,
216
- dataIndex ? dataIndex - 1 : commentText.length
217
- );
218
- const scope = scopeLookup[scopeId] ||= { ___id: +scopeId };
219
- const data2 = dataIndex ? commentText.slice(dataIndex) : "";
220
- const token = commentText[commentPrefixLen];
221
- if (token === "*" /* Node */) {
222
- const node = scope[data2] = visit.previousSibling;
223
- scope[data2 + ">" /* Getter */] = () => node;
224
- } else if (token === "$" /* ClosestBranch */) {
225
- closestBranchMarkers.set(scopeId, visit);
226
- } else if (token === "[" /* BranchStart */) {
227
- if (this.___currentScopeId) {
228
- if (dataIndex) {
229
- branchEnd(this.___currentScopeId, visit, visit);
230
- }
231
- this.___scopeStack.push(this.___currentScopeId);
232
- }
233
- this.___currentScopeId = scopeId;
234
- scope.___startNode = visit;
235
- } else if (token === "]" /* BranchEnd */) {
236
- scope[data2] = visit;
237
- const curParent = visit.parentNode;
238
- const startNode = branchEnd(
239
- this.___currentScopeId,
240
- visit,
241
- visit
242
- ).___startNode;
243
- if (curParent !== startNode.parentNode) {
244
- curParent.prepend(startNode);
245
- }
246
- this.___currentScopeId = this.___scopeStack.pop();
247
- } else if (token === "|" /* BranchSingleNode */ || token === "=" /* BranchSingleNodeOnlyChildInParent */) {
248
- let next = data2.indexOf(" ");
249
- let curNode = visit;
250
- scope[~next ? data2.slice(0, next) : data2] = token === "=" /* BranchSingleNodeOnlyChildInParent */ ? visit.parentNode : visit;
251
- while (~next) {
252
- const start = next + 1;
253
- next = data2.indexOf(" ", start);
254
- const childScopeId = data2.slice(start, ~next ? next : data2.length);
255
- curNode = branchEnd(childScopeId, visit, curNode).___endNode;
256
- }
257
- }
258
- }
161
+ function init(runtimeId = DEFAULT_RUNTIME_ID) {
162
+ if (true) {
163
+ if (!runtimeId.match(/^[_$a-z][_$a-z0-9]*$/i)) {
164
+ throw new Error(
165
+ `Invalid runtimeId: "${runtimeId}". The runtimeId must be a valid JavaScript identifier.`
166
+ );
259
167
  }
260
- const resumes = this.r;
261
- if (resumes) {
262
- this.r = [];
263
- const len = resumes.length;
264
- let i = 0;
265
- try {
266
- isResuming = true;
267
- while (i < len) {
268
- const resumeData = resumes[i++];
269
- if (typeof resumeData === "function") {
270
- const scopes = resumeData(serializeContext);
271
- let { $global } = scopeLookup;
272
- if (!$global) {
273
- scopeLookup.$global = $global = scopes.$ || {};
274
- $global.runtimeId = this.___runtimeId;
275
- $global.renderId = this.___renderId;
276
- $global.___nextScopeId = 1e6;
277
- }
278
- for (const scopeId in scopes) {
279
- if (scopeId !== "$") {
280
- const scope = scopes[scopeId];
281
- const prevScope = scopeLookup[scopeId];
282
- scope.$global = $global;
283
- scope.___id = +scopeId;
284
- if (prevScope !== scope) {
285
- scopeLookup[scopeId] = Object.assign(
286
- scope,
287
- prevScope
288
- );
289
- }
290
- const parentBranchId = parentBranchIds.get(scopeId);
291
- if (parentBranchId) {
292
- scope.___closestBranch = scopes[parentBranchId];
168
+ const descriptor = Object.getOwnPropertyDescriptor(window, runtimeId);
169
+ if (descriptor && (descriptor.set || descriptor.configurable === false)) {
170
+ throw new Error(
171
+ `Marko initialized multiple times with the same $global.runtimeId of ${JSON.stringify(runtimeId)}. It could be that there are multiple copies of Marko running on the page.`
172
+ );
173
+ }
174
+ }
175
+ const renders = window[runtimeId];
176
+ const defineRuntime = (desc) => Object.defineProperty(window, runtimeId, desc);
177
+ let resumeRender;
178
+ const initRuntime = (renders2) => {
179
+ defineRuntime({
180
+ value: resumeRender = (renderId) => {
181
+ const render = resumeRender[renderId] = renders2[renderId] || renders2(renderId);
182
+ const walk2 = render.w;
183
+ const commentPrefixLen = render.i.length;
184
+ const scopeStack = [];
185
+ const scopeLookup = render.s = {};
186
+ const serializeContext = {
187
+ _: registeredValues
188
+ };
189
+ const branchIds = /* @__PURE__ */ new Set();
190
+ const parentBranchIds = /* @__PURE__ */ new Map();
191
+ const closestBranchMarkers = /* @__PURE__ */ new Map();
192
+ let currentScopeId;
193
+ render.w = () => {
194
+ walk2.call(render);
195
+ const visits = render.v;
196
+ const resumes = render.r;
197
+ if (visits.length) {
198
+ const visitNodes = new Set(visits);
199
+ let lastEndNode;
200
+ render.v = [];
201
+ const branchEnd = (branchId, visit, reference) => {
202
+ const branch = scopeLookup[branchId] ||= {};
203
+ let endNode = reference;
204
+ while (visitNodes.has(endNode = endNode.previousSibling)) ;
205
+ if (endNode === lastEndNode) {
206
+ endNode = reference.parentNode.insertBefore(
207
+ new Text(),
208
+ reference
209
+ );
210
+ }
211
+ branch.___endNode = lastEndNode = endNode;
212
+ branch.___startNode ||= endNode;
213
+ for (const [markerScopeId, markerNode] of closestBranchMarkers) {
214
+ if (branch.___startNode.compareDocumentPosition(markerNode) & 4 && reference.compareDocumentPosition(markerNode) & 2) {
215
+ parentBranchIds.set(markerScopeId, branchId);
216
+ closestBranchMarkers.delete(markerScopeId);
293
217
  }
294
- if (branchIds.has(scopeId)) {
295
- const branch = scope;
296
- const parentBranch = branch.___closestBranch;
297
- scope.___closestBranch = branch;
298
- if (parentBranch) {
299
- branch.___parentBranch = parentBranch;
300
- (parentBranch.___branchScopes ||= /* @__PURE__ */ new Set()).add(branch);
218
+ }
219
+ branchIds.add(branchId);
220
+ closestBranchMarkers.set(branchId, visit);
221
+ return branch;
222
+ };
223
+ for (const visit of visits) {
224
+ const commentText = visit.data;
225
+ const dataIndex = commentText.indexOf(" ") + 1;
226
+ const scopeId = commentText.slice(
227
+ commentPrefixLen + 1,
228
+ dataIndex ? dataIndex - 1 : commentText.length
229
+ );
230
+ const scope = scopeLookup[scopeId] ||= {
231
+ ___id: +scopeId
232
+ };
233
+ const data2 = dataIndex ? commentText.slice(dataIndex) : "";
234
+ const token = commentText[commentPrefixLen];
235
+ if (token === "*" /* Node */) {
236
+ const node = scope[data2] = visit.previousSibling;
237
+ scope[data2 + ">" /* Getter */] = () => node;
238
+ } else if (token === "$" /* ClosestBranch */) {
239
+ closestBranchMarkers.set(scopeId, visit);
240
+ } else if (token === "[" /* BranchStart */) {
241
+ if (currentScopeId) {
242
+ if (dataIndex) {
243
+ branchEnd(currentScopeId, visit, visit);
301
244
  }
245
+ scopeStack.push(currentScopeId);
302
246
  }
303
- if (true) {
304
- scope.___debugId = "server-" + scopeId;
247
+ currentScopeId = scopeId;
248
+ scope.___startNode = visit;
249
+ } else if (token === "]" /* BranchEnd */) {
250
+ scope[data2] = visit;
251
+ const curParent = visit.parentNode;
252
+ const startNode = branchEnd(
253
+ currentScopeId,
254
+ visit,
255
+ visit
256
+ ).___startNode;
257
+ if (curParent !== startNode.parentNode) {
258
+ curParent.prepend(startNode);
259
+ }
260
+ currentScopeId = scopeStack.pop();
261
+ } else if (token === "|" /* BranchSingleNode */ || token === "=" /* BranchSingleNodeOnlyChildInParent */) {
262
+ let next = data2.indexOf(" ");
263
+ let curNode = visit;
264
+ scope[~next ? data2.slice(0, next) : data2] = token === "=" /* BranchSingleNodeOnlyChildInParent */ ? visit.parentNode : visit;
265
+ while (~next) {
266
+ const start = next + 1;
267
+ next = data2.indexOf(" ", start);
268
+ const childScopeId = data2.slice(
269
+ start,
270
+ ~next ? next : data2.length
271
+ );
272
+ curNode = branchEnd(childScopeId, visit, curNode).___endNode;
305
273
  }
306
274
  }
307
275
  }
308
- } else if (i === len || typeof resumes[i] !== "string") {
309
- delete this.___renders[this.___renderId];
310
- } else {
311
- registeredValues[resumes[i++]](
312
- scopeLookup[resumeData],
313
- scopeLookup[resumeData]
314
- );
315
276
  }
316
- }
317
- } finally {
318
- isResuming = false;
277
+ if (resumes) {
278
+ try {
279
+ render.r = [];
280
+ isResuming = true;
281
+ for (let i = 0; i < resumes.length; i++) {
282
+ const serialized = resumes[i];
283
+ if (typeof serialized === "function") {
284
+ const scopes = serialized(serializeContext);
285
+ let { $global } = scopeLookup;
286
+ if (!$global) {
287
+ scopeLookup.$global = $global = scopes.$ || {};
288
+ $global.runtimeId = runtimeId;
289
+ $global.renderId = renderId;
290
+ $global.___nextScopeId = 1e6;
291
+ }
292
+ for (const scopeId in scopes) {
293
+ if (scopeId !== "$") {
294
+ const scope = scopes[scopeId];
295
+ const prevScope = scopeLookup[scopeId];
296
+ scope.$global = $global;
297
+ scope.___id = +scopeId;
298
+ if (prevScope !== scope) {
299
+ scopeLookup[scopeId] = Object.assign(
300
+ scope,
301
+ prevScope
302
+ );
303
+ }
304
+ const parentBranchId = parentBranchIds.get(scopeId);
305
+ if (parentBranchId) {
306
+ scope.___closestBranch = scopes[parentBranchId];
307
+ }
308
+ if (branchIds.has(scopeId)) {
309
+ const branch = scope;
310
+ const parentBranch = branch.___closestBranch;
311
+ scope.___closestBranch = branch;
312
+ if (parentBranch) {
313
+ branch.___parentBranch = parentBranch;
314
+ (parentBranch.___branchScopes ||= /* @__PURE__ */ new Set()).add(
315
+ branch
316
+ );
317
+ }
318
+ }
319
+ if (true) {
320
+ scope.___debugId = "server-" + scopeId;
321
+ }
322
+ }
323
+ }
324
+ } else {
325
+ registeredValues[resumes[++i]](
326
+ scopeLookup[serialized],
327
+ scopeLookup[serialized]
328
+ );
329
+ }
330
+ }
331
+ } finally {
332
+ isResuming = false;
333
+ }
334
+ }
335
+ };
336
+ return render;
319
337
  }
338
+ });
339
+ };
340
+ if (renders) {
341
+ initRuntime(renders);
342
+ for (const renderId in renders) {
343
+ resumeRender(renderId).w();
320
344
  }
345
+ } else {
346
+ defineRuntime({
347
+ configurable: true,
348
+ set: initRuntime
349
+ });
321
350
  }
322
- };
351
+ }
323
352
  var isResuming = false;
324
353
  function register(id, obj) {
325
354
  registeredValues[id] = obj;
@@ -333,42 +362,6 @@ function getRegisteredWithScope(id, scope) {
333
362
  const val = registeredValues[id];
334
363
  return scope ? val(scope) : val;
335
364
  }
336
- function init(runtimeId = DEFAULT_RUNTIME_ID) {
337
- if (true) {
338
- if (!runtimeId.match(/^[_$a-z][_$a-z0-9]*$/i)) {
339
- throw new Error(
340
- `Invalid runtimeId: "${runtimeId}". The runtimeId must be a valid JavaScript identifier.`
341
- );
342
- }
343
- }
344
- const resumeRender = (renderId) => resumeRender[renderId] = renders[renderId] = new Render(renders, runtimeId, renderId);
345
- let renders;
346
- if (window[runtimeId]) {
347
- setRenders(window[runtimeId]);
348
- } else {
349
- Object.defineProperty(window, runtimeId, {
350
- configurable: true,
351
- set: setRenders
352
- });
353
- }
354
- function setRenders(v) {
355
- if (true) {
356
- if (renders) {
357
- throw new Error(
358
- "Marko tried to initialize multiple times. It could be that there are multiple instances of Marko running on the page."
359
- );
360
- }
361
- }
362
- renders = v;
363
- for (const renderId in v) {
364
- resumeRender(renderId);
365
- }
366
- Object.defineProperty(window, runtimeId, {
367
- configurable: true,
368
- value: resumeRender
369
- });
370
- }
371
- }
372
365
  function nodeRef(id, key) {
373
366
  return register(id, (scope) => () => scope[key]());
374
367
  }
@@ -1888,7 +1881,7 @@ var compat = {
1888
1881
  if (Array.isArray(value2) && typeof value2[0] === "string") {
1889
1882
  return getRegisteredWithScope(
1890
1883
  value2[0],
1891
- value2.length === 2 && window[runtimeId]?.[componentIdPrefix === "s" ? "_" : componentIdPrefix]?.___scopeLookup[value2[1]]
1884
+ value2.length === 2 && window[runtimeId]?.[componentIdPrefix === "s" ? "_" : componentIdPrefix]?.s[value2[1]]
1892
1885
  );
1893
1886
  }
1894
1887
  return value2;
@@ -2190,9 +2190,6 @@ function prepareChunk(chunk) {
2190
2190
  hasWalk = true;
2191
2191
  resumes = resumes ? resumes + "," + effects : effects;
2192
2192
  }
2193
- if (boundary.done && (resumes || state.hasWrittenResume)) {
2194
- resumes = resumes ? resumes + ",0" : "0";
2195
- }
2196
2193
  if (resumes) {
2197
2194
  if (state.hasWrittenResume) {
2198
2195
  scripts = concatScripts(
@@ -2106,9 +2106,6 @@ function prepareChunk(chunk) {
2106
2106
  hasWalk = true;
2107
2107
  resumes = resumes ? resumes + "," + effects : effects;
2108
2108
  }
2109
- if (boundary.done && (resumes || state.hasWrittenResume)) {
2110
- resumes = resumes ? resumes + ",0" : "0";
2111
- }
2112
2109
  if (resumes) {
2113
2110
  if (state.hasWrittenResume) {
2114
2111
  scripts = concatScripts(
@@ -1,8 +1,8 @@
1
1
  import { type Scope } from "../common/types";
2
2
  import type { Signal } from "./signals";
3
+ export declare function init(runtimeId?: string): void;
3
4
  export declare let isResuming: boolean;
4
5
  export declare function register<T>(id: string, obj: T): T;
5
6
  export declare function registerBoundSignal<T extends Signal<unknown>>(id: string, signal: T): T;
6
7
  export declare function getRegisteredWithScope(id: string, scope?: Scope): unknown;
7
- export declare function init(runtimeId?: string): void;
8
8
  export declare function nodeRef(id: string, key: string): (scope: Scope) => () => any;