@askrjs/askr 0.0.41 → 0.0.43

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 (55) hide show
  1. package/dist/benchmark.js +2 -2
  2. package/dist/boot/index.d.ts.map +1 -1
  3. package/dist/boot/index.js +2 -0
  4. package/dist/boot/index.js.map +1 -1
  5. package/dist/common/route-activity.js +30 -0
  6. package/dist/common/route-activity.js.map +1 -0
  7. package/dist/common/router.d.ts +4 -2
  8. package/dist/common/router.d.ts.map +1 -1
  9. package/dist/data/index.d.ts +21 -2
  10. package/dist/data/index.d.ts.map +1 -1
  11. package/dist/data/index.js +56 -3
  12. package/dist/data/index.js.map +1 -1
  13. package/dist/data/invalidation-listeners.js +15 -0
  14. package/dist/data/invalidation-listeners.js.map +1 -0
  15. package/dist/renderer/dom.js +35 -20
  16. package/dist/renderer/dom.js.map +1 -1
  17. package/dist/renderer/evaluate.js +27 -17
  18. package/dist/renderer/evaluate.js.map +1 -1
  19. package/dist/renderer/for-commit.js +9 -0
  20. package/dist/renderer/for-commit.js.map +1 -1
  21. package/dist/resources/index.d.ts +2 -2
  22. package/dist/resources/index.js +2 -2
  23. package/dist/router/match.js +31 -6
  24. package/dist/router/match.js.map +1 -1
  25. package/dist/router/navigate.d.ts.map +1 -1
  26. package/dist/router/navigate.js +30 -4
  27. package/dist/router/navigate.js.map +1 -1
  28. package/dist/router/route.d.ts +6 -1
  29. package/dist/router/route.d.ts.map +1 -1
  30. package/dist/router/route.js +35 -8
  31. package/dist/router/route.js.map +1 -1
  32. package/dist/runtime/child-scope.js +1 -0
  33. package/dist/runtime/child-scope.js.map +1 -1
  34. package/dist/runtime/component.d.ts +8 -1
  35. package/dist/runtime/component.d.ts.map +1 -1
  36. package/dist/runtime/component.js +207 -44
  37. package/dist/runtime/component.js.map +1 -1
  38. package/dist/runtime/fastlane.js +6 -0
  39. package/dist/runtime/fastlane.js.map +1 -1
  40. package/dist/runtime/for.d.ts.map +1 -1
  41. package/dist/runtime/for.js +18 -1
  42. package/dist/runtime/for.js.map +1 -1
  43. package/dist/runtime/operations.d.ts +11 -3
  44. package/dist/runtime/operations.d.ts.map +1 -1
  45. package/dist/runtime/operations.js +161 -22
  46. package/dist/runtime/operations.js.map +1 -1
  47. package/dist/runtime/readable.d.ts +1 -0
  48. package/dist/runtime/readable.d.ts.map +1 -1
  49. package/dist/runtime/readable.js +7 -4
  50. package/dist/runtime/readable.js.map +1 -1
  51. package/dist/testing/index.d.ts +53 -0
  52. package/dist/testing/index.d.ts.map +1 -0
  53. package/dist/testing/index.js +172 -0
  54. package/dist/testing/index.js.map +1 -0
  55. package/package.json +11 -3
@@ -2,7 +2,7 @@ import { isDevelopmentEnvironment, isProductionEnvironment } from "../common/env
2
2
  import { logger } from "../dev/logger.js";
3
3
  import { globalScheduler } from "./scheduler.js";
4
4
  import { withContext } from "./context.js";
5
- import { cleanupReadableSubscriptions, finalizeReadableSubscriptions } from "./readable.js";
5
+ import { cleanupReadableSubscriptions, finalizeReadableSubscriptions, finalizeReadableSubscriptionsFromSnapshot } from "./readable.js";
6
6
  import { incDevCounter, setDevValue } from "./dev-namespace.js";
7
7
  import { isPromiseLike } from "../common/promise.js";
8
8
  import { isBulkCommitActive } from "./fastlane.js";
@@ -20,6 +20,7 @@ function createComponentInstance(id, fn, props, target) {
20
20
  fn,
21
21
  props,
22
22
  target,
23
+ parentInstance: currentInstance,
23
24
  portalScope: currentInstance?.portalScope ?? currentPortalScope ?? null,
24
25
  mounted: false,
25
26
  abortController: null,
@@ -33,7 +34,9 @@ function createComponentInstance(id, fn, props, target) {
33
34
  expectedStateIndices: [],
34
35
  firstRenderComplete: false,
35
36
  mountOperations: [],
37
+ commitOperations: [],
36
38
  cleanupFns: [],
39
+ lifecycleSlots: [],
37
40
  lifecycleGeneration: 0,
38
41
  hasPendingUpdate: false,
39
42
  ownerFrame: null,
@@ -81,46 +84,166 @@ function setCurrentComponentInstance(instance) {
81
84
  function getCurrentPortalScope() {
82
85
  return currentInstance?.portalScope ?? currentPortalScope;
83
86
  }
84
- function registerMountOperation(operation) {
87
+ let currentLifecycleCommitBatch = null;
88
+ function beginLifecycleCommitBatch() {
89
+ const batch = {
90
+ parent: currentLifecycleCommitBatch,
91
+ entries: [],
92
+ entriesByInstance: /* @__PURE__ */ new Map(),
93
+ readCommits: [],
94
+ readCommitsByInstance: /* @__PURE__ */ new Map(),
95
+ renderSnapshots: [],
96
+ renderSnapshotsByInstance: /* @__PURE__ */ new Map(),
97
+ active: true
98
+ };
99
+ currentLifecycleCommitBatch = batch;
100
+ return batch;
101
+ }
102
+ function closeLifecycleCommitBatch(batch) {
103
+ if (!batch.active) return false;
104
+ batch.active = false;
105
+ currentLifecycleCommitBatch = batch.parent;
106
+ return true;
107
+ }
108
+ function enqueueLifecycleCommit(batch, instance, wasFirstMount) {
109
+ const existing = batch.entriesByInstance.get(instance);
110
+ if (existing) {
111
+ existing.wasFirstMount = existing.wasFirstMount || wasFirstMount;
112
+ return;
113
+ }
114
+ const entry = {
115
+ instance,
116
+ wasFirstMount
117
+ };
118
+ batch.entriesByInstance.set(instance, entry);
119
+ batch.entries.push(entry);
120
+ }
121
+ function enqueueReadSubscriptionCommit(batch, instance, token, pendingReadSources) {
122
+ const existing = batch.readCommitsByInstance.get(instance);
123
+ const commit = existing ?? {
124
+ instance,
125
+ token,
126
+ pendingReadSources
127
+ };
128
+ commit.token = token;
129
+ commit.pendingReadSources = pendingReadSources ? new Set(pendingReadSources) : void 0;
130
+ if (!existing) {
131
+ batch.readCommitsByInstance.set(instance, commit);
132
+ batch.readCommits.push(commit);
133
+ }
134
+ }
135
+ function enqueueInlineRenderSnapshot(batch, snapshot) {
136
+ if (batch.renderSnapshotsByInstance.has(snapshot.instance)) return;
137
+ batch.renderSnapshotsByInstance.set(snapshot.instance, snapshot);
138
+ batch.renderSnapshots.push(snapshot);
139
+ }
140
+ function captureInlineRenderSnapshot(instance) {
141
+ if (!currentLifecycleCommitBatch?.active) return;
142
+ enqueueInlineRenderSnapshot(currentLifecycleCommitBatch, {
143
+ instance,
144
+ props: instance.props,
145
+ ownerFrame: instance.ownerFrame,
146
+ portalScope: instance.portalScope,
147
+ parentInstance: instance.parentInstance,
148
+ isRoot: instance.isRoot
149
+ });
150
+ }
151
+ function finalizeInlineReadSubscriptions(instance, token, pendingReadSources) {
152
+ if (currentLifecycleCommitBatch?.active) {
153
+ enqueueReadSubscriptionCommit(currentLifecycleCommitBatch, instance, token, pendingReadSources);
154
+ return;
155
+ }
156
+ finalizeReadableSubscriptionsFromSnapshot(instance, token, pendingReadSources);
157
+ }
158
+ function flushLifecycleCommitBatch(batch) {
159
+ if (!closeLifecycleCommitBatch(batch)) return;
160
+ if (batch.parent?.active) {
161
+ for (const snapshot of batch.renderSnapshots) enqueueInlineRenderSnapshot(batch.parent, snapshot);
162
+ for (const commit of batch.readCommits) enqueueReadSubscriptionCommit(batch.parent, commit.instance, commit.token, commit.pendingReadSources);
163
+ for (const entry of batch.entries) enqueueLifecycleCommit(batch.parent, entry.instance, entry.wasFirstMount);
164
+ return;
165
+ }
166
+ for (const commit of batch.readCommits) finalizeReadableSubscriptionsFromSnapshot(commit.instance, commit.token, commit.pendingReadSources);
167
+ for (const entry of batch.entries) executeCommittedLifecycleOperations(entry.instance, entry.wasFirstMount);
168
+ }
169
+ function discardLifecycleCommitBatch(batch) {
170
+ if (!closeLifecycleCommitBatch(batch)) return;
171
+ for (let index = batch.renderSnapshots.length - 1; index >= 0; index -= 1) {
172
+ const snapshot = batch.renderSnapshots[index];
173
+ snapshot.instance.props = snapshot.props;
174
+ snapshot.instance.ownerFrame = snapshot.ownerFrame;
175
+ snapshot.instance.portalScope = snapshot.portalScope;
176
+ snapshot.instance.parentInstance = snapshot.parentInstance;
177
+ snapshot.instance.isRoot = snapshot.isRoot;
178
+ }
179
+ for (const entry of batch.entries) {
180
+ if (entry.wasFirstMount) entry.instance.mountOperations = [];
181
+ discardCommitOperations(entry.instance);
182
+ }
183
+ }
184
+ function registerCommitOperation(operation) {
85
185
  const instance = getCurrentComponentInstance();
86
186
  if (instance) {
87
187
  if (isBulkCommitActive()) {
88
- if (isDevelopmentEnvironment()) throw new Error("registerMountOperation called during bulk commit fast-lane");
188
+ if (isDevelopmentEnvironment()) throw new Error("registerCommitOperation called during bulk commit fast-lane");
89
189
  return;
90
190
  }
91
- instance.mountOperations.push(operation);
191
+ instance.commitOperations.push(operation);
92
192
  }
93
193
  }
194
+ function settleLifecycleOperationResult(instance, lifecycleGeneration, result) {
195
+ if (isPromiseLike(result)) Promise.resolve(result).then((cleanup) => {
196
+ if (typeof cleanup === "function") {
197
+ if (instance.lifecycleGeneration === lifecycleGeneration && instance.mounted) {
198
+ instance.cleanupFns.push(cleanup);
199
+ return;
200
+ }
201
+ try {
202
+ cleanup();
203
+ } catch (err) {
204
+ logger.error("[Askr] async mount cleanup failed:", err);
205
+ }
206
+ }
207
+ }, (err) => {
208
+ logger.error("[Askr] async mount operation failed:", err);
209
+ });
210
+ else if (typeof result === "function") instance.cleanupFns.push(result);
211
+ }
94
212
  /**
95
213
  * Execute all mount operations for a component
96
214
  * These run after the component is rendered and mounted to the DOM
97
215
  */
98
216
  function executeMountOperations(instance) {
99
- if (!instance.isRoot) return;
100
217
  const mountOperations = instance.mountOperations;
101
218
  if (mountOperations.length === 0) return;
102
219
  const lifecycleGeneration = instance.lifecycleGeneration;
103
- for (const operation of mountOperations) {
104
- const result = operation();
105
- if (isPromiseLike(result)) Promise.resolve(result).then((cleanup) => {
106
- if (typeof cleanup === "function") {
107
- if (instance.lifecycleGeneration === lifecycleGeneration && instance.mounted) {
108
- instance.cleanupFns.push(cleanup);
109
- return;
110
- }
111
- try {
112
- cleanup();
113
- } catch (err) {
114
- logger.error("[Askr] async mount cleanup failed:", err);
115
- }
116
- }
117
- }, (err) => {
118
- logger.error("[Askr] async mount operation failed:", err);
119
- });
120
- else if (typeof result === "function") instance.cleanupFns.push(result);
121
- }
220
+ for (const operation of mountOperations) settleLifecycleOperationResult(instance, lifecycleGeneration, operation());
122
221
  instance.mountOperations = [];
123
222
  }
223
+ function executeCommitOperations(instance) {
224
+ const commitOperations = instance.commitOperations;
225
+ if (commitOperations.length === 0) return;
226
+ instance.commitOperations = [];
227
+ const lifecycleGeneration = instance.lifecycleGeneration;
228
+ for (const operation of commitOperations) settleLifecycleOperationResult(instance, lifecycleGeneration, operation());
229
+ }
230
+ function discardCommitOperations(instance) {
231
+ instance.commitOperations = [];
232
+ }
233
+ function executeCommittedLifecycleOperations(instance, wasFirstMount) {
234
+ if (wasFirstMount && instance.mountOperations.length > 0) executeMountOperations(instance);
235
+ if (instance.commitOperations.length > 0) executeCommitOperations(instance);
236
+ }
237
+ function commitLifecycleForInstance(instance, wasFirstMount) {
238
+ if (currentLifecycleCommitBatch) {
239
+ enqueueLifecycleCommit(currentLifecycleCommitBatch, instance, wasFirstMount);
240
+ return;
241
+ }
242
+ executeCommittedLifecycleOperations(instance, wasFirstMount);
243
+ }
244
+ function commitRenderedComponent(instance) {
245
+ if (instance.mounted && instance.commitOperations.length > 0) commitLifecycleForInstance(instance, false);
246
+ }
124
247
  function mountInstanceInline(instance, target) {
125
248
  instance.target = target;
126
249
  try {
@@ -135,7 +258,7 @@ function mountInstanceInline(instance, target) {
135
258
  instance.notifyUpdate = instance._enqueueRun;
136
259
  const wasFirstMount = !instance.mounted;
137
260
  instance.mounted = true;
138
- if (wasFirstMount && instance.mountOperations.length > 0) executeMountOperations(instance);
261
+ commitLifecycleForInstance(instance, wasFirstMount);
139
262
  }
140
263
  /**
141
264
  * Run a component synchronously: execute function, handle result
@@ -186,7 +309,13 @@ function runComponent(instance) {
186
309
  instance._currentRenderToken = nextRenderToken();
187
310
  instance._pendingReadSources = void 0;
188
311
  const domSnapshot = instance.target ? instance.target.innerHTML : "";
189
- const result = executeComponentSync(instance);
312
+ let result;
313
+ try {
314
+ result = executeComponentSync(instance);
315
+ } catch (err) {
316
+ discardCommitOperations(instance);
317
+ throw err;
318
+ }
190
319
  if (isPromiseLike(result)) throw new Error("Async components are not supported. Components must be synchronous.");
191
320
  else {
192
321
  const fastlaneBridge = globalThis.__ASKR_FASTLANE;
@@ -203,6 +332,7 @@ function runComponent(instance) {
203
332
  if (result === null || result === void 0) {
204
333
  finalizeReadSubscriptions(instance);
205
334
  warnUnusedStateReads(instance);
335
+ commitRenderedComponent(instance);
206
336
  return;
207
337
  }
208
338
  const placeholder = instance._placeholder;
@@ -218,16 +348,24 @@ function runComponent(instance) {
218
348
  };
219
349
  const oldInstance = currentInstance;
220
350
  currentInstance = instance;
351
+ const lifecycleBatch = beginLifecycleCommitBatch();
221
352
  try {
222
- withContext(executionFrame, () => {
223
- evaluate(result, host);
224
- });
225
- parent.replaceChild(host, placeholder);
353
+ try {
354
+ withContext(executionFrame, () => {
355
+ evaluate(result, host);
356
+ });
357
+ parent.replaceChild(host, placeholder);
358
+ } catch (err) {
359
+ discardLifecycleCommitBatch(lifecycleBatch);
360
+ throw err;
361
+ }
226
362
  instance.target = host;
227
363
  instance._placeholder = void 0;
228
364
  host.__ASKR_INSTANCE = instance;
365
+ flushLifecycleCommitBatch(lifecycleBatch);
229
366
  finalizeReadSubscriptions(instance);
230
367
  warnUnusedStateReads(instance);
368
+ commitRenderedComponent(instance);
231
369
  } finally {
232
370
  currentInstance = oldInstance;
233
371
  }
@@ -235,6 +373,7 @@ function runComponent(instance) {
235
373
  }
236
374
  if (instance.target) {
237
375
  let oldChildren = [];
376
+ let restoredOldChildren = false;
238
377
  try {
239
378
  const wasFirstMount = !instance.mounted;
240
379
  const oldInstance = currentInstance;
@@ -244,17 +383,27 @@ function runComponent(instance) {
244
383
  values: null
245
384
  };
246
385
  oldChildren = Array.from(instance.target.childNodes);
386
+ const lifecycleBatch = beginLifecycleCommitBatch();
247
387
  try {
248
- withContext(executionFrame, () => {
249
- evaluate(result, instance.target);
250
- });
388
+ try {
389
+ withContext(executionFrame, () => {
390
+ evaluate(result, instance.target, void 0, instance);
391
+ });
392
+ } catch (e) {
393
+ discardLifecycleCommitBatch(lifecycleBatch);
394
+ throw e;
395
+ }
251
396
  } catch (e) {
252
397
  try {
253
398
  const newChildren = Array.from(instance.target.childNodes);
254
- for (const n of newChildren) try {
255
- cleanupInstancesUnder(n);
256
- } catch (err) {
257
- logger.warn("[Askr] error cleaning up failed commit children:", err);
399
+ const preservedChildren = new Set(oldChildren);
400
+ for (const n of newChildren) {
401
+ if (preservedChildren.has(n)) continue;
402
+ try {
403
+ cleanupInstancesUnder(n);
404
+ } catch (err) {
405
+ logger.warn("[Askr] error cleaning up failed commit children:", err);
406
+ }
258
407
  }
259
408
  } catch (_err) {}
260
409
  try {
@@ -262,21 +411,28 @@ function runComponent(instance) {
262
411
  setDevValue("__LAST_DOM_REPLACE_STACK_COMPONENT_RESTORE", (/* @__PURE__ */ new Error()).stack);
263
412
  } catch (e) {}
264
413
  instance.target.replaceChildren(...oldChildren);
414
+ restoredOldChildren = true;
265
415
  throw e;
266
416
  } finally {
267
417
  currentInstance = oldInstance;
268
418
  }
419
+ flushLifecycleCommitBatch(lifecycleBatch);
269
420
  finalizeReadSubscriptions(instance);
270
421
  warnUnusedStateReads(instance);
271
422
  instance.mounted = true;
272
- if (wasFirstMount && instance.mountOperations.length > 0) executeMountOperations(instance);
423
+ executeCommittedLifecycleOperations(instance, wasFirstMount);
273
424
  } catch (renderError) {
425
+ discardCommitOperations(instance);
274
426
  try {
275
427
  const currentChildren = Array.from(instance.target.childNodes);
276
- for (const n of currentChildren) try {
277
- cleanupInstancesUnder(n);
278
- } catch (err) {
279
- logger.warn("[Askr] error cleaning up partial children during rollback:", err);
428
+ const preservedChildren = restoredOldChildren ? new Set(oldChildren) : null;
429
+ for (const n of currentChildren) {
430
+ if (preservedChildren?.has(n)) continue;
431
+ try {
432
+ cleanupInstancesUnder(n);
433
+ } catch (err) {
434
+ logger.warn("[Askr] error cleaning up partial children during rollback:", err);
435
+ }
280
436
  }
281
437
  } catch (_err) {}
282
438
  try {
@@ -298,6 +454,7 @@ function runComponent(instance) {
298
454
  * Rendering happens separately through runComponent.
299
455
  */
300
456
  function renderComponentInline(instance) {
457
+ instance.notifyUpdate = instance._enqueueRun;
301
458
  const hadToken = instance._currentRenderToken !== void 0;
302
459
  const prevToken = instance._currentRenderToken;
303
460
  const prevPendingReads = instance._pendingReadSources;
@@ -309,7 +466,8 @@ function renderComponentInline(instance) {
309
466
  }
310
467
  try {
311
468
  const result = executeComponentSync(instance);
312
- if (!hadToken) finalizeReadSubscriptions(instance);
469
+ if (!hadToken) finalizeInlineReadSubscriptions(instance, instance._currentRenderToken, instance._pendingReadSources);
470
+ commitRenderedComponent(instance);
313
471
  return result;
314
472
  } finally {
315
473
  instance._currentRenderToken = prevToken;
@@ -338,6 +496,7 @@ function executeComponentSync(instance) {
338
496
  currentInstance = instance;
339
497
  currentPortalScope = instance.portalScope ?? savedPortalScope;
340
498
  stateIndex = 0;
499
+ let didComplete = false;
341
500
  try {
342
501
  const renderStartTime = isDevelopmentEnvironment() ? Date.now() : 0;
343
502
  const context = { get signal() {
@@ -350,8 +509,10 @@ function executeComponentSync(instance) {
350
509
  const renderTime = Date.now() - renderStartTime;
351
510
  if (renderTime > 5) warnInstanceOnce(instance, "slow-render", `[askr] Slow render detected: ${renderTime}ms. Consider optimizing component performance.`);
352
511
  if (!instance.firstRenderComplete) instance.firstRenderComplete = true;
512
+ didComplete = true;
353
513
  return result;
354
514
  } finally {
515
+ if (!didComplete) discardCommitOperations(instance);
355
516
  currentInstance = null;
356
517
  currentPortalScope = savedPortalScope;
357
518
  }
@@ -493,6 +654,8 @@ function cleanupComponent(instance) {
493
654
  instance.lifecycleGeneration++;
494
655
  instance.evaluationGeneration++;
495
656
  instance.mountOperations = [];
657
+ instance.commitOperations = [];
658
+ instance.lifecycleSlots = [];
496
659
  instance.hasPendingUpdate = false;
497
660
  instance.notifyUpdate = null;
498
661
  instance._placeholder = void 0;
@@ -517,6 +680,6 @@ function warnInstanceOnce(instance, key, message) {
517
680
  logger.warn(message);
518
681
  }
519
682
  //#endregion
520
- export { claimHookIndex, cleanupComponent, createComponentInstance, finalizeReadSubscriptions, getCurrentComponentInstance, getCurrentInstance, getCurrentPortalScope, getCurrentStateIndex, getSignal, mountComponent, mountInstanceInline, registerMountOperation, registerOwnedChildScope, renderComponentInline, renderScopedComponent, setCurrentComponentInstance, unregisterOwnedChildScope, warnUnusedStateReads };
683
+ export { captureInlineRenderSnapshot, claimHookIndex, cleanupComponent, createComponentInstance, finalizeReadSubscriptions, getCurrentComponentInstance, getCurrentInstance, getCurrentPortalScope, getCurrentStateIndex, getSignal, mountComponent, mountInstanceInline, registerCommitOperation, registerOwnedChildScope, renderComponentInline, renderScopedComponent, setCurrentComponentInstance, unregisterOwnedChildScope, warnUnusedStateReads };
521
684
 
522
685
  //# sourceMappingURL=component.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"component.js","names":[],"sources":["../../src/runtime/component.ts"],"sourcesContent":["/**\n * Component instance lifecycle management\n * Internal only — users never see this\n */\n\nimport { type State } from './state';\nimport { globalScheduler } from './scheduler';\nimport type { Props } from '../common/props';\nimport type { ComponentFunction } from '../common/component';\nimport {\n // withContext is the sole primitive for context restoration\n withContext,\n type ContextFrame,\n} from './context';\nimport {\n type ReadableSource,\n finalizeReadableSubscriptions,\n cleanupReadableSubscriptions,\n} from './readable';\nimport {\n isDevelopmentEnvironment,\n isProductionEnvironment,\n} from '../common/env';\nimport { logger } from '../dev/logger';\nimport { incDevCounter, setDevValue } from './dev-namespace';\nimport { isPromiseLike } from '../common/promise';\n\nexport type { ComponentFunction } from '../common/component';\n\nexport interface ComponentInstance {\n id: string;\n fn: ComponentFunction;\n props: Props;\n target: Element | null;\n portalScope: object | null;\n mounted: boolean;\n abortController: AbortController | null; // Lazily created per-component abort lifecycle\n ssr?: boolean; // Set to true for SSR temporary instances\n // Opt-in strict cleanup mode: when true cleanup errors are aggregated and re-thrown\n cleanupStrict?: boolean;\n stateValues: State<unknown>[]; // Persistent state storage across renders\n evaluationGeneration: number; // Prevents stale async evaluation completions\n notifyUpdate: (() => void) | null; // Callback for state updates (persisted on instance)\n // Internal: prebound helpers to avoid per-update closures (allocation hot-path)\n _pendingFlushTask?: () => void; // Clears hasPendingUpdate and triggers notifyUpdate\n _pendingRunTask?: () => void; // Clears hasPendingUpdate and runs component\n _enqueueRun?: () => void; // Batches run requests and enqueues _pendingRunTask\n stateIndexCheck: number; // Track state indices to catch conditional calls\n expectedStateIndices: number[]; // Expected sequence of render-scoped hook indices (frozen after first render)\n firstRenderComplete: boolean; // Flag to detect transition from first to subsequent renders\n mountOperations: Array<\n () => void | (() => void) | PromiseLike<void | (() => void)>\n >; // Operations to run when component mounts\n cleanupFns: Array<() => void>; // Cleanup functions to run on unmount\n lifecycleGeneration: number; // Invalidates async mount-operation settlement after disposal\n hasPendingUpdate: boolean; // Flag to batch state updates (coalescing)\n ownerFrame: ContextFrame | null; // Provider chain for this component (set by Scope, never overwritten)\n isRoot?: boolean;\n\n // Render-tracking for precise subscriptions (internal)\n _currentRenderToken?: number; // Token for the in-progress render (set before render)\n lastRenderToken?: number; // Token of the last *committed* render\n _pendingReadSources?: Set<ReadableSource<unknown>>; // Readables read during the in-progress render\n _lastReadSources?: Set<ReadableSource<unknown>>; // Readables read during the last committed render\n devWarningsEmitted?: Set<string>; // Dev-only warning dedupe for this instance\n\n // Placeholder for null-returning components. When a component initially returns\n // null, we create a comment placeholder so updates can replace it with content.\n _placeholder?: Comment;\n _ownedChildScopes?: Set<{\n key: string | number;\n dispose(): void;\n }>;\n errorBoundaryState?: {\n error: unknown | null;\n resetKey: unknown;\n notified: boolean;\n };\n}\n\nexport function createComponentInstance(\n id: string,\n fn: ComponentFunction,\n props: Props,\n target: Element | null\n): ComponentInstance {\n const instance: ComponentInstance = {\n id,\n fn,\n props,\n target,\n portalScope: currentInstance?.portalScope ?? currentPortalScope ?? null,\n mounted: false,\n abortController: null,\n stateValues: [],\n evaluationGeneration: 0,\n notifyUpdate: null,\n // Prebound helpers (initialized below) to avoid per-update allocations\n _pendingFlushTask: undefined,\n _pendingRunTask: undefined,\n _enqueueRun: undefined,\n stateIndexCheck: -1,\n expectedStateIndices: [],\n firstRenderComplete: false,\n mountOperations: [],\n cleanupFns: [],\n lifecycleGeneration: 0,\n hasPendingUpdate: false,\n ownerFrame: null, // Will be set by renderer when vnode is marked\n ssr: false,\n cleanupStrict: false,\n isRoot: false,\n\n // Render-tracking (for precise state subscriptions)\n _currentRenderToken: undefined,\n lastRenderToken: 0,\n _pendingReadSources: undefined,\n _lastReadSources: undefined,\n devWarningsEmitted: undefined,\n };\n\n // Initialize prebound helper tasks once per instance to avoid allocations\n instance._pendingRunTask = () => {\n // Clear pending flag when the run task executes\n instance.hasPendingUpdate = false;\n if (instance.notifyUpdate === null) {\n return;\n }\n // Execute component run (will set up notifyUpdate before render)\n runComponent(instance);\n };\n\n instance._enqueueRun = () => {\n if (!instance.hasPendingUpdate) {\n instance.hasPendingUpdate = true;\n // Enqueue single run task (coalesces multiple writes)\n globalScheduler.enqueue(instance._pendingRunTask!);\n }\n };\n\n // Default state-driven updates enqueue the run task directly. Specialized\n // runtimes (for example `For` item instances) can still override this hook.\n instance._pendingFlushTask = instance._pendingRunTask;\n\n return instance;\n}\n\nlet currentInstance: ComponentInstance | null = null;\nlet currentPortalScope: object | null = null;\nlet stateIndex = 0;\n\ntype OwnedChildScope = {\n key: string | number;\n dispose(): void;\n};\n\nfunction ensureAbortController(instance: ComponentInstance): AbortController {\n let controller = instance.abortController;\n if (!controller || controller.signal.aborted) {\n controller = new AbortController();\n instance.abortController = controller;\n }\n return controller;\n}\n\n// Export for state.ts to access\nexport function getCurrentComponentInstance(): ComponentInstance | null {\n return currentInstance;\n}\n\n// Export for SSR to set temporary instance\nexport function setCurrentComponentInstance(\n instance: ComponentInstance | null\n): void {\n currentInstance = instance;\n currentPortalScope = instance?.portalScope ?? null;\n}\n\nexport function getCurrentPortalScope(): object | null {\n return currentInstance?.portalScope ?? currentPortalScope;\n}\n\n/**\n * Register a mount operation that will run after the component is mounted\n * Used by operations (task, on, timer, etc) to execute after render completes\n */\nimport { isBulkCommitActive } from './fastlane';\nimport { evaluate, cleanupInstancesUnder } from '../renderer';\n\nexport function registerMountOperation(\n operation: () => void | (() => void) | PromiseLike<void | (() => void)>\n): void {\n const instance = getCurrentComponentInstance();\n if (instance) {\n // If we're in bulk-commit fast lane, registering mount operations is a\n // violation of the fast-lane preconditions. Throw in dev, otherwise ignore\n // silently in production (we must avoid scheduling work during bulk commit).\n if (isBulkCommitActive()) {\n if (isDevelopmentEnvironment()) {\n throw new Error(\n 'registerMountOperation called during bulk commit fast-lane'\n );\n }\n return;\n }\n instance.mountOperations.push(operation);\n }\n}\n\n/**\n * Execute all mount operations for a component\n * These run after the component is rendered and mounted to the DOM\n */\nfunction executeMountOperations(instance: ComponentInstance): void {\n // Only execute mount operations for root app instance. Child component\n // operations are currently registered but should not be executed (per\n // contract tests). They remain registered for cleanup purposes.\n if (!instance.isRoot) return;\n\n const mountOperations = instance.mountOperations;\n if (mountOperations.length === 0) {\n return;\n }\n\n const lifecycleGeneration = instance.lifecycleGeneration;\n\n for (const operation of mountOperations) {\n const result = operation();\n if (isPromiseLike(result)) {\n Promise.resolve(result).then(\n (cleanup) => {\n if (typeof cleanup === 'function') {\n if (\n instance.lifecycleGeneration === lifecycleGeneration &&\n instance.mounted\n ) {\n instance.cleanupFns.push(cleanup);\n return;\n }\n\n try {\n cleanup();\n } catch (err) {\n logger.error('[Askr] async mount cleanup failed:', err);\n }\n }\n },\n (err) => {\n logger.error('[Askr] async mount operation failed:', err);\n }\n );\n } else if (typeof result === 'function') {\n instance.cleanupFns.push(result);\n }\n }\n // Clear the operations array so they don't run again on subsequent renders\n instance.mountOperations = [];\n}\n\nexport function mountInstanceInline(\n instance: ComponentInstance,\n target: Element | null\n): void {\n instance.target = target;\n // Record backref on host element so renderer can clean up when the\n // node is removed. Avoids leaks if the node is detached or replaced.\n try {\n if (target instanceof Element) {\n const host = target as Element & {\n __ASKR_INSTANCE?: ComponentInstance;\n __ASKR_INSTANCES?: ComponentInstance[];\n };\n const instances = host.__ASKR_INSTANCES ?? [];\n const nextInstances = instances.filter((entry) => entry !== instance);\n nextInstances.push(instance);\n host.__ASKR_INSTANCES = nextInstances;\n host.__ASKR_INSTANCE = nextInstances[0] ?? instance;\n }\n } catch (err) {\n void err;\n }\n\n // Ensure notifyUpdate is available for async resource completions that may\n // try to trigger re-render. This mirrors the setup in executeComponent().\n // Use prebound enqueue helper to avoid allocating a new closure\n instance.notifyUpdate = instance._enqueueRun!;\n\n const wasFirstMount = !instance.mounted;\n instance.mounted = true;\n if (wasFirstMount && instance.mountOperations.length > 0) {\n executeMountOperations(instance);\n }\n}\n\n/**\n * Run a component synchronously: execute function, handle result\n * This is the internal workhorse that manages async continuations and generation tracking.\n * Must always be called through the scheduler.\n *\n * ACTOR INVARIANT: This function is enqueued as a task, never called directly.\n */\nlet _globalRenderCounter = 0;\n\nfunction resetRenderState(instance: ComponentInstance): void {\n instance.stateIndexCheck = -1;\n\n for (const state of instance.stateValues) {\n if (state) {\n state._hasBeenRead = false;\n }\n }\n\n instance._pendingReadSources = undefined;\n}\n\nfunction nextRenderToken(): number {\n return ++_globalRenderCounter;\n}\n\nexport function renderScopedComponent<T>(\n instance: ComponentInstance,\n startStateIndex: number,\n render: () => T\n): T {\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n const savedStateIndex = stateIndex;\n\n instance.notifyUpdate = instance._enqueueRun!;\n resetRenderState(instance);\n instance._currentRenderToken = nextRenderToken();\n\n currentInstance = instance;\n currentPortalScope = instance.portalScope ?? savedPortalScope;\n stateIndex = startStateIndex;\n\n let didComplete = false;\n\n try {\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n const result = withContext(executionFrame, render);\n didComplete = true;\n return result;\n } finally {\n if (!didComplete) {\n instance._pendingReadSources = undefined;\n instance._currentRenderToken = undefined;\n }\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n stateIndex = savedStateIndex;\n }\n}\n\nfunction runComponent(instance: ComponentInstance): void {\n // CRITICAL: Ensure notifyUpdate is available for state.set() calls during this render.\n // This must be set before executeComponentSync() runs, not after.\n // Use prebound enqueue helper to avoid allocating per-render closures\n instance.notifyUpdate = instance._enqueueRun!;\n\n // Assign a token for this in-progress render and start a fresh pending-read set\n instance._currentRenderToken = nextRenderToken();\n instance._pendingReadSources = undefined;\n const domSnapshot = instance.target ? instance.target.innerHTML : '';\n\n const result = executeComponentSync(instance);\n if (isPromiseLike(result)) {\n // Async components are not supported. Components must be synchronous and\n // must not return a Promise from their render function.\n throw new Error(\n 'Async components are not supported. Components must be synchronous.'\n );\n } else {\n // Try runtime fast-lane synchronously; if it activates we do not enqueue\n // follow-up work and the commit happens atomically in this task.\n // (Runtime fast-lane has conservative preconditions.)\n const fastlaneBridge = (\n globalThis as {\n __ASKR_FASTLANE?: {\n tryRuntimeFastLaneSync?: (\n instance: unknown,\n result: unknown\n ) => boolean;\n };\n }\n ).__ASKR_FASTLANE;\n try {\n const used = fastlaneBridge?.tryRuntimeFastLaneSync?.(instance, result);\n if (used) {\n warnUnusedStateReads(instance);\n return;\n }\n } catch (err) {\n // If invariant check failed in dev, surface the error; otherwise fall back\n if (isDevelopmentEnvironment()) throw err;\n }\n\n // Fallback: enqueue the render/commit normally\n globalScheduler.enqueue(() => {\n // Handle placeholder-based updates: when a component initially returned null,\n // we created a comment placeholder. If it now has content, we need to create\n // a host element and replace the placeholder.\n if (!instance.target && instance._placeholder) {\n // Component previously returned null (has placeholder), check if now has content\n if (result === null || result === undefined) {\n // Still null - nothing to do, keep placeholder\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n return;\n }\n\n // Has content now - need to create DOM and replace placeholder\n const placeholder = instance._placeholder;\n const parent = placeholder.parentNode;\n if (!parent) {\n // Placeholder was removed from DOM - can't render\n logger.warn(\n '[Askr] placeholder no longer in DOM, cannot render component'\n );\n return;\n }\n\n // Create a new host element for the content\n const host = document.createElement('div');\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n\n // Set up instance for normal updates\n const oldInstance = currentInstance;\n currentInstance = instance;\n try {\n withContext(executionFrame, () => {\n evaluate(result, host);\n });\n\n // Replace placeholder with host\n parent.replaceChild(host, placeholder);\n\n // Set up instance for future updates\n instance.target = host;\n instance._placeholder = undefined;\n (\n host as Element & { __ASKR_INSTANCE?: ComponentInstance }\n ).__ASKR_INSTANCE = instance;\n\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n } finally {\n currentInstance = oldInstance;\n }\n return;\n }\n\n if (instance.target) {\n let oldChildren: Node[] = [];\n try {\n const wasFirstMount = !instance.mounted;\n // Ensure nested component executions during evaluation have access to\n // the current component instance. This allows nested components to\n // call `state()`, `resource()`, and other runtime helpers which\n // rely on `getCurrentComponentInstance()` being available.\n const oldInstance = currentInstance;\n currentInstance = instance;\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n // Capture snapshot of current children (by reference) so we can\n // restore them on render failure without losing event listeners or\n // instance attachments.\n oldChildren = Array.from(instance.target.childNodes);\n\n try {\n withContext(executionFrame, () => {\n evaluate(result, instance.target);\n });\n } catch (e) {\n // If evaluation failed, attempt to cleanup any partially-added nodes\n // and restore the old children to preserve listeners and instances.\n try {\n const newChildren = Array.from(instance.target.childNodes);\n for (const n of newChildren) {\n try {\n cleanupInstancesUnder(n);\n } catch (err) {\n logger.warn(\n '[Askr] error cleaning up failed commit children:',\n err\n );\n }\n }\n } catch (_err) {\n void _err;\n }\n\n // Restore original children by re-inserting the old node references\n // this preserves attached listeners and instance backrefs.\n try {\n incDevCounter('__DOM_REPLACE_COUNT');\n setDevValue(\n '__LAST_DOM_REPLACE_STACK_COMPONENT_RESTORE',\n new Error().stack\n );\n } catch (e) {\n void e;\n }\n instance.target.replaceChildren(...oldChildren);\n throw e;\n } finally {\n currentInstance = oldInstance;\n }\n\n // Commit succeeded — finalize recorded state reads so subscriptions reflect\n // the last *committed* render. This updates per-state reader maps\n // deterministically and synchronously with the commit.\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n\n instance.mounted = true;\n // Execute mount operations after first mount (do NOT run these with\n // currentInstance set - they may perform state mutations/registrations)\n if (wasFirstMount && instance.mountOperations.length > 0) {\n executeMountOperations(instance);\n }\n } catch (renderError) {\n // Atomic rendering: rollback on render error. Attempt non-lossy restore of\n // original child node references to preserve listeners/instances.\n try {\n const currentChildren = Array.from(instance.target.childNodes);\n for (const n of currentChildren) {\n try {\n cleanupInstancesUnder(n);\n } catch (err) {\n logger.warn(\n '[Askr] error cleaning up partial children during rollback:',\n err\n );\n }\n }\n } catch (_err) {\n void _err;\n }\n\n try {\n incDevCounter('__DOM_REPLACE_COUNT');\n setDevValue(\n '__LAST_DOM_REPLACE_STACK_COMPONENT_ROLLBACK',\n new Error().stack\n );\n instance.target.replaceChildren(...oldChildren);\n } catch {\n // Fallback to innerHTML restore if replaceChildren fails for some reason.\n instance.target.innerHTML = domSnapshot;\n }\n\n throw renderError;\n }\n }\n });\n }\n}\n\n/**\n * Execute a component's render function synchronously.\n * Returns either a vnode/promise immediately (does NOT render).\n * Rendering happens separately through runComponent.\n */\nexport function renderComponentInline(\n instance: ComponentInstance\n): unknown | Promise<unknown> {\n // Ensure inline executions (rendered during parent's evaluate) still\n // receive a render token and have their state reads finalized so\n // subscriptions are correctly recorded. If this function is called\n // as part of a scheduled run, the token will already be set by\n // runComponent and we should not overwrite it.\n const hadToken = instance._currentRenderToken !== undefined;\n const prevToken = instance._currentRenderToken;\n const prevPendingReads = instance._pendingReadSources;\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n if (!hadToken) {\n instance._currentRenderToken = nextRenderToken();\n instance._pendingReadSources = undefined;\n }\n\n try {\n const result = executeComponentSync(instance);\n // If we set the token for inline execution, finalize subscriptions now\n // because the component is effectively committed as part of the parent's\n // synchronous evaluation.\n if (!hadToken) {\n finalizeReadSubscriptions(instance);\n }\n return result;\n } finally {\n // Restore previous token/read states for nested inline render scenarios\n instance._currentRenderToken = prevToken;\n instance._pendingReadSources = prevPendingReads;\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n }\n}\n\nexport function warnUnusedStateReads(instance: ComponentInstance): void {\n for (let i = 0; i < instance.stateValues.length; i++) {\n const state = instance.stateValues[i];\n const hasCommittedUsage =\n (state?._readers?.size ?? 0) > 0 ||\n ((state as { _derivedSubscribers?: Set<unknown> } | undefined)\n ?._derivedSubscribers?.size ?? 0) > 0;\n\n if (\n state &&\n !state._hasBeenRead &&\n !state._hasEverBeenRead &&\n !hasCommittedUsage\n ) {\n try {\n const name = instance.fn?.name || '<anonymous>';\n warnInstanceOnce(\n instance,\n `unused-state:${i}`,\n `[askr] Unused state variable detected in ${name} at index ${i}. State should be read during render or removed.`\n );\n } catch {\n warnInstanceOnce(\n instance,\n `unused-state:${i}`,\n `[askr] Unused state variable detected. State should be read during render or removed.`\n );\n }\n }\n }\n}\n\nfunction executeComponentSync(\n instance: ComponentInstance\n): unknown | Promise<unknown> {\n resetRenderState(instance);\n incDevCounter('componentRuns');\n incDevCounter('componentReruns');\n\n const savedPortalScope = currentPortalScope;\n currentInstance = instance;\n currentPortalScope = instance.portalScope ?? savedPortalScope;\n stateIndex = 0;\n\n try {\n // Track render time in dev mode\n const renderStartTime = isDevelopmentEnvironment() ? Date.now() : 0;\n\n // Create context object with abort signal\n const context = {\n get signal(): AbortSignal {\n return ensureAbortController(instance).signal;\n },\n };\n\n // Execute component within its owner frame (provider chain).\n // This ensures all context reads see the correct provider values.\n // We create a new execution frame whose parent is the ownerFrame. The\n // `values` map is lazily allocated to avoid per-render Map allocations\n // for components that do not use context.\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n const result = withContext(executionFrame, () =>\n instance.fn(instance.props, context)\n );\n\n // Check render time\n const renderTime = Date.now() - renderStartTime;\n if (renderTime > 5) {\n warnInstanceOnce(\n instance,\n 'slow-render',\n `[askr] Slow render detected: ${renderTime}ms. Consider optimizing component performance.`\n );\n }\n\n // Mark first render complete after successful execution\n // This enables hook order validation on subsequent renders\n if (!instance.firstRenderComplete) {\n instance.firstRenderComplete = true;\n }\n\n return result;\n } finally {\n // Synchronous path: we did not push a fresh frame, so nothing to pop here.\n currentInstance = null;\n currentPortalScope = savedPortalScope;\n }\n}\n\n/**\n * Public entry point: Execute component with full lifecycle (execute + render)\n * Handles both initial mount and re-execution. Always enqueues through scheduler.\n * Single entry point to avoid lifecycle divergence.\n */\nexport function executeComponent(instance: ComponentInstance): void {\n // Lazily recreate abort controller only when signal is actually requested.\n instance.abortController = null;\n\n // Setup notifyUpdate callback using prebound helper to avoid per-call closures\n instance.notifyUpdate = instance._enqueueRun!;\n\n // Enqueue the initial component run\n globalScheduler.enqueue(instance._pendingRunTask!);\n}\n\nexport function getCurrentInstance(): ComponentInstance | null {\n return currentInstance;\n}\n\n/**\n * Get the abort signal for the current component\n * Used to cancel async operations on unmount/navigation\n *\n * The signal is guaranteed to be aborted when:\n * - Component unmounts\n * - Navigation occurs (different route)\n * - Parent is destroyed\n *\n * IMPORTANT: getSignal() must be called during component render execution.\n * It captures the current component instance from context.\n *\n * @returns AbortSignal that will be aborted when component unmounts\n * @throws Error if called outside component execution\n *\n * @example\n * ```ts\n * // ✅ Correct: called during render, used in async operation\n * export async function UserPage({ id }: { id: string }) {\n * const signal = getSignal();\n * const user = await fetch(`/api/users/${id}`, { signal });\n * return <div>{user.name}</div>;\n * }\n *\n * // ✅ Correct: passed to event handler\n * export function Button() {\n * const signal = getSignal();\n * return {\n * type: 'button',\n * props: {\n * onClick: async () => {\n * const data = await fetch(url, { signal });\n * }\n * }\n * };\n * }\n *\n * // ❌ Wrong: called outside component context\n * const signal = getSignal(); // Error: not in component\n * ```\n */\nexport function getSignal(): AbortSignal {\n if (!currentInstance) {\n throw new Error(\n 'getSignal() can only be called during component render execution. ' +\n 'Ensure you are calling this from inside your component function.'\n );\n }\n return ensureAbortController(currentInstance).signal;\n}\n\n/**\n * Finalize read subscriptions for an instance after a successful commit.\n * - Update per-state readers map to point to this instance's last committed token\n * - Remove this instance from states it no longer reads\n * This is deterministic and runs synchronously with commit to ensure\n * subscribers are only notified when they actually read a state in their\n * last committed render.\n */\nexport function finalizeReadSubscriptions(instance: ComponentInstance): void {\n finalizeReadableSubscriptions(instance);\n}\n\nexport function getNextStateIndex(): number {\n return stateIndex++;\n}\n\nexport function claimHookIndex(\n instance: ComponentInstance,\n hookName: string\n): number {\n const index = getNextStateIndex();\n\n if (index < instance.stateIndexCheck) {\n throw new Error(\n `Hook index violation: ${hookName}() call at index ${index}, ` +\n `but previously saw index ${instance.stateIndexCheck}. ` +\n `This happens when render-scoped hooks are called conditionally (inside if/for/etc). ` +\n `Move all ${hookName}() calls to the top level of your component function, ` +\n `before any conditionals.`\n );\n }\n\n instance.stateIndexCheck = index;\n\n if (instance.firstRenderComplete) {\n if (instance.expectedStateIndices[index] !== index) {\n throw new Error(\n `Hook order violation: ${hookName}() called at index ${index}, ` +\n `but this index was not in the first render's sequence [${instance.expectedStateIndices.join(', ')}]. ` +\n `This usually means ${hookName}() is inside a conditional or loop. ` +\n `Move all render-scoped hooks to the top level of your component function.`\n );\n }\n } else {\n instance.expectedStateIndices.push(index);\n }\n\n return index;\n}\n\nexport function getCurrentStateIndex(): number {\n return stateIndex;\n}\n\nexport function resetStateIndex(): void {\n stateIndex = 0;\n}\n\nexport function setStateIndex(value: number): void {\n stateIndex = value;\n}\n\n/**\n * Mount a component instance.\n * This is just an alias to executeComponent() to maintain API compatibility.\n * All lifecycle logic is unified in executeComponent().\n */\nexport function mountComponent(instance: ComponentInstance): void {\n executeComponent(instance);\n}\n\n/**\n * Clean up component — abort pending operations\n * Called on unmount or route change\n */\nexport function cleanupComponent(instance: ComponentInstance): void {\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n currentInstance = null;\n currentPortalScope = null;\n\n try {\n const cleanupErrors: unknown[] = [];\n const recordCleanupError = (message: string, err: unknown): void => {\n if (instance.cleanupStrict) {\n cleanupErrors.push(err);\n } else if (isDevelopmentEnvironment()) {\n logger.warn(message, err);\n }\n };\n\n const ownedChildScopes = instance._ownedChildScopes;\n if (ownedChildScopes && ownedChildScopes.size > 0) {\n instance._ownedChildScopes = new Set();\n for (const scope of ownedChildScopes) {\n try {\n scope.dispose();\n } catch (err) {\n recordCleanupError('[Askr] child scope cleanup threw:', err);\n }\n }\n }\n\n // Execute cleanup functions (from mount effects)\n const cleanupFns = instance.cleanupFns;\n instance.cleanupFns = [];\n for (const cleanup of cleanupFns) {\n try {\n cleanup();\n } catch (err) {\n recordCleanupError('[Askr] cleanup function threw:', err);\n }\n }\n\n // Remove deterministic state subscriptions for this instance\n try {\n cleanupReadableSubscriptions(instance);\n } catch (err) {\n recordCleanupError('[Askr] readable subscription cleanup threw:', err);\n }\n\n // Abort all pending operations\n try {\n if (\n instance.abortController &&\n !instance.abortController.signal.aborted\n ) {\n instance.abortController.abort();\n }\n } catch (err) {\n recordCleanupError('[Askr] abort controller cleanup threw:', err);\n }\n instance.abortController = null;\n\n // Clear update callback to prevent dangling references and stale updates\n instance.lifecycleGeneration++;\n instance.evaluationGeneration++;\n instance.mountOperations = [];\n instance.hasPendingUpdate = false;\n instance.notifyUpdate = null;\n instance._placeholder = undefined;\n\n // Mark instance as unmounted so external tracking (e.g., portal host lists)\n // can deterministically prune stale instances. Not marking this leads to\n // retained \"mounted\" flags across cleanup boundaries which breaks\n // owner selection in the portal fallback.\n instance.mounted = false;\n\n if (cleanupErrors.length > 0) {\n throw new AggregateError(\n cleanupErrors,\n `Cleanup failed for component ${instance.id}`\n );\n }\n } finally {\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n }\n}\n\nexport function registerOwnedChildScope(\n instance: ComponentInstance,\n scope: OwnedChildScope\n): void {\n const scopes = (instance._ownedChildScopes ??= new Set());\n scopes.add(scope);\n}\n\nexport function unregisterOwnedChildScope(\n instance: ComponentInstance,\n scope: OwnedChildScope\n): void {\n instance._ownedChildScopes?.delete(scope);\n}\n\nfunction warnInstanceOnce(\n instance: ComponentInstance,\n key: string,\n message: string\n): void {\n if (isProductionEnvironment()) return;\n const warnings = (instance.devWarningsEmitted ??= new Set());\n if (warnings.has(key)) return;\n warnings.add(key);\n logger.warn(message);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgFA,SAAgB,wBACd,IACA,IACA,OACA,QACmB;CACnB,MAAM,WAA8B;EAClC;EACA;EACA;EACA;EACA,aAAa,iBAAiB,eAAe,sBAAsB;EACnE,SAAS;EACT,iBAAiB;EACjB,aAAa,CAAC;EACd,sBAAsB;EACtB,cAAc;EAEd,mBAAmB;EACnB,iBAAiB;EACjB,aAAa;EACb,iBAAiB;EACjB,sBAAsB,CAAC;EACvB,qBAAqB;EACrB,iBAAiB,CAAC;EAClB,YAAY,CAAC;EACb,qBAAqB;EACrB,kBAAkB;EAClB,YAAY;EACZ,KAAK;EACL,eAAe;EACf,QAAQ;EAGR,qBAAqB;EACrB,iBAAiB;EACjB,qBAAqB;EACrB,kBAAkB;EAClB,oBAAoB;CACtB;CAGA,SAAS,wBAAwB;EAE/B,SAAS,mBAAmB;EAC5B,IAAI,SAAS,iBAAiB,MAC5B;EAGF,aAAa,QAAQ;CACvB;CAEA,SAAS,oBAAoB;EAC3B,IAAI,CAAC,SAAS,kBAAkB;GAC9B,SAAS,mBAAmB;GAE5B,gBAAgB,QAAQ,SAAS,eAAgB;EACnD;CACF;CAIA,SAAS,oBAAoB,SAAS;CAEtC,OAAO;AACT;AAEA,IAAI,kBAA4C;AAChD,IAAI,qBAAoC;AACxC,IAAI,aAAa;AAOjB,SAAS,sBAAsB,UAA8C;CAC3E,IAAI,aAAa,SAAS;CAC1B,IAAI,CAAC,cAAc,WAAW,OAAO,SAAS;EAC5C,aAAa,IAAI,gBAAgB;EACjC,SAAS,kBAAkB;CAC7B;CACA,OAAO;AACT;AAGA,SAAgB,8BAAwD;CACtE,OAAO;AACT;AAGA,S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,mBAAmB,kCAAkC,GAAG;EAC1D;EAIF,IAAI;GACF,6BAA6B,QAAQ;EACvC,SAAS,KAAK;GACZ,mBAAmB,+CAA+C,GAAG;EACvE;EAGA,IAAI;GACF,IACE,SAAS,mBACT,CAAC,SAAS,gBAAgB,OAAO,SAEjC,SAAS,gBAAgB,MAAM;EAEnC,SAAS,KAAK;GACZ,mBAAmB,0CAA0C,GAAG;EAClE;EACA,SAAS,kBAAkB;EAG3B,SAAS;EACT,SAAS;EACT,SAAS,kBAAkB,CAAC;EAC5B,SAAS,mBAAmB;EAC5B,SAAS,eAAe;EACxB,SAAS,eAAe;EAMxB,SAAS,UAAU;EAEnB,IAAI,cAAc,SAAS,GACzB,MAAM,IAAI,eACR,eACA,gCAAgC,SAAS,IAC3C;CAEJ,UAAU;EACR,kBAAkB;EAClB,qBAAqB;CACvB;AACF;AAEA,SAAgB,wBACd,UACA,OACM;CAEN,CADgB,SAAS,sCAAsB,IAAI,IAAI,GAChD,IAAI,KAAK;AAClB;AAEA,SAAgB,0BACd,UACA,OACM;CACN,SAAS,mBAAmB,OAAO,KAAK;AAC1C;AAEA,SAAS,iBACP,UACA,KACA,SACM;CACN,IAAI,wBAAwB,GAAG;CAC/B,MAAM,WAAY,SAAS,uCAAuB,IAAI,IAAI;CAC1D,IAAI,SAAS,IAAI,GAAG,GAAG;CACvB,SAAS,IAAI,GAAG;CAChB,OAAO,KAAK,OAAO;AACrB"}
1
+ {"version":3,"file":"component.js","names":[],"sources":["../../src/runtime/component.ts"],"sourcesContent":["/**\n * Component instance lifecycle management\n * Internal only — users never see this\n */\n\nimport { type State } from './state';\nimport { globalScheduler } from './scheduler';\nimport type { Props } from '../common/props';\nimport type { ComponentFunction } from '../common/component';\nimport {\n // withContext is the sole primitive for context restoration\n withContext,\n type ContextFrame,\n} from './context';\nimport {\n type ReadableSource,\n finalizeReadableSubscriptions,\n finalizeReadableSubscriptionsFromSnapshot,\n cleanupReadableSubscriptions,\n} from './readable';\nimport {\n isDevelopmentEnvironment,\n isProductionEnvironment,\n} from '../common/env';\nimport { logger } from '../dev/logger';\nimport { incDevCounter, setDevValue } from './dev-namespace';\nimport { isPromiseLike } from '../common/promise';\n\nexport type { ComponentFunction } from '../common/component';\n\nexport interface ComponentInstance {\n id: string;\n fn: ComponentFunction;\n props: Props;\n target: Element | null;\n parentInstance: ComponentInstance | null;\n portalScope: object | null;\n mounted: boolean;\n abortController: AbortController | null; // Lazily created per-component abort lifecycle\n ssr?: boolean; // Set to true for SSR temporary instances\n // Opt-in strict cleanup mode: when true cleanup errors are aggregated and re-thrown\n cleanupStrict?: boolean;\n stateValues: State<unknown>[]; // Persistent state storage across renders\n evaluationGeneration: number; // Prevents stale async evaluation completions\n notifyUpdate: (() => void) | null; // Callback for state updates (persisted on instance)\n // Internal: prebound helpers to avoid per-update closures (allocation hot-path)\n _pendingFlushTask?: () => void; // Clears hasPendingUpdate and triggers notifyUpdate\n _pendingRunTask?: () => void; // Clears hasPendingUpdate and runs component\n _enqueueRun?: () => void; // Batches run requests and enqueues _pendingRunTask\n stateIndexCheck: number; // Track state indices to catch conditional calls\n expectedStateIndices: number[]; // Expected sequence of render-scoped hook indices (frozen after first render)\n firstRenderComplete: boolean; // Flag to detect transition from first to subsequent renders\n mountOperations: Array<\n () => void | (() => void) | PromiseLike<void | (() => void)>\n >; // Operations to run when component mounts\n commitOperations: Array<\n () => void | (() => void) | PromiseLike<void | (() => void)>\n >; // Operations to run after a successful committed render\n cleanupFns: Array<() => void>; // Cleanup functions to run on unmount\n lifecycleSlots: unknown[]; // Render-scoped lifecycle primitive storage\n lifecycleGeneration: number; // Invalidates async mount-operation settlement after disposal\n hasPendingUpdate: boolean; // Flag to batch state updates (coalescing)\n ownerFrame: ContextFrame | null; // Provider chain for this component (set by Scope, never overwritten)\n isRoot?: boolean;\n\n // Render-tracking for precise subscriptions (internal)\n _currentRenderToken?: number; // Token for the in-progress render (set before render)\n lastRenderToken?: number; // Token of the last *committed* render\n _pendingReadSources?: Set<ReadableSource<unknown>>; // Readables read during the in-progress render\n _lastReadSources?: Set<ReadableSource<unknown>>; // Readables read during the last committed render\n devWarningsEmitted?: Set<string>; // Dev-only warning dedupe for this instance\n\n // Placeholder for null-returning components. When a component initially returns\n // null, we create a comment placeholder so updates can replace it with content.\n _placeholder?: Comment;\n _ownedChildScopes?: Set<{\n key: string | number;\n dispose(): void;\n }>;\n errorBoundaryState?: {\n error: unknown | null;\n resetKey: unknown;\n notified: boolean;\n };\n}\n\nexport function createComponentInstance(\n id: string,\n fn: ComponentFunction,\n props: Props,\n target: Element | null\n): ComponentInstance {\n const instance: ComponentInstance = {\n id,\n fn,\n props,\n target,\n parentInstance: currentInstance,\n portalScope: currentInstance?.portalScope ?? currentPortalScope ?? null,\n mounted: false,\n abortController: null,\n stateValues: [],\n evaluationGeneration: 0,\n notifyUpdate: null,\n // Prebound helpers (initialized below) to avoid per-update allocations\n _pendingFlushTask: undefined,\n _pendingRunTask: undefined,\n _enqueueRun: undefined,\n stateIndexCheck: -1,\n expectedStateIndices: [],\n firstRenderComplete: false,\n mountOperations: [],\n commitOperations: [],\n cleanupFns: [],\n lifecycleSlots: [],\n lifecycleGeneration: 0,\n hasPendingUpdate: false,\n ownerFrame: null, // Will be set by renderer when vnode is marked\n ssr: false,\n cleanupStrict: false,\n isRoot: false,\n\n // Render-tracking (for precise state subscriptions)\n _currentRenderToken: undefined,\n lastRenderToken: 0,\n _pendingReadSources: undefined,\n _lastReadSources: undefined,\n devWarningsEmitted: undefined,\n };\n\n // Initialize prebound helper tasks once per instance to avoid allocations\n instance._pendingRunTask = () => {\n // Clear pending flag when the run task executes\n instance.hasPendingUpdate = false;\n if (instance.notifyUpdate === null) {\n return;\n }\n // Execute component run (will set up notifyUpdate before render)\n runComponent(instance);\n };\n\n instance._enqueueRun = () => {\n if (!instance.hasPendingUpdate) {\n instance.hasPendingUpdate = true;\n // Enqueue single run task (coalesces multiple writes)\n globalScheduler.enqueue(instance._pendingRunTask!);\n }\n };\n\n // Default state-driven updates enqueue the run task directly. Specialized\n // runtimes (for example `For` item instances) can still override this hook.\n instance._pendingFlushTask = instance._pendingRunTask;\n\n return instance;\n}\n\nlet currentInstance: ComponentInstance | null = null;\nlet currentPortalScope: object | null = null;\nlet stateIndex = 0;\n\ntype OwnedChildScope = {\n key: string | number;\n dispose(): void;\n};\n\nfunction ensureAbortController(instance: ComponentInstance): AbortController {\n let controller = instance.abortController;\n if (!controller || controller.signal.aborted) {\n controller = new AbortController();\n instance.abortController = controller;\n }\n return controller;\n}\n\n// Export for state.ts to access\nexport function getCurrentComponentInstance(): ComponentInstance | null {\n return currentInstance;\n}\n\n// Export for SSR to set temporary instance\nexport function setCurrentComponentInstance(\n instance: ComponentInstance | null\n): void {\n currentInstance = instance;\n currentPortalScope = instance?.portalScope ?? null;\n}\n\nexport function getCurrentPortalScope(): object | null {\n return currentInstance?.portalScope ?? currentPortalScope;\n}\n\n/**\n * Register a mount operation that will run after the component is mounted\n * Used by operations (task, on, timer, etc) to execute after render completes\n */\nimport { isBulkCommitActive } from './fastlane';\nimport { evaluate, cleanupInstancesUnder } from '../renderer';\n\ntype LifecycleOperation = () =>\n | void\n | (() => void)\n | PromiseLike<void | (() => void)>;\n\ntype LifecycleCommitBatchEntry = {\n instance: ComponentInstance;\n wasFirstMount: boolean;\n};\n\ntype ReadSubscriptionCommit = {\n instance: ComponentInstance;\n token: number;\n pendingReadSources: Set<ReadableSource<unknown>> | undefined;\n};\n\ntype InlineRenderSnapshot = {\n instance: ComponentInstance;\n props: Props;\n ownerFrame: ContextFrame | null;\n portalScope: object | null;\n parentInstance: ComponentInstance | null;\n isRoot: boolean | undefined;\n};\n\ntype LifecycleCommitBatch = {\n parent: LifecycleCommitBatch | null;\n entries: LifecycleCommitBatchEntry[];\n entriesByInstance: Map<ComponentInstance, LifecycleCommitBatchEntry>;\n readCommits: ReadSubscriptionCommit[];\n readCommitsByInstance: Map<ComponentInstance, ReadSubscriptionCommit>;\n renderSnapshots: InlineRenderSnapshot[];\n renderSnapshotsByInstance: Map<ComponentInstance, InlineRenderSnapshot>;\n active: boolean;\n};\n\nlet currentLifecycleCommitBatch: LifecycleCommitBatch | null = null;\n\nfunction beginLifecycleCommitBatch(): LifecycleCommitBatch {\n const batch: LifecycleCommitBatch = {\n parent: currentLifecycleCommitBatch,\n entries: [],\n entriesByInstance: new Map(),\n readCommits: [],\n readCommitsByInstance: new Map(),\n renderSnapshots: [],\n renderSnapshotsByInstance: new Map(),\n active: true,\n };\n currentLifecycleCommitBatch = batch;\n return batch;\n}\n\nfunction closeLifecycleCommitBatch(batch: LifecycleCommitBatch): boolean {\n if (!batch.active) {\n return false;\n }\n\n batch.active = false;\n currentLifecycleCommitBatch = batch.parent;\n return true;\n}\n\nfunction enqueueLifecycleCommit(\n batch: LifecycleCommitBatch,\n instance: ComponentInstance,\n wasFirstMount: boolean\n): void {\n const existing = batch.entriesByInstance.get(instance);\n if (existing) {\n existing.wasFirstMount = existing.wasFirstMount || wasFirstMount;\n return;\n }\n\n const entry = { instance, wasFirstMount };\n batch.entriesByInstance.set(instance, entry);\n batch.entries.push(entry);\n}\n\nfunction enqueueReadSubscriptionCommit(\n batch: LifecycleCommitBatch,\n instance: ComponentInstance,\n token: number,\n pendingReadSources: Set<ReadableSource<unknown>> | undefined\n): void {\n const existing = batch.readCommitsByInstance.get(instance);\n const commit = existing ?? {\n instance,\n token,\n pendingReadSources,\n };\n\n commit.token = token;\n commit.pendingReadSources = pendingReadSources\n ? new Set(pendingReadSources)\n : undefined;\n\n if (!existing) {\n batch.readCommitsByInstance.set(instance, commit);\n batch.readCommits.push(commit);\n }\n}\n\nfunction enqueueInlineRenderSnapshot(\n batch: LifecycleCommitBatch,\n snapshot: InlineRenderSnapshot\n): void {\n if (batch.renderSnapshotsByInstance.has(snapshot.instance)) {\n return;\n }\n\n batch.renderSnapshotsByInstance.set(snapshot.instance, snapshot);\n batch.renderSnapshots.push(snapshot);\n}\n\nexport function captureInlineRenderSnapshot(instance: ComponentInstance): void {\n if (!currentLifecycleCommitBatch?.active) {\n return;\n }\n\n enqueueInlineRenderSnapshot(currentLifecycleCommitBatch, {\n instance,\n props: instance.props,\n ownerFrame: instance.ownerFrame,\n portalScope: instance.portalScope,\n parentInstance: instance.parentInstance,\n isRoot: instance.isRoot,\n });\n}\n\nfunction finalizeInlineReadSubscriptions(\n instance: ComponentInstance,\n token: number,\n pendingReadSources: Set<ReadableSource<unknown>> | undefined\n): void {\n if (currentLifecycleCommitBatch?.active) {\n enqueueReadSubscriptionCommit(\n currentLifecycleCommitBatch,\n instance,\n token,\n pendingReadSources\n );\n return;\n }\n\n finalizeReadableSubscriptionsFromSnapshot(\n instance,\n token,\n pendingReadSources\n );\n}\n\nfunction flushLifecycleCommitBatch(batch: LifecycleCommitBatch): void {\n if (!closeLifecycleCommitBatch(batch)) {\n return;\n }\n\n if (batch.parent?.active) {\n for (const snapshot of batch.renderSnapshots) {\n enqueueInlineRenderSnapshot(batch.parent, snapshot);\n }\n for (const commit of batch.readCommits) {\n enqueueReadSubscriptionCommit(\n batch.parent,\n commit.instance,\n commit.token,\n commit.pendingReadSources\n );\n }\n for (const entry of batch.entries) {\n enqueueLifecycleCommit(batch.parent, entry.instance, entry.wasFirstMount);\n }\n return;\n }\n\n for (const commit of batch.readCommits) {\n finalizeReadableSubscriptionsFromSnapshot(\n commit.instance,\n commit.token,\n commit.pendingReadSources\n );\n }\n\n for (const entry of batch.entries) {\n executeCommittedLifecycleOperations(entry.instance, entry.wasFirstMount);\n }\n}\n\nfunction discardLifecycleCommitBatch(batch: LifecycleCommitBatch): void {\n if (!closeLifecycleCommitBatch(batch)) {\n return;\n }\n\n for (let index = batch.renderSnapshots.length - 1; index >= 0; index -= 1) {\n const snapshot = batch.renderSnapshots[index]!;\n snapshot.instance.props = snapshot.props;\n snapshot.instance.ownerFrame = snapshot.ownerFrame;\n snapshot.instance.portalScope = snapshot.portalScope;\n snapshot.instance.parentInstance = snapshot.parentInstance;\n snapshot.instance.isRoot = snapshot.isRoot;\n }\n\n for (const entry of batch.entries) {\n if (entry.wasFirstMount) {\n entry.instance.mountOperations = [];\n }\n discardCommitOperations(entry.instance);\n }\n}\n\nexport function registerMountOperation(operation: LifecycleOperation): void {\n const instance = getCurrentComponentInstance();\n if (instance) {\n // If we're in bulk-commit fast lane, registering mount operations is a\n // violation of the fast-lane preconditions. Throw in dev, otherwise ignore\n // silently in production (we must avoid scheduling work during bulk commit).\n if (isBulkCommitActive()) {\n if (isDevelopmentEnvironment()) {\n throw new Error(\n 'registerMountOperation called during bulk commit fast-lane'\n );\n }\n return;\n }\n instance.mountOperations.push(operation);\n }\n}\n\nexport function registerCommitOperation(operation: LifecycleOperation): void {\n const instance = getCurrentComponentInstance();\n if (instance) {\n if (isBulkCommitActive()) {\n if (isDevelopmentEnvironment()) {\n throw new Error(\n 'registerCommitOperation called during bulk commit fast-lane'\n );\n }\n return;\n }\n instance.commitOperations.push(operation);\n }\n}\n\nfunction settleLifecycleOperationResult(\n instance: ComponentInstance,\n lifecycleGeneration: number,\n result: void | (() => void) | PromiseLike<void | (() => void)>\n): void {\n if (isPromiseLike(result)) {\n Promise.resolve(result).then(\n (cleanup) => {\n if (typeof cleanup === 'function') {\n if (\n instance.lifecycleGeneration === lifecycleGeneration &&\n instance.mounted\n ) {\n instance.cleanupFns.push(cleanup);\n return;\n }\n\n try {\n cleanup();\n } catch (err) {\n logger.error('[Askr] async mount cleanup failed:', err);\n }\n }\n },\n (err) => {\n logger.error('[Askr] async mount operation failed:', err);\n }\n );\n } else if (typeof result === 'function') {\n instance.cleanupFns.push(result);\n }\n}\n\n/**\n * Execute all mount operations for a component\n * These run after the component is rendered and mounted to the DOM\n */\nfunction executeMountOperations(instance: ComponentInstance): void {\n const mountOperations = instance.mountOperations;\n if (mountOperations.length === 0) {\n return;\n }\n\n const lifecycleGeneration = instance.lifecycleGeneration;\n\n for (const operation of mountOperations) {\n settleLifecycleOperationResult(instance, lifecycleGeneration, operation());\n }\n // Clear the operations array so they don't run again on subsequent renders\n instance.mountOperations = [];\n}\n\nfunction executeCommitOperations(instance: ComponentInstance): void {\n const commitOperations = instance.commitOperations;\n if (commitOperations.length === 0) {\n return;\n }\n\n instance.commitOperations = [];\n const lifecycleGeneration = instance.lifecycleGeneration;\n\n for (const operation of commitOperations) {\n settleLifecycleOperationResult(instance, lifecycleGeneration, operation());\n }\n}\n\nfunction discardCommitOperations(instance: ComponentInstance): void {\n instance.commitOperations = [];\n}\n\nfunction executeCommittedLifecycleOperations(\n instance: ComponentInstance,\n wasFirstMount: boolean\n): void {\n if (wasFirstMount && instance.mountOperations.length > 0) {\n executeMountOperations(instance);\n }\n if (instance.commitOperations.length > 0) {\n executeCommitOperations(instance);\n }\n}\n\nfunction commitLifecycleForInstance(\n instance: ComponentInstance,\n wasFirstMount: boolean\n): void {\n if (currentLifecycleCommitBatch) {\n enqueueLifecycleCommit(\n currentLifecycleCommitBatch,\n instance,\n wasFirstMount\n );\n return;\n }\n\n executeCommittedLifecycleOperations(instance, wasFirstMount);\n}\n\nexport function commitRenderedComponent(instance: ComponentInstance): void {\n if (instance.mounted && instance.commitOperations.length > 0) {\n commitLifecycleForInstance(instance, false);\n }\n}\n\nexport function mountInstanceInline(\n instance: ComponentInstance,\n target: Element | null\n): void {\n instance.target = target;\n // Record backref on host element so renderer can clean up when the\n // node is removed. Avoids leaks if the node is detached or replaced.\n try {\n if (target instanceof Element) {\n const host = target as Element & {\n __ASKR_INSTANCE?: ComponentInstance;\n __ASKR_INSTANCES?: ComponentInstance[];\n };\n const instances = host.__ASKR_INSTANCES ?? [];\n const nextInstances = instances.filter((entry) => entry !== instance);\n nextInstances.push(instance);\n host.__ASKR_INSTANCES = nextInstances;\n host.__ASKR_INSTANCE = nextInstances[0] ?? instance;\n }\n } catch (err) {\n void err;\n }\n\n // Ensure notifyUpdate is available for async resource completions that may\n // try to trigger re-render. This mirrors the setup in executeComponent().\n // Use prebound enqueue helper to avoid allocating a new closure\n instance.notifyUpdate = instance._enqueueRun!;\n\n const wasFirstMount = !instance.mounted;\n instance.mounted = true;\n commitLifecycleForInstance(instance, wasFirstMount);\n}\n\n/**\n * Run a component synchronously: execute function, handle result\n * This is the internal workhorse that manages async continuations and generation tracking.\n * Must always be called through the scheduler.\n *\n * ACTOR INVARIANT: This function is enqueued as a task, never called directly.\n */\nlet _globalRenderCounter = 0;\n\nfunction resetRenderState(instance: ComponentInstance): void {\n instance.stateIndexCheck = -1;\n\n for (const state of instance.stateValues) {\n if (state) {\n state._hasBeenRead = false;\n }\n }\n\n instance._pendingReadSources = undefined;\n}\n\nfunction nextRenderToken(): number {\n return ++_globalRenderCounter;\n}\n\nexport function renderScopedComponent<T>(\n instance: ComponentInstance,\n startStateIndex: number,\n render: () => T\n): T {\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n const savedStateIndex = stateIndex;\n\n instance.notifyUpdate = instance._enqueueRun!;\n resetRenderState(instance);\n instance._currentRenderToken = nextRenderToken();\n\n currentInstance = instance;\n currentPortalScope = instance.portalScope ?? savedPortalScope;\n stateIndex = startStateIndex;\n\n let didComplete = false;\n\n try {\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n const result = withContext(executionFrame, render);\n didComplete = true;\n return result;\n } finally {\n if (!didComplete) {\n instance._pendingReadSources = undefined;\n instance._currentRenderToken = undefined;\n }\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n stateIndex = savedStateIndex;\n }\n}\n\nfunction runComponent(instance: ComponentInstance): void {\n // CRITICAL: Ensure notifyUpdate is available for state.set() calls during this render.\n // This must be set before executeComponentSync() runs, not after.\n // Use prebound enqueue helper to avoid allocating per-render closures\n instance.notifyUpdate = instance._enqueueRun!;\n\n // Assign a token for this in-progress render and start a fresh pending-read set\n instance._currentRenderToken = nextRenderToken();\n instance._pendingReadSources = undefined;\n const domSnapshot = instance.target ? instance.target.innerHTML : '';\n\n let result: unknown | Promise<unknown>;\n try {\n result = executeComponentSync(instance);\n } catch (err) {\n discardCommitOperations(instance);\n throw err;\n }\n if (isPromiseLike(result)) {\n // Async components are not supported. Components must be synchronous and\n // must not return a Promise from their render function.\n throw new Error(\n 'Async components are not supported. Components must be synchronous.'\n );\n } else {\n // Try runtime fast-lane synchronously; if it activates we do not enqueue\n // follow-up work and the commit happens atomically in this task.\n // (Runtime fast-lane has conservative preconditions.)\n const fastlaneBridge = (\n globalThis as {\n __ASKR_FASTLANE?: {\n tryRuntimeFastLaneSync?: (\n instance: unknown,\n result: unknown\n ) => boolean;\n };\n }\n ).__ASKR_FASTLANE;\n try {\n const used = fastlaneBridge?.tryRuntimeFastLaneSync?.(instance, result);\n if (used) {\n warnUnusedStateReads(instance);\n return;\n }\n } catch (err) {\n // If invariant check failed in dev, surface the error; otherwise fall back\n if (isDevelopmentEnvironment()) throw err;\n }\n\n // Fallback: enqueue the render/commit normally\n globalScheduler.enqueue(() => {\n // Handle placeholder-based updates: when a component initially returned null,\n // we created a comment placeholder. If it now has content, we need to create\n // a host element and replace the placeholder.\n if (!instance.target && instance._placeholder) {\n // Component previously returned null (has placeholder), check if now has content\n if (result === null || result === undefined) {\n // Still null - nothing to do, keep placeholder\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n commitRenderedComponent(instance);\n return;\n }\n\n // Has content now - need to create DOM and replace placeholder\n const placeholder = instance._placeholder;\n const parent = placeholder.parentNode;\n if (!parent) {\n // Placeholder was removed from DOM - can't render\n logger.warn(\n '[Askr] placeholder no longer in DOM, cannot render component'\n );\n return;\n }\n\n // Create a new host element for the content\n const host = document.createElement('div');\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n\n // Set up instance for normal updates\n const oldInstance = currentInstance;\n currentInstance = instance;\n const lifecycleBatch = beginLifecycleCommitBatch();\n try {\n try {\n withContext(executionFrame, () => {\n evaluate(result, host);\n });\n\n // Replace placeholder with host\n parent.replaceChild(host, placeholder);\n } catch (err) {\n discardLifecycleCommitBatch(lifecycleBatch);\n throw err;\n }\n\n // Set up instance for future updates\n instance.target = host;\n instance._placeholder = undefined;\n (\n host as Element & { __ASKR_INSTANCE?: ComponentInstance }\n ).__ASKR_INSTANCE = instance;\n\n flushLifecycleCommitBatch(lifecycleBatch);\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n commitRenderedComponent(instance);\n } finally {\n currentInstance = oldInstance;\n }\n return;\n }\n\n if (instance.target) {\n let oldChildren: Node[] = [];\n let restoredOldChildren = false;\n try {\n const wasFirstMount = !instance.mounted;\n // Ensure nested component executions during evaluation have access to\n // the current component instance. This allows nested components to\n // call `state()`, `resource()`, and other runtime helpers which\n // rely on `getCurrentComponentInstance()` being available.\n const oldInstance = currentInstance;\n currentInstance = instance;\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n // Capture snapshot of current children (by reference) so we can\n // restore them on render failure without losing event listeners or\n // instance attachments.\n oldChildren = Array.from(instance.target.childNodes);\n\n const lifecycleBatch = beginLifecycleCommitBatch();\n try {\n try {\n withContext(executionFrame, () => {\n evaluate(result, instance.target, undefined, instance);\n });\n } catch (e) {\n discardLifecycleCommitBatch(lifecycleBatch);\n throw e;\n }\n } catch (e) {\n // If evaluation failed, attempt to cleanup any partially-added nodes\n // and restore the old children to preserve listeners and instances.\n try {\n const newChildren = Array.from(instance.target.childNodes);\n const preservedChildren = new Set(oldChildren);\n for (const n of newChildren) {\n if (preservedChildren.has(n)) {\n continue;\n }\n try {\n cleanupInstancesUnder(n);\n } catch (err) {\n logger.warn(\n '[Askr] error cleaning up failed commit children:',\n err\n );\n }\n }\n } catch (_err) {\n void _err;\n }\n\n // Restore original children by re-inserting the old node references\n // this preserves attached listeners and instance backrefs.\n try {\n incDevCounter('__DOM_REPLACE_COUNT');\n setDevValue(\n '__LAST_DOM_REPLACE_STACK_COMPONENT_RESTORE',\n new Error().stack\n );\n } catch (e) {\n void e;\n }\n instance.target.replaceChildren(...oldChildren);\n restoredOldChildren = true;\n throw e;\n } finally {\n currentInstance = oldInstance;\n }\n\n flushLifecycleCommitBatch(lifecycleBatch);\n\n // Commit succeeded — finalize recorded state reads so subscriptions reflect\n // the last *committed* render. This updates per-state reader maps\n // deterministically and synchronously with the commit.\n finalizeReadSubscriptions(instance);\n warnUnusedStateReads(instance);\n\n instance.mounted = true;\n // Execute mount operations after first mount (do NOT run these with\n // currentInstance set - they may perform state mutations/registrations)\n executeCommittedLifecycleOperations(instance, wasFirstMount);\n } catch (renderError) {\n discardCommitOperations(instance);\n // Atomic rendering: rollback on render error. Attempt non-lossy restore of\n // original child node references to preserve listeners/instances.\n try {\n const currentChildren = Array.from(instance.target.childNodes);\n const preservedChildren = restoredOldChildren\n ? new Set(oldChildren)\n : null;\n for (const n of currentChildren) {\n if (preservedChildren?.has(n)) {\n continue;\n }\n try {\n cleanupInstancesUnder(n);\n } catch (err) {\n logger.warn(\n '[Askr] error cleaning up partial children during rollback:',\n err\n );\n }\n }\n } catch (_err) {\n void _err;\n }\n\n try {\n incDevCounter('__DOM_REPLACE_COUNT');\n setDevValue(\n '__LAST_DOM_REPLACE_STACK_COMPONENT_ROLLBACK',\n new Error().stack\n );\n instance.target.replaceChildren(...oldChildren);\n } catch {\n // Fallback to innerHTML restore if replaceChildren fails for some reason.\n instance.target.innerHTML = domSnapshot;\n }\n\n throw renderError;\n }\n }\n });\n }\n}\n\n/**\n * Execute a component's render function synchronously.\n * Returns either a vnode/promise immediately (does NOT render).\n * Rendering happens separately through runComponent.\n */\nexport function renderComponentInline(\n instance: ComponentInstance\n): unknown | Promise<unknown> {\n // Reused inline instances can cross renderer cleanup boundaries while their\n // host node is retained. Make sure state writes still enqueue this instance.\n instance.notifyUpdate = instance._enqueueRun!;\n\n // Ensure inline executions (rendered during parent's evaluate) still\n // receive a render token and have their state reads finalized so\n // subscriptions are correctly recorded. If this function is called\n // as part of a scheduled run, the token will already be set by\n // runComponent and we should not overwrite it.\n const hadToken = instance._currentRenderToken !== undefined;\n const prevToken = instance._currentRenderToken;\n const prevPendingReads = instance._pendingReadSources;\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n if (!hadToken) {\n instance._currentRenderToken = nextRenderToken();\n instance._pendingReadSources = undefined;\n }\n\n try {\n const result = executeComponentSync(instance);\n // If we set the token for inline execution, finalize subscriptions now\n // unless the parent DOM commit is still provisional. Renderer commit\n // batches flush these reads only after DOM evaluation succeeds.\n if (!hadToken) {\n finalizeInlineReadSubscriptions(\n instance,\n instance._currentRenderToken!,\n instance._pendingReadSources\n );\n }\n commitRenderedComponent(instance);\n return result;\n } finally {\n // Restore previous token/read states for nested inline render scenarios\n instance._currentRenderToken = prevToken;\n instance._pendingReadSources = prevPendingReads;\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n }\n}\n\nexport function warnUnusedStateReads(instance: ComponentInstance): void {\n for (let i = 0; i < instance.stateValues.length; i++) {\n const state = instance.stateValues[i];\n const hasCommittedUsage =\n (state?._readers?.size ?? 0) > 0 ||\n ((state as { _derivedSubscribers?: Set<unknown> } | undefined)\n ?._derivedSubscribers?.size ?? 0) > 0;\n\n if (\n state &&\n !state._hasBeenRead &&\n !state._hasEverBeenRead &&\n !hasCommittedUsage\n ) {\n try {\n const name = instance.fn?.name || '<anonymous>';\n warnInstanceOnce(\n instance,\n `unused-state:${i}`,\n `[askr] Unused state variable detected in ${name} at index ${i}. State should be read during render or removed.`\n );\n } catch {\n warnInstanceOnce(\n instance,\n `unused-state:${i}`,\n `[askr] Unused state variable detected. State should be read during render or removed.`\n );\n }\n }\n }\n}\n\nfunction executeComponentSync(\n instance: ComponentInstance\n): unknown | Promise<unknown> {\n resetRenderState(instance);\n incDevCounter('componentRuns');\n incDevCounter('componentReruns');\n\n const savedPortalScope = currentPortalScope;\n currentInstance = instance;\n currentPortalScope = instance.portalScope ?? savedPortalScope;\n stateIndex = 0;\n\n let didComplete = false;\n\n try {\n // Track render time in dev mode\n const renderStartTime = isDevelopmentEnvironment() ? Date.now() : 0;\n\n // Create context object with abort signal\n const context = {\n get signal(): AbortSignal {\n return ensureAbortController(instance).signal;\n },\n };\n\n // Execute component within its owner frame (provider chain).\n // This ensures all context reads see the correct provider values.\n // We create a new execution frame whose parent is the ownerFrame. The\n // `values` map is lazily allocated to avoid per-render Map allocations\n // for components that do not use context.\n const executionFrame: ContextFrame = {\n parent: instance.ownerFrame,\n values: null,\n };\n const result = withContext(executionFrame, () =>\n instance.fn(instance.props, context)\n );\n\n // Check render time\n const renderTime = Date.now() - renderStartTime;\n if (renderTime > 5) {\n warnInstanceOnce(\n instance,\n 'slow-render',\n `[askr] Slow render detected: ${renderTime}ms. Consider optimizing component performance.`\n );\n }\n\n // Mark first render complete after successful execution\n // This enables hook order validation on subsequent renders\n if (!instance.firstRenderComplete) {\n instance.firstRenderComplete = true;\n }\n\n didComplete = true;\n return result;\n } finally {\n if (!didComplete) {\n discardCommitOperations(instance);\n }\n // Synchronous path: we did not push a fresh frame, so nothing to pop here.\n currentInstance = null;\n currentPortalScope = savedPortalScope;\n }\n}\n\n/**\n * Public entry point: Execute component with full lifecycle (execute + render)\n * Handles both initial mount and re-execution. Always enqueues through scheduler.\n * Single entry point to avoid lifecycle divergence.\n */\nexport function executeComponent(instance: ComponentInstance): void {\n // Lazily recreate abort controller only when signal is actually requested.\n instance.abortController = null;\n\n // Setup notifyUpdate callback using prebound helper to avoid per-call closures\n instance.notifyUpdate = instance._enqueueRun!;\n\n // Enqueue the initial component run\n globalScheduler.enqueue(instance._pendingRunTask!);\n}\n\nexport function getCurrentInstance(): ComponentInstance | null {\n return currentInstance;\n}\n\n/**\n * Get the abort signal for the current component\n * Used to cancel async operations on unmount/navigation\n *\n * The signal is guaranteed to be aborted when:\n * - Component unmounts\n * - Navigation occurs (different route)\n * - Parent is destroyed\n *\n * IMPORTANT: getSignal() must be called during component render execution.\n * It captures the current component instance from context.\n *\n * @returns AbortSignal that will be aborted when component unmounts\n * @throws Error if called outside component execution\n *\n * @example\n * ```ts\n * // ✅ Correct: called during render, used in async operation\n * export async function UserPage({ id }: { id: string }) {\n * const signal = getSignal();\n * const user = await fetch(`/api/users/${id}`, { signal });\n * return <div>{user.name}</div>;\n * }\n *\n * // ✅ Correct: passed to event handler\n * export function Button() {\n * const signal = getSignal();\n * return {\n * type: 'button',\n * props: {\n * onClick: async () => {\n * const data = await fetch(url, { signal });\n * }\n * }\n * };\n * }\n *\n * // ❌ Wrong: called outside component context\n * const signal = getSignal(); // Error: not in component\n * ```\n */\nexport function getSignal(): AbortSignal {\n if (!currentInstance) {\n throw new Error(\n 'getSignal() can only be called during component render execution. ' +\n 'Ensure you are calling this from inside your component function.'\n );\n }\n return ensureAbortController(currentInstance).signal;\n}\n\n/**\n * Finalize read subscriptions for an instance after a successful commit.\n * - Update per-state readers map to point to this instance's last committed token\n * - Remove this instance from states it no longer reads\n * This is deterministic and runs synchronously with commit to ensure\n * subscribers are only notified when they actually read a state in their\n * last committed render.\n */\nexport function finalizeReadSubscriptions(instance: ComponentInstance): void {\n finalizeReadableSubscriptions(instance);\n}\n\nexport function getNextStateIndex(): number {\n return stateIndex++;\n}\n\nexport function claimHookIndex(\n instance: ComponentInstance,\n hookName: string\n): number {\n const index = getNextStateIndex();\n\n if (index < instance.stateIndexCheck) {\n throw new Error(\n `Hook index violation: ${hookName}() call at index ${index}, ` +\n `but previously saw index ${instance.stateIndexCheck}. ` +\n `This happens when render-scoped hooks are called conditionally (inside if/for/etc). ` +\n `Move all ${hookName}() calls to the top level of your component function, ` +\n `before any conditionals.`\n );\n }\n\n instance.stateIndexCheck = index;\n\n if (instance.firstRenderComplete) {\n if (instance.expectedStateIndices[index] !== index) {\n throw new Error(\n `Hook order violation: ${hookName}() called at index ${index}, ` +\n `but this index was not in the first render's sequence [${instance.expectedStateIndices.join(', ')}]. ` +\n `This usually means ${hookName}() is inside a conditional or loop. ` +\n `Move all render-scoped hooks to the top level of your component function.`\n );\n }\n } else {\n instance.expectedStateIndices.push(index);\n }\n\n return index;\n}\n\nexport function getCurrentStateIndex(): number {\n return stateIndex;\n}\n\nexport function resetStateIndex(): void {\n stateIndex = 0;\n}\n\nexport function setStateIndex(value: number): void {\n stateIndex = value;\n}\n\n/**\n * Mount a component instance.\n * This is just an alias to executeComponent() to maintain API compatibility.\n * All lifecycle logic is unified in executeComponent().\n */\nexport function mountComponent(instance: ComponentInstance): void {\n executeComponent(instance);\n}\n\n/**\n * Clean up component — abort pending operations\n * Called on unmount or route change\n */\nexport function cleanupComponent(instance: ComponentInstance): void {\n const savedInstance = currentInstance;\n const savedPortalScope = currentPortalScope;\n currentInstance = null;\n currentPortalScope = null;\n\n try {\n const cleanupErrors: unknown[] = [];\n const recordCleanupError = (message: string, err: unknown): void => {\n if (instance.cleanupStrict) {\n cleanupErrors.push(err);\n } else if (isDevelopmentEnvironment()) {\n logger.warn(message, err);\n }\n };\n\n const ownedChildScopes = instance._ownedChildScopes;\n if (ownedChildScopes && ownedChildScopes.size > 0) {\n instance._ownedChildScopes = new Set();\n for (const scope of ownedChildScopes) {\n try {\n scope.dispose();\n } catch (err) {\n recordCleanupError('[Askr] child scope cleanup threw:', err);\n }\n }\n }\n\n // Execute cleanup functions (from mount effects)\n const cleanupFns = instance.cleanupFns;\n instance.cleanupFns = [];\n for (const cleanup of cleanupFns) {\n try {\n cleanup();\n } catch (err) {\n recordCleanupError('[Askr] cleanup function threw:', err);\n }\n }\n\n // Remove deterministic state subscriptions for this instance\n try {\n cleanupReadableSubscriptions(instance);\n } catch (err) {\n recordCleanupError('[Askr] readable subscription cleanup threw:', err);\n }\n\n // Abort all pending operations\n try {\n if (\n instance.abortController &&\n !instance.abortController.signal.aborted\n ) {\n instance.abortController.abort();\n }\n } catch (err) {\n recordCleanupError('[Askr] abort controller cleanup threw:', err);\n }\n instance.abortController = null;\n\n // Clear update callback to prevent dangling references and stale updates\n instance.lifecycleGeneration++;\n instance.evaluationGeneration++;\n instance.mountOperations = [];\n instance.commitOperations = [];\n instance.lifecycleSlots = [];\n instance.hasPendingUpdate = false;\n instance.notifyUpdate = null;\n instance._placeholder = undefined;\n\n // Mark instance as unmounted so external tracking (e.g., portal host lists)\n // can deterministically prune stale instances. Not marking this leads to\n // retained \"mounted\" flags across cleanup boundaries which breaks\n // owner selection in the portal fallback.\n instance.mounted = false;\n\n if (cleanupErrors.length > 0) {\n throw new AggregateError(\n cleanupErrors,\n `Cleanup failed for component ${instance.id}`\n );\n }\n } finally {\n currentInstance = savedInstance;\n currentPortalScope = savedPortalScope;\n }\n}\n\nexport function registerOwnedChildScope(\n instance: ComponentInstance,\n scope: OwnedChildScope\n): void {\n const scopes = (instance._ownedChildScopes ??= new Set());\n scopes.add(scope);\n}\n\nexport function unregisterOwnedChildScope(\n instance: ComponentInstance,\n scope: OwnedChildScope\n): void {\n instance._ownedChildScopes?.delete(scope);\n}\n\nfunction warnInstanceOnce(\n instance: ComponentInstance,\n key: string,\n message: string\n): void {\n if (isProductionEnvironment()) return;\n const warnings = (instance.devWarningsEmitted ??= new Set());\n if (warnings.has(key)) return;\n warnings.add(key);\n logger.warn(message);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAsFA,SAAgB,wBACd,IACA,IACA,OACA,QACmB;CACnB,MAAM,WAA8B;EAClC;EACA;EACA;EACA;EACA,gBAAgB;EAChB,aAAa,iBAAiB,eAAe,sBAAsB;EACnE,SAAS;EACT,iBAAiB;EACjB,aAAa,CAAC;EACd,sBAAsB;EACtB,cAAc;EAEd,mBAAmB;EACnB,iBAAiB;EACjB,aAAa;EACb,iBAAiB;EACjB,sBAAsB,CAAC;EACvB,qBAAqB;EACrB,iBAAiB,CAAC;EAClB,kBAAkB,CAAC;EACnB,YAAY,CAAC;EACb,gBAAgB,CAAC;EACjB,qBAAqB;EACrB,kBAAkB;EAClB,YAAY;EACZ,KAAK;EACL,eAAe;EACf,QAAQ;EAGR,qBAAqB;EACrB,iBAAiB;EACjB,qBAAqB;EACrB,kBAAkB;EAClB,oBAAoB;CACtB;CAGA,SAAS,wBAAwB;EAE/B,SAAS,mBAAmB;EAC5B,IAAI,SAAS,iBAAiB,M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