@playfast/reform 0.0.4 → 0.0.7

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/dist/boundary/boundary.d.ts +72 -0
  2. package/dist/boundary/boundary.d.ts.map +1 -0
  3. package/dist/boundary/boundary.js +83 -0
  4. package/dist/boundary/boundary.js.map +1 -0
  5. package/dist/calc/asyncCalc.d.ts +91 -0
  6. package/dist/calc/asyncCalc.d.ts.map +1 -0
  7. package/dist/calc/asyncCalc.js +95 -0
  8. package/dist/calc/asyncCalc.js.map +1 -0
  9. package/{src/calc/asyncData.ts → dist/calc/asyncData.d.ts} +22 -53
  10. package/dist/calc/asyncData.d.ts.map +1 -0
  11. package/dist/calc/asyncData.js +34 -0
  12. package/dist/calc/asyncData.js.map +1 -0
  13. package/dist/calc/calc.d.ts +57 -0
  14. package/dist/calc/calc.d.ts.map +1 -0
  15. package/dist/calc/calc.js +58 -0
  16. package/dist/calc/calc.js.map +1 -0
  17. package/dist/calc/calcFamily.d.ts +57 -0
  18. package/dist/calc/calcFamily.d.ts.map +1 -0
  19. package/dist/calc/calcFamily.js +124 -0
  20. package/dist/calc/calcFamily.js.map +1 -0
  21. package/dist/calc/compose.d.ts +16 -0
  22. package/dist/calc/compose.d.ts.map +1 -0
  23. package/dist/calc/compose.js +7 -0
  24. package/dist/calc/compose.js.map +1 -0
  25. package/dist/channel/channel.d.ts +115 -0
  26. package/dist/channel/channel.d.ts.map +1 -0
  27. package/dist/channel/channel.js +138 -0
  28. package/dist/channel/channel.js.map +1 -0
  29. package/dist/compose/composition.d.ts +72 -0
  30. package/dist/compose/composition.d.ts.map +1 -0
  31. package/dist/compose/composition.js +46 -0
  32. package/dist/compose/composition.js.map +1 -0
  33. package/{src/compose/host.ts → dist/compose/host.d.ts} +8 -6
  34. package/dist/compose/host.d.ts.map +1 -0
  35. package/dist/compose/host.js +6 -0
  36. package/dist/compose/host.js.map +1 -0
  37. package/dist/compose/props.d.ts +13 -0
  38. package/dist/compose/props.d.ts.map +1 -0
  39. package/{src/compose/props.ts → dist/compose/props.js} +5 -3
  40. package/dist/compose/props.js.map +1 -0
  41. package/dist/compose/provide.d.ts +22 -0
  42. package/dist/compose/provide.d.ts.map +1 -0
  43. package/dist/compose/provide.js +28 -0
  44. package/dist/compose/provide.js.map +1 -0
  45. package/{src/compose/slot.ts → dist/compose/slot.d.ts} +19 -36
  46. package/dist/compose/slot.d.ts.map +1 -0
  47. package/dist/compose/slot.js +23 -0
  48. package/dist/compose/slot.js.map +1 -0
  49. package/dist/compose/ui.d.ts +135 -0
  50. package/dist/compose/ui.d.ts.map +1 -0
  51. package/dist/compose/ui.js +63 -0
  52. package/dist/compose/ui.js.map +1 -0
  53. package/dist/definition/definition.d.ts +33 -0
  54. package/dist/definition/definition.d.ts.map +1 -0
  55. package/dist/definition/definition.js +42 -0
  56. package/dist/definition/definition.js.map +1 -0
  57. package/dist/event/event.d.ts +33 -0
  58. package/dist/event/event.d.ts.map +1 -0
  59. package/dist/event/event.js +30 -0
  60. package/dist/event/event.js.map +1 -0
  61. package/dist/event/eventGroup.d.ts +9 -0
  62. package/dist/event/eventGroup.d.ts.map +1 -0
  63. package/dist/event/eventGroup.js +4 -0
  64. package/dist/event/eventGroup.js.map +1 -0
  65. package/{src/feature/feature.ts → dist/feature/feature.d.ts} +92 -178
  66. package/dist/feature/feature.d.ts.map +1 -0
  67. package/dist/feature/feature.js +98 -0
  68. package/dist/feature/feature.js.map +1 -0
  69. package/dist/index.d.ts +53 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +62 -0
  72. package/dist/index.js.map +1 -0
  73. package/{src/internal/capture.ts → dist/internal/capture.d.ts} +11 -13
  74. package/dist/internal/capture.d.ts.map +1 -0
  75. package/dist/internal/capture.js +12 -0
  76. package/dist/internal/capture.js.map +1 -0
  77. package/{src/internal/ctx.ts → dist/internal/ctx.d.ts} +4 -8
  78. package/dist/internal/ctx.d.ts.map +1 -0
  79. package/dist/internal/ctx.js +2 -0
  80. package/dist/internal/ctx.js.map +1 -0
  81. package/dist/internal/errors.d.ts +89 -0
  82. package/dist/internal/errors.d.ts.map +1 -0
  83. package/dist/internal/errors.js +102 -0
  84. package/dist/internal/errors.js.map +1 -0
  85. package/dist/internal/inspect.d.ts +17 -0
  86. package/dist/internal/inspect.d.ts.map +1 -0
  87. package/dist/internal/inspect.js +32 -0
  88. package/dist/internal/inspect.js.map +1 -0
  89. package/dist/internal/queryDriver.d.ts +65 -0
  90. package/dist/internal/queryDriver.d.ts.map +1 -0
  91. package/dist/internal/queryDriver.js +134 -0
  92. package/dist/internal/queryDriver.js.map +1 -0
  93. package/dist/internal/reuse.d.ts +10 -0
  94. package/dist/internal/reuse.d.ts.map +1 -0
  95. package/dist/internal/reuse.js +68 -0
  96. package/dist/internal/reuse.js.map +1 -0
  97. package/dist/internal/scheduler.d.ts +47 -0
  98. package/dist/internal/scheduler.d.ts.map +1 -0
  99. package/dist/internal/scheduler.js +70 -0
  100. package/dist/internal/scheduler.js.map +1 -0
  101. package/{src/internal/seeds.ts → dist/internal/seeds.d.ts} +3 -6
  102. package/dist/internal/seeds.d.ts.map +1 -0
  103. package/dist/internal/seeds.js +17 -0
  104. package/dist/internal/seeds.js.map +1 -0
  105. package/dist/internal/sources.d.ts +39 -0
  106. package/dist/internal/sources.d.ts.map +1 -0
  107. package/dist/internal/sources.js +59 -0
  108. package/dist/internal/sources.js.map +1 -0
  109. package/dist/internal/stateRegistry.d.ts +2 -0
  110. package/dist/internal/stateRegistry.d.ts.map +1 -0
  111. package/dist/internal/stateRegistry.js +28 -0
  112. package/dist/internal/stateRegistry.js.map +1 -0
  113. package/dist/internal/store.d.ts +47 -0
  114. package/dist/internal/store.d.ts.map +1 -0
  115. package/dist/internal/store.js +73 -0
  116. package/dist/internal/store.js.map +1 -0
  117. package/{src/internal/track.ts → dist/internal/track.d.ts} +11 -15
  118. package/dist/internal/track.d.ts.map +1 -0
  119. package/dist/internal/track.js +19 -0
  120. package/dist/internal/track.js.map +1 -0
  121. package/dist/procedure/procedure.d.ts +40 -0
  122. package/dist/procedure/procedure.d.ts.map +1 -0
  123. package/dist/procedure/procedure.js +50 -0
  124. package/dist/procedure/procedure.js.map +1 -0
  125. package/dist/reducer/reducer.d.ts +44 -0
  126. package/dist/reducer/reducer.d.ts.map +1 -0
  127. package/dist/reducer/reducer.js +63 -0
  128. package/dist/reducer/reducer.js.map +1 -0
  129. package/dist/remote/remoteState.d.ts +119 -0
  130. package/dist/remote/remoteState.d.ts.map +1 -0
  131. package/dist/remote/remoteState.js +270 -0
  132. package/dist/remote/remoteState.js.map +1 -0
  133. package/dist/runtime/bus.d.ts +27 -0
  134. package/dist/runtime/bus.d.ts.map +1 -0
  135. package/{src/runtime/bus.ts → dist/runtime/bus.js} +8 -23
  136. package/dist/runtime/bus.js.map +1 -0
  137. package/dist/runtime/loop.d.ts +45 -0
  138. package/dist/runtime/loop.d.ts.map +1 -0
  139. package/dist/runtime/loop.js +117 -0
  140. package/dist/runtime/loop.js.map +1 -0
  141. package/dist/scene/scene.d.ts +44 -0
  142. package/dist/scene/scene.d.ts.map +1 -0
  143. package/dist/scene/scene.js +31 -0
  144. package/dist/scene/scene.js.map +1 -0
  145. package/dist/state/state.d.ts +37 -0
  146. package/dist/state/state.d.ts.map +1 -0
  147. package/dist/state/state.js +46 -0
  148. package/dist/state/state.js.map +1 -0
  149. package/dist/state/stateFamily.d.ts +79 -0
  150. package/dist/state/stateFamily.d.ts.map +1 -0
  151. package/dist/state/stateFamily.js +99 -0
  152. package/dist/state/stateFamily.js.map +1 -0
  153. package/dist/state/stateGroup.d.ts +50 -0
  154. package/dist/state/stateGroup.d.ts.map +1 -0
  155. package/dist/state/stateGroup.js +54 -0
  156. package/dist/state/stateGroup.js.map +1 -0
  157. package/{src/state/token.ts → dist/state/token.d.ts} +13 -21
  158. package/dist/state/token.d.ts.map +1 -0
  159. package/dist/state/token.js +20 -0
  160. package/dist/state/token.js.map +1 -0
  161. package/dist/synced/syncedStore.d.ts +51 -0
  162. package/dist/synced/syncedStore.d.ts.map +1 -0
  163. package/dist/synced/syncedStore.js +43 -0
  164. package/dist/synced/syncedStore.js.map +1 -0
  165. package/{src/ui/node.ts → dist/ui/node.d.ts} +3 -3
  166. package/dist/ui/node.d.ts.map +1 -0
  167. package/dist/ui/node.js +2 -0
  168. package/dist/ui/node.js.map +1 -0
  169. package/{src/ui/trigger.ts → dist/ui/trigger.d.ts} +2 -1
  170. package/dist/ui/trigger.d.ts.map +1 -0
  171. package/dist/ui/trigger.js +2 -0
  172. package/dist/ui/trigger.js.map +1 -0
  173. package/dist/wire/tree.d.ts +66 -0
  174. package/dist/wire/tree.d.ts.map +1 -0
  175. package/dist/wire/tree.js +60 -0
  176. package/dist/wire/tree.js.map +1 -0
  177. package/dist/wire/triggers.d.ts +44 -0
  178. package/dist/wire/triggers.d.ts.map +1 -0
  179. package/dist/wire/triggers.js +31 -0
  180. package/dist/wire/triggers.js.map +1 -0
  181. package/package.json +32 -11
  182. package/src/boundary/boundary.ts +0 -192
  183. package/src/calc/asyncCalc.ts +0 -267
  184. package/src/calc/calc.ts +0 -120
  185. package/src/calc/calcFamily.ts +0 -194
  186. package/src/calc/compose.ts +0 -69
  187. package/src/channel/channel.ts +0 -265
  188. package/src/compose/composition.ts +0 -112
  189. package/src/compose/provide.ts +0 -70
  190. package/src/compose/ui.ts +0 -97
  191. package/src/definition/definition.ts +0 -74
  192. package/src/event/event.ts +0 -65
  193. package/src/event/eventGroup.ts +0 -14
  194. package/src/index.ts +0 -140
  195. package/src/internal/errors.ts +0 -69
  196. package/src/internal/inspect.ts +0 -34
  197. package/src/internal/queryDriver.ts +0 -247
  198. package/src/internal/reuse.ts +0 -73
  199. package/src/internal/scheduler.ts +0 -91
  200. package/src/internal/sources.ts +0 -104
  201. package/src/internal/store.ts +0 -105
  202. package/src/procedure/procedure.ts +0 -89
  203. package/src/reducer/reducer.ts +0 -137
  204. package/src/remote/remoteState.ts +0 -571
  205. package/src/runtime/loop.ts +0 -169
  206. package/src/scene/scene.ts +0 -76
  207. package/src/state/state.ts +0 -91
  208. package/src/state/stateFamily.ts +0 -171
  209. package/src/state/stateGroup.ts +0 -78
@@ -1,91 +0,0 @@
1
- import { Context, Effect, Layer, MutableRef, Option } from 'effect'
2
-
3
- /**
4
- * Coalesces store notifications into a single microtask flush so a batch of
5
- * writes wakes each subscriber at most once (RESEARCH #3). React owns frame
6
- * timing; we only collapse the notifications.
7
- *
8
- * One scheduler is shared by every store in a runtime, so writes across
9
- * different stores in the same tick batch into one flush. The instance is
10
- * resolved per store at creation: `Notifications` when a runtime provides it
11
- * (per-runtime isolation — see `resolveScheduler`), else the process-wide
12
- * `defaultScheduler`.
13
- */
14
- export interface Scheduler {
15
- readonly schedule: (listeners: Iterable<() => void>) => void
16
- }
17
-
18
- export const makeScheduler = (): Scheduler => {
19
- const pending = new Set<() => void>()
20
- const armed = MutableRef.make(false)
21
-
22
- const flush = () => {
23
- MutableRef.set(armed, false)
24
- // Drain to a fixpoint within ONE microtask. A derived store's `onChange`
25
- // re-schedules during the pass (it is itself a listener of its upstream), so
26
- // resolving the whole dependency graph here — instead of re-arming a fresh
27
- // microtask per layer — collapses a depth-N propagation into a single flush
28
- // and wakes each leaf subscriber once at its final value. Converges because
29
- // a derived store schedules only when its output actually moves (Equal).
30
- while (pending.size > 0) {
31
- const due = [...pending]
32
- pending.clear()
33
- for (const listener of due) {
34
- // One listener's throw (a defect in a user calc body) must not abandon
35
- // the rest of the flush; surface it to the host's global handler on a
36
- // fresh microtask instead of unwinding this loop.
37
- try {
38
- listener()
39
- } catch (error) {
40
- queueMicrotask(() => {
41
- throw error
42
- })
43
- }
44
- }
45
- }
46
- }
47
-
48
- const schedule = (listeners: Iterable<() => void>) => {
49
- for (const listener of listeners) pending.add(listener)
50
- if (pending.size > 0 && !MutableRef.get(armed)) {
51
- MutableRef.set(armed, true)
52
- queueMicrotask(flush)
53
- }
54
- }
55
-
56
- return { schedule }
57
- }
58
-
59
- /**
60
- * The process-wide scheduler, used by any store created outside a runtime that
61
- * provides its own `Notifications`. Fine for a single client app (microtask
62
- * ordering is global anyway); a runtime that needs isolation — concurrent SSR,
63
- * multiple mounted roots — provides `Notifications` upstream of its stores.
64
- */
65
- export const defaultScheduler: Scheduler = makeScheduler()
66
-
67
- /**
68
- * Optional per-runtime scheduler. When a runtime provides it upstream of its
69
- * state/calc layers, those stores coalesce on it instead of the global default,
70
- * isolating their notification timing from other runtimes in the same process.
71
- */
72
- export class Notifications extends Context.Tag('reform/Notifications')<Notifications, Scheduler>() {}
73
-
74
- /**
75
- * A fresh per-runtime scheduler. Merged into `Engine`, so a runtime that wires
76
- * its state/calc layers *downstream* of `Engine` gets isolated notification
77
- * timing; the common sibling wiring falls back to `defaultScheduler` (still
78
- * correct, just process-shared).
79
- */
80
- export const notificationsLayer: Layer.Layer<Notifications> = Layer.sync(Notifications, makeScheduler)
81
-
82
- /**
83
- * Resolve the scheduler a store should use: the runtime's `Notifications` if one
84
- * is in context, else the global default. Uses `serviceOption` so it imposes no
85
- * hard requirement — a store layer wired as a sibling of the runtime (the common
86
- * pattern) simply falls back to the default.
87
- */
88
- export const resolveScheduler: Effect.Effect<Scheduler> = Effect.map(
89
- Effect.serviceOption(Notifications),
90
- Option.getOrElse(() => defaultScheduler),
91
- )
@@ -1,104 +0,0 @@
1
- import { type Context, Effect, Equal, Predicate } from 'effect'
2
- import { type AnySource, type SourceName, type SourceValue } from '../state/token'
3
- import { type Store } from './store'
4
-
5
- // The reactive plumbing shared by `Calc` and `AsyncCalc`: read a list of input
6
- // `Source`s, snapshot them by name, project a comparable invalidation key, and
7
- // subscribe to changes. Both derived primitives sense their inputs the same way
8
- // and differ only in what they do with a change (recompute vs. re-run a query).
9
-
10
- /** The decoded inputs handed to a calc body, keyed by each source's name. */
11
- export type InputsObject<Inputs extends ReadonlyArray<AnySource>> = {
12
- readonly [S in Inputs[number] as SourceName<S>]: SourceValue<S>
13
- }
14
-
15
- /**
16
- * The store services a list of store-carriers requires, as a *union of distinct*
17
- * stores (each carrier has its own backing tag), not a single store of a union.
18
- * Structural over the `store` tag, so it serves both calc inputs (`Source`s) and
19
- * state-group members (`StateClass`es) — anything carrying a `store` tag.
20
- */
21
- export type StoresOf<Items extends ReadonlyArray<unknown>> = {
22
- [I in keyof Items]: Items[I] extends { readonly store: Context.Tag<infer Svc, any> } ? Svc : never
23
- }[number]
24
-
25
- /** The store requirement a calc's inputs impose. */
26
- export type InputStores<Inputs extends ReadonlyArray<AnySource>> = StoresOf<Inputs>
27
-
28
- /** Project inputs into a comparable key; the calc reacts only when it moves. */
29
- export type InvalidateBy<Inputs extends ReadonlyArray<AnySource>> = (
30
- inputs: InputsObject<Inputs>,
31
- ) => ReadonlyArray<unknown>
32
-
33
- /**
34
- * The runtime key an input contributes to the inputs object. Prefer the
35
- * explicit `manifest.name` — the string handed to `make` — over the structural
36
- * `name`: a `Calc`/`AsyncCalc` *class* satisfies `Source` through its static
37
- * `name`, and a user subclass declaration (`class Feed extends Calc.make(…)`)
38
- * defines its OWN `name` from the class binding, shadowing the explicit one.
39
- * That binding is what minifiers rename, so keying the snapshot by `input.name`
40
- * made every subclassed calc input `undefined` in production builds (and would
41
- * silently mis-key in dev whenever the binding differs from the `make` name —
42
- * the type-level key, `SourceName<S>`, is always the `make` string). The
43
- * manifest is inherited through the static prototype chain and never shadowed.
44
- * `StateToken`s carry no manifest; their `name` is an instance field holding
45
- * the explicit member key, so the fallback is always the declared string.
46
- */
47
- const inputKey = (input: AnySource): string =>
48
- Predicate.hasProperty(input, 'manifest') &&
49
- Predicate.isRecord(input.manifest) &&
50
- Predicate.isString(input.manifest.name)
51
- ? input.manifest.name
52
- : input.name
53
-
54
- /** Element-wise value equality of two invalidation keys. */
55
- export const sameKey = (a: ReadonlyArray<unknown>, b: ReadonlyArray<unknown>): boolean =>
56
- a.length === b.length && a.every((value, i) => Equal.equals(value, b[i]))
57
-
58
- /** The sensing surface both `Calc.live` and `AsyncCalc.live` build on. */
59
- export interface WiredSources<Inputs extends ReadonlyArray<AnySource>> {
60
- /** Snapshot every input by name — the decoded inputs object a calc body sees. */
61
- readonly snapshot: () => InputsObject<Inputs>
62
- /** The invalidation key for an inputs object: the projection, or all input values. */
63
- readonly keyOf: (inputs: InputsObject<Inputs>) => ReadonlyArray<unknown>
64
- /** Subscribe to every source at once; the returned thunk unsubscribes from all. */
65
- readonly subscribe: (listener: () => void) => () => void
66
- }
67
-
68
- /**
69
- * Read each input's backing store and return the shared sensing surface. The
70
- * caller owns lifecycle: register the `subscribe` result's unsubscribe as a
71
- * scope finalizer (both live bodies run under `Layer.scoped`).
72
- */
73
- export const wireSources = <Inputs extends ReadonlyArray<AnySource>>(
74
- inputs: Inputs,
75
- invalidateBy?: InvalidateBy<Inputs>,
76
- ): Effect.Effect<WiredSources<Inputs>, never, InputStores<Inputs>> =>
77
- Effect.gen(function* () {
78
- const sources: Array<Store<unknown>> = []
79
- for (const input of inputs) sources.push(yield* input.store)
80
-
81
- const snapshot = (): InputsObject<Inputs> => {
82
- const args: Record<string, unknown> = {}
83
- inputs.forEach((input, i) => {
84
- // `noUncheckedIndexedAccess`: `sources` is built 1:1 with `inputs`, but
85
- // guard the index so the read is honestly total.
86
- const source = sources[i]
87
- if (source !== undefined) args[inputKey(input)] = source.getSnapshot()
88
- })
89
- return args as InputsObject<Inputs>
90
- }
91
- const keyOf = (args: InputsObject<Inputs>): ReadonlyArray<unknown> =>
92
- invalidateBy ? invalidateBy(args) : Object.values(args)
93
- const subscribe = (listener: () => void): (() => void) => {
94
- const unsubscribes = sources.map((source) => source.subscribe(listener))
95
- return () => {
96
- for (const unsubscribe of unsubscribes) unsubscribe()
97
- }
98
- }
99
- return { snapshot, keyOf, subscribe }
100
- // Yielding each `input.store` (a `Context.Tag<Store<unknown>>`) widens the
101
- // inferred requirement to `Store<unknown>`; restate the precise per-input
102
- // `InputStores<Inputs>` union the signature promises. This is the single
103
- // place that cast lives — `Calc.live`/`AsyncCalc.live` inherit it.
104
- }) as Effect.Effect<WiredSources<Inputs>, never, InputStores<Inputs>>
@@ -1,105 +0,0 @@
1
- import { Equal, MutableRef } from 'effect'
2
- import { type Inspectable, inspectable } from './inspect'
3
- import { defaultScheduler, type Scheduler } from './scheduler'
4
-
5
- /**
6
- * A reactive cell living outside Effect. The single source of truth for one
7
- * slice of state. `set` is internal: only the reduce loop ever holds a Store
8
- * (reducers return values, the loop writes them), which is how "state changes
9
- * only in the reduce loop" is enforced structurally — there is no public writer.
10
- *
11
- * This is the synchronous, React-facing analog of Effect's `SubscriptionRef`
12
- * (a `Ref` + `PubSub` + a `changes` `Stream`). We don't use `SubscriptionRef`
13
- * directly because `useSyncExternalStore` requires a *synchronous* `getSnapshot`
14
- * and a *synchronous* `subscribe` returning an unsubscribe thunk, whereas
15
- * `SubscriptionRef.get` is an `Effect` and its `changes` is a `Stream` (a fiber
16
- * per subscription). Mutable state is held in a `MutableRef` (Effect's mutable
17
- * cell), so there is no reassigned binding.
18
- *
19
- * `getSnapshot` + `subscribe` are the stable pair the React bridge binds to;
20
- * `getSnapshot` returns the same reference until the value actually changes.
21
- * `getVersion` returns a monotonic counter bumped on every real change — the
22
- * host sums it across a render's tracked stores for a `useSyncExternalStore`
23
- * snapshot, which closes the tear/dropped-update window a value snapshot can't.
24
- */
25
- export interface Store<in out A> extends Inspectable {
26
- get(): A
27
- set(value: A): void
28
- getSnapshot(): A
29
- getVersion(): number
30
- subscribe(listener: () => void): () => void
31
- }
32
-
33
- export const makeStore = <A>(initial: A, scheduler: Scheduler = defaultScheduler): Store<A> => {
34
- // The store is a mutable reactive slot by design, so its value lives in a
35
- // `MutableRef` we update in place (no reassigned binding).
36
- const value = MutableRef.make(initial)
37
- const version = MutableRef.make(0)
38
- const listeners = new Set<() => void>()
39
- return {
40
- get: () => MutableRef.get(value),
41
- getSnapshot: () => MutableRef.get(value),
42
- getVersion: () => MutableRef.get(version),
43
- set: (next) => {
44
- if (Equal.equals(MutableRef.get(value), next)) return
45
- MutableRef.set(value, next)
46
- MutableRef.update(version, (n) => n + 1)
47
- scheduler.schedule(listeners)
48
- },
49
- subscribe: (listener) => {
50
- listeners.add(listener)
51
- return () => {
52
- listeners.delete(listener)
53
- }
54
- },
55
- ...inspectable(() => ({ _id: 'reform/Store', value: MutableRef.get(value) })),
56
- }
57
- }
58
-
59
- /**
60
- * A read-only `Store` whose value is computed from upstream sources.
61
- *
62
- * Reads are pull-fresh: `get`/`getSnapshot` call `compute` directly, so a
63
- * headless snapshot always reflects the current source state without waiting for
64
- * a notification (`compute` must memoize so an unchanged read returns a stable
65
- * reference — `useSyncExternalStore` requires it). *Notifications*, by contrast,
66
- * run through the same coalescing scheduler as `makeStore`: an upstream change
67
- * recomputes once and wakes subscribers only when the output actually moves (by
68
- * `Equal.equals`), so every store kind shares one notification timing. `set` is
69
- * a no-op — the only writer is the upstream subscription.
70
- */
71
- export const makeDerivedStore = <A>(
72
- compute: () => A,
73
- subscribeUpstream: (onChange: () => void) => () => void,
74
- scheduler: Scheduler = defaultScheduler,
75
- ): { readonly store: Store<A>; readonly unsubscribe: () => void } => {
76
- const listeners = new Set<() => void>()
77
- const version = MutableRef.make(0)
78
- // The last output we notified against — tracked only here, never touched by
79
- // reads, so a `get` racing ahead of `onChange` can't suppress a notification.
80
- const seen = MutableRef.make<{ readonly value: A } | undefined>({ value: compute() })
81
- const onChange = () => {
82
- const previous = MutableRef.get(seen)
83
- const next = compute()
84
- MutableRef.set(seen, { value: next })
85
- if (previous === undefined || !Equal.equals(previous.value, next)) {
86
- MutableRef.update(version, (n) => n + 1)
87
- scheduler.schedule(listeners)
88
- }
89
- }
90
- const unsubscribe = subscribeUpstream(onChange)
91
- const store: Store<A> = {
92
- get: compute,
93
- getSnapshot: compute,
94
- getVersion: () => MutableRef.get(version),
95
- set: () => {},
96
- subscribe: (listener) => {
97
- listeners.add(listener)
98
- return () => {
99
- listeners.delete(listener)
100
- }
101
- },
102
- ...inspectable(() => ({ _id: 'reform/Store', value: compute() })),
103
- }
104
- return { store, unsubscribe }
105
- }
@@ -1,89 +0,0 @@
1
- import { Effect, Layer } from 'effect'
2
- import type { YieldWrap } from 'effect/Utils'
3
- import { type ChannelClass, type ProcedureEntry, Procedures } from '../channel/channel'
4
- import { type Manifest, definitionClass } from '../definition/definition'
5
- import { type AnyEvent, type EventType } from '../event/event'
6
- import { type Ctx } from '../internal/ctx'
7
- import { Bus, narrowHandled } from '../runtime/bus'
8
-
9
- /** The reflectable descriptor a procedure carries. */
10
- export interface ProcedureManifest extends Manifest {
11
- readonly kind: 'Procedure'
12
- }
13
-
14
- export interface ProcedureClass<Events extends ReadonlyArray<AnyEvent>> {
15
- new (): {}
16
- readonly manifest: ProcedureManifest
17
- /** The trigger events (marker data; read by `Procedure.live`). */
18
- readonly events: Events
19
- /** The channel this procedure's runs are scheduled on (marker data). */
20
- readonly channel: ChannelClass
21
- }
22
-
23
- export interface ProcedureConfig<Events extends ReadonlyArray<AnyEvent>> {
24
- readonly events: Events
25
- /** The scheduling lane. Replaces the old `concurrency` option. */
26
- readonly channel: ChannelClass
27
- }
28
-
29
- export type AnyProcedure = ProcedureClass<ReadonlyArray<AnyEvent>>
30
-
31
- /**
32
- * A cross-event-loop flow. The *definition* declares its trigger events and the
33
- * channel it runs on; the *implementation* is the effectful body. Procedures no
34
- * longer subscribe to the bus themselves — the single drain loop routes events
35
- * to the channel, which runs the registered bodies under its policy.
36
- */
37
- export const make = <const Events extends ReadonlyArray<AnyEvent>>(
38
- name: string,
39
- config: ProcedureConfig<Events>,
40
- ): ProcedureClass<Events> =>
41
- definitionClass<ProcedureClass<Events>>({
42
- manifest: { kind: 'Procedure' as const, name } satisfies ProcedureManifest,
43
- events: config.events,
44
- channel: config.channel,
45
- })
46
-
47
- /**
48
- * Register the flow body into its channel. The body may read state and dispatch
49
- * events, but never writes state (the core invariant); its context requirements
50
- * (RPC clients, etc.) are captured here so the channel fiber can run it with no
51
- * outstanding R.
52
- */
53
- export const live = <
54
- Events extends ReadonlyArray<AnyEvent>,
55
- Eff extends YieldWrap<Effect.Effect<any, any, any>>,
56
- Done,
57
- >(
58
- procedure: ProcedureClass<Events>,
59
- body: (event: EventType<Events[number]>) => Generator<Eff, Done, never>,
60
- ): Layer.Layer<never, never, Procedures | Bus | Ctx<Eff>> => {
61
- const name = procedure.manifest.name
62
- const channelName = procedure.channel.manifest.name
63
- const handles = new Set(procedure.events.map((e) => e.tag))
64
- // Scoped registration (see `Reducer.live`): an eager procedure's scope is the
65
- // root runtime's, so behavior is unchanged; a lazy feature's procedure registers
66
- // on mount and unregisters on unmount, so it stops running and is reclaimed.
67
- return Layer.scopedDiscard(
68
- Effect.gen(function* () {
69
- const procedures = yield* Procedures
70
- if (procedures.entries.some((e) => e.name === name)) {
71
- yield* Effect.logWarning(`reform: duplicate procedure name '${name}' registered`)
72
- }
73
- // Snapshot the body's full context (Bus + RPC clients) so `run` is total.
74
- const runtime = yield* Effect.runtime<Bus | Ctx<Eff>>()
75
- const entry: ProcedureEntry = {
76
- name,
77
- channelName,
78
- // The channel only routes events whose tag is in `handles`, so at runtime
79
- // `event` is always one of `Events`; `narrowHandled` restores the body's
80
- // declared event union from the erased `Tagged` envelope.
81
- handles,
82
- run: (event) => Effect.provide(Effect.gen(() => body(narrowHandled(event))), runtime),
83
- }
84
- yield* Effect.acquireRelease(Effect.sync(() => procedures.register(entry)), () =>
85
- Effect.sync(() => procedures.unregister(entry)),
86
- )
87
- }),
88
- )
89
- }
@@ -1,137 +0,0 @@
1
- import { Effect, Layer } from 'effect'
2
- import { type Manifest, definitionClass } from '../definition/definition'
3
- import { AsyncReducer } from '../internal/errors'
4
- import { type AnyEvent, type EventType } from '../event/event'
5
- import type { Store } from '../internal/store'
6
- import { type AnyState, type StateClass, type StateValue } from '../state/state'
7
- import {
8
- type AnyFamily,
9
- type FamilyKey,
10
- type FamilyStore,
11
- type FamilyValue,
12
- Tombstone,
13
- } from '../state/stateFamily'
14
- import { narrowHandled, type Tagged } from '../runtime/bus'
15
- import { type ReducerEntry, Reducers } from '../runtime/loop'
16
-
17
- /** The reflectable descriptor a reducer carries, like every other primitive's `*Manifest`. */
18
- export interface ReducerManifest extends Manifest {
19
- readonly kind: 'Reducer'
20
- }
21
-
22
- export interface StateReducerConfig<S extends AnyState, Events extends ReadonlyArray<AnyEvent>> {
23
- readonly states: readonly [S]
24
- readonly events: Events
25
- }
26
-
27
- export interface FamilyReducerConfig<F extends AnyFamily, Events extends ReadonlyArray<AnyEvent>> {
28
- readonly family: F
29
- readonly keyOf: (event: EventType<Events[number]>) => FamilyKey<F>
30
- readonly events: Events
31
- }
32
-
33
- export interface StateReducerClass<S extends AnyState, Events extends ReadonlyArray<AnyEvent>> {
34
- new (): {}
35
- readonly manifest: ReducerManifest
36
- /** Marker data read by `Reducer.live`. */
37
- readonly config: StateReducerConfig<S, Events>
38
- readonly handles: ReadonlySet<string>
39
- }
40
-
41
- export interface FamilyReducerClass<F extends AnyFamily, Events extends ReadonlyArray<AnyEvent>> {
42
- new (): {}
43
- readonly manifest: ReducerManifest
44
- readonly config: FamilyReducerConfig<F, Events>
45
- readonly handles: ReadonlySet<string>
46
- }
47
-
48
- /**
49
- * Reducers are the only writers of state. The *definition* (`Reducer.make`)
50
- * declares which state and events it touches; the *implementation*
51
- * (`Reducer.live`) is the pure fold. An unhandled event is a no-op because
52
- * it is never in `handles`.
53
- */
54
- export function make<S extends AnyState, const Events extends ReadonlyArray<AnyEvent>>(
55
- name: string,
56
- config: StateReducerConfig<S, Events>,
57
- ): StateReducerClass<S, Events>
58
- export function make<F extends AnyFamily, const Events extends ReadonlyArray<AnyEvent>>(
59
- name: string,
60
- config: FamilyReducerConfig<F, Events>,
61
- ): FamilyReducerClass<F, Events>
62
- export function make(
63
- name: string,
64
- config:
65
- | StateReducerConfig<AnyState, ReadonlyArray<AnyEvent>>
66
- | FamilyReducerConfig<AnyFamily, ReadonlyArray<AnyEvent>>,
67
- ): unknown {
68
- return definitionClass<unknown>({
69
- manifest: { kind: 'Reducer' as const, name } satisfies ReducerManifest,
70
- config,
71
- handles: new Set(config.events.map((e) => e.tag)),
72
- })
73
- }
74
-
75
- /** Reject an accidentally-async fold up front: a reducer must be a pure sync write. */
76
- const sync = <A>(value: A): A => {
77
- // Loose `!= null` so a reducer returning `undefined` doesn't crash the guard.
78
- if (value != null && typeof (value as { then?: unknown }).then === 'function') {
79
- throw new AsyncReducer()
80
- }
81
- return value
82
- }
83
-
84
- export function live<S extends AnyState, Events extends ReadonlyArray<AnyEvent>>(
85
- reducer: StateReducerClass<S, Events>,
86
- fold: (value: StateValue<S>, event: EventType<Events[number]>) => StateValue<S>,
87
- ): Layer.Layer<never, never, Reducers | Store<StateValue<S>>>
88
- export function live<F extends AnyFamily, Events extends ReadonlyArray<AnyEvent>>(
89
- reducer: FamilyReducerClass<F, Events>,
90
- fold: (entry: FamilyValue<F>, event: EventType<Events[number]>) => FamilyValue<F> | Tombstone,
91
- ): Layer.Layer<never, never, Reducers | FamilyStore<FamilyKey<F>, FamilyValue<F>>>
92
- export function live(
93
- reducer:
94
- | StateReducerClass<AnyState, ReadonlyArray<AnyEvent>>
95
- | FamilyReducerClass<AnyFamily, ReadonlyArray<AnyEvent>>,
96
- fold: (acc: any, event: any) => any,
97
- ): Layer.Layer<never, never, any> {
98
- const { config, handles } = reducer
99
- const name = reducer.manifest.name
100
- // Scoped registration: register on build, unregister on scope close. For an eager
101
- // app the scope is the root runtime's (closes only at dispose), so behavior is
102
- // unchanged; for a lazy feature the scope is the feature's, so unmount removes the
103
- // entry — it stops folding and is reclaimed instead of leaking onto a dead store.
104
- return Layer.scopedDiscard(
105
- Effect.gen(function* () {
106
- const reducers = yield* Reducers
107
- if (reducers.entries.some((e) => e.name === name)) {
108
- yield* Effect.logWarning(`reform: duplicate reducer name '${name}' registered`)
109
- }
110
- const entry: ReducerEntry = yield* Effect.gen(function* () {
111
- if ('family' in config) {
112
- const family = yield* config.family.store
113
- return {
114
- name,
115
- handles,
116
- apply: (event: Tagged) => {
117
- // The loop only invokes `apply` for events in `handles`, so `event`
118
- // is one of this reducer's declared events; `narrowHandled` restores
119
- // that typed view from the erased `Tagged` envelope `keyOf` is fed.
120
- const key = config.keyOf(narrowHandled(event))
121
- const next = fold(family.at(key).get(), event)
122
- // A fold may return the eviction sentinel to drop the key's store
123
- // (bounding an otherwise-unbounded family) instead of a new value.
124
- if (next === Tombstone) family.forget(key)
125
- else family.at(key).set(sync(next))
126
- },
127
- }
128
- }
129
- const store = yield* config.states[0].store
130
- return { name, handles, apply: (event: Tagged) => store.set(sync(fold(store.get(), event))) }
131
- })
132
- yield* Effect.acquireRelease(Effect.sync(() => reducers.register(entry)), () =>
133
- Effect.sync(() => reducers.unregister(entry)),
134
- )
135
- }),
136
- )
137
- }