brumate 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/dist/{boot-D-IdPy7B.d.ts → boot-C25Sa6vq.d.ts} +5 -2
  2. package/dist/cli.js +2 -2
  3. package/dist/{client-C0UCNL3J.js → client-BD6XT-s5.js} +435 -110
  4. package/dist/client-BD6XT-s5.js.map +1 -0
  5. package/dist/{client-MD0ZmcFR.d.ts → client-D0CqSxoO.d.ts} +114 -8
  6. package/dist/{cookies-Av5WFmcf.js → cookies-BzllthiF.js} +2 -1
  7. package/dist/cookies-BzllthiF.js.map +1 -0
  8. package/dist/{defer-D0bo1Gqx.js → defer-BbD7JQhj.js} +6 -2
  9. package/dist/defer-BbD7JQhj.js.map +1 -0
  10. package/dist/devtools-CQgD7NPB.js +623 -0
  11. package/dist/devtools-CQgD7NPB.js.map +1 -0
  12. package/dist/devtools-panel-BVB-W5q4.js +1930 -0
  13. package/dist/devtools-panel-BVB-W5q4.js.map +1 -0
  14. package/dist/events-CSs7OwIC.js +85 -0
  15. package/dist/events-CSs7OwIC.js.map +1 -0
  16. package/dist/{files-Bnn1JqHx.js → files-BXSSrQgc.js} +18 -5
  17. package/dist/files-BXSSrQgc.js.map +1 -0
  18. package/dist/forms-Bg-CwGqX.js +406 -0
  19. package/dist/forms-Bg-CwGqX.js.map +1 -0
  20. package/dist/forms-Cl21hkHt.d.ts +281 -0
  21. package/dist/highlight.d.ts +17 -0
  22. package/dist/highlight.js +269 -0
  23. package/dist/highlight.js.map +1 -0
  24. package/dist/{html-y1q7-t7D.d.ts → html-Bq1rFxX_.d.ts} +36 -4
  25. package/dist/{html-bHZbkK-h.js → html-mNTKzaDD.js} +79 -8
  26. package/dist/html-mNTKzaDD.js.map +1 -0
  27. package/dist/index.d.ts +47 -2
  28. package/dist/index.js +3 -3
  29. package/dist/index.js.map +1 -1
  30. package/dist/{inspect-BJtACnQp.js → inspect-BHpOw1LF.js} +625 -48
  31. package/dist/inspect-BHpOw1LF.js.map +1 -0
  32. package/dist/{loader-B18i3_-T.js → loader-Dos_3NP0.js} +5 -4
  33. package/dist/loader-Dos_3NP0.js.map +1 -0
  34. package/dist/{navigate-BwuZveDF.js → navigate-O78d_zGN.js} +45 -2
  35. package/dist/navigate-O78d_zGN.js.map +1 -0
  36. package/dist/{produce-D4K7qpaL.d.ts → produce-D0V0ICkz.d.ts} +45 -3
  37. package/dist/{produce-CWfJ4byV.js → produce-Doc91GkX.js} +113 -6
  38. package/dist/produce-Doc91GkX.js.map +1 -0
  39. package/dist/{rpc-B-Zkn6Ch.js → rpc-ygMRPmN4.js} +20 -10
  40. package/dist/rpc-ygMRPmN4.js.map +1 -0
  41. package/dist/runtime/client.d.ts +4 -4
  42. package/dist/runtime/client.js +4 -4
  43. package/dist/runtime/devtools.d.ts +188 -3
  44. package/dist/runtime/devtools.js +1 -1
  45. package/dist/runtime/events.d.ts +15 -0
  46. package/dist/runtime/events.js +1 -1
  47. package/dist/runtime/forms.d.ts +2 -0
  48. package/dist/runtime/forms.js +1 -0
  49. package/dist/runtime/gather.d.ts +25 -0
  50. package/dist/runtime/gather.js +33 -0
  51. package/dist/runtime/gather.js.map +1 -0
  52. package/dist/runtime/hot.d.ts +1 -1
  53. package/dist/runtime/html.d.ts +2 -2
  54. package/dist/runtime/html.js +1 -1
  55. package/dist/runtime/loader.d.ts +2 -2
  56. package/dist/runtime/loader.js +2 -2
  57. package/dist/runtime/server.d.ts +5 -4
  58. package/dist/runtime/server.js +5 -5
  59. package/dist/runtime/signals.d.ts +38 -2
  60. package/dist/runtime/signals.js +12 -2
  61. package/dist/runtime/signals.js.map +1 -1
  62. package/dist/runtime/static-loader.d.ts +2 -2
  63. package/dist/runtime/static-loader.js +1 -1
  64. package/dist/runtime/testing.d.ts +2 -2
  65. package/dist/runtime/testing.js +2 -2
  66. package/dist/runtime/values.d.ts +2 -0
  67. package/dist/runtime/values.js +24 -0
  68. package/dist/runtime/values.js.map +1 -0
  69. package/dist/{server-BqfM95r1.js → server-CxVDYE_V.js} +776 -121
  70. package/dist/server-CxVDYE_V.js.map +1 -0
  71. package/dist/{server-BA9SJ2Vm.d.ts → server-DuFYRXrd.d.ts} +284 -15
  72. package/dist/{static-loader-BzE9SRaj.d.ts → static-loader-Drpr0AzI.d.ts} +2 -2
  73. package/dist/{static-loader-DOygOFj5.js → static-loader-nZssh66R.js} +168 -51
  74. package/dist/static-loader-nZssh66R.js.map +1 -0
  75. package/dist/{stream-JaMHMdBD.js → stream-DCYZO4IF.js} +89 -10
  76. package/dist/stream-DCYZO4IF.js.map +1 -0
  77. package/dist/values-B8C7CLvG.d.ts +63 -0
  78. package/dist/vite.d.ts +8 -1
  79. package/dist/vite.js +170 -20
  80. package/dist/vite.js.map +1 -1
  81. package/forms/forms.brm.ts +141 -0
  82. package/package.json +31 -2
  83. package/dist/client-C0UCNL3J.js.map +0 -1
  84. package/dist/cookies-Av5WFmcf.js.map +0 -1
  85. package/dist/defer-D0bo1Gqx.js.map +0 -1
  86. package/dist/devtools-C50VH8gZ.js +0 -113
  87. package/dist/devtools-C50VH8gZ.js.map +0 -1
  88. package/dist/devtools-panel-AYjfwPhj.js +0 -435
  89. package/dist/devtools-panel-AYjfwPhj.js.map +0 -1
  90. package/dist/events-BKWRqtMg.js +0 -52
  91. package/dist/events-BKWRqtMg.js.map +0 -1
  92. package/dist/files-Bnn1JqHx.js.map +0 -1
  93. package/dist/html-bHZbkK-h.js.map +0 -1
  94. package/dist/inspect-BJtACnQp.js.map +0 -1
  95. package/dist/loader-B18i3_-T.js.map +0 -1
  96. package/dist/navigate-BwuZveDF.js.map +0 -1
  97. package/dist/produce-CWfJ4byV.js.map +0 -1
  98. package/dist/rpc-B-Zkn6Ch.js.map +0 -1
  99. package/dist/server-BqfM95r1.js.map +0 -1
  100. package/dist/static-loader-DOygOFj5.js.map +0 -1
  101. package/dist/stream-JaMHMdBD.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { _ as textValue, a as attributeText, h as spread, n as actionAttributes, t as VOID_ELEMENTS, u as insertValue } from "./html-bHZbkK-h.js";
2
- import { a as parseId, i as isAsyncIterable, n as ElementReference, o as stateValues, r as FunctionReference } from "./produce-CWfJ4byV.js";
1
+ import { a as attributeText, b as textValue, d as handlerEntry, f as insertValue, n as actionAttributes, o as bindEntry, t as VOID_ELEMENTS, v as spread$1 } from "./html-mNTKzaDD.js";
2
+ import { a as SlotReference, c as isSlotReference, d as parseId, f as stateValues, i as FunctionReference, m as useSerialEntry, n as Binding, o as isAsyncIterable, r as ElementReference, s as isBinding } from "./produce-Doc91GkX.js";
3
3
  import { n as ROUTE_KEY } from "./context-CTiSzQRc.js";
4
4
  import { n as trustedHTML, t as loadOnce } from "./skew-FRmAy9iy.js";
5
5
  //#region src/runtime/client.ts
@@ -75,20 +75,39 @@ function readRecord([script, code]) {
75
75
  /**
76
76
  * Updates scopes that the server rendered before values changed on the
77
77
  * client, such as the body of a `#loading` block that streams in after an
78
- * event. Effects do not run: they run only when a value changes.
78
+ * event. Effects do not run: they run only when a value changes. With
79
+ * `memosOnly`, as for scopes that the client just rendered with its values,
80
+ * only the memos run, so the ones that load start.
79
81
  */
80
- function refresh(added) {
82
+ function refresh(added, memosOnly = false) {
83
+ const byModule = /* @__PURE__ */ new Map();
81
84
  for (const scope of added) {
82
85
  const id = rootOf(scope).m;
83
86
  if (!id) continue;
84
- loadModule(id).then(async (module) => {
85
- const names = /* @__PURE__ */ new Set();
86
- for (const slots of Object.values(module.g)) for (const entries of Object.values(slots)) for (const [kind, name] of entries) if (kind === scope.k && name[0] !== "e") names.add(name);
87
- if (names.size === 0) return;
88
- const updates = await loadPart(module, "update");
89
- for (const name of [...names].toSorted((a, b) => Number(b[0] === "m") - Number(a[0] === "m"))) if (scopes.has(scope.id)) updates[name](scope);
90
- });
87
+ const list = byModule.get(id);
88
+ if (list) list.push(scope);
89
+ else byModule.set(id, [scope]);
90
+ }
91
+ for (const [id, list] of byModule) loadModule(id, "state").then(async (module) => {
92
+ const byKind = /* @__PURE__ */ new Map();
93
+ for (const scope of list) if (!byKind.has(scope.k)) byKind.set(scope.k, refreshNames(module, scope.k, memosOnly));
94
+ let any = false;
95
+ for (const names of byKind.values()) any ||= names.length > 0;
96
+ if (!any) return;
97
+ const updates = await loadPart(module, "update", "state");
98
+ for (const scope of list) for (const name of byKind.get(scope.k)) if (scopes.has(scope.id)) updates[name](scope);
99
+ });
100
+ }
101
+ /** The names of the updates of a kind of scope that `refresh` runs, memos first. */
102
+ function refreshNames(module, kind, memosOnly) {
103
+ const memos = /* @__PURE__ */ new Set();
104
+ const others = /* @__PURE__ */ new Set();
105
+ for (const slots of Object.values(module.g)) for (const entries of Object.values(slots)) for (const [entryKind, name] of entries) {
106
+ if (entryKind !== kind || name[0] === "e") continue;
107
+ if (name[0] === "m") memos.add(name);
108
+ else if (!memosOnly) others.add(name);
91
109
  }
110
+ return [...memos, ...others];
92
111
  }
93
112
  /** Reads the groups of scope records back into one record per scope. See `PackedState`. */
94
113
  function unpackState(state) {
@@ -152,6 +171,7 @@ function addScopes(state, root) {
152
171
  c: record.c,
153
172
  v: record.v,
154
173
  x: record.x,
174
+ b: record.b,
155
175
  children: /* @__PURE__ */ new Set(),
156
176
  r: root
157
177
  };
@@ -281,6 +301,8 @@ function leavePage() {
281
301
  scopes.clear();
282
302
  comments.clear();
283
303
  elements.clear();
304
+ unindexed.length = 0;
305
+ rendered.length = 0;
284
306
  pendingEffects.clear();
285
307
  hiding = void 0;
286
308
  loads.clear();
@@ -301,6 +323,7 @@ function leavePage() {
301
323
  /** Reads the page state. The loader calls it once, before the first handler. */
302
324
  function init(value) {
303
325
  options = value;
326
+ if (value.serial) useSerialEntry(value.serial);
304
327
  rpcPrefix = value.rpc ?? "/_brumate";
305
328
  build = value.build;
306
329
  const queue = stateQueue();
@@ -315,12 +338,44 @@ function init(value) {
315
338
  watchTitles(document);
316
339
  if (DEV) devtools()?.attach?.({
317
340
  scopes,
318
- set
341
+ set,
342
+ handle
319
343
  });
320
344
  }
345
+ /**
346
+ * The handlers that the core makes from references, with their references,
347
+ * so a spread in markup that the browser renders passes them on in `b-on`,
348
+ * as a spread on the server does.
349
+ */
350
+ const handlerReferences = /* @__PURE__ */ new WeakMap();
351
+ /** The modifiers that the wrappers of `brumate/events` put on a handler. */
352
+ const MODIFIERS = Symbol.for("brumate.modifiers");
353
+ /**
354
+ * Renders an object of attributes. With `on`, a handler in it that the core
355
+ * made from a reference goes into `on` as a `b-on` entry, with the scope and
356
+ * the arguments of its reference and the modifiers of its wrappers.
357
+ */
358
+ function spread(attributes, on) {
359
+ if (on) for (const [name, value] of Object.entries(attributes)) {
360
+ const reference = /^on[A-Z]/.test(name) && typeof value === "function" ? handlerReferences.get(value) : void 0;
361
+ if (reference) on.push(handlerEntry(name, reference.ref, reference.scope, reference.modifiers, reference.args));
362
+ if ((name === "value" || name === "checked") && isBinding(value)) on.push(bindEntry(name, value.ref, setterReference(value.set)));
363
+ }
364
+ return spread$1(attributes);
365
+ }
366
+ /** The reference of the setter of a binding: as the state has it, or of a handler that the core made. */
367
+ function setterReference(set) {
368
+ if (set instanceof FunctionReference) return set;
369
+ return typeof set === "function" ? handlerReferences.get(set) : void 0;
370
+ }
321
371
  /** Replaces function references with functions that call them. */
322
372
  function callable(value, seen) {
323
- if (value instanceof FunctionReference) return isTemplate(value.ref) ? templateReference(value.ref, scopes.get(value.scope)) : (...args) => call(value.ref, scopes.get(value.scope), args);
373
+ if (value instanceof FunctionReference) {
374
+ if (isTemplate(value.ref)) return templateReference(value.ref, scopes.get(value.scope));
375
+ const handler = (...args) => call(value.ref, scopes.get(value.scope), [...value.args ?? [], ...args]);
376
+ handlerReferences.set(handler, value);
377
+ return handler;
378
+ }
324
379
  if (typeof value === "object" && value !== null && !seen.has(value)) {
325
380
  seen.add(value);
326
381
  const prototype = Object.getPrototypeOf(value);
@@ -331,7 +386,7 @@ function callable(value, seen) {
331
386
  }
332
387
  return value;
333
388
  }
334
- function loadModule(id) {
389
+ function loadModule(id, reason = "write") {
335
390
  let promise = loading.get(id);
336
391
  if (!promise) {
337
392
  promise = Promise.resolve(options.load(id)).then((loaded) => {
@@ -344,28 +399,33 @@ function loadModule(id) {
344
399
  return module;
345
400
  });
346
401
  loading.set(id, promise);
402
+ if (DEV) devtools()?.load?.(id, "entry", reason, promise);
347
403
  }
348
404
  return promise;
349
405
  }
350
- function loadPart(module, name) {
406
+ function loadPart(module, name, reason = "write") {
351
407
  const key = `${module.id}:${name}`;
352
408
  let promise = parts.get(key);
353
409
  if (!promise) {
354
- promise = (module.load ? module.load(name) : Promise.resolve(module)).then((part) => {
410
+ let loaded;
411
+ if (!module.load) loaded = Promise.resolve(module);
412
+ else loaded = options.loadPart ? options.loadPart(module, name) : module.load(name);
413
+ promise = loaded.then((part) => {
355
414
  loadedParts.set(key, part);
356
415
  if (DEV) devtools()?.part?.(module.id, name);
357
416
  return part;
358
417
  });
359
418
  parts.set(key, promise);
419
+ if (DEV) devtools()?.load?.(module.id, module.load ? name : "all", reason, promise);
360
420
  }
361
421
  return promise;
362
422
  }
363
423
  /** Waits forever once the page loads again, so no error shows before it goes. */
364
424
  let reloading;
365
- async function resolve(ref) {
425
+ async function resolve(ref, reason) {
366
426
  const [id, name] = ref.split("#");
367
427
  try {
368
- return (await loadPart(await loadModule(id), partOf(name)))[name];
428
+ return (await loadPart(await loadModule(id, reason), partOf(name), reason))[name];
369
429
  } catch (error) {
370
430
  const next = await serverBuild();
371
431
  if (!reloading && next !== void 0 && loadOnce(next)) {
@@ -395,7 +455,7 @@ async function serverBuild() {
395
455
  }
396
456
  }
397
457
  async function call(ref, scope, args) {
398
- return (await resolve(ref))(scope, ...args);
458
+ return (await resolve(ref, "call"))(scope, ...args);
399
459
  }
400
460
  /** True for a reference to a `#template` that a component got as a prop, such as `Page#t3`. */
401
461
  function isTemplate(ref) {
@@ -409,7 +469,7 @@ const loadingTemplates = /* @__PURE__ */ new Set();
409
469
  */
410
470
  function templateReference(ref, scope) {
411
471
  let render;
412
- const code = resolve(ref).then((handler) => {
472
+ const code = resolve(ref, "template").then((handler) => {
413
473
  render = handler;
414
474
  }).finally(() => loadingTemplates.delete(code));
415
475
  loadingTemplates.add(code);
@@ -433,14 +493,42 @@ function withTarget(event, element) {
433
493
  * the compiler checks is in the same scope.
434
494
  */
435
495
  function handle(element, entry, event) {
436
- const [ref, owner] = entry.split("@");
496
+ const [ref, owner, encoded] = entry.split("@");
497
+ const bound = encoded ? JSON.parse(decodeURIComponent(encoded)) : [];
437
498
  const anchor = owner ?? element.closest("[b-b]")?.getAttribute("b-b")?.split(":")[0];
438
499
  const scope = anchor ? scopes.get(parseId(anchor)) : void 0;
439
500
  if (scope && hiding?.hidden(scope) && element.hasAttribute("b-listen")) return Promise.resolve();
440
- const args = event ? [withTarget(event, element)] : [];
501
+ if (ref.startsWith("$set:")) {
502
+ const field = element;
503
+ return call(ref.slice(5), scope, [/^(checkbox|radio)$/i.test(field.type) ? field.checked : field.value]);
504
+ }
505
+ if (ref.startsWith("$bind#")) {
506
+ const [hops = 0, slot = 0] = ref.slice(6).split(".").map(Number);
507
+ let target = scope;
508
+ for (let hop = 0; hop < hops; hop++) target = target?.p;
509
+ const field = element;
510
+ if (target) set(target, slot, /^(checkbox|radio)$/i.test(field.type) ? field.checked : field.value);
511
+ return Promise.resolve();
512
+ }
513
+ const args = event ? [...bound, withTarget(event, element)] : bound;
514
+ const begun = DEV ? performance.now() : 0;
441
515
  if (DEV) devtools()?.handler?.(ref, element, event);
442
- const loaded = resolve(ref);
443
- const started = lastHandler.then(() => loaded).then((handler) => ({ value: handler(scope, ...args) }));
516
+ const loaded = resolve(ref, "event");
517
+ const started = lastHandler.then(() => loaded).then((handler) => {
518
+ if (!DEV) return { value: handler(scope, ...args) };
519
+ const ready = performance.now();
520
+ const value = handler(scope, ...args);
521
+ devtools()?.handled?.({
522
+ ref,
523
+ element,
524
+ event: event?.type,
525
+ begun,
526
+ ready,
527
+ end: performance.now(),
528
+ result: value
529
+ });
530
+ return { value };
531
+ });
444
532
  lastHandler = started.catch(() => {});
445
533
  return started.then((result) => result.value).catch((error) => {
446
534
  fail(scope, error);
@@ -459,7 +547,7 @@ function fail(scope, error) {
459
547
  for (let current = scope; current; current = current.p ?? current.o) {
460
548
  const id = rootOf(current).m;
461
549
  if (!id) continue;
462
- const module = await loadModule(id);
550
+ const module = await loadModule(id, "error");
463
551
  const entry = module.t?.[current.k];
464
552
  if (!entry) continue;
465
553
  let owner = current.p;
@@ -478,7 +566,7 @@ function fail(scope, error) {
478
566
  }
479
567
  /** Renders the `#catch` body of a `#try` with an error, in place of what the block shows. */
480
568
  async function catchInto(owner, module, [, anchor, , fallback, kinds], error) {
481
- const updates = await loadPart(module, "update");
569
+ const updates = await loadPart(module, "update", "error");
482
570
  const start = findComment(`b${owner.id}:${anchor}`);
483
571
  if (!start) return;
484
572
  const end = rangeEnd(start);
@@ -501,14 +589,46 @@ function get(scope, slot) {
501
589
  return value;
502
590
  }
503
591
  let writes = 0;
592
+ /**
593
+ * Writes a slot from code of a component. A prop that a parent bound with
594
+ * `$bind` writes the parent's slot too, through any number of levels, and
595
+ * the parent's update of the prop then finds the value already there. A
596
+ * binding with a setter, `$bind(name, fn)`, calls it in place of the write,
597
+ * and the parent's update of the prop writes the value that it keeps.
598
+ */
504
599
  function set(scope, slot, value) {
600
+ const bound = scope.b?.[slot];
601
+ if (bound === void 0 || isSlotReference(bound)) {
602
+ writeSlot(scope, slot, value);
603
+ const target = bound && scopes.get(bound.scope);
604
+ if (target) set(target, bound.slot, value);
605
+ } else if (bound instanceof FunctionReference) call(bound.ref, scopes.get(bound.scope), [...bound.args ?? [], value]);
606
+ else if (typeof bound === "function") bound(value);
607
+ return value;
608
+ }
609
+ /** Writes a slot, and updates what reads it. */
610
+ function writeSlot(scope, slot, value) {
505
611
  if (!Object.is(scope.s[slot], value)) {
506
612
  writes++;
507
613
  scope.s[slot] = value;
508
614
  if (DEV) devtools()?.write?.(scope, slot, value);
509
615
  trigger(scope, slot);
510
616
  }
511
- return value;
617
+ }
618
+ /** What `$bind(name)` passes a child, in markup that the browser renders. */
619
+ function binding(value, scope, slot, set) {
620
+ return new Binding(value, new SlotReference(typeof scope === "object" ? scope.id : scope, slot), set);
621
+ }
622
+ /**
623
+ * Records the props that a parent bound with `$bind`, among the props that a
624
+ * component the browser renders writes: `slots` has the slot of each, by its name.
625
+ */
626
+ function bindings(scope, props, slots) {
627
+ const target = typeof scope === "object" ? scope : scopes.get(scope);
628
+ for (const [name, slot] of Object.entries(slots)) {
629
+ const value = props[name];
630
+ if (target && isBinding(value)) (target.b ??= {})[slot] = value.target;
631
+ }
512
632
  }
513
633
  /**
514
634
  * The names of a call of a module function, for its shared code. Each ref
@@ -762,20 +882,39 @@ function trigger(scope, slot) {
762
882
  run(module, updates, scope, slot);
763
883
  return;
764
884
  }
765
- loadModule(id).then(async (loaded) => {
885
+ const loading = loadModule(id).then(async (loaded) => {
766
886
  if (loaded.g[scope.k]?.[slot]) {
767
887
  const code = await loadPart(loaded, "update");
768
888
  await Promise.allSettled(loadingTemplates);
769
889
  run(loaded, code, scope, slot);
770
890
  }
771
891
  });
892
+ loadingUpdates.add(loading);
893
+ loading.finally(() => loadingUpdates.delete(loading));
894
+ }
895
+ /** The updates that wait for their code. */
896
+ const loadingUpdates = /* @__PURE__ */ new Set();
897
+ /**
898
+ * Resolves when the updates of the writes so far have run, including those
899
+ * whose code is loading. `transition` of `brumate/events` waits for it.
900
+ */
901
+ async function updated() {
902
+ while (loadingUpdates.size > 0) await Promise.allSettled(loadingUpdates);
772
903
  }
904
+ globalThis[Symbol.for("brumate.updated")] = updated;
773
905
  const pendingEffects = /* @__PURE__ */ new Map();
774
906
  let running = 0;
775
907
  let flushing = false;
776
908
  /** Counts changes to the scope tree, so `run` knows when the descendants it found are out of date. */
777
909
  let treeChanges = 0;
910
+ /** The updates that ran since the outermost `run` started, and when it started, for the devtools. */
911
+ let updateCount = 0;
912
+ let updateStart = 0;
778
913
  function run(module, updates, scope, slot) {
914
+ if (DEV && running === 0) {
915
+ updateCount = 0;
916
+ updateStart = performance.now();
917
+ }
779
918
  running++;
780
919
  let found;
781
920
  let foundAt = treeChanges;
@@ -801,10 +940,14 @@ function run(module, updates, scope, slot) {
801
940
  } finally {
802
941
  running--;
803
942
  }
804
- if (running === 0) flushEffects();
943
+ if (running === 0) {
944
+ flushEffects();
945
+ if (DEV) devtools()?.updated?.(updateCount, updateStart, performance.now());
946
+ }
805
947
  }
806
948
  /** Runs an update on a scope. An effect waits until the other updates have run. */
807
949
  function runUpdate(fn, name, target) {
950
+ if (DEV) updateCount++;
808
951
  if (name[0] === "e") pendingEffects.set(`${target.id}:${name}`, () => {
809
952
  if (scopes.has(target.id)) fn(target);
810
953
  });
@@ -834,8 +977,8 @@ function descendants(scope, kind, result = []) {
834
977
  return result;
835
978
  }
836
979
  function destroy(scope) {
837
- for (const child of Array.from(scope.children)) destroy(child);
838
- const start = comments.get(`P${scope.id}`);
980
+ if (scope.children.size > 0) for (const child of Array.from(scope.children)) destroy(child);
981
+ const start = portals > 0 ? comments.get(`P${scope.id}`) : void 0;
839
982
  if (start?.isConnected) {
840
983
  const end = rangeEnd(start);
841
984
  clear(start, end);
@@ -843,14 +986,16 @@ function destroy(scope) {
843
986
  end.remove();
844
987
  }
845
988
  removeScope(scope);
989
+ sweepIndexes();
846
990
  }
847
991
  /**
848
992
  * Removes a scope from the page: its cleanups run, and its memo state goes,
849
993
  * so a load that settles later is not written. Its markup stays.
850
994
  */
851
995
  function removeScope(scope) {
852
- for (const cleanup of scope.cleanups?.values() ?? []) cleanup();
853
- for (let slot = 0; slot < scope.s.length; slot++) {
996
+ if (scope.cleanups) for (const cleanup of scope.cleanups.values()) cleanup();
997
+ const waiting = memoControllers.size + timers.size + loads.size + loadingSlots.size + (guesses?.map.size ?? 0);
998
+ for (let slot = 0; waiting > 0 && slot < scope.s.length; slot++) {
854
999
  const key = `${scope.id}:${slot}`;
855
1000
  memoControllers.get(key)?.abort();
856
1001
  memoControllers.delete(key);
@@ -872,17 +1017,75 @@ const MARKER = /^[btilP]-?\d/;
872
1017
  function markerKey(data) {
873
1018
  return data[0] === "b" ? data.split(":").slice(0, 2).join(":") : data;
874
1019
  }
875
- function indexMarkers(root) {
1020
+ const SCOPE_MARKER = /^[tb](-?[\d.]+):/;
1021
+ /** True for markup that names a scope from the server: a `b-b` or marker with an ID that is not negative. */
1022
+ const SERVER_SCOPES = /b-b="\d|<!--[tb]\d/;
1023
+ /** The scopes that the client made since the last insert, which the next insert holds. */
1024
+ const rendered = [];
1025
+ /** How many `#portal` markers the page has had, so a scope looks for its own only when any have. */
1026
+ let portals = 0;
1027
+ /**
1028
+ * Indexes the markers and the bound elements under `root`, so that updates find them without a
1029
+ * search of the page. A search for each row of a long list takes time that grows with the list.
1030
+ * With `ids`, it also collects the IDs of the scopes that the markup names. The IDs of scopes
1031
+ * that the client made are negative.
1032
+ */
1033
+ function indexMarkers(root, ids, withElements = true) {
1034
+ if (withElements) indexElements(root, ids);
876
1035
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
877
1036
  for (let node = walker.nextNode(); node; node = walker.nextNode()) {
878
1037
  const data = node.data;
879
- if (MARKER.test(data)) comments.set(markerKey(data), node);
1038
+ if (!MARKER.test(data)) continue;
1039
+ comments.set(markerKey(data), node);
1040
+ if (data[0] === "P") portals++;
1041
+ const match = ids && SCOPE_MARKER.exec(data);
1042
+ if (match) ids.add(parseId(match[1]));
880
1043
  }
881
1044
  }
1045
+ /** The size of the indexes at which they next drop the nodes that left the page. */
1046
+ let sweepAt = 4096;
1047
+ /**
1048
+ * Drops the nodes that left the page from the indexes, once they have grown to twice their size
1049
+ * after the last sweep, so a list that the client fills and empties again does not keep its old
1050
+ * rows. A node that comes back is indexed again when it is inserted.
1051
+ */
1052
+ function sweepIndexes() {
1053
+ if (comments.size + elements.size < sweepAt) return;
1054
+ for (const [key, node] of comments) if (!node.isConnected) comments.delete(key);
1055
+ for (const [key, node] of elements) if (!node.isConnected) elements.delete(key);
1056
+ sweepAt = Math.max(4096, 2 * (comments.size + elements.size));
1057
+ }
1058
+ function indexElements(root, ids) {
1059
+ if (root.nodeType === 1) indexElement(root, ids);
1060
+ if (root.nodeType === 1 || root.nodeType === 9 || root.nodeType === 11) for (const element of root.querySelectorAll("[b-b]")) indexElement(element, ids);
1061
+ }
1062
+ /**
1063
+ * The nodes that inserts of the client put in the page, from the first to the last of each,
1064
+ * whose bound elements are not indexed yet. A list that the client creates is often never
1065
+ * updated row by row, so its elements are indexed when an update first needs one of them.
1066
+ */
1067
+ const unindexed = [];
1068
+ function indexInserts() {
1069
+ for (const [first, last] of unindexed.splice(0)) if (first.isConnected) for (let node = first; node; node = node.nextSibling) {
1070
+ indexElements(node);
1071
+ if (node === last) break;
1072
+ }
1073
+ else if (last.isConnected) for (let node = last; node; node = node.previousSibling) indexElements(node);
1074
+ }
1075
+ function indexElement(element, ids) {
1076
+ const key = element.getAttribute("b-b");
1077
+ if (key === null) return;
1078
+ elements.set(key, element);
1079
+ ids?.add(parseId(key.slice(0, key.indexOf(":"))));
1080
+ }
882
1081
  /** Indexes the markers in the nodes that mutation records added. */
883
1082
  function indexAdded(records) {
884
1083
  for (const record of records) for (const node of record.addedNodes) if (node.nodeType === 1) indexMarkers(node);
885
- else if (node.nodeType === 8 && MARKER.test(node.data)) comments.set(markerKey(node.data), node);
1084
+ else if (node.nodeType === 8 && MARKER.test(node.data)) {
1085
+ const data = node.data;
1086
+ comments.set(markerKey(data), node);
1087
+ if (data[0] === "P") portals++;
1088
+ }
886
1089
  }
887
1090
  /**
888
1091
  * Finds a marker in the page. Returns undefined when the markup is not in
@@ -902,10 +1105,17 @@ function findElement(scope, anchor) {
902
1105
  const key = `${scope}:${anchor}`;
903
1106
  let element = elements.get(key);
904
1107
  if (!element?.isConnected) {
905
- element = document.querySelector(`[b-b="${key}"]`) ?? void 0;
906
- if (element) elements.set(key, element);
1108
+ if (markerObserver) {
1109
+ indexAdded(markerObserver.takeRecords());
1110
+ indexInserts();
1111
+ element = elements.get(key);
1112
+ }
1113
+ if (!element?.isConnected) {
1114
+ element = document.querySelector(`[b-b="${key}"]`) ?? void 0;
1115
+ if (element) elements.set(key, element);
1116
+ }
907
1117
  }
908
- return element ?? null;
1118
+ return element?.isConnected ? element : null;
909
1119
  }
910
1120
  /** Returns the marker that ends the range that `start` opens. */
911
1121
  function rangeEnd(start) {
@@ -920,7 +1130,11 @@ function rangeEnd(start) {
920
1130
  throw new Error(`Missing end marker for <!--${start.data}-->`);
921
1131
  }
922
1132
  function clear(start, end) {
923
- while (start.nextSibling && start.nextSibling !== end) start.nextSibling.remove();
1133
+ if (start.nextSibling === end || !start.nextSibling) return;
1134
+ const range = document.createRange();
1135
+ range.setStartAfter(start);
1136
+ range.setEndBefore(end);
1137
+ range.deleteContents();
924
1138
  }
925
1139
  /**
926
1140
  * Inserts rendered HTML before a node, or at the end of a parent, and runs
@@ -931,35 +1145,31 @@ function insertHTML(before, html) {
931
1145
  template.innerHTML = trustedHTML(html);
932
1146
  const fragment = template.content;
933
1147
  if (markerObserver) indexAdded(markerObserver.takeRecords());
934
- indexMarkers(fragment);
1148
+ const client = rendered.splice(0);
1149
+ const fromServer = SERVER_SCOPES.test(html);
1150
+ const ids = fromServer ? /* @__PURE__ */ new Set() : void 0;
1151
+ indexMarkers(fragment, ids, fromServer);
935
1152
  options.listen(fragment);
936
1153
  watchTitles(fragment);
937
- const server = serverScopes(fragment);
1154
+ const server = [];
1155
+ for (const id of ids ?? []) {
1156
+ const scope = typeof id === "number" && id < 0 ? void 0 : scopes.get(id);
1157
+ if (scope) server.push(scope);
1158
+ }
1159
+ if (!fromServer && fragment.firstChild) {
1160
+ unindexed.push([fragment.firstChild, fragment.lastChild]);
1161
+ if (unindexed.length > 256) indexInserts();
1162
+ }
938
1163
  const added = hiding?.bodies.size ? [...fragment.childNodes] : void 0;
939
1164
  if ("parent" in before) before.parent.append(fragment);
940
1165
  else before.parentNode.insertBefore(fragment, before);
941
1166
  if (added) hiding.added(added);
942
1167
  markerObserver?.takeRecords();
1168
+ options.inserted?.();
943
1169
  for (const fn of afterInsert.splice(0)) fn();
944
1170
  if (server.length > 0) refresh(server);
945
- }
946
- /**
947
- * Returns the scopes from the server that markup refers to. Markup that the
948
- * client renders has client scopes, so these come from HTML that the server
949
- * rendered, such as the `children` of a component.
950
- */
951
- function serverScopes(root) {
952
- const ids = /* @__PURE__ */ new Set();
953
- for (const element of root.querySelectorAll("[b-b]")) ids.add(parseId(element.getAttribute("b-b").split(":")[0]));
954
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
955
- for (let node = walker.nextNode(); node; node = walker.nextNode()) {
956
- const match = /^[tb]([\d.]+):/.exec(node.data);
957
- if (match) ids.add(parseId(match[1]));
958
- }
959
- return [...ids].flatMap((id) => {
960
- const scope = scopes.get(id);
961
- return scope ? [scope] : [];
962
- });
1171
+ const live = client.filter((scope) => scopes.has(scope.id));
1172
+ if (live.length > 0) refresh(live, true);
963
1173
  }
964
1174
  /**
965
1175
  * Renders a `#portal` body into its target, between markers with its scope
@@ -990,8 +1200,8 @@ function writeText(scope, anchor, value) {
990
1200
  const element = findElement(scope.id, anchor);
991
1201
  if (element) {
992
1202
  const written = textValue(value);
993
- element.textContent = written;
994
- if (element instanceof HTMLTextAreaElement) element.value = written;
1203
+ if (element.textContent !== written) element.textContent = written;
1204
+ if (element instanceof HTMLTextAreaElement && element.value !== written) element.value = written;
995
1205
  }
996
1206
  }
997
1207
  /**
@@ -1001,8 +1211,9 @@ function writeText(scope, anchor, value) {
1001
1211
  function write(parent, first, end, value) {
1002
1212
  if ((typeof value === "string" || typeof value === "number") && first?.nodeType === 3 && first.nextSibling === end) {
1003
1213
  const saved = hiding?.nodes.get(first);
1004
- if (saved) saved.v = String(value);
1005
- else first.data = String(value);
1214
+ const text = String(value);
1215
+ if (saved) saved.v = text;
1216
+ else if (first.data !== text) first.data = text;
1006
1217
  return;
1007
1218
  }
1008
1219
  while (first && first !== end) {
@@ -1012,9 +1223,21 @@ function write(parent, first, end, value) {
1012
1223
  }
1013
1224
  insertHTML(end ?? { parent }, insertValue(value));
1014
1225
  }
1226
+ /** The `value` and `checked` that updates last wrote to each field. */
1227
+ const fieldValues = /* @__PURE__ */ new WeakMap();
1015
1228
  function setAttribute(element, name, value) {
1016
- if (name === "value" || name === "checked") element[name] = name === "checked" ? Boolean(value) : value ?? "";
1229
+ if (isBinding(value)) value = value.value;
1230
+ if (name === "value" || name === "checked") {
1231
+ const field = element;
1232
+ const next = name === "checked" ? Boolean(value) : String(value ?? "");
1233
+ const written = fieldValues.get(element) ?? /* @__PURE__ */ new Map();
1234
+ fieldValues.set(element, written);
1235
+ const before = written.has(name) ? written.get(name) : field[name];
1236
+ written.set(name, next);
1237
+ if (!Object.is(before, next) && field[name] !== next) field[name] = next;
1238
+ }
1017
1239
  const written = attributeText(name, value);
1240
+ if (element.getAttribute(name) === (written ?? null)) return;
1018
1241
  if (written === void 0) element.removeAttribute(name);
1019
1242
  else element.setAttribute(name, written);
1020
1243
  if (name === "style") hiding?.styled(element);
@@ -1067,7 +1290,7 @@ function rekey(scope, anchor, value, render, kinds) {
1067
1290
  * body throws, the fallback renders again with the error.
1068
1291
  */
1069
1292
  async function reset(scope, anchor, id, body, fallback, kinds) {
1070
- const updates = await loadPart(await loadModule(id), "update");
1293
+ const updates = await loadPart(await loadModule(id, "reset"), "update", "reset");
1071
1294
  const start = findComment(`b${scope.id}:${anchor}`);
1072
1295
  if (!start) return;
1073
1296
  const end = rangeEnd(start);
@@ -1254,69 +1477,138 @@ function createHiding() {
1254
1477
  }
1255
1478
  };
1256
1479
  }
1480
+ const ITEM_MARKER = /^i(-?[\d.]+)$/;
1257
1481
  /** Updates the items of `#for` or `#repeat`, keeping the elements of items whose keys stay. */
1482
+ /** The items of a `#for` list: an array as it is, since `list` only reads it, or any iterable as an array. */
1483
+ function listOf(value) {
1484
+ return Array.isArray(value) ? value : Array.from(value);
1485
+ }
1258
1486
  function list(scope, anchor, items, key, render, assign, fallback, fallbackKinds) {
1259
1487
  const start = findComment(`b${scope.id}:${anchor}`);
1260
1488
  if (!start) return;
1261
1489
  const end = rangeEnd(start);
1262
1490
  const previous = /* @__PURE__ */ new Map();
1263
- const others = [];
1491
+ let others;
1264
1492
  let position = 0;
1265
1493
  for (let node = start.nextSibling; node && node !== end;) {
1266
- const data = node.nodeType === 8 ? node.data : void 0;
1267
- const id = data ? /^i(-?[\d.]+)$/.exec(data)?.[1] : node.getAttribute?.("b-b")?.split(":")[0];
1494
+ let data;
1495
+ let id;
1496
+ if (node.nodeType === 8) {
1497
+ data = node.data;
1498
+ id = ITEM_MARKER.exec(data)?.[1];
1499
+ } else if (node.nodeType === 1) {
1500
+ const bound = node.getAttribute("b-b");
1501
+ id = bound === null ? void 0 : bound.slice(0, bound.indexOf(":"));
1502
+ }
1268
1503
  const item = id === void 0 ? void 0 : scopes.get(parseId(id));
1269
1504
  let last = node;
1270
- if (item && (data || item.p === scope && !fallbackKinds.includes(item.k))) {
1271
- if (data) last = rangeEnd(node);
1272
- const nodes = [node];
1273
- for (let current = node; current !== last;) nodes.push(current = current.nextSibling);
1274
- previous.set(key(item.i, position++), {
1505
+ if (item && (data !== void 0 || item.p === scope && !fallbackKinds.includes(item.k))) {
1506
+ if (data !== void 0) last = rangeEnd(node);
1507
+ previous.set(key(item.i, position), {
1275
1508
  scope: item,
1276
- nodes
1509
+ first: node,
1510
+ last,
1511
+ position: position++,
1512
+ kept: false,
1513
+ still: false
1277
1514
  });
1278
- } else others.push(node);
1515
+ } else (others ??= []).push(node);
1279
1516
  node = last.nextSibling;
1280
1517
  }
1281
- if (items.length === 0) {
1282
- if (previous.size === 0 && others.length > 0) return;
1518
+ const count = items.length;
1519
+ if (count === 0) {
1520
+ if (previous.size === 0 && others) return;
1283
1521
  for (const item of previous.values()) destroy(item.scope);
1284
1522
  clear(start, end);
1285
1523
  if (fallback) insertHTML(end, fallback(scope));
1286
1524
  return;
1287
1525
  }
1288
1526
  disposeKinds(scope, fallbackKinds);
1289
- for (const node of others) node.parentNode?.removeChild(node);
1527
+ if (others) for (const node of others) node.parentNode?.removeChild(node);
1290
1528
  const parent = end.parentNode;
1529
+ const keys = Array.from({ length: count });
1530
+ const kept = [];
1531
+ for (let index = 0; index < count; index++) {
1532
+ const id = keys[index] = key(items[index], index);
1533
+ const item = previous.get(id);
1534
+ if (item && !item.kept) {
1535
+ item.kept = true;
1536
+ kept.push(item);
1537
+ }
1538
+ }
1539
+ markInPlace(kept);
1540
+ if (kept.length === 0 && previous.size > 0) {
1541
+ for (const item of previous.values()) destroy(item.scope);
1542
+ previous.clear();
1543
+ clear(start, end);
1544
+ }
1291
1545
  let cursor = start.nextSibling;
1292
1546
  let added = "";
1293
- const flush = () => {
1547
+ for (let index = 0; index < count; index++) {
1548
+ const id = keys[index];
1549
+ const item = previous.get(id);
1550
+ if (!item) {
1551
+ added += render(scope, items[index], index);
1552
+ continue;
1553
+ }
1294
1554
  if (added) {
1295
1555
  insertHTML(cursor, added);
1296
1556
  added = "";
1297
1557
  }
1298
- };
1299
- items.forEach((value, index) => {
1300
- const id = key(value, index);
1301
- const item = previous.get(id);
1302
- if (item) {
1303
- flush();
1304
- previous.delete(id);
1305
- assign(item.scope, value, index);
1306
- if (item.nodes[0] === cursor) cursor = item.nodes.at(-1).nextSibling;
1307
- else for (const node of item.nodes) parent.insertBefore(node, cursor);
1308
- } else added += render(scope, value, index);
1309
- });
1310
- flush();
1558
+ previous.delete(id);
1559
+ assign(item.scope, items[index], index);
1560
+ if (item.still) cursor = item.last.nextSibling;
1561
+ else for (let node = item.first;;) {
1562
+ const next = node.nextSibling;
1563
+ parent.insertBefore(node, cursor);
1564
+ if (node === item.last) break;
1565
+ node = next;
1566
+ }
1567
+ }
1568
+ if (added) insertHTML(cursor, added);
1311
1569
  for (const item of previous.values()) {
1312
1570
  destroy(item.scope);
1313
- for (const node of item.nodes) node.parentNode?.removeChild(node);
1571
+ for (let node = item.first; node;) {
1572
+ const next = node === item.last ? null : node.nextSibling;
1573
+ node.parentNode?.removeChild(node);
1574
+ node = next;
1575
+ }
1576
+ }
1577
+ }
1578
+ /**
1579
+ * Marks the items of a list that keep their places: the longest run of `kept`, the items that
1580
+ * stay in their new order, whose old positions go up.
1581
+ */
1582
+ function markInPlace(kept) {
1583
+ let sorted = true;
1584
+ for (let index = 1; index < kept.length; index++) if (kept[index - 1].position > kept[index].position) {
1585
+ sorted = false;
1586
+ break;
1587
+ }
1588
+ if (sorted) {
1589
+ for (const item of kept) item.still = true;
1590
+ return;
1591
+ }
1592
+ const tails = [];
1593
+ const before = [];
1594
+ for (let index = 0; index < kept.length; index++) {
1595
+ const position = kept[index].position;
1596
+ let low = 0;
1597
+ let high = tails.length;
1598
+ while (low < high) {
1599
+ const middle = low + high >> 1;
1600
+ if (kept[tails[middle]].position < position) low = middle + 1;
1601
+ else high = middle;
1602
+ }
1603
+ before[index] = low > 0 ? tails[low - 1] : -1;
1604
+ tails[low] = index;
1314
1605
  }
1606
+ for (let index = tails.at(-1) ?? -1; index >= 0; index = before[index]) kept[index].still = true;
1315
1607
  }
1316
1608
  /** Sets a style variable on the top-level elements of a component instance. */
1317
1609
  function setVar(scope, name, value) {
1318
1610
  const css = value === null || value === void 0 || value === false ? "" : String(value);
1319
- for (const element of document.querySelectorAll(`[b-v="${scope.id}"]`)) element.style.setProperty(name, css);
1611
+ for (const element of document.querySelectorAll(`[b-v="${scope.id}"]`)) if (element.style.getPropertyValue(name) !== css) element.style.setProperty(name, css);
1320
1612
  }
1321
1613
  /** The scope that provides a context to `scope`: itself or the nearest one above it. */
1322
1614
  function findProvider(scope, key) {
@@ -1375,7 +1667,23 @@ function watchTitles(root) {
1375
1667
  });
1376
1668
  }
1377
1669
  /** Writes a prop of a child component. */
1670
+ const spreadKeys = /* @__PURE__ */ new WeakMap();
1671
+ /**
1672
+ * Updates the props of a child component from a spread. A key that the
1673
+ * spread passed before and no longer has becomes undefined. `after` names
1674
+ * the props after the spread, which win over it.
1675
+ */
1676
+ function spreadProps(scope, anchor, value, after) {
1677
+ const byAnchor = spreadKeys.get(scope) ?? /* @__PURE__ */ new Map();
1678
+ spreadKeys.set(scope, byAnchor);
1679
+ const before = byAnchor.get(anchor) ?? /* @__PURE__ */ new Set();
1680
+ const keys = new Set(Object.keys(value ?? {}).filter((key) => !after.includes(key)));
1681
+ byAnchor.set(anchor, keys);
1682
+ for (const key of keys) prop(scope, anchor, key, value[key]);
1683
+ for (const key of before) if (!keys.has(key)) prop(scope, anchor, key, void 0);
1684
+ }
1378
1685
  function prop(scope, anchor, name, value) {
1686
+ if (isBinding(value)) value = value.value;
1379
1687
  const id = scope.c?.[anchor];
1380
1688
  const child = id === void 0 ? void 0 : scopes.get(id);
1381
1689
  if (!child?.m) return;
@@ -1383,7 +1691,7 @@ function prop(scope, anchor, name, value) {
1383
1691
  const slot = module.props[name];
1384
1692
  if (slot !== void 0) {
1385
1693
  if (isThenable(value)) asyncMemo(child, slot, Promise.resolve(value));
1386
- else set(child, slot, value);
1694
+ else writeSlot(child, slot, value);
1387
1695
  return;
1388
1696
  }
1389
1697
  const all = module.props["*"];
@@ -1394,7 +1702,7 @@ function prop(scope, anchor, name, value) {
1394
1702
  };
1395
1703
  const module = modules.get(child.m);
1396
1704
  if (module) apply(module);
1397
- else loadModule(child.m).then(apply);
1705
+ else loadModule(child.m, "prop").then(apply);
1398
1706
  }
1399
1707
  /** Runs an effect or a `$client` callback, after the cleanup of its previous run. */
1400
1708
  function effect(scope, name, callback) {
@@ -1402,12 +1710,22 @@ function effect(scope, name, callback) {
1402
1710
  scope.cleanups ??= /* @__PURE__ */ new Map();
1403
1711
  scope.cleanups.get(name)?.();
1404
1712
  let cleanup;
1713
+ const start = DEV ? performance.now() : 0;
1405
1714
  try {
1406
1715
  cleanup = callback();
1407
1716
  } catch (error) {
1408
1717
  fail(scope, error);
1409
1718
  }
1410
- scope.cleanups.set(name, typeof cleanup === "function" ? cleanup : noop);
1719
+ let stored = typeof cleanup === "function" ? cleanup : noop;
1720
+ if (DEV) {
1721
+ devtools()?.effect?.(scope, name, "run", performance.now() - start);
1722
+ const own = stored;
1723
+ stored = () => {
1724
+ devtools()?.effect?.(scope, name, "cleanup", 0);
1725
+ own();
1726
+ };
1727
+ }
1728
+ scope.cleanups.set(name, stored);
1411
1729
  }
1412
1730
  function noop() {}
1413
1731
  /** Renders the attributes of a form or button that posts to a server function. */
@@ -1419,12 +1737,12 @@ function action(name, id, method, result) {
1419
1737
  * call loads, so the core holds only this.
1420
1738
  */
1421
1739
  function submitAction(form, submitter) {
1422
- import("./rpc-B-Zkn6Ch.js").then((module) => module.sendAction(form, submitter));
1740
+ import("./rpc-ygMRPmN4.js").then((module) => module.sendAction(form, submitter));
1423
1741
  }
1424
1742
  /** Calls a server function. */
1425
1743
  async function rpc(id, args) {
1426
1744
  const signal = memoSignal();
1427
- return track(import("./rpc-B-Zkn6Ch.js").then((module) => module.call(rpcPrefix, id, args, signal)));
1745
+ return track(import("./rpc-ygMRPmN4.js").then((module) => module.call(rpcPrefix, id, args, signal)));
1428
1746
  }
1429
1747
  /** Renders a value inserted in markup. Children render here, at `scope`, for their contexts. */
1430
1748
  function insert(value, _ctx, scope) {
@@ -1463,7 +1781,8 @@ function children(from, html) {
1463
1781
  return new Children(from, html);
1464
1782
  }
1465
1783
  function resolveScope(scope) {
1466
- return typeof scope === "object" ? scope : scope === void 0 ? void 0 : scopes.get(scope);
1784
+ if (typeof scope === "object") return scope;
1785
+ return scope === void 0 ? void 0 : scopes.get(scope);
1467
1786
  }
1468
1787
  /**
1469
1788
  * Renders a child component on the client. With `scope` and `anchor`,
@@ -1484,7 +1803,7 @@ function dynamic(tag, attributes, inner, _ctx, scope) {
1484
1803
  children: inner
1485
1804
  } : attributes, void 0, scope);
1486
1805
  const name = String(tag);
1487
- const open = `<${name}${spread(attributes)}>`;
1806
+ const open = `<${name}${spread$1(attributes)}>`;
1488
1807
  return VOID_ELEMENTS.has(name) ? open : `${open}${inner ? insert(inner, void 0, scope) : ""}</${name}>`;
1489
1808
  }
1490
1809
  /** Work that waits until rendered markup is in the page. */
@@ -1503,13 +1822,16 @@ function createScope(kind, parent, module) {
1503
1822
  s: [],
1504
1823
  children: /* @__PURE__ */ new Set()
1505
1824
  };
1825
+ rendered.push(created);
1506
1826
  if (module !== void 0 && owner) {
1507
1827
  created.o = owner;
1508
1828
  owner = void 0;
1509
1829
  }
1510
1830
  const up = created.o ?? parentScope;
1511
- const inserted = insertedChildren.findLast((entry) => entry.from === up);
1512
- if (inserted) created.t = inserted.at;
1831
+ for (let index = insertedChildren.length - 1; index >= 0; index--) if (insertedChildren[index].from === up) {
1832
+ created.t = insertedChildren[index].at;
1833
+ break;
1834
+ }
1513
1835
  parentScope?.children.add(created);
1514
1836
  treeChanges++;
1515
1837
  scopes.set(id, created);
@@ -1580,12 +1902,13 @@ function updateRoute() {
1580
1902
  const renderedLoads = /* @__PURE__ */ new Set();
1581
1903
  function setSlots(id, values) {
1582
1904
  scopes.get(id).s = values;
1583
- values.forEach((value, slot) => {
1905
+ for (let slot = 0; slot < values.length; slot++) {
1906
+ const value = values[slot];
1584
1907
  if (isThenable(value)) {
1585
1908
  renderedLoads.add(`${id}:${slot}`);
1586
1909
  Promise.resolve(value).catch(noop);
1587
1910
  }
1588
- });
1911
+ }
1589
1912
  }
1590
1913
  function setItem(id, value) {
1591
1914
  scopes.get(id).i = value;
@@ -1620,10 +1943,12 @@ function addStyle(id, css) {
1620
1943
  return "";
1621
1944
  }
1622
1945
  /** A function prop rendered on the client calls the parent's exported handler. */
1623
- function handlerProp(value, ref, scope) {
1946
+ function handlerProp(value, ref, scope, bound = [], modifiers) {
1624
1947
  if (typeof value !== "function" || isTemplate(ref)) return value;
1625
1948
  const instance = resolveScope(scope);
1626
- return (...args) => call(ref, instance, args);
1949
+ const handler = (...args) => call(ref, instance, [...bound, ...args]);
1950
+ handlerReferences.set(handler, new FunctionReference(ref, instance?.id ?? scope, bound.length > 0 ? bound : void 0, modifiers ?? value[MODIFIERS]));
1951
+ return handler;
1627
1952
  }
1628
1953
  /**
1629
1954
  * What the loader calls. It is one export, so the loader's import of the core
@@ -1638,6 +1963,6 @@ const loader = {
1638
1963
  updateRoute
1639
1964
  };
1640
1965
  //#endregion
1641
- export { rpcPrefix as $, handlerProp as A, makeId as B, elementAt as C, get as D, forget as E, leaveRoutes as F, prop as G, offscreen as H, list as I, remount as J, provideValue as K, loadModule as L, init as M, insert as N, guess as O, leavePage as P, rpc as Q, loadPart as R, effect as S, fill as T, pending as U, memo as V, portalTo as W, removeScope as X, removeRoot as Y, reset as Z, createScope as _, writeText as _t, addStyle as a, setOwner as at, dispose as b, attr as c, skew as ct, build as d, timed as dt, scopeOf as et, cells as f, title as ft, content as g, updateRoute as gt, consumeContext as h, updateContext as ht, addPortal as i, setItem as it, hooks as j, handle as k, attrs as l, submitAction as lt, component as m, update as mt, action as n, set as nt, afterInsert as o, setSlots as ot, children as p, track as pt, rekey as q, addHead as r, setGlobal as rt, asyncMemo as s, setVar as st, DEVTOOLS as t, scopes as tt, branch as u, text as ut, deferInBrowser as v, fail as w, dynamic as x, descendants as y, loader as z };
1966
+ export { removeScope as $, guess as A, loadModule as B, dynamic as C, writeText as Ct, fill as D, fail as E, insert as F, offscreen as G, loader as H, leavePage as I, prop as J, pending as K, leaveRoutes as L, handlerProp as M, hooks as N, forget as O, init as P, removeRoot as Q, list as R, dispose as S, updated as St, elementAt as T, makeId as U, loadPart as V, memo as W, rekey as X, provideValue as Y, remount as Z, consumeContext as _, title as _t, addStyle as a, set as at, deferInBrowser as b, updateContext as bt, attr as c, setOwner as ct, bindings as d, skew as dt, reset as et, branch as f, spread as ft, component as g, timed as gt, children as h, text as ht, addPortal as i, scopes as it, handle as j, get as k, attrs as l, setSlots as lt, cells as m, submitAction as mt, action as n, rpcPrefix as nt, afterInsert as o, setGlobal as ot, build as p, spreadProps as pt, portalTo as q, addHead as r, scopeOf as rt, asyncMemo as s, setItem as st, DEVTOOLS as t, rpc as tt, binding as u, setVar as ut, content as v, track as vt, effect as w, descendants as x, updateRoute as xt, createScope as y, update as yt, listOf as z };
1642
1967
 
1643
- //# sourceMappingURL=client-C0UCNL3J.js.map
1968
+ //# sourceMappingURL=client-BD6XT-s5.js.map