@kayjs/kay 2.0.0-beta.0 → 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 +10 -10
  5. package/src/build/files.ts +136 -0
  6. package/src/build/graph.ts +158 -0
  7. package/src/build/index.ts +204 -1460
  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 +297 -94
  18. package/src/cli/new.ts +53 -32
  19. package/src/compiler/analyze.ts +666 -91
  20. package/src/compiler/dom.ts +808 -743
  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 +329 -342
  26. package/src/context.ts +120 -0
  27. package/src/dev/index.ts +271 -75
  28. package/src/dom/action.ts +23 -13
  29. package/src/dom/extras.ts +121 -83
  30. package/src/dom/index.ts +114 -77
  31. package/src/dom/island.ts +71 -36
  32. package/src/dom/loop.ts +28 -20
  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 +114 -18
  37. package/src/reactivity/index.ts +237 -213
  38. package/src/reactivity/selector.ts +11 -9
  39. package/src/reactivity/store.ts +36 -11
  40. package/src/server/action.ts +147 -76
  41. package/src/server/compress.ts +116 -96
  42. package/src/server/cookies.ts +46 -16
  43. package/src/server/env.ts +18 -13
  44. package/src/server/http.ts +107 -54
  45. package/src/server/index.ts +887 -870
  46. package/src/server/loader.ts +120 -0
  47. package/src/server/router.ts +22 -8
  48. package/src/server/security.ts +48 -58
  49. package/src/server/standard-schema.ts +42 -11
  50. package/src/server/static-dir.ts +144 -0
  51. package/src/ssr/context.ts +95 -10
  52. package/src/ssr/index.ts +167 -66
  53. package/src/style.ts +33 -0
  54. package/src/test/index.ts +2 -1
  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,33 +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
- po: SubNode | undefined // prevOwned
40
- no: SubNode | undefined // nextOwned
41
- nq: SubNode | undefined // nextQueued
42
- k: (() => void) | undefined // cleanup returned by the effect
43
- o: Owner | undefined // owner
44
50
  r(): void // run
45
51
  }
46
52
 
47
- export interface Owner {
48
- p: Owner | undefined // parent
49
- fc: Owner | undefined // firstChild
50
- ps: Owner | undefined // prevSibling
51
- ns: Owner | undefined // nextSibling
52
- 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
53
60
  cl: (() => void)[] | undefined // cleanups
54
- x: boolean // disposed
61
+ cx?: Map<symbol, unknown> | undefined // context values provided at this owner
62
+ id?: IdScope | undefined // deterministic useId() scope
55
63
  }
56
64
 
57
65
  export interface Signal<T> {
@@ -68,15 +76,16 @@ let currentSub: SubNode | undefined
68
76
  let currentOwner: Owner | undefined
69
77
  let globalEpoch = 0
70
78
  let batchDepth = 0
71
- let qHead: SubNode | undefined
72
- let qTail: SubNode | undefined
79
+ let q: SubNode[] = []
80
+ let qNext: SubNode[] = []
73
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
74
84
 
75
85
  function linkSub(dep: DepNode, sub: SubNode): void {
76
- if (dep.ls === sub && dep.le === sub.e)
86
+ if (dep.le === sub.e)
77
87
  return
78
88
 
79
- dep.ls = sub
80
89
  dep.le = sub.e
81
90
 
82
91
  const cur = sub.c
@@ -94,15 +103,12 @@ function linkSub(dep: DepNode, sub: SubNode): void {
94
103
  e: sub.e,
95
104
  pd: undefined,
96
105
  nd: undefined,
97
- ps: dep.st,
106
+ ps: undefined,
98
107
  ns: undefined,
99
108
  }
100
109
 
101
- if (dep.st !== undefined)
102
- dep.st.ns = link
103
- else
104
- dep.s = link
105
- dep.st = link
110
+ if (!(sub.f & UNOBSERVED))
111
+ addSub(link)
106
112
 
107
113
  if (cur !== undefined) {
108
114
  const prev = cur.pd
@@ -125,10 +131,50 @@ function linkSub(dep: DepNode, sub: SubNode): void {
125
131
  }
126
132
  }
127
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
+
128
176
  function unlink(l: Link): void {
129
- const { d, s, pd, nd, ps, ns } = l
130
- if (d.ls === s)
131
- d.ls = undefined
177
+ const { s, pd, nd } = l
132
178
  if (pd !== undefined)
133
179
  pd.nd = nd
134
180
  else
@@ -137,14 +183,8 @@ function unlink(l: Link): void {
137
183
  nd.pd = pd
138
184
  else
139
185
  s.dt = pd
140
- if (ps !== undefined)
141
- ps.ns = ns
142
- else
143
- d.s = ns
144
- if (ns !== undefined)
145
- ns.ps = ps
146
- else
147
- d.st = ps
186
+ if (!(s.f & UNOBSERVED))
187
+ removeSub(l)
148
188
  }
149
189
 
150
190
  function unlinkRemaining(sub: SubNode): void {
@@ -162,35 +202,33 @@ function unlinkRemaining(sub: SubNode): void {
162
202
  function unlinkAll(sub: SubNode): void {
163
203
  let l = sub.d
164
204
  sub.d = sub.dt = sub.c = undefined
205
+ if (sub.f & UNOBSERVED)
206
+ return
165
207
  while (l !== undefined) {
166
208
  const next = l.nd
167
- const { d, ps, ns } = l
168
- if (d.ls === sub)
169
- d.ls = undefined
170
- if (ps !== undefined)
171
- ps.ns = ns
172
- else
173
- d.s = ns
174
- if (ns !== undefined)
175
- ns.ps = ps
176
- else
177
- d.st = ps
209
+ removeSub(l)
178
210
  l = next
179
211
  }
180
212
  }
181
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
+
182
227
  function enqueue(sub: SubNode): void {
183
228
  if (sub.f & (QUEUED | DISPOSED))
184
229
  return
185
230
  sub.f |= QUEUED
186
- sub.nq = undefined
187
- if (qTail !== undefined) {
188
- qTail.nq = sub
189
- qTail = sub
190
- }
191
- else {
192
- qHead = qTail = sub
193
- }
231
+ q.push(sub)
194
232
  }
195
233
 
196
234
  function propagate(dep: DepNode, flag: number): void {
@@ -225,36 +263,34 @@ function flush(): void {
225
263
  const errors: unknown[] = []
226
264
  try {
227
265
  let rounds = 0
228
- while (qHead !== undefined) {
229
- if (++rounds > 1000)
230
- throw new Error('kay: reactive cycle')
231
- let node: SubNode | undefined = qHead
232
- qHead = qTail = undefined
233
- while (node !== undefined) {
234
- const next: SubNode | undefined = node.nq
235
- node.nq = undefined
236
- node.f &= ~QUEUED
237
- try {
238
- if (!(node.f & DISPOSED) && shouldRun(node))
239
- node.r()
240
- }
241
- catch (err) {
242
- 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
+ }
243
284
  }
244
- node = next
245
285
  }
286
+ round.length = 0
246
287
  }
247
288
  }
248
289
  finally {
249
290
  flushing = false
250
- let left = qHead
251
- qHead = qTail = undefined
252
- while (left !== undefined) {
253
- const next: SubNode | undefined = left.nq
254
- left.nq = undefined
255
- left.f &= ~QUEUED
256
- left = next
257
- }
291
+ for (const node of q)
292
+ node.f &= ~QUEUED
293
+ q.length = 0
258
294
  }
259
295
  if (errors.length === 1)
260
296
  throw errors[0]
@@ -268,12 +304,13 @@ function shouldRun(sub: SubNode): boolean {
268
304
  return false
269
305
  if (f & DIRTY)
270
306
  return true
271
- if (!(f & CHECK))
307
+ if (!(f & CHECK) && (!(f & UNOBSERVED) || (sub as ComputedNode<unknown>).w === writes))
272
308
  return false
309
+ const w = writes
273
310
  let l = sub.d
274
311
  while (l !== undefined) {
275
312
  const dep = l.d
276
- if (dep.f !== undefined && (dep.f & COMPUTED)) {
313
+ if (dep.f & COMPUTED) {
277
314
  const comp = dep as unknown as ComputedNode<unknown>
278
315
  if (shouldRun(comp))
279
316
  comp.r()
@@ -285,6 +322,8 @@ function shouldRun(sub: SubNode): boolean {
285
322
  l = l.nd
286
323
  }
287
324
  sub.f &= ~CHECK
325
+ if (f & UNOBSERVED)
326
+ (sub as ComputedNode<unknown>).w = w
288
327
  return false
289
328
  }
290
329
 
@@ -292,8 +331,8 @@ class SignalNode<T> implements DepNode, Signal<T> {
292
331
  v = 0
293
332
  s: Link | undefined = undefined
294
333
  st: Link | undefined = undefined
295
- ls: SubNode | undefined = undefined
296
334
  le = -1
335
+ f = 0
297
336
  _v: T
298
337
 
299
338
  constructor(initial: T) {
@@ -311,6 +350,7 @@ class SignalNode<T> implements DepNode, Signal<T> {
311
350
  return
312
351
  this._v = next
313
352
  this.v++
353
+ writes++
314
354
  if (this.s !== undefined) {
315
355
  propagate(this, DIRTY)
316
356
  flush()
@@ -326,51 +366,45 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
326
366
  v = 0
327
367
  s: Link | undefined = undefined
328
368
  st: Link | undefined = undefined
329
- ls: SubNode | undefined = undefined
330
369
  le = -1
331
- f = COMPUTED | DIRTY
370
+ f = COMPUTED | DIRTY | UNOBSERVED
332
371
  e = -1
333
372
  d: Link | undefined = undefined
334
373
  dt: Link | undefined = undefined
335
374
  c: Link | undefined = undefined
336
- po: SubNode | undefined = undefined
337
- no: SubNode | undefined = undefined
338
- nq: SubNode | undefined = undefined
375
+ po: Owned | undefined = undefined
376
+ no: Owned | undefined = undefined
339
377
  k: (() => void) | undefined = undefined
340
378
  o: Owner | undefined = undefined
341
379
  _f: () => T
342
380
  _v!: T
343
381
  _i = false
382
+ w = -1
344
383
 
345
384
  constructor(fn: () => T, owner: Owner | undefined) {
346
385
  this._f = fn
347
- if (owner !== undefined) {
348
- if (owner.x) {
349
- this.f |= DISPOSED
350
- }
351
- else {
352
- this.o = owner
353
- const first = owner.fs
354
- this.no = first
355
- if (first !== undefined)
356
- first.po = this
357
- owner.fs = this
358
- }
359
- }
386
+ if (owner !== undefined)
387
+ adopt(this, owner)
360
388
  }
361
389
 
362
- r(): void {
390
+ r(observe?: boolean): void {
363
391
  if (this.f & DISPOSED)
364
392
  return
365
393
  if (this.f & RUNNING)
366
394
  throw new Error('kay: cyclic computed')
367
395
  const prevSub = currentSub
368
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
369
402
  this.f = (this.f | RUNNING) & ~(DIRTY | CHECK)
370
403
  this.e = ++globalEpoch
371
404
  this.c = this.d
372
405
  currentSub = this
373
406
  currentOwner = undefined
407
+ let ok = false
374
408
  try {
375
409
  const next = (this._f)()
376
410
  if (!this._i || !Object.is(this._v, next)) {
@@ -378,10 +412,14 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
378
412
  this._i = true
379
413
  this.v++
380
414
  }
415
+ this.w = w
416
+ ok = true
381
417
  }
382
418
  finally {
383
419
  unlinkRemaining(this)
384
420
  this.f &= ~RUNNING
421
+ if (direct && !ok && this.s === undefined)
422
+ unobserve(this)
385
423
  currentSub = prevSub
386
424
  currentOwner = prevOwner
387
425
  }
@@ -390,8 +428,8 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
390
428
  get value(): T {
391
429
  if (!(this.f & DISPOSED)) {
392
430
  if (shouldRun(this))
393
- this.r()
394
- if (currentSub !== undefined)
431
+ this.r(currentSub !== undefined && !(currentSub.f & UNOBSERVED))
432
+ if (currentSub !== undefined && !(this.f & DISPOSED))
395
433
  linkSub(this, currentSub)
396
434
  }
397
435
  return this._v
@@ -411,44 +449,35 @@ class EffectNode implements SubNode, Owner {
411
449
  d: Link | undefined = undefined
412
450
  dt: Link | undefined = undefined
413
451
  c: Link | undefined = undefined
414
- po: SubNode | undefined = undefined
415
- no: SubNode | undefined = undefined
416
- nq: SubNode | undefined = undefined
452
+ po: Owned | undefined = undefined
453
+ no: Owned | undefined = undefined
417
454
  k: (() => void) | undefined = undefined
418
- o: Owner | undefined
419
- p: Owner | undefined
420
- fc: Owner | undefined = undefined
421
- ps: Owner | undefined = undefined
422
- ns: Owner | undefined = undefined
423
- fs: SubNode | undefined = undefined
455
+ o: Owner | undefined = undefined
456
+ fs: Owned | undefined = undefined
424
457
  cl: (() => void)[] | undefined = undefined
425
- x = false
426
458
  _f: () => void | (() => void)
427
459
 
428
460
  constructor(fn: () => void | (() => void), owner: Owner | undefined) {
429
461
  this._f = fn
430
- this.o = this.p = owner
431
- if (owner !== undefined) {
432
- const first = owner.fs
433
- this.no = first
434
- if (first !== undefined)
435
- first.po = this
436
- owner.fs = this
437
- }
462
+ if (owner !== undefined)
463
+ adopt(this, owner)
438
464
  }
439
465
 
440
466
  r(): void {
441
467
  if (this.f & DISPOSED)
442
468
  return
469
+ this.f &= ~(DIRTY | CHECK)
443
470
  releaseOwned(this)
444
471
  if (this.k !== undefined) {
445
472
  const fn = this.k
446
473
  this.k = undefined
447
474
  fn()
448
475
  }
476
+ if (this.f & DISPOSED)
477
+ return
449
478
  const prevSub = currentSub
450
479
  const prevOwner = currentOwner
451
- this.f = (this.f | RUNNING) & ~(DIRTY | CHECK)
480
+ this.f |= RUNNING
452
481
  this.e = ++globalEpoch
453
482
  this.c = this.d
454
483
  currentSub = this
@@ -471,96 +500,65 @@ class EffectNode implements SubNode, Owner {
471
500
  }
472
501
  }
473
502
 
474
- export function createOwner(parent: Owner | undefined = currentOwner): Owner {
475
- const owner: Owner = {
476
- p: parent,
477
- fc: undefined,
478
- ps: undefined,
479
- ns: undefined,
480
- fs: undefined,
481
- cl: undefined,
482
- x: false,
483
- }
484
- if (parent !== undefined) {
485
- const first = parent.fc
486
- owner.ns = first
487
- if (first !== undefined)
488
- first.ps = owner
489
- 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
490
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)
491
521
  return owner
492
522
  }
493
523
 
494
- function unlinkOwner(owner: Owner): void {
495
- const parent = owner.p
496
- if (parent === undefined)
497
- return
498
- const prev = owner.ps
499
- const next = owner.ns
500
- if (prev !== undefined)
501
- prev.ns = next
502
- else if (parent.fc === owner)
503
- parent.fc = next
504
- if (next !== undefined)
505
- next.ps = prev
506
- owner.p = owner.ps = owner.ns = undefined
507
- }
508
-
509
- function unlinkOwnedSub(sub: SubNode): void {
510
- const owner = sub.o
524
+ function unadopt(node: Owned): void {
525
+ const owner = node.o
511
526
  if (owner === undefined)
512
527
  return
513
- const prev = sub.po
514
- const next = sub.no
515
- if (prev !== undefined)
516
- prev.no = next
517
- else if (owner.fs === sub)
518
- owner.fs = next
519
- if (next !== undefined)
520
- next.po = prev
521
- sub.o = sub.po = sub.no = undefined
522
- }
523
-
524
- export function disposeOwner(owner: Owner): void {
525
- if (owner.x)
526
- return
527
- owner.x = true
528
- unlinkOwner(owner)
529
- releaseOwned(owner)
530
- }
531
-
532
- function disposeSub(sub: SubNode): void {
533
- if (sub.f & DISPOSED)
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)
534
542
  return
535
- sub.f |= DISPOSED
536
- unlinkOwnedSub(sub)
537
- unlinkAll(sub)
538
- if (sub.f & EFFECT)
539
- disposeOwner(sub as EffectNode)
540
- if (sub.k !== undefined) {
541
- const fn = sub.k
542
- 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
543
552
  fn()
544
553
  }
545
554
  }
546
555
 
556
+ // Pops the head each time, so a cleanup that disposes a sibling cannot cut the walk short.
547
557
  function releaseOwned(owner: Owner): void {
548
- let child = owner.fc
549
- owner.fc = undefined
550
- while (child !== undefined) {
551
- const next = child.ns
552
- child.p = child.ps = child.ns = undefined
553
- disposeOwner(child)
554
- child = next
555
- }
556
-
557
- let sub = owner.fs
558
- owner.fs = undefined
559
- while (sub !== undefined) {
560
- const next = sub.no
561
- sub.o = sub.po = sub.no = undefined
562
- disposeSub(sub)
563
- sub = next
558
+ let node: Owned | undefined
559
+ while ((node = owner.fs) !== undefined) {
560
+ unadopt(node)
561
+ disposeOwner(node as Owner)
564
562
  }
565
563
 
566
564
  if (owner.cl !== undefined) {
@@ -589,6 +587,10 @@ export function getOwner(): Owner | undefined {
589
587
  return currentOwner
590
588
  }
591
589
 
590
+ export function isTracking(): boolean {
591
+ return currentSub !== undefined
592
+ }
593
+
592
594
  export function root<T>(fn: (dispose: () => void) => T): T {
593
595
  const owner = createOwner(undefined)
594
596
  return runWithOwner(owner, () => fn(() => disposeOwner(owner)))
@@ -603,23 +605,34 @@ export function computed<T>(fn: () => T): ReadonlySignal<T> {
603
605
  }
604
606
 
605
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.')
606
610
  const node = new EffectNode(fn, currentOwner)
607
611
  node.r()
608
- return () => {
609
- if (!(node.f & DISPOSED))
610
- disposeSub(node)
611
- }
612
+ return () => disposeOwner(node)
612
613
  }
613
614
 
615
+ // An error thrown by fn wins over one thrown by the effects it triggered, which is logged instead.
614
616
  export function batch<T>(fn: () => T): T {
615
617
  batchDepth++
618
+ let result: T
616
619
  try {
617
- return fn()
620
+ result = fn()
618
621
  }
619
- finally {
620
- if (--batchDepth === 0)
621
- 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
622
632
  }
633
+ if (--batchDepth === 0)
634
+ flush()
635
+ return result
623
636
  }
624
637
 
625
638
  export function untrack<T>(fn: () => T): T {
@@ -634,19 +647,30 @@ export function untrack<T>(fn: () => T): T {
634
647
  }
635
648
 
636
649
  export function onCleanup(fn: () => void): void {
637
- 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.')
638
653
  return
639
- if (currentOwner.x) {
654
+ }
655
+ if (currentOwner.f & DISPOSED) {
640
656
  fn()
641
657
  return
642
658
  }
643
659
  ;(currentOwner.cl ??= []).push(fn)
644
660
  }
645
661
 
662
+ // A render creates its nodes and inserts them in the same task, so a microtask later they are in the document.
646
663
  export function onMount(fn: () => void | (() => void)): void {
647
664
  if (typeof document === 'undefined')
648
665
  return
649
- const cleanup = untrack(fn)
650
- if (typeof cleanup === 'function')
651
- 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
+ })
652
676
  }