@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.1

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/package.json +1 -1
  2. package/src/attrs.ts +35 -0
  3. package/src/build/bundle.ts +442 -0
  4. package/src/build/compile.ts +20 -11
  5. package/src/build/files.ts +136 -0
  6. package/src/build/graph.ts +158 -0
  7. package/src/build/index.ts +231 -1296
  8. package/src/build/islands.ts +177 -0
  9. package/src/build/manifest.ts +224 -0
  10. package/src/build/plugin-host.ts +184 -0
  11. package/src/build/prerender.ts +93 -0
  12. package/src/build/report.ts +157 -0
  13. package/src/build/resolve.ts +58 -0
  14. package/src/build/routes.ts +135 -0
  15. package/src/build/static.ts +73 -0
  16. package/src/cli/check.ts +228 -114
  17. package/src/cli/index.ts +298 -79
  18. package/src/cli/new.ts +67 -35
  19. package/src/compiler/analyze.ts +666 -91
  20. package/src/compiler/dom.ts +822 -629
  21. package/src/compiler/mistakes.ts +354 -0
  22. package/src/compiler/patch.ts +1 -1
  23. package/src/compiler/server-only.ts +164 -21
  24. package/src/compiler/shared.ts +442 -0
  25. package/src/compiler/ssr.ts +390 -324
  26. package/src/context.ts +120 -0
  27. package/src/dev/index.ts +291 -76
  28. package/src/dom/action.ts +23 -13
  29. package/src/dom/extras.ts +157 -84
  30. package/src/dom/index.ts +154 -33
  31. package/src/dom/island.ts +117 -39
  32. package/src/dom/loop.ts +34 -21
  33. package/src/index.ts +2 -1
  34. package/src/jsx-runtime.ts +11 -0
  35. package/src/plugins/context.ts +57 -0
  36. package/src/plugins/index.ts +120 -17
  37. package/src/reactivity/index.ts +250 -157
  38. package/src/reactivity/selector.ts +11 -9
  39. package/src/reactivity/store.ts +37 -12
  40. package/src/server/action.ts +217 -74
  41. package/src/server/compress.ts +155 -56
  42. package/src/server/cookies.ts +59 -22
  43. package/src/server/env.ts +18 -13
  44. package/src/server/http.ts +201 -33
  45. package/src/server/index.ts +964 -737
  46. package/src/server/loader.ts +120 -0
  47. package/src/server/router.ts +27 -8
  48. package/src/server/security.ts +87 -39
  49. package/src/server/standard-schema.ts +42 -11
  50. package/src/server/static-dir.ts +144 -0
  51. package/src/ssr/context.ts +96 -10
  52. package/src/ssr/index.ts +225 -56
  53. package/src/style.ts +33 -0
  54. package/src/test/index.ts +5 -0
  55. package/src/unrenderable.ts +35 -0
@@ -9,6 +9,10 @@ const DISPOSED = 8
9
9
  const COMPUTED = 16
10
10
  const EFFECT = 32
11
11
  const QUEUED = 64
12
+ // A computed nothing subscribes to: it keeps its deps list but stays out of its sources' subscriber lists, checking versions on read.
13
+ const UNOBSERVED = 128
14
+ // Queued for the current flush round (distinct from QUEUED, which marks the next round).
15
+ const PENDING = 256
12
16
 
13
17
  interface Link {
14
18
  d: DepNode
@@ -25,31 +29,37 @@ interface DepNode {
25
29
  v: number // version
26
30
  s: Link | undefined // subs head
27
31
  st: Link | undefined // subs tail
28
- ls: SubNode | undefined // lastSub
29
32
  le: number // lastEpoch
30
- f?: number // flags (only on ComputedNode)
33
+ f: number // flags (0 on SignalNode, bitmask on ComputedNode)
31
34
  }
32
35
 
33
- interface SubNode {
36
+ // Anything an owner holds: a child owner, an effect or a computed.
37
+ interface Owned {
34
38
  f: number // flags
39
+ o: Owner | undefined // owner
40
+ po: Owned | undefined // prevOwned
41
+ no: Owned | undefined // nextOwned
42
+ k: (() => void) | undefined // cleanup returned by the effect
43
+ }
44
+
45
+ interface SubNode extends Owned {
35
46
  e: number // epoch
36
47
  d: Link | undefined // deps head
37
48
  dt: Link | undefined // deps tail
38
49
  c: Link | undefined // cursor
39
- no: SubNode | undefined // nextOwned
40
- nq: SubNode | undefined // nextQueued
41
- k: (() => void) | undefined // cleanup returned by the effect
42
- o: Owner | undefined // owner
43
50
  r(): void // run
44
51
  }
45
52
 
46
- export interface Owner {
47
- p: Owner | undefined // parent
48
- fc: Owner | undefined // firstChild
49
- ns: Owner | undefined // nextSibling
50
- fs: SubNode | undefined // firstSub
53
+ export interface IdScope {
54
+ p: string
55
+ n: number
56
+ }
57
+
58
+ export interface Owner extends Owned {
59
+ fs: Owned | undefined // first owned
51
60
  cl: (() => void)[] | undefined // cleanups
52
- x: boolean // disposed
61
+ cx?: Map<symbol, unknown> | undefined // context values provided at this owner
62
+ id?: IdScope | undefined // deterministic useId() scope
53
63
  }
54
64
 
55
65
  export interface Signal<T> {
@@ -66,15 +76,16 @@ let currentSub: SubNode | undefined
66
76
  let currentOwner: Owner | undefined
67
77
  let globalEpoch = 0
68
78
  let batchDepth = 0
69
- let qHead: SubNode | undefined
70
- let qTail: SubNode | undefined
79
+ let q: SubNode[] = []
80
+ let qNext: SubNode[] = []
71
81
  let flushing = false
82
+ // Signal writes so far: an unobserved computed that checked at the same count can skip its version walk.
83
+ let writes = 0
72
84
 
73
85
  function linkSub(dep: DepNode, sub: SubNode): void {
74
- if (dep.ls === sub && dep.le === sub.e)
86
+ if (dep.le === sub.e)
75
87
  return
76
88
 
77
- dep.ls = sub
78
89
  dep.le = sub.e
79
90
 
80
91
  const cur = sub.c
@@ -92,15 +103,12 @@ function linkSub(dep: DepNode, sub: SubNode): void {
92
103
  e: sub.e,
93
104
  pd: undefined,
94
105
  nd: undefined,
95
- ps: dep.st,
106
+ ps: undefined,
96
107
  ns: undefined,
97
108
  }
98
109
 
99
- if (dep.st !== undefined)
100
- dep.st.ns = link
101
- else
102
- dep.s = link
103
- dep.st = link
110
+ if (!(sub.f & UNOBSERVED))
111
+ addSub(link)
104
112
 
105
113
  if (cur !== undefined) {
106
114
  const prev = cur.pd
@@ -123,8 +131,50 @@ function linkSub(dep: DepNode, sub: SubNode): void {
123
131
  }
124
132
  }
125
133
 
134
+ // Puts a link at the end of its source's subscriber list; a computed gaining its first subscriber follows its own sources again.
135
+ function addSub(link: Link): void {
136
+ const dep = link.d
137
+ const tail = dep.st
138
+ link.ps = tail
139
+ link.ns = undefined
140
+ if (tail !== undefined) {
141
+ tail.ns = link
142
+ dep.st = link
143
+ return
144
+ }
145
+ dep.s = dep.st = link
146
+ if (dep.f & UNOBSERVED) {
147
+ const comp = dep as unknown as SubNode
148
+ comp.f &= ~UNOBSERVED
149
+ for (let l = comp.d; l !== undefined; l = l.nd)
150
+ addSub(l)
151
+ }
152
+ }
153
+
154
+ function unobserve(comp: SubNode): void {
155
+ comp.f |= UNOBSERVED
156
+ for (let l = comp.d; l !== undefined; l = l.nd)
157
+ removeSub(l)
158
+ }
159
+
160
+ // Takes a link out of its source's subscriber list; a computed losing its last subscriber lets go of its own sources.
161
+ function removeSub(link: Link): void {
162
+ const { d, ps, ns } = link
163
+ if (ps !== undefined)
164
+ ps.ns = ns
165
+ else
166
+ d.s = ns
167
+ if (ns !== undefined)
168
+ ns.ps = ps
169
+ else
170
+ d.st = ps
171
+ link.ps = link.ns = undefined
172
+ if (d.s === undefined && (d.f & (COMPUTED | UNOBSERVED)) === COMPUTED)
173
+ unobserve(d as unknown as SubNode)
174
+ }
175
+
126
176
  function unlink(l: Link): void {
127
- const { d, s, pd, nd, ps, ns } = l
177
+ const { s, pd, nd } = l
128
178
  if (pd !== undefined)
129
179
  pd.nd = nd
130
180
  else
@@ -133,14 +183,8 @@ function unlink(l: Link): void {
133
183
  nd.pd = pd
134
184
  else
135
185
  s.dt = pd
136
- if (ps !== undefined)
137
- ps.ns = ns
138
- else
139
- d.s = ns
140
- if (ns !== undefined)
141
- ns.ps = ps
142
- else
143
- d.st = ps
186
+ if (!(s.f & UNOBSERVED))
187
+ removeSub(l)
144
188
  }
145
189
 
146
190
  function unlinkRemaining(sub: SubNode): void {
@@ -158,33 +202,33 @@ function unlinkRemaining(sub: SubNode): void {
158
202
  function unlinkAll(sub: SubNode): void {
159
203
  let l = sub.d
160
204
  sub.d = sub.dt = sub.c = undefined
205
+ if (sub.f & UNOBSERVED)
206
+ return
161
207
  while (l !== undefined) {
162
208
  const next = l.nd
163
- const { d, ps, ns } = l
164
- if (ps !== undefined)
165
- ps.ns = ns
166
- else
167
- d.s = ns
168
- if (ns !== undefined)
169
- ns.ps = ps
170
- else
171
- d.st = ps
209
+ removeSub(l)
172
210
  l = next
173
211
  }
174
212
  }
175
213
 
214
+ // A child effect can sit ahead of its parent in the queue; the parent runs first, since its run may dispose the child.
215
+ function runParentFirst(sub: SubNode): void {
216
+ sub.f &= ~PENDING
217
+ for (let o = sub.o; o !== undefined; o = o.o) {
218
+ if (o.f & (PENDING | QUEUED)) {
219
+ runParentFirst(o as unknown as SubNode)
220
+ break
221
+ }
222
+ }
223
+ if (!(sub.f & DISPOSED) && shouldRun(sub))
224
+ sub.r()
225
+ }
226
+
176
227
  function enqueue(sub: SubNode): void {
177
228
  if (sub.f & (QUEUED | DISPOSED))
178
229
  return
179
230
  sub.f |= QUEUED
180
- sub.nq = undefined
181
- if (qTail !== undefined) {
182
- qTail.nq = sub
183
- qTail = sub
184
- }
185
- else {
186
- qHead = qTail = sub
187
- }
231
+ q.push(sub)
188
232
  }
189
233
 
190
234
  function propagate(dep: DepNode, flag: number): void {
@@ -219,36 +263,34 @@ function flush(): void {
219
263
  const errors: unknown[] = []
220
264
  try {
221
265
  let rounds = 0
222
- while (qHead !== undefined) {
223
- if (++rounds > 1000)
224
- throw new Error('kay: reactive cycle')
225
- let node: SubNode | undefined = qHead
226
- qHead = qTail = undefined
227
- while (node !== undefined) {
228
- const next: SubNode | undefined = node.nq
229
- node.nq = undefined
230
- node.f &= ~QUEUED
231
- try {
232
- if (!(node.f & DISPOSED) && shouldRun(node))
233
- node.r()
234
- }
235
- catch (err) {
236
- errors.push(err)
266
+ while (q.length > 0) {
267
+ if (++rounds > 1000) {
268
+ errors.push(new Error('kay: reactive cycle'))
269
+ break
270
+ }
271
+ const round = q
272
+ q = qNext
273
+ qNext = round
274
+ for (const node of round)
275
+ node.f = (node.f & ~QUEUED) | PENDING
276
+ for (const node of round) {
277
+ if (node.f & PENDING) {
278
+ try {
279
+ runParentFirst(node)
280
+ }
281
+ catch (err) {
282
+ errors.push(err)
283
+ }
237
284
  }
238
- node = next
239
285
  }
286
+ round.length = 0
240
287
  }
241
288
  }
242
289
  finally {
243
290
  flushing = false
244
- let left = qHead
245
- qHead = qTail = undefined
246
- while (left !== undefined) {
247
- const next: SubNode | undefined = left.nq
248
- left.nq = undefined
249
- left.f &= ~QUEUED
250
- left = next
251
- }
291
+ for (const node of q)
292
+ node.f &= ~QUEUED
293
+ q.length = 0
252
294
  }
253
295
  if (errors.length === 1)
254
296
  throw errors[0]
@@ -258,14 +300,17 @@ function flush(): void {
258
300
 
259
301
  function shouldRun(sub: SubNode): boolean {
260
302
  const f = sub.f
303
+ if (f & DISPOSED)
304
+ return false
261
305
  if (f & DIRTY)
262
306
  return true
263
- if (!(f & CHECK))
307
+ if (!(f & CHECK) && (!(f & UNOBSERVED) || (sub as ComputedNode<unknown>).w === writes))
264
308
  return false
309
+ const w = writes
265
310
  let l = sub.d
266
311
  while (l !== undefined) {
267
312
  const dep = l.d
268
- if (dep.f !== undefined && (dep.f & COMPUTED)) {
313
+ if (dep.f & COMPUTED) {
269
314
  const comp = dep as unknown as ComputedNode<unknown>
270
315
  if (shouldRun(comp))
271
316
  comp.r()
@@ -277,6 +322,8 @@ function shouldRun(sub: SubNode): boolean {
277
322
  l = l.nd
278
323
  }
279
324
  sub.f &= ~CHECK
325
+ if (f & UNOBSERVED)
326
+ (sub as ComputedNode<unknown>).w = w
280
327
  return false
281
328
  }
282
329
 
@@ -284,8 +331,8 @@ class SignalNode<T> implements DepNode, Signal<T> {
284
331
  v = 0
285
332
  s: Link | undefined = undefined
286
333
  st: Link | undefined = undefined
287
- ls: SubNode | undefined = undefined
288
334
  le = -1
335
+ f = 0
289
336
  _v: T
290
337
 
291
338
  constructor(initial: T) {
@@ -303,6 +350,7 @@ class SignalNode<T> implements DepNode, Signal<T> {
303
350
  return
304
351
  this._v = next
305
352
  this.v++
353
+ writes++
306
354
  if (this.s !== undefined) {
307
355
  propagate(this, DIRTY)
308
356
  flush()
@@ -318,35 +366,45 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
318
366
  v = 0
319
367
  s: Link | undefined = undefined
320
368
  st: Link | undefined = undefined
321
- ls: SubNode | undefined = undefined
322
369
  le = -1
323
- f = COMPUTED | DIRTY
370
+ f = COMPUTED | DIRTY | UNOBSERVED
324
371
  e = -1
325
372
  d: Link | undefined = undefined
326
373
  dt: Link | undefined = undefined
327
374
  c: Link | undefined = undefined
328
- no: SubNode | undefined = undefined
329
- nq: SubNode | undefined = undefined
375
+ po: Owned | undefined = undefined
376
+ no: Owned | undefined = undefined
330
377
  k: (() => void) | undefined = undefined
331
378
  o: Owner | undefined = undefined
332
379
  _f: () => T
333
380
  _v!: T
334
381
  _i = false
382
+ w = -1
335
383
 
336
- constructor(fn: () => T) {
384
+ constructor(fn: () => T, owner: Owner | undefined) {
337
385
  this._f = fn
386
+ if (owner !== undefined)
387
+ adopt(this, owner)
338
388
  }
339
389
 
340
- r(): void {
390
+ r(observe?: boolean): void {
391
+ if (this.f & DISPOSED)
392
+ return
341
393
  if (this.f & RUNNING)
342
394
  throw new Error('kay: cyclic computed')
343
395
  const prevSub = currentSub
344
396
  const prevOwner = currentOwner
397
+ const w = writes
398
+ // A first run for an observed reader links straight into its sources; roll back if the compute throws.
399
+ const direct = observe && this.d === undefined
400
+ if (direct)
401
+ this.f &= ~UNOBSERVED
345
402
  this.f = (this.f | RUNNING) & ~(DIRTY | CHECK)
346
403
  this.e = ++globalEpoch
347
404
  this.c = this.d
348
405
  currentSub = this
349
406
  currentOwner = undefined
407
+ let ok = false
350
408
  try {
351
409
  const next = (this._f)()
352
410
  if (!this._i || !Object.is(this._v, next)) {
@@ -354,25 +412,31 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
354
412
  this._i = true
355
413
  this.v++
356
414
  }
415
+ this.w = w
416
+ ok = true
357
417
  }
358
418
  finally {
359
419
  unlinkRemaining(this)
360
420
  this.f &= ~RUNNING
421
+ if (direct && !ok && this.s === undefined)
422
+ unobserve(this)
361
423
  currentSub = prevSub
362
424
  currentOwner = prevOwner
363
425
  }
364
426
  }
365
427
 
366
428
  get value(): T {
367
- if (shouldRun(this))
368
- this.r()
369
- if (currentSub !== undefined)
370
- linkSub(this, currentSub)
429
+ if (!(this.f & DISPOSED)) {
430
+ if (shouldRun(this))
431
+ this.r(currentSub !== undefined && !(currentSub.f & UNOBSERVED))
432
+ if (currentSub !== undefined && !(this.f & DISPOSED))
433
+ linkSub(this, currentSub)
434
+ }
371
435
  return this._v
372
436
  }
373
437
 
374
438
  peek(): T {
375
- if (shouldRun(this))
439
+ if (!(this.f & DISPOSED) && shouldRun(this))
376
440
  this.r()
377
441
  return this._v
378
442
  }
@@ -385,39 +449,35 @@ class EffectNode implements SubNode, Owner {
385
449
  d: Link | undefined = undefined
386
450
  dt: Link | undefined = undefined
387
451
  c: Link | undefined = undefined
388
- no: SubNode | undefined = undefined
389
- nq: SubNode | undefined = undefined
452
+ po: Owned | undefined = undefined
453
+ no: Owned | undefined = undefined
390
454
  k: (() => void) | undefined = undefined
391
- o: Owner | undefined
392
- p: Owner | undefined
393
- fc: Owner | undefined = undefined
394
- ns: Owner | undefined = undefined
395
- fs: SubNode | undefined = undefined
455
+ o: Owner | undefined = undefined
456
+ fs: Owned | undefined = undefined
396
457
  cl: (() => void)[] | undefined = undefined
397
- x = false
398
458
  _f: () => void | (() => void)
399
459
 
400
460
  constructor(fn: () => void | (() => void), owner: Owner | undefined) {
401
461
  this._f = fn
402
- this.o = this.p = owner
403
- if (owner !== undefined) {
404
- this.no = owner.fs
405
- owner.fs = this
406
- }
462
+ if (owner !== undefined)
463
+ adopt(this, owner)
407
464
  }
408
465
 
409
466
  r(): void {
410
467
  if (this.f & DISPOSED)
411
468
  return
469
+ this.f &= ~(DIRTY | CHECK)
412
470
  releaseOwned(this)
413
471
  if (this.k !== undefined) {
414
472
  const fn = this.k
415
473
  this.k = undefined
416
474
  fn()
417
475
  }
476
+ if (this.f & DISPOSED)
477
+ return
418
478
  const prevSub = currentSub
419
479
  const prevOwner = currentOwner
420
- this.f = (this.f | RUNNING) & ~(DIRTY | CHECK)
480
+ this.f |= RUNNING
421
481
  this.e = ++globalEpoch
422
482
  this.c = this.d
423
483
  currentSub = this
@@ -440,58 +500,65 @@ class EffectNode implements SubNode, Owner {
440
500
  }
441
501
  }
442
502
 
443
- export function createOwner(parent: Owner | undefined = currentOwner): Owner {
444
- const owner: Owner = {
445
- p: parent,
446
- fc: undefined,
447
- ns: undefined,
448
- fs: undefined,
449
- cl: undefined,
450
- x: false,
451
- }
452
- if (parent !== undefined) {
453
- owner.ns = parent.fc
454
- parent.fc = owner
503
+ // A node adopted by a disposed owner is born disposed; otherwise it joins the head of the owner's list.
504
+ function adopt(node: Owned, owner: Owner): void {
505
+ if (owner.f & DISPOSED) {
506
+ node.f |= DISPOSED
507
+ return
455
508
  }
509
+ node.o = owner
510
+ const first = owner.fs
511
+ node.no = first
512
+ if (first !== undefined)
513
+ first.po = node
514
+ owner.fs = node
515
+ }
516
+
517
+ export function createOwner(parent: Owner | undefined = currentOwner): Owner {
518
+ const owner: Owner = { f: 0, o: undefined, po: undefined, no: undefined, k: undefined, fs: undefined, cl: undefined }
519
+ if (parent !== undefined)
520
+ adopt(owner, parent)
456
521
  return owner
457
522
  }
458
523
 
459
- export function disposeOwner(owner: Owner): void {
460
- if (owner.x)
524
+ function unadopt(node: Owned): void {
525
+ const owner = node.o
526
+ if (owner === undefined)
527
+ return
528
+ const { po, no } = node
529
+ if (po !== undefined)
530
+ po.no = no
531
+ else if (owner.fs === node)
532
+ owner.fs = no
533
+ if (no !== undefined)
534
+ no.po = po
535
+ node.o = node.po = node.no = undefined
536
+ }
537
+
538
+ // Disposes an owner, an effect or a computed: leaves its owner, drops its sources, releases what it owns, runs its cleanup.
539
+ export function disposeOwner(node: Owner): void {
540
+ const f = node.f
541
+ if (f & DISPOSED)
461
542
  return
462
- owner.x = true
463
- releaseOwned(owner)
464
- }
465
-
466
- function disposeSub(sub: SubNode): void {
467
- sub.f |= DISPOSED
468
- unlinkAll(sub)
469
- if (sub.f & EFFECT)
470
- disposeOwner(sub as EffectNode)
471
- if (sub.k !== undefined) {
472
- const fn = sub.k
473
- sub.k = undefined
543
+ node.f |= DISPOSED
544
+ unadopt(node)
545
+ if (f & (COMPUTED | EFFECT))
546
+ unlinkAll(node as unknown as SubNode)
547
+ if (!(f & COMPUTED))
548
+ releaseOwned(node)
549
+ if (node.k !== undefined) {
550
+ const fn = node.k
551
+ node.k = undefined
474
552
  fn()
475
553
  }
476
554
  }
477
555
 
556
+ // Pops the head each time, so a cleanup that disposes a sibling cannot cut the walk short.
478
557
  function releaseOwned(owner: Owner): void {
479
- let child = owner.fc
480
- owner.fc = undefined
481
- while (child !== undefined) {
482
- const next = child.ns
483
- child.ns = undefined
484
- disposeOwner(child)
485
- child = next
486
- }
487
-
488
- let sub = owner.fs
489
- owner.fs = undefined
490
- while (sub !== undefined) {
491
- const next = sub.no
492
- sub.no = undefined
493
- disposeSub(sub)
494
- sub = next
558
+ let node: Owned | undefined
559
+ while ((node = owner.fs) !== undefined) {
560
+ unadopt(node)
561
+ disposeOwner(node as Owner)
495
562
  }
496
563
 
497
564
  if (owner.cl !== undefined) {
@@ -520,6 +587,10 @@ export function getOwner(): Owner | undefined {
520
587
  return currentOwner
521
588
  }
522
589
 
590
+ export function isTracking(): boolean {
591
+ return currentSub !== undefined
592
+ }
593
+
523
594
  export function root<T>(fn: (dispose: () => void) => T): T {
524
595
  const owner = createOwner(undefined)
525
596
  return runWithOwner(owner, () => fn(() => disposeOwner(owner)))
@@ -530,27 +601,38 @@ export function signal<T>(initial: T): Signal<T> {
530
601
  }
531
602
 
532
603
  export function computed<T>(fn: () => T): ReadonlySignal<T> {
533
- return new ComputedNode(fn)
604
+ return new ComputedNode(fn, currentOwner)
534
605
  }
535
606
 
536
607
  export function effect(fn: () => void | (() => void)): () => void {
608
+ if (process.env.NODE_ENV !== 'production' && currentOwner === undefined && currentSub !== undefined && (currentSub.f & COMPUTED))
609
+ console.warn('kay: effect() created while a computed runs has no owner and is never disposed. Create it in a component or an effect.')
537
610
  const node = new EffectNode(fn, currentOwner)
538
611
  node.r()
539
- return () => {
540
- if (!(node.f & DISPOSED))
541
- disposeSub(node)
542
- }
612
+ return () => disposeOwner(node)
543
613
  }
544
614
 
615
+ // An error thrown by fn wins over one thrown by the effects it triggered, which is logged instead.
545
616
  export function batch<T>(fn: () => T): T {
546
617
  batchDepth++
618
+ let result: T
547
619
  try {
548
- return fn()
620
+ result = fn()
549
621
  }
550
- finally {
551
- if (--batchDepth === 0)
552
- flush()
622
+ catch (err) {
623
+ if (--batchDepth === 0) {
624
+ try {
625
+ flush()
626
+ }
627
+ catch (flushErr) {
628
+ console.error(flushErr)
629
+ }
630
+ }
631
+ throw err
553
632
  }
633
+ if (--batchDepth === 0)
634
+ flush()
635
+ return result
554
636
  }
555
637
 
556
638
  export function untrack<T>(fn: () => T): T {
@@ -565,19 +647,30 @@ export function untrack<T>(fn: () => T): T {
565
647
  }
566
648
 
567
649
  export function onCleanup(fn: () => void): void {
568
- if (currentOwner === undefined)
650
+ if (currentOwner === undefined) {
651
+ if (process.env.NODE_ENV !== 'production')
652
+ console.warn('kay: onCleanup() called outside a component or effect never runs.')
569
653
  return
570
- if (currentOwner.x) {
654
+ }
655
+ if (currentOwner.f & DISPOSED) {
571
656
  fn()
572
657
  return
573
658
  }
574
659
  ;(currentOwner.cl ??= []).push(fn)
575
660
  }
576
661
 
662
+ // A render creates its nodes and inserts them in the same task, so a microtask later they are in the document.
577
663
  export function onMount(fn: () => void | (() => void)): void {
578
664
  if (typeof document === 'undefined')
579
665
  return
580
- const cleanup = untrack(fn)
581
- if (typeof cleanup === 'function')
582
- onCleanup(cleanup)
666
+ const owner = currentOwner
667
+ queueMicrotask(() => {
668
+ if (owner !== undefined && owner.f & DISPOSED)
669
+ return
670
+ runWithOwner(owner, () => {
671
+ const cleanup = fn()
672
+ if (typeof cleanup === 'function')
673
+ onCleanup(cleanup)
674
+ })
675
+ })
583
676
  }