prunelle 0.0.0-stage → 0.7.2

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 (222) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +245 -0
  3. package/package.json +62 -4
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/ambient.d.ts +6 -0
  8. package/src/attrs.ts +43 -0
  9. package/src/build/bundle.ts +661 -0
  10. package/src/build/byte-size.ts +11 -0
  11. package/src/build/carry.ts +57 -0
  12. package/src/build/client-entries.ts +107 -0
  13. package/src/build/client-plugin.ts +175 -0
  14. package/src/build/client-prune.ts +168 -0
  15. package/src/build/compile.ts +130 -0
  16. package/src/build/ecoindex.ts +238 -0
  17. package/src/build/files.ts +171 -0
  18. package/src/build/first-load.ts +235 -0
  19. package/src/build/graph.ts +237 -0
  20. package/src/build/index.ts +678 -0
  21. package/src/build/is-inside.ts +12 -0
  22. package/src/build/island-promote.ts +311 -0
  23. package/src/build/islands.ts +452 -0
  24. package/src/build/manifest.ts +204 -0
  25. package/src/build/output.ts +81 -0
  26. package/src/build/plugin-host.ts +294 -0
  27. package/src/build/precompress.ts +139 -0
  28. package/src/build/prerender.ts +288 -0
  29. package/src/build/reach.ts +200 -0
  30. package/src/build/render-rules.ts +205 -0
  31. package/src/build/report.ts +290 -0
  32. package/src/build/resolve.ts +52 -0
  33. package/src/build/routes.ts +342 -0
  34. package/src/build/server-entry.ts +38 -0
  35. package/src/build/static-headers.ts +112 -0
  36. package/src/build/static.ts +209 -0
  37. package/src/build/status-pages.ts +127 -0
  38. package/src/build/style-bundles.ts +526 -0
  39. package/src/cli/check-forms.ts +636 -0
  40. package/src/cli/check-imports.ts +31 -0
  41. package/src/cli/check-links.ts +632 -0
  42. package/src/cli/check-security.ts +64 -0
  43. package/src/cli/check.ts +409 -0
  44. package/src/cli/commands.ts +372 -0
  45. package/src/cli/docs.ts +403 -0
  46. package/src/cli/index.ts +70 -0
  47. package/src/cli/inspect.ts +406 -0
  48. package/src/cli/mcp-tools.ts +492 -0
  49. package/src/cli/mcp.ts +205 -0
  50. package/src/cli/new.ts +52 -0
  51. package/src/cli/render.ts +416 -0
  52. package/src/cli/upgrade-names.ts +209 -0
  53. package/src/cli/upgrade.ts +367 -0
  54. package/src/compiler/analyze.ts +651 -0
  55. package/src/compiler/ast.ts +82 -0
  56. package/src/compiler/browser-globals.ts +250 -0
  57. package/src/compiler/cacheability-private.ts +566 -0
  58. package/src/compiler/cacheability.ts +335 -0
  59. package/src/compiler/compliance-eco.ts +71 -0
  60. package/src/compiler/compliance-rgaa.ts +614 -0
  61. package/src/compiler/compliance-urls.ts +148 -0
  62. package/src/compiler/compliance.ts +653 -0
  63. package/src/compiler/data-keys.ts +302 -0
  64. package/src/compiler/dev-source.ts +87 -0
  65. package/src/compiler/dom-attrs.ts +452 -0
  66. package/src/compiler/dom.ts +641 -0
  67. package/src/compiler/emit.ts +131 -0
  68. package/src/compiler/expr-context.ts +587 -0
  69. package/src/compiler/foreign-imports.ts +88 -0
  70. package/src/compiler/index.ts +8 -0
  71. package/src/compiler/interactivity.ts +366 -0
  72. package/src/compiler/mistakes.ts +520 -0
  73. package/src/compiler/node-env.ts +50 -0
  74. package/src/compiler/patch.ts +97 -0
  75. package/src/compiler/props-body.ts +60 -0
  76. package/src/compiler/props.ts +511 -0
  77. package/src/compiler/scope.ts +635 -0
  78. package/src/compiler/server-only.ts +365 -0
  79. package/src/compiler/shared.ts +397 -0
  80. package/src/compiler/signal-reads.ts +246 -0
  81. package/src/compiler/slots.ts +227 -0
  82. package/src/compiler/spread-handlers.ts +295 -0
  83. package/src/compiler/ssr-helpers.ts +555 -0
  84. package/src/compiler/ssr-island.ts +90 -0
  85. package/src/compiler/ssr-prunelle-tags.ts +69 -0
  86. package/src/compiler/ssr.ts +525 -0
  87. package/src/context.ts +199 -0
  88. package/src/dev/child.ts +30 -0
  89. package/src/dev/error-log.ts +177 -0
  90. package/src/dev/host-check.ts +32 -0
  91. package/src/dev/index.ts +268 -0
  92. package/src/dev/modules.ts +68 -0
  93. package/src/dev/open-editor.ts +228 -0
  94. package/src/dev/protocol.ts +24 -0
  95. package/src/dev/queue.ts +79 -0
  96. package/src/dev/reload.ts +159 -0
  97. package/src/dev/supervisor.ts +212 -0
  98. package/src/dev/watchers.ts +276 -0
  99. package/src/dom/action.ts +187 -0
  100. package/src/dom/attrs.ts +84 -0
  101. package/src/dom/claim.ts +120 -0
  102. package/src/dom/extras.ts +579 -0
  103. package/src/dom/index.ts +329 -0
  104. package/src/dom/island.ts +318 -0
  105. package/src/dom/loop.ts +484 -0
  106. package/src/dom/revive.ts +36 -0
  107. package/src/eco/declaration.ts +328 -0
  108. package/src/eco/index.ts +69 -0
  109. package/src/index.ts +22 -0
  110. package/src/jsx-runtime.ts +566 -0
  111. package/src/plugins/config.ts +92 -0
  112. package/src/plugins/context.ts +109 -0
  113. package/src/plugins/index.ts +14 -0
  114. package/src/plugins/load.ts +90 -0
  115. package/src/plugins/order.ts +148 -0
  116. package/src/plugins/public.ts +15 -0
  117. package/src/plugins/requires.ts +47 -0
  118. package/src/plugins/services.ts +119 -0
  119. package/src/plugins/types.ts +278 -0
  120. package/src/reactivity/graph.ts +259 -0
  121. package/src/reactivity/index.ts +541 -0
  122. package/src/reactivity/selector.ts +78 -0
  123. package/src/reactivity/store.ts +252 -0
  124. package/src/server/action-flash.ts +158 -0
  125. package/src/server/action-protect.ts +199 -0
  126. package/src/server/action-registry.ts +78 -0
  127. package/src/server/action.ts +511 -0
  128. package/src/server/app-entry.ts +23 -0
  129. package/src/server/app-secret.ts +63 -0
  130. package/src/server/assets.ts +168 -0
  131. package/src/server/compress.ts +646 -0
  132. package/src/server/consent.ts +290 -0
  133. package/src/server/cookies.ts +153 -0
  134. package/src/server/document.ts +286 -0
  135. package/src/server/env.ts +47 -0
  136. package/src/server/errors.ts +396 -0
  137. package/src/server/form-schema.ts +337 -0
  138. package/src/server/health.ts +275 -0
  139. package/src/server/http.ts +588 -0
  140. package/src/server/index.ts +615 -0
  141. package/src/server/lifecycle.ts +75 -0
  142. package/src/server/loader.ts +131 -0
  143. package/src/server/page.ts +122 -0
  144. package/src/server/param-matchers.ts +112 -0
  145. package/src/server/plugin-host.ts +267 -0
  146. package/src/server/prerendered.ts +71 -0
  147. package/src/server/public.ts +20 -0
  148. package/src/server/rate-limit.ts +199 -0
  149. package/src/server/region.ts +96 -0
  150. package/src/server/request-body.ts +27 -0
  151. package/src/server/request-path.ts +73 -0
  152. package/src/server/request-scope.ts +22 -0
  153. package/src/server/route-cache.ts +541 -0
  154. package/src/server/route-table.ts +424 -0
  155. package/src/server/router.ts +261 -0
  156. package/src/server/routing.ts +388 -0
  157. package/src/server/sanitize-html.ts +309 -0
  158. package/src/server/security.ts +550 -0
  159. package/src/server/serve-handlers.ts +93 -0
  160. package/src/server/served-file.ts +128 -0
  161. package/src/server/standard-schema.ts +104 -0
  162. package/src/server/start.ts +143 -0
  163. package/src/server/static-dir.ts +246 -0
  164. package/src/server/stream.ts +233 -0
  165. package/src/ssr/consent-banner.ts +58 -0
  166. package/src/ssr/context.ts +399 -0
  167. package/src/ssr/index.ts +624 -0
  168. package/src/ssr/navigation.ts +96 -0
  169. package/src/ssr/region.ts +192 -0
  170. package/src/style.ts +96 -0
  171. package/src/test/browser.ts +201 -0
  172. package/src/test/index.ts +262 -0
  173. package/src/ts-plugin/diagnostics.ts +505 -0
  174. package/src/ts-plugin/fixes.ts +364 -0
  175. package/src/ts-plugin/index.ts +194 -0
  176. package/src/unrenderable.ts +35 -0
  177. package/templates/_shared/AGENTS.md +10 -0
  178. package/templates/_shared/CLAUDE.md +1 -0
  179. package/templates/_shared/gitignore +8 -0
  180. package/templates/_shared/mcp.json +11 -0
  181. package/templates/_shared/public/robots.txt +2 -0
  182. package/templates/_shared/src/routes/legal.tsx +22 -0
  183. package/templates/_shared/tsconfig.json +25 -0
  184. package/templates/app/.env.example +8 -0
  185. package/templates/app/package.json +22 -0
  186. package/templates/app/prunelle.config.ts +13 -0
  187. package/templates/app/src/auth.ts +12 -0
  188. package/templates/app/src/mail.ts +4 -0
  189. package/templates/app/src/routes/_layout.tsx +40 -0
  190. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  191. package/templates/app/src/routes/dashboard/index.tsx +47 -0
  192. package/templates/app/src/routes/index.tsx +15 -0
  193. package/templates/app/src/routes/login.tsx +53 -0
  194. package/templates/app/src/routes/privacy.tsx +25 -0
  195. package/templates/app/src/styles/app.css +14 -0
  196. package/templates/app/tests/app.test.ts +73 -0
  197. package/templates/blog/content/posts/welcome.md +9 -0
  198. package/templates/blog/package.json +22 -0
  199. package/templates/blog/prunelle.config.ts +23 -0
  200. package/templates/blog/src/posts.ts +26 -0
  201. package/templates/blog/src/routes/_layout.tsx +25 -0
  202. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  203. package/templates/blog/src/routes/index.tsx +33 -0
  204. package/templates/blog/src/styles/app.css +15 -0
  205. package/templates/fullstack/package.json +19 -0
  206. package/templates/fullstack/prunelle.config.ts +6 -0
  207. package/templates/fullstack/src/actions/greet.ts +19 -0
  208. package/templates/fullstack/src/components/Counter.tsx +10 -0
  209. package/templates/fullstack/src/components/GreetForm.tsx +28 -0
  210. package/templates/fullstack/src/routes/_layout.tsx +18 -0
  211. package/templates/fullstack/src/routes/api/health.ts +5 -0
  212. package/templates/fullstack/src/routes/index.tsx +13 -0
  213. package/templates/fullstack/src/styles/app.css +39 -0
  214. package/templates/fullstack/tests/app.test.ts +37 -0
  215. package/templates/minimal/package.json +19 -0
  216. package/templates/minimal/prunelle.config.ts +6 -0
  217. package/templates/minimal/src/components/Counter.tsx +10 -0
  218. package/templates/minimal/src/routes/_layout.tsx +18 -0
  219. package/templates/minimal/src/routes/index.tsx +11 -0
  220. package/templates/minimal/src/styles/app.css +32 -0
  221. package/ts-plugin.cjs +4277 -0
  222. package/README.md +0 -3
@@ -0,0 +1,541 @@
1
+ // Prunelle reactive kernel: push-pull doubly-linked-list dependency graph.
2
+ // Glitch-free lazy computed values, O(1) link/unlink, compact internal fields for tiny bundle size.
3
+
4
+ import type { DepNode, Link, Owned, Owner, ReadonlySignal, Signal, SubNode } from './graph'
5
+ import { adopt, linkSub, throwErrors, unadopt, unlinkAll, unlinkRemaining, unlinkSubs, unobserve } from './graph'
6
+
7
+ export type { IdScope, Owner, ReadonlySignal, Signal } from './graph'
8
+
9
+ const enum F {
10
+ CLEAN = 0,
11
+ CHECK = 1,
12
+ DIRTY = 2,
13
+ RUNNING = 4,
14
+ DISPOSED = 8,
15
+ COMPUTED = 16,
16
+ EFFECT = 32,
17
+ QUEUED = 64,
18
+ // A computed nothing subscribes to: it keeps its deps list but stays out of its sources' subscriber lists, checking versions on read.
19
+ UNOBSERVED = 128,
20
+ // Queued for the current flush round (distinct from QUEUED, which marks the next round).
21
+ PENDING = 256,
22
+ }
23
+
24
+ let currentSub: SubNode | undefined
25
+ let currentOwner: Owner | undefined
26
+ let globalEpoch = 0
27
+ let batchDepth = 0
28
+ let q: SubNode[] = []
29
+ let qNext: SubNode[] = []
30
+ 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
33
+ // Bumped after a flush that threw so observed computeds still left DIRTY/CHECK re-propagate downstream on recovery.
34
+ let propWave = 0
35
+
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)) {
46
+ try {
47
+ if (shouldRun(sub))
48
+ sub.r()
49
+ }
50
+ catch (err) {
51
+ if (!catchEffectError?.(sub, err))
52
+ errors.push(err)
53
+ }
54
+ }
55
+ }
56
+
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
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ // 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
+ function flush(): void {
88
+ if (flushing || batchDepth > 0)
89
+ return
90
+ flushing = true
91
+ const errors: unknown[] = []
92
+ try {
93
+ let rounds = 0
94
+ while (q.length > 0) {
95
+ if (++rounds > 1000) {
96
+ errors.push(new Error('prunelle: reactive cycle'))
97
+ break
98
+ }
99
+ const round = q
100
+ q = qNext
101
+ qNext = round
102
+ for (const node of round)
103
+ node.f = (node.f & ~F.QUEUED) | F.PENDING
104
+ for (const node of round) {
105
+ if (node.f & F.PENDING)
106
+ runParentFirst(node, errors)
107
+ }
108
+ round.length = 0
109
+ }
110
+ }
111
+ finally {
112
+ flushing = false
113
+ for (const node of q)
114
+ node.f &= ~F.QUEUED
115
+ q.length = 0
116
+ if (errors.length > 0)
117
+ propWave++
118
+ }
119
+ throwErrors(errors)
120
+ }
121
+
122
+ // Installed by the first <Catch>, so an island without one carries no owner walk.
123
+ let catchEffectError: ((node: Owned, err: unknown) => boolean) | undefined
124
+
125
+ export function onEffectError(fn: (node: Owned, err: unknown) => boolean): void {
126
+ catchEffectError = fn
127
+ }
128
+
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
154
+ }
155
+
156
+ class SignalNode<T> implements DepNode, Signal<T> {
157
+ v = 0
158
+ s: Link | undefined
159
+ st: Link | undefined
160
+ le = -1
161
+ f = 0
162
+ declare u?: (() => void) | undefined
163
+ _v: T
164
+
165
+ constructor(initial: T) {
166
+ this._v = initial
167
+ }
168
+
169
+ get value(): T {
170
+ if (currentSub)
171
+ linkSub(this, currentSub)
172
+ return this._v
173
+ }
174
+
175
+ set value(next: T) {
176
+ if (Object.is(this._v, next))
177
+ return
178
+ this._v = next
179
+ this.v++
180
+ writes++
181
+ if (this.s) {
182
+ propagate(this, F.DIRTY)
183
+ flush()
184
+ }
185
+ }
186
+
187
+ peek(): T {
188
+ return this._v
189
+ }
190
+ }
191
+
192
+ class ComputedNode<T> implements DepNode, SubNode, ReadonlySignal<T> {
193
+ v = 0
194
+ s: Link | undefined
195
+ st: Link | undefined
196
+ le = -1
197
+ f = F.COMPUTED | F.DIRTY | F.UNOBSERVED
198
+ 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
208
+ w = -1
209
+ pw = 0
210
+
211
+ constructor(fn: () => T, owner: Owner | undefined) {
212
+ this._f = fn
213
+ if (owner)
214
+ adopt(this, owner)
215
+ }
216
+
217
+ r(observe?: boolean): void {
218
+ if (this.f & F.DISPOSED)
219
+ return
220
+ if (this.f & F.RUNNING)
221
+ throw new Error('prunelle: cyclic computed')
222
+ const prevSub = currentSub
223
+ const prevOwner = currentOwner
224
+ const w = writes
225
+ // A first run for an observed reader links straight into its sources; roll back if the compute throws.
226
+ const direct = observe && !this.d
227
+ if (direct)
228
+ this.f &= ~F.UNOBSERVED
229
+ this.f = (this.f | F.RUNNING) & ~(F.DIRTY | F.CHECK)
230
+ this.e = ++globalEpoch
231
+ this.c = this.d
232
+ currentSub = this
233
+ currentOwner = undefined
234
+ let ok = false
235
+ try {
236
+ const next = (this._f)()
237
+ if (!this._i || !Object.is(this._v, next)) {
238
+ this._v = next
239
+ this._i = true
240
+ this.v++
241
+ }
242
+ this.w = w
243
+ ok = true
244
+ }
245
+ finally {
246
+ unlinkRemaining(this)
247
+ this.f &= ~F.RUNNING
248
+ if (!ok) {
249
+ this.f |= F.DIRTY
250
+ if (direct && !this.s)
251
+ unobserve(this)
252
+ }
253
+ currentSub = prevSub
254
+ currentOwner = prevOwner
255
+ }
256
+ }
257
+
258
+ get value(): T {
259
+ if (!(this.f & F.DISPOSED)) {
260
+ 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)
268
+ }
269
+ }
270
+ return this._v
271
+ }
272
+
273
+ peek(): T {
274
+ if (!(this.f & F.DISPOSED) && shouldRun(this))
275
+ this.r()
276
+ return this._v
277
+ }
278
+ }
279
+
280
+ // An effect is also the owner of what its run creates (effects, onCleanup), released before the next run.
281
+ class EffectNode implements SubNode, Owner {
282
+ f = F.EFFECT | F.DIRTY
283
+ 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)
293
+
294
+ constructor(fn: () => void | (() => void), owner: Owner | undefined) {
295
+ this._f = fn
296
+ if (owner)
297
+ adopt(this, owner)
298
+ }
299
+
300
+ r(): void {
301
+ if (this.f & F.DISPOSED)
302
+ return
303
+ this.f &= ~(F.DIRTY | F.CHECK)
304
+ releaseOwned(this)
305
+ if (this.f & F.DISPOSED)
306
+ return
307
+ const prevSub = currentSub
308
+ const prevOwner = currentOwner
309
+ this.f |= F.RUNNING
310
+ this.e = ++globalEpoch
311
+ this.c = this.d
312
+ currentSub = this
313
+ currentOwner = this
314
+ try {
315
+ const ret = (this._f)()
316
+ if (typeof ret === 'function') {
317
+ if (this.f & F.DISPOSED)
318
+ ret()
319
+ else
320
+ ;(this.cl ??= []).unshift(ret)
321
+ }
322
+ }
323
+ finally {
324
+ unlinkRemaining(this)
325
+ this.f &= ~F.RUNNING
326
+ currentSub = prevSub
327
+ currentOwner = prevOwner
328
+ }
329
+ }
330
+ }
331
+
332
+ export function createOwner(parent: Owner | undefined = currentOwner): Owner {
333
+ const owner: Owner = { f: 0 }
334
+ if (parent)
335
+ adopt(owner, parent)
336
+ return owner
337
+ }
338
+
339
+ // Disposes an owner, an effect or a computed: leaves its owner, drops its sources, releases what it owns, runs its cleanup.
340
+ export function disposeOwner(node: Owner): void {
341
+ const f = node.f
342
+ if (f & F.DISPOSED)
343
+ return
344
+ node.f |= F.DISPOSED
345
+ unadopt(node)
346
+ if (f & (F.COMPUTED | F.EFFECT))
347
+ unlinkAll(node as unknown as SubNode)
348
+ if (f & F.COMPUTED)
349
+ unlinkSubs(node as unknown as DepNode)
350
+ else
351
+ releaseOwned(node)
352
+ }
353
+
354
+ // 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.
355
+ function releaseOwned(owner: Owner): void {
356
+ let errors: unknown[] | undefined
357
+ let node: Owned | undefined
358
+ while ((node = owner.fs)) {
359
+ unadopt(node)
360
+ try {
361
+ disposeOwner(node as Owner)
362
+ }
363
+ catch (err) {
364
+ ;(errors ??= []).push(err)
365
+ }
366
+ }
367
+
368
+ const fns = owner.cl
369
+ if (fns) {
370
+ owner.cl = undefined
371
+ for (let i = fns.length - 1; i >= 0; i--) {
372
+ try {
373
+ fns[i]!()
374
+ }
375
+ catch (err) {
376
+ ;(errors ??= []).push(err)
377
+ }
378
+ }
379
+ }
380
+
381
+ throwErrors(errors)
382
+ }
383
+
384
+ export function runWithOwner<T>(owner: Owner | undefined, fn: () => T): T {
385
+ const prevOwner = currentOwner
386
+ const prevSub = currentSub
387
+ currentOwner = owner
388
+ currentSub = undefined
389
+ try {
390
+ return fn()
391
+ }
392
+ finally {
393
+ currentOwner = prevOwner
394
+ currentSub = prevSub
395
+ }
396
+ }
397
+
398
+ // Like runWithOwner, but the running computation still tracks what fn reads.
399
+ export function runOwned<T>(owner: Owner, fn: () => T): T {
400
+ const prev = currentOwner
401
+ currentOwner = owner
402
+ try {
403
+ return fn()
404
+ }
405
+ finally {
406
+ currentOwner = prev
407
+ }
408
+ }
409
+
410
+ export function getOwner(): Owner | undefined {
411
+ return currentOwner
412
+ }
413
+
414
+ export function isTracking(): boolean {
415
+ return currentSub !== undefined
416
+ }
417
+
418
+ /** Internal: true when the active subscriber will attach to any source it reads (observed and not disposed). */
419
+ export function isObserving(): boolean {
420
+ return !!currentSub && !(currentSub.f & (F.UNOBSERVED | F.DISPOSED))
421
+ }
422
+
423
+ /** Internal: total signal writes so far, used by selector() to detect un-flushed changes without importing computed. */
424
+ export function writeCount(): number {
425
+ return writes
426
+ }
427
+
428
+ /** Internal: signal that runs `onUnsub` and bumps its version when its last subscriber detaches. */
429
+ export function watchedSignal<T>(initial: T, onUnsub: () => void): Signal<T> {
430
+ const s = new SignalNode(initial)
431
+ s.u = () => {
432
+ s.v++
433
+ writes++
434
+ onUnsub()
435
+ }
436
+ return s
437
+ }
438
+
439
+ export function root<T>(fn: (dispose: () => void) => T): T {
440
+ const owner = createOwner(undefined)
441
+ return runWithOwner(owner, () => fn(() => disposeOwner(owner)))
442
+ }
443
+
444
+ export function signal<T>(initial: T): Signal<T> {
445
+ return new SignalNode(initial)
446
+ }
447
+
448
+ export function computed<T>(fn: () => T): ReadonlySignal<T> {
449
+ return new ComputedNode(fn, currentOwner)
450
+ }
451
+
452
+ declare const PRUNELLE_BROWSER: boolean | undefined
453
+
454
+ let serverRenders = 0
455
+
456
+ /** Internal: runs an island's server render, during which `effect` and `onMount` do nothing. */
457
+ export function renderOnServer<T>(fn: () => T): T {
458
+ serverRenders++
459
+ try {
460
+ return fn()
461
+ }
462
+ finally {
463
+ serverRenders--
464
+ }
465
+ }
466
+
467
+ function noop(): void {}
468
+
469
+ // 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.
470
+ export function effect(fn: () => void | (() => void)): () => void {
471
+ if ((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && serverRenders > 0)
472
+ return noop
473
+ if (process.env.NODE_ENV !== 'production' && !currentOwner && currentSub && (currentSub.f & F.COMPUTED))
474
+ console.warn('prunelle: effect() created while a computed runs has no owner and is never disposed. Create it in a component or an effect.')
475
+ const node = new EffectNode(fn, currentOwner)
476
+ node.r()
477
+ return () => disposeOwner(node)
478
+ }
479
+
480
+ // An error thrown by fn wins over one thrown by the effects it triggered, which is logged instead.
481
+ export function batch<T>(fn: () => T): T {
482
+ batchDepth++
483
+ let result: T
484
+ try {
485
+ result = fn()
486
+ }
487
+ catch (err) {
488
+ if (--batchDepth === 0) {
489
+ try {
490
+ flush()
491
+ }
492
+ catch (flushErr) {
493
+ console.error(flushErr)
494
+ }
495
+ }
496
+ throw err
497
+ }
498
+ if (--batchDepth === 0)
499
+ flush()
500
+ return result
501
+ }
502
+
503
+ export function untrack<T>(fn: () => T): T {
504
+ const prev = currentSub
505
+ currentSub = undefined
506
+ try {
507
+ return fn()
508
+ }
509
+ finally {
510
+ currentSub = prev
511
+ }
512
+ }
513
+
514
+ export function onCleanup(fn: () => void): void {
515
+ if (!currentOwner) {
516
+ if (process.env.NODE_ENV !== 'production')
517
+ console.warn('prunelle: onCleanup() called outside a component or effect never runs.')
518
+ return
519
+ }
520
+ if (currentOwner.f & F.DISPOSED) {
521
+ fn()
522
+ return
523
+ }
524
+ ;(currentOwner.cl ??= []).push(fn)
525
+ }
526
+
527
+ // A render creates its nodes and inserts them in the same task, so a microtask later they are in the document.
528
+ export function onMount(fn: () => void | (() => void)): void {
529
+ if (((typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER) && serverRenders > 0) || typeof document === 'undefined')
530
+ return
531
+ const owner = currentOwner
532
+ queueMicrotask(() => {
533
+ if (owner && owner.f & F.DISPOSED)
534
+ return
535
+ runWithOwner(owner, () => {
536
+ const cleanup = fn()
537
+ if (typeof cleanup === 'function')
538
+ onCleanup(cleanup)
539
+ })
540
+ })
541
+ }
@@ -0,0 +1,78 @@
1
+ // O(1) row selection primitive: zero WeakSet overhead per row.
2
+
3
+ import type { Signal } from './index'
4
+ import { effect, isObserving, isTracking, untrack, watchedSignal, writeCount } from './index'
5
+
6
+ export interface Selector<K> {
7
+ is(key: K): boolean
8
+ }
9
+
10
+ export function selector<K>(source: () => K): Selector<K> {
11
+ const subs = new Map<K, Signal<boolean>>()
12
+ let current: K | undefined
13
+ let active = false
14
+ let checked = -1
15
+ let latest: K | undefined
16
+ let pending: Set<K> | undefined
17
+
18
+ const readLatest = (): K => {
19
+ const w = writeCount()
20
+ if (checked !== w) {
21
+ latest = untrack(source)
22
+ checked = w
23
+ }
24
+ return latest as K
25
+ }
26
+
27
+ effect(() => {
28
+ const next = source()
29
+ latest = next
30
+ checked = writeCount()
31
+ if (pending !== undefined) {
32
+ for (const k of pending) {
33
+ const s = subs.get(k)
34
+ if (s !== undefined)
35
+ s.value = Object.is(next, k)
36
+ }
37
+ pending = undefined
38
+ }
39
+ if (active && Object.is(current, next))
40
+ return
41
+
42
+ if (active) {
43
+ const prev = subs.get(current as K)
44
+ if (prev !== undefined)
45
+ prev.value = false
46
+ }
47
+
48
+ active = true
49
+ current = next
50
+ const nextSub = subs.get(next)
51
+ if (nextSub !== undefined)
52
+ nextSub.value = true
53
+ })
54
+
55
+ return {
56
+ is(key: K): boolean {
57
+ if (!active)
58
+ return Object.is(source(), key)
59
+ if (!isObserving())
60
+ return Object.is(isTracking() ? source() : readLatest(), key)
61
+
62
+ const val = readLatest()
63
+ const matched = Object.is(val, key)
64
+ let sub = subs.get(key)
65
+ if (sub === undefined) {
66
+ sub = watchedSignal(matched, () => {
67
+ if (subs.get(key) === sub)
68
+ subs.delete(key)
69
+ })
70
+ subs.set(key, sub)
71
+ if (!Object.is(current, val))
72
+ (pending ??= new Set()).add(key)
73
+ }
74
+ sub.value
75
+ return matched
76
+ },
77
+ }
78
+ }