prunelle 0.8.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 (209) hide show
  1. package/llms.txt +68 -51
  2. package/package.json +7 -2
  3. package/src/a11y/declaration.ts +10 -17
  4. package/src/a11y/index.ts +69 -125
  5. package/src/animate.ts +127 -0
  6. package/src/build/asides.ts +51 -0
  7. package/src/build/bundle.ts +94 -136
  8. package/src/build/client-entries.ts +6 -6
  9. package/src/build/client-outputs.ts +75 -0
  10. package/src/build/client-plugin.ts +9 -4
  11. package/src/build/ecoindex.ts +4 -2
  12. package/src/build/files.ts +18 -0
  13. package/src/build/graph.ts +14 -6
  14. package/src/build/index.ts +23 -11
  15. package/src/build/island-promote.ts +42 -2
  16. package/src/build/islands.ts +63 -9
  17. package/src/build/key-types.ts +31 -0
  18. package/src/build/manifest.ts +117 -1
  19. package/src/build/plugin-host.ts +13 -1
  20. package/src/build/prerender.ts +8 -3
  21. package/src/build/reach.ts +18 -4
  22. package/src/build/report.ts +1 -1
  23. package/src/build/resolve.ts +5 -1
  24. package/src/build/route-types.ts +16 -0
  25. package/src/build/routes.ts +42 -15
  26. package/src/build/seed-ids.ts +65 -0
  27. package/src/build/seed-writes.ts +111 -0
  28. package/src/build/static-headers.ts +2 -2
  29. package/src/build/static.ts +1 -1
  30. package/src/cli/check-cache.ts +51 -0
  31. package/src/cli/check-consent.ts +95 -0
  32. package/src/cli/check-forms.ts +10 -2
  33. package/src/cli/check-links-patterns.ts +101 -0
  34. package/src/cli/check-links.ts +30 -105
  35. package/src/cli/check-regions.ts +185 -0
  36. package/src/cli/check-security.ts +50 -6
  37. package/src/cli/check.ts +49 -21
  38. package/src/cli/commands.ts +138 -16
  39. package/src/cli/docs.ts +21 -2
  40. package/src/cli/form-opaque-children.ts +40 -0
  41. package/src/cli/index.ts +1 -1
  42. package/src/cli/inspect.ts +2 -0
  43. package/src/cli/mcp-tools.ts +2 -2
  44. package/src/cli/new.ts +9 -5
  45. package/src/cli/upgrade-names.ts +14 -2
  46. package/src/cli/upgrade.ts +3 -0
  47. package/src/compiler/analyze.ts +22 -16
  48. package/src/compiler/cacheability-private.ts +13 -3
  49. package/src/compiler/cacheability.ts +24 -2
  50. package/src/compiler/compliance-contrast.ts +322 -0
  51. package/src/compiler/compliance-empty.ts +24 -0
  52. package/src/compiler/compliance-rgaa-content.ts +95 -0
  53. package/src/compiler/compliance-rgaa-criteria.ts +45 -0
  54. package/src/compiler/compliance-rgaa-pages.ts +140 -0
  55. package/src/compiler/compliance-rgaa.ts +19 -0
  56. package/src/compiler/compliance-rules.ts +72 -0
  57. package/src/compiler/compliance-third-party.ts +59 -0
  58. package/src/compiler/compliance-urls.ts +52 -0
  59. package/src/compiler/compliance.ts +61 -93
  60. package/src/compiler/cookie-inventory.ts +119 -0
  61. package/src/compiler/dom-attrs.ts +178 -22
  62. package/src/compiler/dom.ts +31 -44
  63. package/src/compiler/emit.ts +17 -6
  64. package/src/compiler/expr-context.ts +260 -207
  65. package/src/compiler/foreign-imports.ts +5 -3
  66. package/src/compiler/mistakes.ts +18 -9
  67. package/src/compiler/props-body.ts +263 -21
  68. package/src/compiler/props-pattern.ts +211 -0
  69. package/src/compiler/props.ts +324 -343
  70. package/src/compiler/signal-reads.ts +142 -29
  71. package/src/compiler/spread-handlers.ts +25 -13
  72. package/src/compiler/ssr-emit.ts +510 -0
  73. package/src/compiler/ssr-helpers.ts +165 -121
  74. package/src/compiler/ssr.ts +28 -441
  75. package/src/context.ts +14 -1
  76. package/src/dev/child.ts +1 -1
  77. package/src/dev/host-check.ts +2 -1
  78. package/src/dev/hot-islands.ts +71 -0
  79. package/src/dev/index.ts +31 -18
  80. package/src/dev/protocol.ts +1 -1
  81. package/src/dev/queue.ts +3 -2
  82. package/src/dev/reload.ts +5 -2
  83. package/src/dev/supervisor.ts +3 -1
  84. package/src/dev/watchers.ts +6 -2
  85. package/src/dom/action.ts +97 -14
  86. package/src/dom/attrs.ts +93 -25
  87. package/src/dom/extras.ts +91 -50
  88. package/src/dom/field-marks.ts +32 -0
  89. package/src/dom/hot.ts +9 -0
  90. package/src/dom/index.ts +44 -18
  91. package/src/dom/island-hot.ts +29 -0
  92. package/src/dom/island.ts +13 -2
  93. package/src/dom/loop.ts +259 -188
  94. package/src/dom/prefetch.ts +135 -0
  95. package/src/dom/regions.ts +515 -0
  96. package/src/dom/swap.ts +246 -0
  97. package/src/dom/trusted.ts +22 -0
  98. package/src/dom/upload.ts +44 -0
  99. package/src/eco/declaration.ts +44 -41
  100. package/src/eco/index.ts +8 -5
  101. package/src/href.ts +31 -0
  102. package/src/index.ts +12 -5
  103. package/src/plugins/config.ts +1 -1
  104. package/src/plugins/context.ts +7 -1
  105. package/src/plugins/daily.ts +27 -0
  106. package/src/plugins/index.ts +3 -3
  107. package/src/plugins/order.ts +1 -1
  108. package/src/plugins/public.ts +12 -5
  109. package/src/plugins/services.ts +88 -14
  110. package/src/plugins/types.ts +128 -10
  111. package/src/reactivity/graph.ts +244 -41
  112. package/src/reactivity/hmr.ts +101 -0
  113. package/src/reactivity/index.ts +243 -239
  114. package/src/reactivity/resource.ts +129 -0
  115. package/src/reactivity/seed.ts +141 -0
  116. package/src/reactivity/selector.ts +85 -47
  117. package/src/reactivity/server-seed.ts +202 -0
  118. package/src/reactivity/store.ts +331 -97
  119. package/src/rgpd/draft.ts +171 -0
  120. package/src/rgpd/forms.ts +85 -0
  121. package/src/rgpd/index.ts +57 -0
  122. package/src/server/action-flash.ts +1 -0
  123. package/src/server/action-protect.ts +3 -0
  124. package/src/server/action-registry.ts +11 -9
  125. package/src/server/action.ts +51 -19
  126. package/src/server/app-secret.ts +14 -8
  127. package/src/server/cache-invalidation.ts +97 -0
  128. package/src/server/compress.ts +55 -18
  129. package/src/server/consent.ts +21 -5
  130. package/src/server/cookie-inventory.ts +78 -0
  131. package/src/server/cookies.ts +77 -8
  132. package/src/server/cors.ts +98 -0
  133. package/src/server/csp-report.ts +90 -0
  134. package/src/server/discard.ts +13 -0
  135. package/src/server/document.ts +2 -2
  136. package/src/server/env.ts +1 -1
  137. package/src/server/errors.ts +9 -2
  138. package/src/server/field-errors.ts +91 -0
  139. package/src/server/form-schema.ts +16 -11
  140. package/src/server/health.ts +41 -11
  141. package/src/server/http.ts +28 -11
  142. package/src/server/index.ts +122 -307
  143. package/src/server/loader.ts +2 -0
  144. package/src/server/page.ts +3 -9
  145. package/src/server/plugin-host.ts +66 -16
  146. package/src/server/plugin-route-shadow.ts +24 -0
  147. package/src/server/public.ts +14 -3
  148. package/src/server/rate-limit.ts +49 -10
  149. package/src/server/region.ts +22 -2
  150. package/src/server/request-handler.ts +406 -0
  151. package/src/server/request-log.ts +94 -0
  152. package/src/server/route-cache.ts +55 -8
  153. package/src/server/route-input.ts +54 -0
  154. package/src/server/route-table.ts +53 -27
  155. package/src/server/router.ts +2 -1
  156. package/src/server/routing.ts +104 -45
  157. package/src/server/sanitize-html.ts +4 -2
  158. package/src/server/security.ts +38 -8
  159. package/src/server/serve-handlers.ts +16 -6
  160. package/src/server/served-file.ts +19 -14
  161. package/src/server/shared-store.ts +323 -0
  162. package/src/server/start.ts +12 -5
  163. package/src/server/static-dir.ts +34 -12
  164. package/src/server/stream.ts +2 -1
  165. package/src/server/surface.ts +39 -0
  166. package/src/server/webhook.ts +50 -0
  167. package/src/server/workers.ts +168 -0
  168. package/src/ssr/consent-banner.ts +20 -16
  169. package/src/ssr/consent-texts.ts +40 -0
  170. package/src/ssr/context.ts +174 -53
  171. package/src/ssr/cookie-table.ts +75 -0
  172. package/src/ssr/index.ts +63 -58
  173. package/src/ssr/navigation.ts +161 -14
  174. package/src/ssr/region.ts +9 -3
  175. package/src/style.ts +12 -5
  176. package/src/test/browser.ts +53 -23
  177. package/src/test/build-reuse.ts +85 -0
  178. package/src/test/index.ts +51 -8
  179. package/src/test/mount.ts +53 -0
  180. package/src/test/outside.ts +94 -0
  181. package/src/ts-plugin/diagnostics.ts +56 -18
  182. package/templates/_shared/AGENTS.md +1 -1
  183. package/templates/_shared/Dockerfile +22 -0
  184. package/templates/_shared/dockerignore +9 -0
  185. package/templates/_shared/gitignore +2 -0
  186. package/templates/app/.env.example +3 -1
  187. package/templates/app/compose.yaml +32 -0
  188. package/templates/app/db/migrations/001_notes.sql +7 -0
  189. package/templates/app/db/schema.ts +11 -0
  190. package/templates/app/package.json +1 -0
  191. package/templates/app/prunelle.config.ts +6 -0
  192. package/templates/app/src/actions/notes.ts +26 -0
  193. package/templates/app/src/auth.ts +4 -2
  194. package/templates/app/src/db.ts +5 -0
  195. package/templates/app/src/routes/dashboard/index.tsx +26 -1
  196. package/templates/app/src/routes/privacy.tsx +6 -2
  197. package/templates/app/tests/app.test.ts +14 -3
  198. package/templates/plugin/AGENTS.md +11 -0
  199. package/templates/plugin/CLAUDE.md +1 -0
  200. package/templates/plugin/README.md +27 -0
  201. package/templates/plugin/gitignore +2 -0
  202. package/templates/plugin/llms.txt +19 -0
  203. package/templates/plugin/package.json +33 -0
  204. package/templates/plugin/src/index.ts +40 -0
  205. package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
  206. package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
  207. package/templates/plugin/tests/plugin.test.ts +42 -0
  208. package/templates/plugin/tsconfig.json +19 -0
  209. package/ts-plugin.cjs +4058 -2246
@@ -2,9 +2,16 @@
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
+ import type { HmrHost } from './hmr'
7
+ import { inServerRender, isSharedWrite, recordSharedWrite, shareOutsideRender } from './server-seed'
6
8
 
7
9
  export type { IdScope, Owner, ReadonlySignal, Signal } from './graph'
10
+ export { bumpWrites, disposeOwner, writeCount } from './graph'
11
+ export type { RequestSeeds } from './server-seed'
12
+ export { isSharedWrite, nameSeed, recordSharedWrite, renderOnServer, shareOutsideRender, sharedPath } from './server-seed'
13
+
14
+ declare const PRUNELLE_BROWSER: boolean | undefined
8
15
 
9
16
  const enum F {
10
17
  CLEAN = 0,
@@ -19,6 +26,10 @@ const enum F {
19
26
  UNOBSERVED = 128,
20
27
  // Queued for the current flush round (distinct from QUEUED, which marks the next round).
21
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,
22
33
  }
23
34
 
24
35
  let currentSub: SubNode | undefined
@@ -28,142 +39,120 @@ let batchDepth = 0
28
39
  let q: SubNode[] = []
29
40
  let qNext: SubNode[] = []
30
41
  let flushing = false
31
- // Signal writes so far: an unobserved computed that checked at the same count can skip its version walk.
32
- let writes = 0
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.
43
+ let rec: ((target: Signal<unknown> | (() => void), prev?: unknown, next?: unknown) => void) | undefined
33
44
  // Bumped after a flush that threw so observed computeds still left DIRTY/CHECK re-propagate downstream on recovery.
34
45
  let propWave = 0
35
46
 
36
- // A child effect can sit ahead of its parent in the queue; the parent runs first, since its run may dispose the child.
37
- function runParentFirst(sub: SubNode, errors: unknown[]): void {
38
- sub.f &= ~(F.PENDING | F.QUEUED)
39
- for (let o = sub.o; o; o = o.o) {
40
- if (o.f & (F.PENDING | F.QUEUED)) {
41
- runParentFirst(o as unknown as SubNode, errors)
42
- break
43
- }
44
- }
45
- 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)) {
46
53
  try {
47
- if (shouldRun(sub))
48
- sub.r()
54
+ if ((f & F.DIRTY) || checkDeps?.(node))
55
+ node.r()
49
56
  }
50
57
  catch (err) {
51
- if (!catchEffectError?.(sub, err))
52
- errors.push(err)
58
+ if (!catchEffectError?.(node, err)) {
59
+ (errors ??= []).push(err)
60
+ }
53
61
  }
54
62
  }
63
+ return errors
55
64
  }
56
65
 
57
- function enqueue(sub: SubNode): void {
58
- if (sub.f & (F.QUEUED | F.DISPOSED))
59
- return
60
- sub.f |= F.QUEUED
61
- q.push(sub)
62
- }
63
-
64
- function propagate(dep: DepNode, flag: number): void {
65
- for (let l = dep.s; l; l = l.ns) {
66
- const sub = l.s
67
- const f = sub.f
68
- if (f & F.DISPOSED)
69
- continue
70
- const prev = f & (F.DIRTY | F.CHECK)
71
- if (prev < flag)
72
- sub.f = (f & ~(F.DIRTY | F.CHECK)) | flag
73
- if (f & F.EFFECT) {
74
- enqueue(sub)
75
- }
76
- else if (f & F.COMPUTED) {
77
- const comp = sub as ComputedNode<unknown>
78
- if (prev === F.CLEAN || comp.pw !== propWave) {
79
- comp.pw = propWave
80
- propagate(comp, F.CHECK)
81
- }
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
82
73
  }
83
74
  }
75
+ return runEffect(sub, errors)
84
76
  }
85
77
 
86
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.
87
79
  function flush(): void {
88
- if (flushing || batchDepth > 0)
80
+ if (flushing || batchDepth > 0 || q.length === 0)
89
81
  return
90
82
  flushing = true
91
- const errors: unknown[] = []
83
+ let errors: unknown[] | undefined
92
84
  try {
93
85
  let rounds = 0
94
86
  while (q.length > 0) {
95
87
  if (++rounds > 1000) {
96
- errors.push(new Error('prunelle: reactive cycle'))
88
+ (errors ??= []).push(new Error('prunelle: reactive cycle'))
97
89
  break
98
90
  }
99
91
  const round = q
100
92
  q = qNext
101
93
  qNext = round
102
- 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]!
103
98
  node.f = (node.f & ~F.QUEUED) | F.PENDING
104
- for (const node of round) {
105
- if (node.f & F.PENDING)
106
- 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
+ }
107
114
  }
108
115
  round.length = 0
109
116
  }
110
117
  }
111
118
  finally {
112
119
  flushing = false
113
- for (const node of q)
114
- node.f &= ~F.QUEUED
120
+ for (let i = 0; i < q.length; i++)
121
+ q[i]!.f &= ~F.QUEUED
115
122
  q.length = 0
116
- 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)
117
127
  propWave++
118
128
  }
119
- throwErrors(errors)
129
+ if (errors !== undefined)
130
+ throwErrors(errors)
120
131
  }
121
132
 
122
133
  // Installed by the first <Catch>, so an island without one carries no owner walk.
123
134
  let catchEffectError: ((node: Owned, err: unknown) => boolean) | undefined
124
135
 
125
- export function onEffectError(fn: (node: Owned, err: unknown) => boolean): void {
136
+ export const onEffectError = (fn: (node: Owned, err: unknown) => boolean): void => {
126
137
  catchEffectError = fn
127
138
  }
128
139
 
129
- function shouldRun(sub: SubNode): boolean {
130
- const f = sub.f
131
- if (f & F.DISPOSED)
132
- return false
133
- if (f & F.DIRTY)
134
- return true
135
- if (!(f & F.CHECK) && (!(f & F.UNOBSERVED) || (sub as ComputedNode<unknown>).w === writes))
136
- return false
137
- const w = writes
138
- for (let l = sub.d; l; l = l.nd) {
139
- const dep = l.d
140
- if (dep.f & F.COMPUTED) {
141
- const comp = dep as unknown as ComputedNode<unknown>
142
- if (shouldRun(comp))
143
- comp.r()
144
- }
145
- if (dep.v !== l.v) {
146
- sub.f |= F.DIRTY
147
- return true
148
- }
149
- }
150
- sub.f &= ~F.CHECK
151
- if (f & F.UNOBSERVED)
152
- (sub as ComputedNode<unknown>).w = w
153
- return false
140
+ export interface SignalOptions<T> {
141
+ equals?: ((prev: T, next: T) => boolean) | undefined
154
142
  }
155
143
 
156
- class SignalNode<T> implements DepNode, Signal<T> {
144
+ export class SignalNode<T> implements DepNode, Signal<T> {
157
145
  v = 0
158
- s: Link | undefined
159
- st: Link | undefined
146
+ declare s: Link | undefined
147
+ declare st: Link | undefined
160
148
  le = -1
161
149
  f = 0
162
- declare u?: (() => void) | undefined
163
- _v: T
150
+ declare _v: T
151
+ declare _eq: ((prev: T, next: T) => boolean) | undefined
164
152
 
165
- constructor(initial: T) {
153
+ constructor(initial: T, equals?: (prev: T, next: T) => boolean) {
166
154
  this._v = initial
155
+ this._eq = equals
167
156
  }
168
157
 
169
158
  get value(): T {
@@ -173,13 +162,18 @@ class SignalNode<T> implements DepNode, Signal<T> {
173
162
  }
174
163
 
175
164
  set value(next: T) {
176
- if (Object.is(this._v, next))
165
+ if (this._eq ? this._eq(this._v, next) : Object.is(this._v, next))
177
166
  return
167
+ if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && isSharedWrite(this)) {
168
+ const prev = this._v
169
+ recordSharedWrite(this, [], () => this.value = next, () => this.value = prev, next)
170
+ }
171
+ rec?.(this, this._v, next)
178
172
  this._v = next
179
173
  this.v++
180
- writes++
174
+ bumpWrites()
181
175
  if (this.s) {
182
- propagate(this, F.DIRTY)
176
+ propagate(this, F.DIRTY, q, propWave)
183
177
  flush()
184
178
  }
185
179
  }
@@ -191,25 +185,27 @@ class SignalNode<T> implements DepNode, Signal<T> {
191
185
 
192
186
  class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
193
187
  v = 0
194
- s: Link | undefined
195
- st: Link | undefined
188
+ declare s: Link | undefined
189
+ declare st: Link | undefined
196
190
  le = -1
197
191
  f = F.COMPUTED | F.DIRTY | F.UNOBSERVED
198
192
  e = -1
199
- d: Link | undefined
200
- dt: Link | undefined
201
- c: Link | undefined
202
- po: Owned | undefined
203
- no: Owned | undefined
204
- o: Owner | undefined
205
- _f: () => T
206
- _v!: T
207
- _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
208
202
  w = -1
209
203
  pw = 0
210
204
 
211
- constructor(fn: () => T, owner: Owner | undefined) {
205
+ constructor(fn: () => T, owner: Owner | undefined, equals?: (prev: T, next: T) => boolean) {
212
206
  this._f = fn
207
+ this._eq = equals
208
+ checkDeps = shouldRun
213
209
  if (owner)
214
210
  adopt(this, owner)
215
211
  }
@@ -221,7 +217,7 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
221
217
  throw new Error('prunelle: cyclic computed')
222
218
  const prevSub = currentSub
223
219
  const prevOwner = currentOwner
224
- const w = writes
220
+ const w = writeCount()
225
221
  // A first run for an observed reader links straight into its sources; roll back if the compute throws.
226
222
  const direct = observe && !this.d
227
223
  if (direct)
@@ -234,16 +230,17 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
234
230
  let ok = false
235
231
  try {
236
232
  const next = (this._f)()
237
- 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))) {
238
234
  this._v = next
239
- this._i = true
235
+ this.f |= F.INIT
240
236
  this.v++
241
237
  }
242
238
  this.w = w
243
239
  ok = true
244
240
  }
245
241
  finally {
246
- unlinkRemaining(this)
242
+ if (this.c !== undefined)
243
+ unlinkRemaining(this)
247
244
  this.f &= ~F.RUNNING
248
245
  if (!ok) {
249
246
  this.f |= F.DIRTY
@@ -256,22 +253,26 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
256
253
  }
257
254
 
258
255
  get value(): T {
259
- if (!(this.f & F.DISPOSED)) {
256
+ const f = this.f
257
+ if (!(f & F.DISPOSED)) {
260
258
  const sub = currentSub
261
- if (shouldRun(this))
262
- this.r(!!sub && !(sub.f & (F.UNOBSERVED | F.DISPOSED)))
263
- if (!(this.f & F.DISPOSED)) {
264
- if (sub)
265
- linkSub(this, sub)
266
- if (!this.s && !(this.f & F.UNOBSERVED))
267
- 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
268
264
  }
265
+ if (sub)
266
+ linkSub(this, sub)
267
+ if (!this.s && !(this.f & F.UNOBSERVED))
268
+ unobserve(this)
269
269
  }
270
270
  return this._v
271
271
  }
272
272
 
273
273
  peek(): T {
274
- 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))
275
276
  this.r()
276
277
  return this._v
277
278
  }
@@ -279,17 +280,17 @@ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
279
280
 
280
281
  // An effect is also the owner of what its run creates (effects, onCleanup), released before the next run.
281
282
  class EffectNode implements SubNode, Owner {
282
- f = F.EFFECT | F.DIRTY
283
+ f = F.EFFECT
283
284
  e = -1
284
- d: Link | undefined
285
- dt: Link | undefined
286
- c: Link | undefined
287
- po: Owned | undefined
288
- no: Owned | undefined
289
- o: Owner | undefined
290
- fs: Owned | undefined
291
- cl: (() => void)[] | undefined
292
- _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)
293
294
 
294
295
  constructor(fn: () => void | (() => void), owner: Owner | undefined) {
295
296
  this._f = fn
@@ -298,23 +299,26 @@ class EffectNode implements SubNode, Owner {
298
299
  }
299
300
 
300
301
  r(): void {
301
- if (this.f & F.DISPOSED)
302
+ const f = this.f
303
+ if (f & F.DISPOSED)
302
304
  return
303
- this.f &= ~(F.DIRTY | F.CHECK)
305
+ this.f = f & ~(F.DIRTY | F.CHECK)
304
306
  let errors: unknown[] | undefined
305
- try {
306
- releaseOwned(this)
307
- }
308
- catch (err) {
309
- errors = [err]
310
- }
311
- if (this.f & F.DISPOSED) {
312
- throwErrors(errors)
313
- 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
+ }
314
318
  }
319
+ this.f |= F.RUNNING
315
320
  const prevSub = currentSub
316
321
  const prevOwner = currentOwner
317
- this.f |= F.RUNNING
318
322
  this.e = ++globalEpoch
319
323
  this.c = this.d
320
324
  currentSub = this
@@ -322,22 +326,26 @@ class EffectNode implements SubNode, Owner {
322
326
  try {
323
327
  const ret = (this._f)()
324
328
  if (typeof ret === 'function') {
325
- if (this.f & F.DISPOSED)
329
+ if (this.f & F.DISPOSED) {
326
330
  ret()
327
- else
328
- ;(this.cl ??= []).unshift(ret)
331
+ }
332
+ else {
333
+ (this.cl ??= []).unshift(ret)
334
+ }
329
335
  }
330
336
  }
331
337
  catch (err) {
332
- ;(errors ??= []).push(err)
338
+ (errors ??= []).push(err)
333
339
  }
334
340
  finally {
335
- unlinkRemaining(this)
341
+ if (this.c !== undefined)
342
+ unlinkRemaining(this)
336
343
  this.f &= ~F.RUNNING
337
344
  currentSub = prevSub
338
345
  currentOwner = prevOwner
339
346
  }
340
- throwErrors(errors)
347
+ if (errors !== undefined)
348
+ throwErrors(errors)
341
349
  }
342
350
  }
343
351
 
@@ -348,58 +356,27 @@ export function createOwner(parent: Owner | undefined = currentOwner): Owner {
348
356
  return owner
349
357
  }
350
358
 
351
- // Disposes an owner, an effect or a computed: leaves its owner, drops its sources, releases what it owns, runs its cleanup.
352
- export function disposeOwner(node: Owner): void {
353
- const f = node.f
354
- if (f & F.DISPOSED)
355
- return
356
- node.f |= F.DISPOSED
357
- unadopt(node)
358
- if (f & (F.COMPUTED | F.EFFECT))
359
- unlinkAll(node as unknown as SubNode)
360
- if (f & F.COMPUTED)
361
- unlinkSubs(node as unknown as DepNode)
362
- else
363
- releaseOwned(node)
364
- }
365
-
366
- // 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.
367
- function releaseOwned(owner: Owner): void {
368
- let errors: unknown[] | undefined
369
- let node: Owned | undefined
370
- while ((node = owner.fs)) {
371
- unadopt(node)
372
- try {
373
- disposeOwner(node as Owner)
374
- }
375
- catch (err) {
376
- ;(errors ??= []).push(err)
377
- }
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()
378
366
  }
379
-
380
- const fns = owner.cl
381
- if (fns) {
382
- owner.cl = undefined
383
- for (let i = fns.length - 1; i >= 0; i--) {
384
- try {
385
- fns[i]!()
386
- }
387
- catch (err) {
388
- ;(errors ??= []).push(err)
389
- }
390
- }
367
+ finally {
368
+ currentOwner = prevOwner
369
+ currentSub = prevSub
391
370
  }
392
-
393
- throwErrors(errors)
394
371
  }
395
372
 
396
- 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 {
397
374
  const prevOwner = currentOwner
398
375
  const prevSub = currentSub
399
376
  currentOwner = owner
400
377
  currentSub = undefined
401
378
  try {
402
- return fn()
379
+ return fn(a, b)
403
380
  }
404
381
  finally {
405
382
  currentOwner = prevOwner
@@ -420,32 +397,33 @@ export function runOwned<T>(owner: Owner, fn: () => T): T {
420
397
  }
421
398
 
422
399
  export function getOwner(): Owner | undefined {
423
- return currentOwner
424
- }
425
-
426
- export function isTracking(): boolean {
427
- return currentSub !== undefined
400
+ const owner = currentOwner
401
+ if (owner)
402
+ owner.f |= F.KEPT
403
+ return owner
428
404
  }
429
-
405
+ export const isTracking = (): boolean => currentSub !== undefined
430
406
  /** Internal: true when the active subscriber will attach to any source it reads (observed and not disposed). */
431
- export function isObserving(): boolean {
432
- return !!currentSub && !(currentSub.f & (F.UNOBSERVED | F.DISPOSED))
433
- }
434
-
435
- /** Internal: total signal writes so far, used by selector() to detect un-flushed changes without importing computed. */
436
- export function writeCount(): number {
437
- return writes
438
- }
439
-
440
- /** Internal: signal that runs `onUnsub` and bumps its version when its last subscriber detaches. */
441
- export function watchedSignal<T>(initial: T, onUnsub: () => void): Signal<T> {
442
- const s = new SignalNode(initial)
443
- s.u = () => {
444
- s.v++
445
- writes++
446
- onUnsub()
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)
414
+ /** Internal: the undo sink of the running recordWrites(), for the store's own writes. */
415
+ export const writeRecorder = (): ((undo: () => void) => void) | undefined => rec
416
+
417
+ /** Internal: runs `fn` in a batch and pushes onto `undos` what restores each signal and store write it made, in order. */
418
+ export function recordWrites<T>(fn: () => T, undos: (() => void)[]): T {
419
+ const outer = rec
420
+ rec = (target, prev, next) => undos.push(typeof target === 'function' ? target : () => { if (Object.is(target.peek(), next)) target.value = prev })
421
+ try {
422
+ return batch(fn)
423
+ }
424
+ finally {
425
+ rec = outer
447
426
  }
448
- return s
449
427
  }
450
428
 
451
429
  export function root<T>(fn: (dispose: () => void) => T): T {
@@ -453,63 +431,89 @@ export function root<T>(fn: (dispose: () => void) => T): T {
453
431
  return runWithOwner(owner, () => fn(() => disposeOwner(owner)))
454
432
  }
455
433
 
456
- export function signal<T>(initial: T): Signal<T> {
457
- return new SignalNode(initial)
434
+ export function signal<T>(initial: T, options?: SignalOptions<T>): Signal<T> {
435
+ const s = new SignalNode(initial, options?.equals)
436
+ if (process.env.NODE_ENV !== 'production')
437
+ (globalThis as HmrHost).__pruHmr?.note(s as Signal<unknown>)
438
+ if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
439
+ shareOutsideRender(s)
440
+ return s
458
441
  }
459
442
 
460
- export function computed<T>(fn: () => T): ReadonlySignal<T> {
461
- return new ComputedNode(fn, currentOwner)
462
- }
443
+ export const computed = <T>(fn: () => T, options?: SignalOptions<T>): ReadonlySignal<T> => new ComputedNode(fn, currentOwner, options?.equals)
463
444
 
464
- declare const PRUNELLE_BROWSER: boolean | undefined
445
+ function noop(): void {}
465
446
 
466
- let serverRenders = 0
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
+ }
467
461
 
468
- /** Internal: runs an island's server render, during which `effect` and `onMount` do nothing. */
469
- export function renderOnServer<T>(fn: () => T): T {
470
- serverRenders++
462
+ function runEffectNode(node: EffectNode): void {
463
+ batchDepth++
464
+ let threw = true
471
465
  try {
472
- return fn()
466
+ node.r()
467
+ threw = false
473
468
  }
474
469
  finally {
475
- serverRenders--
470
+ flushAfterBatch(threw)
476
471
  }
477
472
  }
478
473
 
479
- function noop(): void {}
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
+ }
480
493
 
481
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.
482
495
  export function effect(fn: () => void | (() => void)): () => void {
483
- if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && serverRenders > 0)
496
+ if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && inServerRender())
484
497
  return noop
485
498
  if (process.env.NODE_ENV !== 'production' && !currentOwner && currentSub && (currentSub.f & F.COMPUTED))
486
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.')
487
500
  const node = new EffectNode(fn, currentOwner)
488
- node.r()
501
+ runEffectNode(node)
489
502
  return () => disposeOwner(node)
490
503
  }
491
504
 
492
505
  // An error thrown by fn wins over one thrown by the effects it triggered, which is logged instead.
493
506
  export function batch<T>(fn: () => T): T {
494
507
  batchDepth++
495
- let result: T
508
+ let threw = true
496
509
  try {
497
- result = fn()
510
+ const result = fn()
511
+ threw = false
512
+ return result
498
513
  }
499
- catch (err) {
500
- if (--batchDepth === 0) {
501
- try {
502
- flush()
503
- }
504
- catch (flushErr) {
505
- console.error(flushErr)
506
- }
507
- }
508
- throw err
514
+ finally {
515
+ flushAfterBatch(threw)
509
516
  }
510
- if (--batchDepth === 0)
511
- flush()
512
- return result
513
517
  }
514
518
 
515
519
  export function untrack<T>(fn: () => T): T {
@@ -538,9 +542,9 @@ export function onCleanup(fn: () => void): void {
538
542
 
539
543
  // A render creates its nodes and inserts them in the same task, so a microtask later they are in the document.
540
544
  export function onMount(fn: () => void | (() => void)): void {
541
- if (((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && serverRenders > 0) || typeof document === 'undefined')
545
+ if (((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && inServerRender()) || typeof document === 'undefined')
542
546
  return
543
- const owner = currentOwner
547
+ const owner = getOwner()
544
548
  queueMicrotask(() => {
545
549
  if (owner && owner.f & F.DISPOSED)
546
550
  return