prunelle 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/llms.txt +18 -18
  2. package/package.json +2 -2
  3. package/src/a11y/declaration.ts +4 -7
  4. package/src/build/bundle.ts +6 -2
  5. package/src/build/index.ts +3 -1
  6. package/src/build/island-promote.ts +42 -2
  7. package/src/build/islands.ts +59 -8
  8. package/src/build/manifest.ts +98 -0
  9. package/src/build/plugin-host.ts +11 -1
  10. package/src/build/prerender.ts +6 -1
  11. package/src/build/reach.ts +4 -3
  12. package/src/build/resolve.ts +5 -1
  13. package/src/build/routes.ts +18 -0
  14. package/src/cli/check-forms.ts +8 -0
  15. package/src/cli/check-links.ts +14 -5
  16. package/src/cli/check.ts +12 -6
  17. package/src/cli/commands.ts +5 -3
  18. package/src/cli/form-opaque-children.ts +40 -0
  19. package/src/compiler/analyze.ts +20 -14
  20. package/src/compiler/cacheability-private.ts +13 -3
  21. package/src/compiler/compliance-contrast.ts +48 -10
  22. package/src/compiler/compliance-rules.ts +72 -0
  23. package/src/compiler/compliance.ts +6 -66
  24. package/src/compiler/dom-attrs.ts +178 -22
  25. package/src/compiler/dom.ts +31 -44
  26. package/src/compiler/emit.ts +17 -6
  27. package/src/compiler/expr-context.ts +260 -207
  28. package/src/compiler/props-body.ts +263 -21
  29. package/src/compiler/props.ts +155 -5
  30. package/src/compiler/signal-reads.ts +142 -29
  31. package/src/compiler/spread-handlers.ts +25 -13
  32. package/src/compiler/ssr-emit.ts +3 -3
  33. package/src/compiler/ssr-helpers.ts +0 -5
  34. package/src/compiler/ssr.ts +4 -2
  35. package/src/dev/host-check.ts +2 -1
  36. package/src/dev/reload.ts +4 -1
  37. package/src/dom/attrs.ts +93 -25
  38. package/src/dom/extras.ts +89 -49
  39. package/src/dom/field-marks.ts +9 -1
  40. package/src/dom/index.ts +19 -13
  41. package/src/dom/loop.ts +259 -188
  42. package/src/dom/prefetch.ts +4 -1
  43. package/src/dom/regions.ts +25 -8
  44. package/src/dom/swap.ts +11 -10
  45. package/src/dom/trusted.ts +4 -4
  46. package/src/eco/declaration.ts +5 -1
  47. package/src/eco/index.ts +3 -0
  48. package/src/index.ts +3 -2
  49. package/src/plugins/config.ts +1 -1
  50. package/src/plugins/public.ts +4 -0
  51. package/src/plugins/types.ts +8 -1
  52. package/src/reactivity/graph.ts +244 -41
  53. package/src/reactivity/hmr.ts +14 -3
  54. package/src/reactivity/index.ts +219 -235
  55. package/src/reactivity/resource.ts +17 -3
  56. package/src/reactivity/seed.ts +118 -16
  57. package/src/reactivity/selector.ts +85 -47
  58. package/src/reactivity/server-seed.ts +31 -23
  59. package/src/reactivity/store.ts +316 -136
  60. package/src/server/action-protect.ts +3 -0
  61. package/src/server/action-registry.ts +5 -1
  62. package/src/server/cache-invalidation.ts +4 -3
  63. package/src/server/compress.ts +52 -17
  64. package/src/server/consent.ts +5 -3
  65. package/src/server/cookies.ts +27 -8
  66. package/src/server/cors.ts +98 -0
  67. package/src/server/csp-report.ts +8 -4
  68. package/src/server/health.ts +9 -0
  69. package/src/server/http.ts +3 -1
  70. package/src/server/index.ts +25 -13
  71. package/src/server/loader.ts +1 -0
  72. package/src/server/plugin-host.ts +41 -9
  73. package/src/server/plugin-route-shadow.ts +24 -0
  74. package/src/server/rate-limit.ts +13 -3
  75. package/src/server/region.ts +19 -2
  76. package/src/server/request-handler.ts +58 -22
  77. package/src/server/request-log.ts +1 -1
  78. package/src/server/route-cache.ts +14 -2
  79. package/src/server/route-table.ts +21 -11
  80. package/src/server/router.ts +2 -1
  81. package/src/server/routing.ts +100 -42
  82. package/src/server/sanitize-html.ts +3 -1
  83. package/src/server/serve-handlers.ts +10 -4
  84. package/src/server/served-file.ts +19 -14
  85. package/src/server/shared-store.ts +10 -1
  86. package/src/server/static-dir.ts +21 -3
  87. package/src/server/webhook.ts +6 -4
  88. package/src/ssr/context.ts +149 -51
  89. package/src/ssr/index.ts +41 -46
  90. package/src/ssr/region.ts +9 -3
  91. package/src/style.ts +12 -5
  92. package/src/test/browser.ts +3 -1
  93. package/src/test/build-reuse.ts +22 -7
  94. package/src/test/index.ts +7 -3
  95. package/src/ts-plugin/diagnostics.ts +56 -18
  96. package/ts-plugin.cjs +465 -131
@@ -2,11 +2,12 @@
2
2
  // Glitch-free lazy computed values, O(1) link/unlink, compact internal fields for tiny bundle size.
3
3
 
4
4
  import type { DepNode, Link, Owned, Owner, ReadonlySignal, Signal, SubNode } from './graph'
5
- import { adopt, linkSub, throwErrors, unadopt, unlinkAll, unlinkRemaining, unlinkSubs, unobserve } from './graph'
5
+ import { adopt, bumpWrites, disposeOwner, linkSub, propagate, releaseOwned, shouldRun, throwErrors, unadopt, unlinkRemaining, unobserve, writeCount } from './graph'
6
6
  import type { HmrHost } from './hmr'
7
7
  import { inServerRender, isSharedWrite, recordSharedWrite, shareOutsideRender } from './server-seed'
8
8
 
9
9
  export type { IdScope, Owner, ReadonlySignal, Signal } from './graph'
10
+ export { bumpWrites, disposeOwner, writeCount } from './graph'
10
11
  export type { RequestSeeds } from './server-seed'
11
12
  export { isSharedWrite, nameSeed, recordSharedWrite, renderOnServer, shareOutsideRender, sharedPath } from './server-seed'
12
13
 
@@ -25,6 +26,10 @@ const enum F {
25
26
  UNOBSERVED = 128,
26
27
  // Queued for the current flush round (distinct from QUEUED, which marks the next round).
27
28
  PENDING = 256,
29
+ // Computed has produced at least one value.
30
+ INIT = 512,
31
+ // Handed out by getOwner(): code may attach cleanups or children to it later.
32
+ KEPT = 1024,
28
33
  }
29
34
 
30
35
  let currentSub: SubNode | undefined
@@ -34,144 +39,120 @@ let batchDepth = 0
34
39
  let q: SubNode[] = []
35
40
  let qNext: SubNode[] = []
36
41
  let flushing = false
37
- // Signal writes so far: an unobserved computed that checked at the same count can skip its version walk.
38
- let writes = 0
39
42
  // Set while recordWrites() runs: each signal write hands it the signal, the value it replaced, and the new value; a store write hands the undo itself.
40
43
  let rec: ((target: Signal<unknown> | (() => void), prev?: unknown, next?: unknown) => void) | undefined
41
44
  // Bumped after a flush that threw so observed computeds still left DIRTY/CHECK re-propagate downstream on recovery.
42
45
  let propWave = 0
43
46
 
44
- // A child effect can sit ahead of its parent in the queue; the parent runs first, since its run may dispose the child.
45
- function runParentFirst(sub: SubNode, errors: unknown[]): void {
46
- sub.f &= ~(F.PENDING | F.QUEUED)
47
- for (let o = sub.o; o; o = o.o) {
48
- if (o.f & (F.PENDING | F.QUEUED)) {
49
- runParentFirst(o as unknown as SubNode, errors)
50
- break
51
- }
52
- }
53
- if (!(sub.f & F.DISPOSED)) {
47
+ // Installed by the first computed: without one no node is ever CHECK, so a page that creates none ships no dependency walk.
48
+ let checkDeps: ((sub: SubNode) => boolean) | undefined
49
+
50
+ function runEffect(node: SubNode, errors: unknown[] | undefined): unknown[] | undefined {
51
+ const f = node.f
52
+ if (!(f & F.DISPOSED)) {
54
53
  try {
55
- if (shouldRun(sub))
56
- sub.r()
54
+ if ((f & F.DIRTY) || checkDeps?.(node))
55
+ node.r()
57
56
  }
58
57
  catch (err) {
59
- if (!catchEffectError?.(sub, err))
60
- errors.push(err)
58
+ if (!catchEffectError?.(node, err)) {
59
+ (errors ??= []).push(err)
60
+ }
61
61
  }
62
62
  }
63
+ return errors
63
64
  }
64
65
 
65
- function enqueue(sub: SubNode): void {
66
- if (sub.f & (F.QUEUED | F.DISPOSED))
67
- return
68
- sub.f |= F.QUEUED
69
- q.push(sub)
70
- }
71
-
72
- function propagate(dep: DepNode, flag: number): void {
73
- for (let l = dep.s; l; l = l.ns) {
74
- const sub = l.s
75
- const f = sub.f
76
- if (f & F.DISPOSED)
77
- continue
78
- const prev = f & (F.DIRTY | F.CHECK)
79
- if (prev < flag)
80
- sub.f = (f & ~(F.DIRTY | F.CHECK)) | flag
81
- if (f & F.EFFECT) {
82
- enqueue(sub)
83
- }
84
- else if (f & F.COMPUTED) {
85
- const comp = sub as ComputedNode<unknown>
86
- if (prev === F.CLEAN || comp.pw !== propWave) {
87
- comp.pw = propWave
88
- propagate(comp, F.CHECK)
89
- }
66
+ // A child effect can sit ahead of its parent in the queue; the parent runs first, since its run may dispose the child.
67
+ function runParentFirst(sub: SubNode, errors: unknown[] | undefined): unknown[] | undefined {
68
+ sub.f &= ~(F.PENDING | F.QUEUED)
69
+ for (let o = sub.o; o; o = o.o) {
70
+ if (o.f & (F.PENDING | F.QUEUED)) {
71
+ errors = runParentFirst(o as unknown as SubNode, errors)
72
+ break
90
73
  }
91
74
  }
75
+ return runEffect(sub, errors)
92
76
  }
93
77
 
94
78
  // One effect throwing must not strand the rest of the queue: every node still runs, then a lone error is rethrown as is and several as one AggregateError.
95
79
  function flush(): void {
96
- if (flushing || batchDepth > 0)
80
+ if (flushing || batchDepth > 0 || q.length === 0)
97
81
  return
98
82
  flushing = true
99
- const errors: unknown[] = []
83
+ let errors: unknown[] | undefined
100
84
  try {
101
85
  let rounds = 0
102
86
  while (q.length > 0) {
103
87
  if (++rounds > 1000) {
104
- errors.push(new Error('prunelle: reactive cycle'))
88
+ (errors ??= []).push(new Error('prunelle: reactive cycle'))
105
89
  break
106
90
  }
107
91
  const round = q
108
92
  q = qNext
109
93
  qNext = round
110
- for (const node of round)
94
+ const len = round.length
95
+ let hasParent = false
96
+ for (let i = 0; i < len; i++) {
97
+ const node = round[i]!
111
98
  node.f = (node.f & ~F.QUEUED) | F.PENDING
112
- for (const node of round) {
113
- if (node.f & F.PENDING)
114
- runParentFirst(node, errors)
99
+ if ((node as Owner).fs !== undefined)
100
+ hasParent = true
101
+ }
102
+ for (let i = 0; i < len; i++) {
103
+ const node = round[i]!
104
+ const f = node.f
105
+ if (!(f & F.PENDING))
106
+ continue
107
+ if (hasParent || q.length > 0) {
108
+ errors = runParentFirst(node, errors)
109
+ }
110
+ else {
111
+ node.f = f & ~F.PENDING
112
+ errors = runEffect(node, errors)
113
+ }
115
114
  }
116
115
  round.length = 0
117
116
  }
118
117
  }
119
118
  finally {
120
119
  flushing = false
121
- for (const node of q)
122
- node.f &= ~F.QUEUED
120
+ for (let i = 0; i < q.length; i++)
121
+ q[i]!.f &= ~F.QUEUED
123
122
  q.length = 0
124
- if (errors.length > 0)
123
+ for (let i = 0; i < qNext.length; i++)
124
+ qNext[i]!.f &= ~F.PENDING
125
+ qNext.length = 0
126
+ if (errors !== undefined)
125
127
  propWave++
126
128
  }
127
- throwErrors(errors)
129
+ if (errors !== undefined)
130
+ throwErrors(errors)
128
131
  }
129
132
 
130
133
  // Installed by the first <Catch>, so an island without one carries no owner walk.
131
134
  let catchEffectError: ((node: Owned, err: unknown) => boolean) | undefined
132
135
 
133
- export function onEffectError(fn: (node: Owned, err: unknown) => boolean): void {
136
+ export const onEffectError = (fn: (node: Owned, err: unknown) => boolean): void => {
134
137
  catchEffectError = fn
135
138
  }
136
139
 
137
- function shouldRun(sub: SubNode): boolean {
138
- const f = sub.f
139
- if (f & F.DISPOSED)
140
- return false
141
- if (f & F.DIRTY)
142
- return true
143
- if (!(f & F.CHECK) && (!(f & F.UNOBSERVED) || (sub as ComputedNode<unknown>).w === writes))
144
- return false
145
- const w = writes
146
- for (let l = sub.d; l; l = l.nd) {
147
- const dep = l.d
148
- if (dep.f & F.COMPUTED) {
149
- const comp = dep as unknown as ComputedNode<unknown>
150
- if (shouldRun(comp))
151
- comp.r()
152
- }
153
- if (dep.v !== l.v) {
154
- sub.f |= F.DIRTY
155
- return true
156
- }
157
- }
158
- sub.f &= ~F.CHECK
159
- if (f & F.UNOBSERVED)
160
- (sub as ComputedNode<unknown>).w = w
161
- return false
140
+ export interface SignalOptions<T> {
141
+ equals?: ((prev: T, next: T) => boolean) | undefined
162
142
  }
163
143
 
164
- class SignalNode<T> implements DepNode, Signal<T> {
144
+ export class SignalNode<T> implements DepNode, Signal<T> {
165
145
  v = 0
166
- s: Link | undefined
167
- st: Link | undefined
146
+ declare s: Link | undefined
147
+ declare st: Link | undefined
168
148
  le = -1
169
149
  f = 0
170
- declare u?: (() => void) | undefined
171
- _v: T
150
+ declare _v: T
151
+ declare _eq: ((prev: T, next: T) => boolean) | undefined
172
152
 
173
- constructor(initial: T) {
153
+ constructor(initial: T, equals?: (prev: T, next: T) => boolean) {
174
154
  this._v = initial
155
+ this._eq = equals
175
156
  }
176
157
 
177
158
  get value(): T {
@@ -181,7 +162,7 @@ class SignalNode<T> implements DepNode, Signal<T> {
181
162
  }
182
163
 
183
164
  set value(next: T) {
184
- if (Object.is(this._v, next))
165
+ if (this._eq ? this._eq(this._v, next) : Object.is(this._v, next))
185
166
  return
186
167
  if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && isSharedWrite(this)) {
187
168
  const prev = this._v
@@ -190,9 +171,9 @@ class SignalNode<T> implements DepNode, Signal<T> {
190
171
  rec?.(this, this._v, next)
191
172
  this._v = next
192
173
  this.v++
193
- writes++
174
+ bumpWrites()
194
175
  if (this.s) {
195
- propagate(this, F.DIRTY)
176
+ propagate(this, F.DIRTY, q, propWave)
196
177
  flush()
197
178
  }
198
179
  }
@@ -204,25 +185,27 @@ class SignalNode<T> implements DepNode, Signal<T> {
204
185
 
205
186
  class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
206
187
  v = 0
207
- s: Link | undefined
208
- st: Link | undefined
188
+ declare s: Link | undefined
189
+ declare st: Link | undefined
209
190
  le = -1
210
191
  f = F.COMPUTED | F.DIRTY | F.UNOBSERVED
211
192
  e = -1
212
- d: Link | undefined
213
- dt: Link | undefined
214
- c: Link | undefined
215
- po: Owned | undefined
216
- no: Owned | undefined
217
- o: Owner | undefined
218
- _f: () => T
219
- _v!: T
220
- _i = false
193
+ declare d: Link | undefined
194
+ declare dt: Link | undefined
195
+ declare c: Link | undefined
196
+ declare po: Owned | undefined
197
+ declare no: Owned | undefined
198
+ declare o: Owner | undefined
199
+ declare _f: () => T
200
+ declare _eq: ((prev: T, next: T) => boolean) | undefined
201
+ declare _v: T
221
202
  w = -1
222
203
  pw = 0
223
204
 
224
- constructor(fn: () => T, owner: Owner | undefined) {
205
+ constructor(fn: () => T, owner: Owner | undefined, equals?: (prev: T, next: T) => boolean) {
225
206
  this._f = fn
207
+ this._eq = equals
208
+ checkDeps = shouldRun
226
209
  if (owner)
227
210
  adopt(this, owner)
228
211
  }
@@ -234,7 +217,7 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
234
217
  throw new Error('prunelle: cyclic computed')
235
218
  const prevSub = currentSub
236
219
  const prevOwner = currentOwner
237
- const w = writes
220
+ const w = writeCount()
238
221
  // A first run for an observed reader links straight into its sources; roll back if the compute throws.
239
222
  const direct = observe && !this.d
240
223
  if (direct)
@@ -247,16 +230,17 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
247
230
  let ok = false
248
231
  try {
249
232
  const next = (this._f)()
250
- if (!this._i || !Object.is(this._v, next)) {
233
+ if (!(this.f & F.INIT) || !(this._eq ? this._eq(this._v, next) : Object.is(this._v, next))) {
251
234
  this._v = next
252
- this._i = true
235
+ this.f |= F.INIT
253
236
  this.v++
254
237
  }
255
238
  this.w = w
256
239
  ok = true
257
240
  }
258
241
  finally {
259
- unlinkRemaining(this)
242
+ if (this.c !== undefined)
243
+ unlinkRemaining(this)
260
244
  this.f &= ~F.RUNNING
261
245
  if (!ok) {
262
246
  this.f |= F.DIRTY
@@ -269,22 +253,26 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
269
253
  }
270
254
 
271
255
  get value(): T {
272
- if (!(this.f & F.DISPOSED)) {
256
+ const f = this.f
257
+ if (!(f & F.DISPOSED)) {
273
258
  const sub = currentSub
274
- if (shouldRun(this))
275
- this.r(!!sub && !(sub.f & (F.UNOBSERVED | F.DISPOSED)))
276
- if (!(this.f & F.DISPOSED)) {
277
- if (sub)
278
- linkSub(this, sub)
279
- if (!this.s && !(this.f & F.UNOBSERVED))
280
- unobserve(this)
259
+ if ((f & (F.DIRTY | F.CHECK)) || ((f & F.UNOBSERVED) && this.w !== writeCount())) {
260
+ if (shouldRun(this))
261
+ this.r(!!sub && !(sub.f & (F.UNOBSERVED | F.DISPOSED)))
262
+ if (this.f & F.DISPOSED)
263
+ return this._v
281
264
  }
265
+ if (sub)
266
+ linkSub(this, sub)
267
+ if (!this.s && !(this.f & F.UNOBSERVED))
268
+ unobserve(this)
282
269
  }
283
270
  return this._v
284
271
  }
285
272
 
286
273
  peek(): T {
287
- if (!(this.f & F.DISPOSED) && shouldRun(this))
274
+ const f = this.f
275
+ if (!(f & F.DISPOSED) && ((f & (F.DIRTY | F.CHECK)) || ((f & F.UNOBSERVED) && this.w !== writeCount())) && shouldRun(this))
288
276
  this.r()
289
277
  return this._v
290
278
  }
@@ -292,17 +280,17 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
292
280
 
293
281
  // An effect is also the owner of what its run creates (effects, onCleanup), released before the next run.
294
282
  class EffectNode implements SubNode, Owner {
295
- f = F.EFFECT | F.DIRTY
283
+ f = F.EFFECT
296
284
  e = -1
297
- d: Link | undefined
298
- dt: Link | undefined
299
- c: Link | undefined
300
- po: Owned | undefined
301
- no: Owned | undefined
302
- o: Owner | undefined
303
- fs: Owned | undefined
304
- cl: (() => void)[] | undefined
305
- _f: () => void | (() => void)
285
+ declare d: Link | undefined
286
+ declare dt: Link | undefined
287
+ declare c: Link | undefined
288
+ declare po: Owned | undefined
289
+ declare no: Owned | undefined
290
+ declare o: Owner | undefined
291
+ declare fs: Owned | undefined
292
+ declare cl: (() => void)[] | undefined
293
+ declare _f: () => void | (() => void)
306
294
 
307
295
  constructor(fn: () => void | (() => void), owner: Owner | undefined) {
308
296
  this._f = fn
@@ -311,23 +299,26 @@ class EffectNode implements SubNode, Owner {
311
299
  }
312
300
 
313
301
  r(): void {
314
- if (this.f & F.DISPOSED)
302
+ const f = this.f
303
+ if (f & F.DISPOSED)
315
304
  return
316
- this.f &= ~(F.DIRTY | F.CHECK)
305
+ this.f = f & ~(F.DIRTY | F.CHECK)
317
306
  let errors: unknown[] | undefined
318
- try {
319
- releaseOwned(this)
320
- }
321
- catch (err) {
322
- errors = [err]
323
- }
324
- if (this.f & F.DISPOSED) {
325
- throwErrors(errors)
326
- return
307
+ if (this.fs !== undefined || this.cl !== undefined) {
308
+ try {
309
+ releaseOwned(this)
310
+ }
311
+ catch (err) {
312
+ errors = [err]
313
+ }
314
+ if (this.f & F.DISPOSED) {
315
+ throwErrors(errors)
316
+ return
317
+ }
327
318
  }
319
+ this.f |= F.RUNNING
328
320
  const prevSub = currentSub
329
321
  const prevOwner = currentOwner
330
- this.f |= F.RUNNING
331
322
  this.e = ++globalEpoch
332
323
  this.c = this.d
333
324
  currentSub = this
@@ -335,22 +326,26 @@ class EffectNode implements SubNode, Owner {
335
326
  try {
336
327
  const ret = (this._f)()
337
328
  if (typeof ret === 'function') {
338
- if (this.f & F.DISPOSED)
329
+ if (this.f & F.DISPOSED) {
339
330
  ret()
340
- else
341
- ;(this.cl ??= []).unshift(ret)
331
+ }
332
+ else {
333
+ (this.cl ??= []).unshift(ret)
334
+ }
342
335
  }
343
336
  }
344
337
  catch (err) {
345
- ;(errors ??= []).push(err)
338
+ (errors ??= []).push(err)
346
339
  }
347
340
  finally {
348
- unlinkRemaining(this)
341
+ if (this.c !== undefined)
342
+ unlinkRemaining(this)
349
343
  this.f &= ~F.RUNNING
350
344
  currentSub = prevSub
351
345
  currentOwner = prevOwner
352
346
  }
353
- throwErrors(errors)
347
+ if (errors !== undefined)
348
+ throwErrors(errors)
354
349
  }
355
350
  }
356
351
 
@@ -361,58 +356,27 @@ export function createOwner(parent: Owner | undefined = currentOwner): Owner {
361
356
  return owner
362
357
  }
363
358
 
364
- // Disposes an owner, an effect or a computed: leaves its owner, drops its sources, releases what it owns, runs its cleanup.
365
- export function disposeOwner(node: Owner): void {
366
- const f = node.f
367
- if (f & F.DISPOSED)
368
- return
369
- node.f |= F.DISPOSED
370
- unadopt(node)
371
- if (f & (F.COMPUTED | F.EFFECT))
372
- unlinkAll(node as unknown as SubNode)
373
- if (f & F.COMPUTED)
374
- unlinkSubs(node as unknown as DepNode)
375
- else
376
- releaseOwned(node)
377
- }
378
-
379
- // Pops the head each time, so a cleanup that disposes a sibling cannot cut the walk short; every child and cleanup runs even if one throws.
380
- function releaseOwned(owner: Owner): void {
381
- let errors: unknown[] | undefined
382
- let node: Owned | undefined
383
- while ((node = owner.fs)) {
384
- unadopt(node)
385
- try {
386
- disposeOwner(node as Owner)
387
- }
388
- catch (err) {
389
- ;(errors ??= []).push(err)
390
- }
359
+ export function runWithOwner<T>(owner: Owner | undefined, fn: () => T): T {
360
+ const prevOwner = currentOwner
361
+ const prevSub = currentSub
362
+ currentOwner = owner
363
+ currentSub = undefined
364
+ try {
365
+ return fn()
391
366
  }
392
-
393
- const fns = owner.cl
394
- if (fns) {
395
- owner.cl = undefined
396
- for (let i = fns.length - 1; i >= 0; i--) {
397
- try {
398
- fns[i]!()
399
- }
400
- catch (err) {
401
- ;(errors ??= []).push(err)
402
- }
403
- }
367
+ finally {
368
+ currentOwner = prevOwner
369
+ currentSub = prevSub
404
370
  }
405
-
406
- throwErrors(errors)
407
371
  }
408
372
 
409
- export function runWithOwner<T>(owner: Owner | undefined, fn: () => T): T {
373
+ export function runInOwner<A, B, R>(owner: Owner | undefined, fn: (a: A, b: B) => R, a: A, b: B): R {
410
374
  const prevOwner = currentOwner
411
375
  const prevSub = currentSub
412
376
  currentOwner = owner
413
377
  currentSub = undefined
414
378
  try {
415
- return fn()
379
+ return fn(a, b)
416
380
  }
417
381
  finally {
418
382
  currentOwner = prevOwner
@@ -433,27 +397,22 @@ export function runOwned<T>(owner: Owner, fn: () => T): T {
433
397
  }
434
398
 
435
399
  export function getOwner(): Owner | undefined {
436
- return currentOwner
437
- }
438
-
439
- export function isTracking(): boolean {
440
- return currentSub !== undefined
400
+ const owner = currentOwner
401
+ if (owner)
402
+ owner.f |= F.KEPT
403
+ return owner
441
404
  }
442
-
405
+ export const isTracking = (): boolean => currentSub !== undefined
443
406
  /** Internal: true when the active subscriber will attach to any source it reads (observed and not disposed). */
444
- export function isObserving(): boolean {
445
- return !!currentSub && !(currentSub.f & (F.UNOBSERVED | F.DISPOSED))
446
- }
447
-
448
- /** Internal: total signal writes so far, used by selector() to detect un-flushed changes without importing computed. */
449
- export function writeCount(): number {
450
- return writes
451
- }
452
-
407
+ export const isObserving = (): boolean => !!currentSub && !(currentSub.f & (F.UNOBSERVED | F.DISPOSED))
408
+ /** Internal: true while inside a batch or effect initialization. */
409
+ export const isBatching = (): boolean => batchDepth > 0
410
+ /** Internal: enters a batch without allocating a callback closure. */
411
+ export const beginBatch = (): void => { batchDepth++ }
412
+ /** Internal: exits a batch started by beginBatch(). */
413
+ export const endBatch = (threw: boolean): void => flushAfterBatch(threw)
453
414
  /** Internal: the undo sink of the running recordWrites(), for the store's own writes. */
454
- export function writeRecorder(): ((undo: () => void) => void) | undefined {
455
- return rec
456
- }
415
+ export const writeRecorder = (): ((undo: () => void) => void) | undefined => rec
457
416
 
458
417
  /** Internal: runs `fn` in a batch and pushes onto `undos` what restores each signal and store write it made, in order. */
459
418
  export function recordWrites<T>(fn: () => T, undos: (() => void)[]): T {
@@ -467,24 +426,13 @@ export function recordWrites<T>(fn: () => T, undos: (() => void)[]): T {
467
426
  }
468
427
  }
469
428
 
470
- /** Internal: signal that runs `onUnsub` and bumps its version when its last subscriber detaches. */
471
- export function watchedSignal<T>(initial: T, onUnsub: () => void): Signal<T> {
472
- const s = new SignalNode(initial)
473
- s.u = () => {
474
- s.v++
475
- writes++
476
- onUnsub()
477
- }
478
- return s
479
- }
480
-
481
429
  export function root<T>(fn: (dispose: () => void) => T): T {
482
430
  const owner = createOwner(undefined)
483
431
  return runWithOwner(owner, () => fn(() => disposeOwner(owner)))
484
432
  }
485
433
 
486
- export function signal<T>(initial: T): Signal<T> {
487
- const s = new SignalNode(initial)
434
+ export function signal<T>(initial: T, options?: SignalOptions<T>): Signal<T> {
435
+ const s = new SignalNode(initial, options?.equals)
488
436
  if (process.env.NODE_ENV !== 'production')
489
437
  (globalThis as HmrHost).__pruHmr?.note(s as Signal<unknown>)
490
438
  if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
@@ -492,12 +440,57 @@ export function signal<T>(initial: T): Signal<T> {
492
440
  return s
493
441
  }
494
442
 
495
- export function computed<T>(fn: () => T): ReadonlySignal<T> {
496
- return new ComputedNode(fn, currentOwner)
497
- }
443
+ export const computed = <T>(fn: () => T, options?: SignalOptions<T>): ReadonlySignal<T> => new ComputedNode(fn, currentOwner, options?.equals)
498
444
 
499
445
  function noop(): void {}
500
446
 
447
+ function flushAfterBatch(threw: boolean): void {
448
+ if (--batchDepth !== 0)
449
+ return
450
+ if (!threw) {
451
+ flush()
452
+ return
453
+ }
454
+ try {
455
+ flush()
456
+ }
457
+ catch (flushErr) {
458
+ console.error(flushErr)
459
+ }
460
+ }
461
+
462
+ function runEffectNode(node: EffectNode): void {
463
+ batchDepth++
464
+ let threw = true
465
+ try {
466
+ node.r()
467
+ threw = false
468
+ }
469
+ finally {
470
+ flushAfterBatch(threw)
471
+ }
472
+ }
473
+
474
+ // Internal void effect for compiled DOM bindings and helpers: skips allocating a dispose closure.
475
+ export function domEffect(fn: () => void): void {
476
+ if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && inServerRender())
477
+ return
478
+ const parent = currentOwner
479
+ if (parent && (parent.f & F.DISPOSED))
480
+ return
481
+ const node = new EffectNode(fn, parent)
482
+ try {
483
+ runEffectNode(node)
484
+ }
485
+ catch (err) {
486
+ disposeOwner(node)
487
+ throw err
488
+ }
489
+ // Nothing can rerun it or attach to it: out of its owner's list, it goes with its DOM.
490
+ if (node.d === undefined && node.fs === undefined && node.cl === undefined && !(node.f & F.KEPT))
491
+ unadopt(node)
492
+ }
493
+
501
494
  // An effect skips the server render, as onMount does: a timer or request it starts would outlive the response. The client build defines PRUNELLE_BROWSER, which drops the check.
502
495
  export function effect(fn: () => void | (() => void)): () => void {
503
496
  if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && inServerRender())
@@ -505,31 +498,22 @@ export function effect(fn: () => void | (() => void)): () => void {
505
498
  if (process.env.NODE_ENV !== 'production' && !currentOwner && currentSub && (currentSub.f & F.COMPUTED))
506
499
  console.warn('prunelle: effect() created while a computed runs has no owner and is never disposed. Create it in a component or an effect.')
507
500
  const node = new EffectNode(fn, currentOwner)
508
- node.r()
501
+ runEffectNode(node)
509
502
  return () => disposeOwner(node)
510
503
  }
511
504
 
512
505
  // An error thrown by fn wins over one thrown by the effects it triggered, which is logged instead.
513
506
  export function batch<T>(fn: () => T): T {
514
507
  batchDepth++
515
- let result: T
508
+ let threw = true
516
509
  try {
517
- result = fn()
510
+ const result = fn()
511
+ threw = false
512
+ return result
518
513
  }
519
- catch (err) {
520
- if (--batchDepth === 0) {
521
- try {
522
- flush()
523
- }
524
- catch (flushErr) {
525
- console.error(flushErr)
526
- }
527
- }
528
- throw err
514
+ finally {
515
+ flushAfterBatch(threw)
529
516
  }
530
- if (--batchDepth === 0)
531
- flush()
532
- return result
533
517
  }
534
518
 
535
519
  export function untrack<T>(fn: () => T): T {
@@ -560,7 +544,7 @@ export function onCleanup(fn: () => void): void {
560
544
  export function onMount(fn: () => void | (() => void)): void {
561
545
  if (((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && inServerRender()) || typeof document === 'undefined')
562
546
  return
563
- const owner = currentOwner
547
+ const owner = getOwner()
564
548
  queueMicrotask(() => {
565
549
  if (owner && owner.f & F.DISPOSED)
566
550
  return