@playfast/reform 0.0.7 → 0.0.8
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.
- package/package.json +2 -2
- package/src/boundary/boundary.test.ts +301 -0
- package/src/boundary/boundary.ts +192 -0
- package/src/calc/asyncCalc.test.ts +556 -0
- package/src/calc/asyncCalc.ts +267 -0
- package/src/calc/asyncData.ts +96 -0
- package/src/calc/calc.test.ts +287 -0
- package/src/calc/calc.ts +120 -0
- package/src/calc/calcFamily.test.ts +206 -0
- package/src/calc/calcFamily.ts +194 -0
- package/src/calc/compose.test.ts +68 -0
- package/src/calc/compose.ts +69 -0
- package/src/channel/channel.ts +269 -0
- package/src/compose/composition.ts +112 -0
- package/{dist/compose/host.d.ts → src/compose/host.ts} +9 -8
- package/{dist/compose/props.js → src/compose/props.ts} +7 -5
- package/src/compose/provide.ts +70 -0
- package/{dist/compose/slot.d.ts → src/compose/slot.ts} +36 -19
- package/src/compose/ui.test.ts +62 -0
- package/src/compose/ui.ts +226 -0
- package/src/compose/ui.typecheck.ts +56 -0
- package/src/definition/definition.ts +76 -0
- package/src/event/event.test.ts +23 -0
- package/src/event/event.ts +65 -0
- package/src/event/eventGroup.ts +14 -0
- package/src/feature/feature.mount.test.ts +82 -0
- package/src/feature/feature.test.ts +60 -0
- package/{dist/feature/feature.d.ts → src/feature/feature.ts} +178 -92
- package/src/feature/feature.typecheck.ts +108 -0
- package/src/index.ts +176 -0
- package/{dist/internal/capture.d.ts → src/internal/capture.ts} +13 -11
- package/{dist/internal/ctx.d.ts → src/internal/ctx.ts} +8 -4
- package/src/internal/errors.test.ts +33 -0
- package/src/internal/errors.ts +145 -0
- package/src/internal/inspect.test.ts +28 -0
- package/src/internal/inspect.ts +34 -0
- package/src/internal/queryDriver.ts +247 -0
- package/src/internal/reuse.test.ts +116 -0
- package/src/internal/reuse.ts +73 -0
- package/src/internal/scheduler.ts +93 -0
- package/{dist/internal/seeds.d.ts → src/internal/seeds.ts} +6 -3
- package/src/internal/sources.ts +104 -0
- package/{dist/internal/stateRegistry.js → src/internal/stateRegistry.ts} +18 -14
- package/src/internal/store.test.ts +80 -0
- package/src/internal/store.ts +105 -0
- package/{dist/internal/track.d.ts → src/internal/track.ts} +17 -11
- package/src/procedure/procedure.ts +89 -0
- package/src/reducer/reducer.ts +137 -0
- package/src/remote/remoteState.test.ts +695 -0
- package/src/remote/remoteState.ts +571 -0
- package/src/remote/remoteState.typecheck.ts +195 -0
- package/src/runtime/bus.ts +40 -0
- package/src/runtime/hardening.test.ts +69 -0
- package/src/runtime/loop.test.ts +178 -0
- package/src/runtime/loop.ts +171 -0
- package/src/scene/scene.ts +76 -0
- package/src/scene/seedScene.test.ts +169 -0
- package/src/state/state.ts +94 -0
- package/src/state/stateFamily.test.ts +138 -0
- package/src/state/stateFamily.ts +174 -0
- package/src/state/stateGroup.ts +105 -0
- package/{dist/state/token.d.ts → src/state/token.ts} +22 -13
- package/src/synced/syncedStore.ts +99 -0
- package/{dist/ui/node.d.ts → src/ui/node.ts} +3 -3
- package/{dist/ui/trigger.d.ts → src/ui/trigger.ts} +1 -2
- package/src/wire/tree.test.ts +81 -0
- package/src/wire/tree.ts +129 -0
- package/src/wire/triggers.test.ts +76 -0
- package/src/wire/triggers.ts +98 -0
- package/dist/boundary/boundary.d.ts +0 -72
- package/dist/boundary/boundary.d.ts.map +0 -1
- package/dist/boundary/boundary.js +0 -83
- package/dist/boundary/boundary.js.map +0 -1
- package/dist/calc/asyncCalc.d.ts +0 -91
- package/dist/calc/asyncCalc.d.ts.map +0 -1
- package/dist/calc/asyncCalc.js +0 -95
- package/dist/calc/asyncCalc.js.map +0 -1
- package/dist/calc/asyncData.d.ts +0 -57
- package/dist/calc/asyncData.d.ts.map +0 -1
- package/dist/calc/asyncData.js +0 -34
- package/dist/calc/asyncData.js.map +0 -1
- package/dist/calc/calc.d.ts +0 -57
- package/dist/calc/calc.d.ts.map +0 -1
- package/dist/calc/calc.js +0 -58
- package/dist/calc/calc.js.map +0 -1
- package/dist/calc/calcFamily.d.ts +0 -57
- package/dist/calc/calcFamily.d.ts.map +0 -1
- package/dist/calc/calcFamily.js +0 -124
- package/dist/calc/calcFamily.js.map +0 -1
- package/dist/calc/compose.d.ts +0 -16
- package/dist/calc/compose.d.ts.map +0 -1
- package/dist/calc/compose.js +0 -7
- package/dist/calc/compose.js.map +0 -1
- package/dist/channel/channel.d.ts +0 -115
- package/dist/channel/channel.d.ts.map +0 -1
- package/dist/channel/channel.js +0 -138
- package/dist/channel/channel.js.map +0 -1
- package/dist/compose/composition.d.ts +0 -72
- package/dist/compose/composition.d.ts.map +0 -1
- package/dist/compose/composition.js +0 -46
- package/dist/compose/composition.js.map +0 -1
- package/dist/compose/host.d.ts.map +0 -1
- package/dist/compose/host.js +0 -6
- package/dist/compose/host.js.map +0 -1
- package/dist/compose/props.d.ts +0 -13
- package/dist/compose/props.d.ts.map +0 -1
- package/dist/compose/props.js.map +0 -1
- package/dist/compose/provide.d.ts +0 -22
- package/dist/compose/provide.d.ts.map +0 -1
- package/dist/compose/provide.js +0 -28
- package/dist/compose/provide.js.map +0 -1
- package/dist/compose/slot.d.ts.map +0 -1
- package/dist/compose/slot.js +0 -23
- package/dist/compose/slot.js.map +0 -1
- package/dist/compose/ui.d.ts +0 -135
- package/dist/compose/ui.d.ts.map +0 -1
- package/dist/compose/ui.js +0 -63
- package/dist/compose/ui.js.map +0 -1
- package/dist/definition/definition.d.ts +0 -33
- package/dist/definition/definition.d.ts.map +0 -1
- package/dist/definition/definition.js +0 -42
- package/dist/definition/definition.js.map +0 -1
- package/dist/event/event.d.ts +0 -33
- package/dist/event/event.d.ts.map +0 -1
- package/dist/event/event.js +0 -30
- package/dist/event/event.js.map +0 -1
- package/dist/event/eventGroup.d.ts +0 -9
- package/dist/event/eventGroup.d.ts.map +0 -1
- package/dist/event/eventGroup.js +0 -4
- package/dist/event/eventGroup.js.map +0 -1
- package/dist/feature/feature.d.ts.map +0 -1
- package/dist/feature/feature.js +0 -98
- package/dist/feature/feature.js.map +0 -1
- package/dist/index.d.ts +0 -53
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -62
- package/dist/index.js.map +0 -1
- package/dist/internal/capture.d.ts.map +0 -1
- package/dist/internal/capture.js +0 -12
- package/dist/internal/capture.js.map +0 -1
- package/dist/internal/ctx.d.ts.map +0 -1
- package/dist/internal/ctx.js +0 -2
- package/dist/internal/ctx.js.map +0 -1
- package/dist/internal/errors.d.ts +0 -89
- package/dist/internal/errors.d.ts.map +0 -1
- package/dist/internal/errors.js +0 -102
- package/dist/internal/errors.js.map +0 -1
- package/dist/internal/inspect.d.ts +0 -17
- package/dist/internal/inspect.d.ts.map +0 -1
- package/dist/internal/inspect.js +0 -32
- package/dist/internal/inspect.js.map +0 -1
- package/dist/internal/queryDriver.d.ts +0 -65
- package/dist/internal/queryDriver.d.ts.map +0 -1
- package/dist/internal/queryDriver.js +0 -134
- package/dist/internal/queryDriver.js.map +0 -1
- package/dist/internal/reuse.d.ts +0 -10
- package/dist/internal/reuse.d.ts.map +0 -1
- package/dist/internal/reuse.js +0 -68
- package/dist/internal/reuse.js.map +0 -1
- package/dist/internal/scheduler.d.ts +0 -47
- package/dist/internal/scheduler.d.ts.map +0 -1
- package/dist/internal/scheduler.js +0 -70
- package/dist/internal/scheduler.js.map +0 -1
- package/dist/internal/seeds.d.ts.map +0 -1
- package/dist/internal/seeds.js +0 -17
- package/dist/internal/seeds.js.map +0 -1
- package/dist/internal/sources.d.ts +0 -39
- package/dist/internal/sources.d.ts.map +0 -1
- package/dist/internal/sources.js +0 -59
- package/dist/internal/sources.js.map +0 -1
- package/dist/internal/stateRegistry.d.ts +0 -2
- package/dist/internal/stateRegistry.d.ts.map +0 -1
- package/dist/internal/stateRegistry.js.map +0 -1
- package/dist/internal/store.d.ts +0 -47
- package/dist/internal/store.d.ts.map +0 -1
- package/dist/internal/store.js +0 -73
- package/dist/internal/store.js.map +0 -1
- package/dist/internal/track.d.ts.map +0 -1
- package/dist/internal/track.js +0 -19
- package/dist/internal/track.js.map +0 -1
- package/dist/procedure/procedure.d.ts +0 -40
- package/dist/procedure/procedure.d.ts.map +0 -1
- package/dist/procedure/procedure.js +0 -50
- package/dist/procedure/procedure.js.map +0 -1
- package/dist/reducer/reducer.d.ts +0 -44
- package/dist/reducer/reducer.d.ts.map +0 -1
- package/dist/reducer/reducer.js +0 -63
- package/dist/reducer/reducer.js.map +0 -1
- package/dist/remote/remoteState.d.ts +0 -119
- package/dist/remote/remoteState.d.ts.map +0 -1
- package/dist/remote/remoteState.js +0 -270
- package/dist/remote/remoteState.js.map +0 -1
- package/dist/runtime/bus.d.ts +0 -27
- package/dist/runtime/bus.d.ts.map +0 -1
- package/dist/runtime/bus.js +0 -21
- package/dist/runtime/bus.js.map +0 -1
- package/dist/runtime/loop.d.ts +0 -45
- package/dist/runtime/loop.d.ts.map +0 -1
- package/dist/runtime/loop.js +0 -117
- package/dist/runtime/loop.js.map +0 -1
- package/dist/scene/scene.d.ts +0 -44
- package/dist/scene/scene.d.ts.map +0 -1
- package/dist/scene/scene.js +0 -31
- package/dist/scene/scene.js.map +0 -1
- package/dist/state/state.d.ts +0 -37
- package/dist/state/state.d.ts.map +0 -1
- package/dist/state/state.js +0 -46
- package/dist/state/state.js.map +0 -1
- package/dist/state/stateFamily.d.ts +0 -79
- package/dist/state/stateFamily.d.ts.map +0 -1
- package/dist/state/stateFamily.js +0 -99
- package/dist/state/stateFamily.js.map +0 -1
- package/dist/state/stateGroup.d.ts +0 -50
- package/dist/state/stateGroup.d.ts.map +0 -1
- package/dist/state/stateGroup.js +0 -54
- package/dist/state/stateGroup.js.map +0 -1
- package/dist/state/token.d.ts.map +0 -1
- package/dist/state/token.js +0 -20
- package/dist/state/token.js.map +0 -1
- package/dist/synced/syncedStore.d.ts +0 -51
- package/dist/synced/syncedStore.d.ts.map +0 -1
- package/dist/synced/syncedStore.js +0 -43
- package/dist/synced/syncedStore.js.map +0 -1
- package/dist/ui/node.d.ts.map +0 -1
- package/dist/ui/node.js +0 -2
- package/dist/ui/node.js.map +0 -1
- package/dist/ui/trigger.d.ts.map +0 -1
- package/dist/ui/trigger.js +0 -2
- package/dist/ui/trigger.js.map +0 -1
- package/dist/wire/tree.d.ts +0 -66
- package/dist/wire/tree.d.ts.map +0 -1
- package/dist/wire/tree.js +0 -60
- package/dist/wire/tree.js.map +0 -1
- package/dist/wire/triggers.d.ts +0 -44
- package/dist/wire/triggers.d.ts.map +0 -1
- package/dist/wire/triggers.js +0 -31
- package/dist/wire/triggers.js.map +0 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Layer } from 'effect'
|
|
2
|
+
import type { CompositionClass, CompositionService } from '../compose/composition'
|
|
3
|
+
import type { UiContract } from '../compose/ui'
|
|
4
|
+
import { CurrentSeedOverrides } from '../internal/seeds'
|
|
5
|
+
import type { Bus, Tagged } from '../runtime/bus'
|
|
6
|
+
|
|
7
|
+
// A scene is a VALUE — a composition plus the closed wiring that runs it: the
|
|
8
|
+
// layers that supply its logic/views (each seeding its own live state) and the
|
|
9
|
+
// boot events to dispatch once the runtime is live. Nothing self-registers on
|
|
10
|
+
// import. A scene is the single handle every consumer takes instead of a bare
|
|
11
|
+
// composition: the react host renders it, a proof runs against it, and the dev
|
|
12
|
+
// tool previews it — all from the same closed `provide`.
|
|
13
|
+
//
|
|
14
|
+
// There is no seeded-`state` dict: variation (Default/Empty/Errored) comes from
|
|
15
|
+
// providing different closed layers, exactly as production wiring does. The
|
|
16
|
+
// composition's contract `C` is preserved (not erased to `unknown`) so a proof's
|
|
17
|
+
// facade stays fully typed from `scene.composition`.
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The services a built reform runtime exposes to its host: every composition's
|
|
21
|
+
* logic and the event `Bus`. A scene's `provide` must close to these (a fully
|
|
22
|
+
* wired app layer provides far more; this is the subset hosts read directly).
|
|
23
|
+
*/
|
|
24
|
+
export type MountedServices = CompositionService | Bus
|
|
25
|
+
|
|
26
|
+
export interface Scene<C extends UiContract = UiContract> {
|
|
27
|
+
readonly kind: 'Scene'
|
|
28
|
+
/** The composition to run, with its contract preserved for typed consumers. */
|
|
29
|
+
readonly composition: CompositionClass<unknown, C>
|
|
30
|
+
/** Closed wiring (logic + views), each layer seeding its own live state. */
|
|
31
|
+
readonly provide: ReadonlyArray<Layer.Layer<MountedServices, never, never>>
|
|
32
|
+
/** Events dispatched once the runtime is live (e.g. `RequestedTodos`). */
|
|
33
|
+
readonly boot?: ReadonlyArray<Tagged>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Define a scene — `scene(TodoApp, { provide: [makeTestApp(client, seeds)] })`.
|
|
38
|
+
* The composition's contract flows through `C`, so consumers stay typed.
|
|
39
|
+
*/
|
|
40
|
+
export const scene = <C extends UiContract>(
|
|
41
|
+
composition: CompositionClass<unknown, C>,
|
|
42
|
+
config: {
|
|
43
|
+
readonly provide: ReadonlyArray<Layer.Layer<MountedServices, never, never>>
|
|
44
|
+
readonly boot?: ReadonlyArray<Tagged>
|
|
45
|
+
},
|
|
46
|
+
): Scene<C> => ({
|
|
47
|
+
kind: 'Scene',
|
|
48
|
+
composition,
|
|
49
|
+
provide: config.provide,
|
|
50
|
+
...(config.boot !== undefined ? { boot: config.boot } : {}),
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Overlay seed values onto a CLOSED scene — the tooling/test seam (dev-tool
|
|
55
|
+
* inspector overrides, proofs). Scenes stay closed for authors: this does not
|
|
56
|
+
* reopen `provide`, it wraps each layer with `Layer.locally(CurrentSeedOverrides,
|
|
57
|
+
* seeds)` so `State.live` boots matching stores from the override instead of
|
|
58
|
+
* the authored seed.
|
|
59
|
+
*
|
|
60
|
+
* Keys are state MEMBER NAMES as reflected in `manifest.states` (e.g. `count`),
|
|
61
|
+
* not group names. If two groups in one scene share a member name, both receive
|
|
62
|
+
* the override (documented limitation). Values are schema-validated at build:
|
|
63
|
+
* an invalid value silently falls back to the authored seed. `StateFamily`
|
|
64
|
+
* entries are not covered.
|
|
65
|
+
*/
|
|
66
|
+
export const seedScene = <C extends UiContract>(
|
|
67
|
+
base: Scene<C>,
|
|
68
|
+
seeds: Readonly<Record<string, unknown>>,
|
|
69
|
+
): Scene<C> =>
|
|
70
|
+
Object.keys(seeds).length === 0
|
|
71
|
+
? base
|
|
72
|
+
: { ...base, provide: base.provide.map(Layer.locally(CurrentSeedOverrides, seeds)) }
|
|
73
|
+
|
|
74
|
+
/** Reflection guard: is this exported value a scene? */
|
|
75
|
+
export const isScene = (v: unknown): v is Scene =>
|
|
76
|
+
typeof v === 'object' && v !== null && (v as { readonly kind?: unknown }).kind === 'Scene'
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Effect, Layer, ManagedRuntime, Schema as S } from 'effect'
|
|
2
|
+
import { expect, test } from 'vitest'
|
|
3
|
+
import {
|
|
4
|
+
type CaptureSinkApi,
|
|
5
|
+
CaptureSink,
|
|
6
|
+
Composition,
|
|
7
|
+
Engine,
|
|
8
|
+
Event,
|
|
9
|
+
provide,
|
|
10
|
+
publish,
|
|
11
|
+
Reducer,
|
|
12
|
+
type RenderEnv,
|
|
13
|
+
type Scene,
|
|
14
|
+
scene,
|
|
15
|
+
seedScene,
|
|
16
|
+
State,
|
|
17
|
+
StateGroup,
|
|
18
|
+
Ui,
|
|
19
|
+
ui,
|
|
20
|
+
} from '../index'
|
|
21
|
+
|
|
22
|
+
// `seedScene` is the tooling seam over CLOSED scenes: the app bundle below seeds
|
|
23
|
+
// `count: 1` itself (no open store), and the overlay must reach the nested
|
|
24
|
+
// `State.live` through `Layer.locally(CurrentSeedOverrides, …)` on the scene's
|
|
25
|
+
// pre-composed layers. The harness mirrors the dev-tool preview: merge the
|
|
26
|
+
// scene's `provide`, mount it in a ManagedRuntime, and read the root
|
|
27
|
+
// composition's rendered props through the headless CaptureSink.
|
|
28
|
+
|
|
29
|
+
class CountState extends State.make('count', S.Number) {}
|
|
30
|
+
class MiniStates extends StateGroup.make(CountState) {}
|
|
31
|
+
|
|
32
|
+
class Bumped extends Event.make('Bumped', S.Struct({})) {}
|
|
33
|
+
|
|
34
|
+
class BumpReducer extends Reducer.make('BumpReducer', {
|
|
35
|
+
states: [CountState],
|
|
36
|
+
events: [Bumped],
|
|
37
|
+
}) {}
|
|
38
|
+
const BumpReducerLive = Reducer.live(BumpReducer, (n) => n + 1)
|
|
39
|
+
|
|
40
|
+
class CounterUi extends ui('Counter')<{ props: { count: number } }>() {}
|
|
41
|
+
class Counter extends Composition.make('Counter', {
|
|
42
|
+
title: 'Counter',
|
|
43
|
+
states: [MiniStates],
|
|
44
|
+
events: [Bumped],
|
|
45
|
+
ui: CounterUi,
|
|
46
|
+
}) {}
|
|
47
|
+
const CounterLive = Composition.live(Counter, function* () {
|
|
48
|
+
const count = yield* StateGroup.select(MiniStates, 'count')
|
|
49
|
+
const view = yield* CounterUi
|
|
50
|
+
return view({ count })
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// The closed app layer a scene provides: views + logic + Engine, with the state
|
|
54
|
+
// group seeding its AUTHORED value `count: 1` itself.
|
|
55
|
+
const presentations = provide(CounterUi, Ui.make(CounterUi, () => null))
|
|
56
|
+
const Views = CounterLive.pipe(Layer.provideMerge(presentations))
|
|
57
|
+
const Logic = Layer.mergeAll(BumpReducerLive).pipe(
|
|
58
|
+
Layer.provideMerge(Layer.mergeAll(Engine, StateGroup.live(MiniStates, { count: 1 }))),
|
|
59
|
+
)
|
|
60
|
+
const MiniApp = Views.pipe(Layer.provideMerge(Logic))
|
|
61
|
+
|
|
62
|
+
const CounterScene = scene(Counter, { provide: [MiniApp] })
|
|
63
|
+
|
|
64
|
+
const headlessEnv: RenderEnv = {
|
|
65
|
+
props: {},
|
|
66
|
+
tracker: { add: () => {} },
|
|
67
|
+
slots: { slot: () => () => null },
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// One ManagedRuntime per scene under test, with its own capture cell so two
|
|
71
|
+
// runtimes in the same test (the no-leakage case) cannot bleed into each other.
|
|
72
|
+
// Each `read` re-renders the root composition, repopulating the cell from the
|
|
73
|
+
// stores that runtime actually built.
|
|
74
|
+
const makeHarness = (s: Scene) => {
|
|
75
|
+
const captured: { count: number } = { count: -1 }
|
|
76
|
+
const sink: CaptureSinkApi = {
|
|
77
|
+
record: (capture) => {
|
|
78
|
+
const props = capture.props
|
|
79
|
+
if (
|
|
80
|
+
typeof props === 'object' &&
|
|
81
|
+
props !== null &&
|
|
82
|
+
'count' in props &&
|
|
83
|
+
typeof props.count === 'number'
|
|
84
|
+
) {
|
|
85
|
+
captured.count = props.count
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
const layer = s.provide
|
|
90
|
+
.reduce((a, b) => Layer.merge(a, b))
|
|
91
|
+
.pipe(Layer.provideMerge(Layer.succeed(CaptureSink, sink)))
|
|
92
|
+
const runtime = ManagedRuntime.make(layer)
|
|
93
|
+
const read = Effect.flatMap(Counter.tag, (c) =>
|
|
94
|
+
Composition.render(c, headlessEnv).pipe(Effect.map(() => captured.count)),
|
|
95
|
+
)
|
|
96
|
+
return { runtime, read }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Drive a read until a predicate holds, handing forked fibers the scheduler.
|
|
100
|
+
const waitUntil = <A, R>(
|
|
101
|
+
read: Effect.Effect<A, never, R>,
|
|
102
|
+
pred: (a: A) => boolean,
|
|
103
|
+
rounds = 1000,
|
|
104
|
+
): Effect.Effect<A, never, R> =>
|
|
105
|
+
Effect.flatMap(read, (a) =>
|
|
106
|
+
pred(a) || rounds <= 0
|
|
107
|
+
? Effect.succeed(a)
|
|
108
|
+
: Effect.flatMap(Effect.yieldNow(), () => waitUntil(read, pred, rounds - 1)),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
test('a seeded scene boots its store from the override', async () => {
|
|
112
|
+
const { runtime, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
113
|
+
try {
|
|
114
|
+
expect(await runtime.runPromise(read)).toBe(5)
|
|
115
|
+
} finally {
|
|
116
|
+
await runtime.dispose()
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('the live reducer drives the SAME overridden store', async () => {
|
|
121
|
+
const { runtime, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
122
|
+
try {
|
|
123
|
+
const next = await runtime.runPromise(
|
|
124
|
+
Effect.gen(function* () {
|
|
125
|
+
expect(yield* read).toBe(5)
|
|
126
|
+
yield* publish('High', Event.construct(Bumped, {}))
|
|
127
|
+
return yield* waitUntil(read, (n) => n === 6)
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
expect(next).toBe(6)
|
|
131
|
+
} finally {
|
|
132
|
+
await runtime.dispose()
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('an absent key leaves the authored seed in place', async () => {
|
|
137
|
+
const { runtime, read } = makeHarness(seedScene(CounterScene, { other: 1 }))
|
|
138
|
+
try {
|
|
139
|
+
expect(await runtime.runPromise(read)).toBe(1)
|
|
140
|
+
} finally {
|
|
141
|
+
await runtime.dispose()
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('a schema-invalid override falls back to the authored seed', async () => {
|
|
146
|
+
const { runtime, read } = makeHarness(seedScene(CounterScene, { count: 'not-a-number' }))
|
|
147
|
+
try {
|
|
148
|
+
expect(await runtime.runPromise(read)).toBe(1)
|
|
149
|
+
} finally {
|
|
150
|
+
await runtime.dispose()
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('seeding does not leak into the original scene', async () => {
|
|
155
|
+
const seeded = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
156
|
+
try {
|
|
157
|
+
expect(await seeded.runtime.runPromise(seeded.read)).toBe(5)
|
|
158
|
+
} finally {
|
|
159
|
+
await seeded.runtime.dispose()
|
|
160
|
+
}
|
|
161
|
+
// Building the ORIGINAL scene's layers again boots from the authored seed —
|
|
162
|
+
// the overlay lives only on the seeded variant's wrapped layers.
|
|
163
|
+
const original = makeHarness(CounterScene)
|
|
164
|
+
try {
|
|
165
|
+
expect(await original.runtime.runPromise(original.read)).toBe(1)
|
|
166
|
+
} finally {
|
|
167
|
+
await original.runtime.dispose()
|
|
168
|
+
}
|
|
169
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Context, Effect, FiberRef, Layer, Option, Schema } from 'effect'
|
|
2
|
+
import { type Manifest, yieldableClass } from '../definition/definition'
|
|
3
|
+
import { resolveScheduler } from '../internal/scheduler'
|
|
4
|
+
import { CurrentSeedOverrides } from '../internal/seeds'
|
|
5
|
+
import { claimStateTag } from '../internal/stateRegistry'
|
|
6
|
+
import { makeStore, type Store } from '../internal/store'
|
|
7
|
+
import { readTracked } from '../internal/track'
|
|
8
|
+
|
|
9
|
+
export interface StateOptions {
|
|
10
|
+
readonly title?: string
|
|
11
|
+
readonly description?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface StateManifest<N extends string, A> extends Manifest {
|
|
15
|
+
readonly kind: 'State'
|
|
16
|
+
readonly name: N
|
|
17
|
+
readonly schema: Schema.Schema<A, any>
|
|
18
|
+
readonly title?: string
|
|
19
|
+
readonly description?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StateClass<out N extends string, in out A> extends Effect.Effect<A, never, Store<A>> {
|
|
23
|
+
new (): {}
|
|
24
|
+
readonly manifest: StateManifest<N, A>
|
|
25
|
+
/** Internal DI tag holding the live store. The loop reads/writes through it. */
|
|
26
|
+
readonly store: Context.Tag<Store<A>, Store<A>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type AnyState = StateClass<string, any>
|
|
30
|
+
export type StateValue<S> = S extends StateClass<string, infer A> ? A : never
|
|
31
|
+
export type StateName<S> = S extends StateClass<infer N, any> ? N : never
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Atomic reactive state. `State.make` is the *definition* (a reflectable
|
|
35
|
+
* manifest + an internal store tag); it carries no starting value — the seed is
|
|
36
|
+
* an implementation detail supplied at create-time by `State.live(state, seed)`.
|
|
37
|
+
* Composed into a `StateGroup` exactly as `@effect/rpc` composes rpcs.
|
|
38
|
+
*/
|
|
39
|
+
export const make = <const N extends string, A>(
|
|
40
|
+
name: N,
|
|
41
|
+
schema: Schema.Schema<A, any>,
|
|
42
|
+
options: StateOptions = {},
|
|
43
|
+
): StateClass<N, A> => {
|
|
44
|
+
const identifier = `reform/state/${name}`
|
|
45
|
+
claimStateTag(identifier)
|
|
46
|
+
const store = Context.GenericTag<Store<A>, Store<A>>(identifier)
|
|
47
|
+
const manifest: StateManifest<N, A> = {
|
|
48
|
+
kind: 'State',
|
|
49
|
+
name,
|
|
50
|
+
schema,
|
|
51
|
+
...(options.title !== undefined ? { title: options.title } : {}),
|
|
52
|
+
...(options.description !== undefined ? { description: options.description } : {}),
|
|
53
|
+
}
|
|
54
|
+
const read = Effect.flatMap(store, readTracked)
|
|
55
|
+
return yieldableClass(read, { manifest, store })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Resolve the value a store should boot from: the ambient seed override when
|
|
60
|
+
* one is present (and valid) for this state's member name, else the authored
|
|
61
|
+
* `initial`. Reads `CurrentSeedOverrides` — empty outside the tooling seam
|
|
62
|
+
* (`Scene.seedScene`), so production wiring always boots from `initial`. An
|
|
63
|
+
* override is decoded against the state's schema; an invalid value silently
|
|
64
|
+
* falls back to `initial` — a half-typed override from the dev tool must never
|
|
65
|
+
* crash a preview.
|
|
66
|
+
*/
|
|
67
|
+
const resolveSeed = <S extends AnyState>(
|
|
68
|
+
state: S,
|
|
69
|
+
initial: StateValue<S>,
|
|
70
|
+
): Effect.Effect<StateValue<S>> =>
|
|
71
|
+
Effect.map(FiberRef.get(CurrentSeedOverrides), (overrides) =>
|
|
72
|
+
state.manifest.name in overrides
|
|
73
|
+
? Option.getOrElse(
|
|
74
|
+
Schema.decodeUnknownOption(state.manifest.schema)(overrides[state.manifest.name]),
|
|
75
|
+
() => initial,
|
|
76
|
+
)
|
|
77
|
+
: initial,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Allocate a state's store, seeded with `initial` — `State.live(Feed, seed)`.
|
|
82
|
+
* The seed lives here (implementation), not on the definition, so the same
|
|
83
|
+
* definition can boot from different values in app vs. test wiring.
|
|
84
|
+
*/
|
|
85
|
+
export const live = <S extends AnyState>(
|
|
86
|
+
state: S,
|
|
87
|
+
initial: StateValue<S>,
|
|
88
|
+
): Layer.Layer<Store<StateValue<S>>> =>
|
|
89
|
+
Layer.effect(
|
|
90
|
+
state.store,
|
|
91
|
+
Effect.zipWith(resolveScheduler, resolveSeed(state, initial), (scheduler, seed) =>
|
|
92
|
+
makeStore(seed, scheduler),
|
|
93
|
+
),
|
|
94
|
+
)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { expect, it } from '@effect/vitest'
|
|
2
|
+
import { Duration, Effect, Layer, Schema as S } from 'effect'
|
|
3
|
+
import { Engine, Event, publish, Reducer, StateFamily } from '../index'
|
|
4
|
+
|
|
5
|
+
// A family is normalized keyed state: one logical slice per key, so a write to
|
|
6
|
+
// one key must wake only that key's subscribers (the point of the primitive).
|
|
7
|
+
|
|
8
|
+
const tick = Effect.sleep(Duration.millis(1))
|
|
9
|
+
|
|
10
|
+
it.live("writing one key does not notify another key's subscribers", () => {
|
|
11
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
12
|
+
const TestLayer = StateFamily.live(Items, 0)
|
|
13
|
+
|
|
14
|
+
return Effect.gen(function* () {
|
|
15
|
+
const family = yield* Items.store
|
|
16
|
+
const a = family.at('a')
|
|
17
|
+
const b = family.at('b')
|
|
18
|
+
const aWakes = { n: 0 }
|
|
19
|
+
const bWakes = { n: 0 }
|
|
20
|
+
a.subscribe(() => {
|
|
21
|
+
aWakes.n += 1
|
|
22
|
+
})
|
|
23
|
+
b.subscribe(() => {
|
|
24
|
+
bWakes.n += 1
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
a.set(1)
|
|
28
|
+
yield* tick
|
|
29
|
+
expect(aWakes.n).toBe(1)
|
|
30
|
+
expect(bWakes.n).toBe(0)
|
|
31
|
+
expect(b.get()).toBe(0)
|
|
32
|
+
}).pipe(Effect.provide(TestLayer))
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it.live('at(key) returns the same store each call, so subscriptions are stable', () => {
|
|
36
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
37
|
+
const TestLayer = StateFamily.live(Items, 0)
|
|
38
|
+
|
|
39
|
+
return Effect.gen(function* () {
|
|
40
|
+
const family = yield* Items.store
|
|
41
|
+
expect(family.at('k')).toBe(family.at('k'))
|
|
42
|
+
}).pipe(Effect.provide(TestLayer))
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it.live('forget(key) drops the store; re-reading the key reseeds it', () => {
|
|
46
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
47
|
+
const TestLayer = StateFamily.live(Items, 0)
|
|
48
|
+
|
|
49
|
+
return Effect.gen(function* () {
|
|
50
|
+
const family = yield* Items.store
|
|
51
|
+
const first = family.at('k')
|
|
52
|
+
first.set(42)
|
|
53
|
+
expect(family.at('k').get()).toBe(42)
|
|
54
|
+
|
|
55
|
+
family.forget('k')
|
|
56
|
+
// A fresh store, reseeded to the family's initial value — not the old 42.
|
|
57
|
+
const reseeded = family.at('k')
|
|
58
|
+
expect(reseeded).not.toBe(first)
|
|
59
|
+
expect(reseeded.get()).toBe(0)
|
|
60
|
+
}).pipe(Effect.provide(TestLayer))
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it.live('evictWhenUnused drops a key once its last subscriber leaves', () => {
|
|
64
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
65
|
+
const TestLayer = StateFamily.live(Items, 0, { evictWhenUnused: true })
|
|
66
|
+
|
|
67
|
+
return Effect.gen(function* () {
|
|
68
|
+
const family = yield* Items.store
|
|
69
|
+
const store = family.at('k')
|
|
70
|
+
const offA = store.subscribe(() => {})
|
|
71
|
+
const offB = store.subscribe(() => {})
|
|
72
|
+
expect(family.size()).toBe(1)
|
|
73
|
+
|
|
74
|
+
// One subscriber leaving keeps the key (another still reads it).
|
|
75
|
+
offA()
|
|
76
|
+
yield* tick
|
|
77
|
+
expect(family.size()).toBe(1)
|
|
78
|
+
|
|
79
|
+
// The last subscriber leaving evicts the key on the next microtask.
|
|
80
|
+
offB()
|
|
81
|
+
yield* tick
|
|
82
|
+
expect(family.size()).toBe(0)
|
|
83
|
+
// Re-reading reseeds a fresh store.
|
|
84
|
+
expect(family.at('k').get()).toBe(0)
|
|
85
|
+
}).pipe(Effect.provide(TestLayer))
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it.live('evictWhenUnused: a same-tick re-subscribe cancels eviction', () => {
|
|
89
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
90
|
+
const TestLayer = StateFamily.live(Items, 0, { evictWhenUnused: true })
|
|
91
|
+
|
|
92
|
+
return Effect.gen(function* () {
|
|
93
|
+
const family = yield* Items.store
|
|
94
|
+
const store = family.at('k')
|
|
95
|
+
store.set(7)
|
|
96
|
+
const off = store.subscribe(() => {})
|
|
97
|
+
off() // count → 0, eviction scheduled for the next microtask
|
|
98
|
+
store.subscribe(() => {}) // re-subscribe in the same tick → count back to 1
|
|
99
|
+
yield* tick
|
|
100
|
+
// Not evicted: the value survives and `at` returns the same live store.
|
|
101
|
+
expect(family.size()).toBe(1)
|
|
102
|
+
expect(family.at('k').get()).toBe(7)
|
|
103
|
+
}).pipe(Effect.provide(TestLayer))
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it.live('a family reducer fold returning Tombstone evicts the key', () => {
|
|
107
|
+
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
108
|
+
class Bumped extends Event.make('Bumped', S.Struct({ id: S.String })) {}
|
|
109
|
+
class Removed extends Event.make('Removed', S.Struct({ id: S.String })) {}
|
|
110
|
+
class ItemsReducer extends Reducer.make('ItemsReducer', {
|
|
111
|
+
family: Items,
|
|
112
|
+
keyOf: (event) => event.id,
|
|
113
|
+
events: [Bumped, Removed],
|
|
114
|
+
}) {}
|
|
115
|
+
const ItemsLive = Reducer.live(ItemsReducer, (n, event) =>
|
|
116
|
+
event._tag === 'Removed' ? StateFamily.Tombstone : n + 1,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const TestLayer = ItemsLive.pipe(
|
|
120
|
+
Layer.provideMerge(Layer.mergeAll(StateFamily.live(Items, 0), Engine)),
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
return Effect.gen(function* () {
|
|
124
|
+
const family = yield* Items.store
|
|
125
|
+
|
|
126
|
+
yield* publish('High', Event.construct(Bumped, { id: 'a' }))
|
|
127
|
+
yield* Effect.sleep(Duration.millis(5))
|
|
128
|
+
const before = family.at('a')
|
|
129
|
+
expect(before.get()).toBe(1)
|
|
130
|
+
|
|
131
|
+
// The fold returns the eviction sentinel: the key's store is dropped.
|
|
132
|
+
yield* publish('High', Event.construct(Removed, { id: 'a' }))
|
|
133
|
+
yield* Effect.sleep(Duration.millis(5))
|
|
134
|
+
const after = family.at('a')
|
|
135
|
+
expect(after).not.toBe(before)
|
|
136
|
+
expect(after.get()).toBe(0)
|
|
137
|
+
}).pipe(Effect.provide(TestLayer))
|
|
138
|
+
})
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Context, Effect, Layer, type Schema } from 'effect'
|
|
2
|
+
import { type Manifest, definitionClass } from '../definition/definition'
|
|
3
|
+
import { resolveScheduler, type Scheduler } from '../internal/scheduler'
|
|
4
|
+
import { claimStateTag } from '../internal/stateRegistry'
|
|
5
|
+
import { makeStore, type Store } from '../internal/store'
|
|
6
|
+
import { readTracked } from '../internal/track'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A reducer fold returns this to evict the key it folded over, instead of a new
|
|
10
|
+
* value. The loop drops the entry's store from the family, so a normalized
|
|
11
|
+
* collection (chat messages, search hits, transient rows) does not grow without
|
|
12
|
+
* bound — the one fix for the otherwise-unbounded per-key `Map`.
|
|
13
|
+
*/
|
|
14
|
+
export const Tombstone: unique symbol = Symbol.for('reform/StateFamily/Tombstone')
|
|
15
|
+
export type Tombstone = typeof Tombstone
|
|
16
|
+
|
|
17
|
+
/** Per-key stores, created lazily so a write to one key wakes only its subscribers. */
|
|
18
|
+
export interface FamilyStore<K, V> {
|
|
19
|
+
at(key: K): Store<V>
|
|
20
|
+
/**
|
|
21
|
+
* Drop a key's store so it stops occupying memory. A live subscriber keeps its
|
|
22
|
+
* own closure over the previous store, so eviction is for keys whose entity is
|
|
23
|
+
* gone (the component is unmounting) — re-`at`-ing the key allocates a fresh,
|
|
24
|
+
* reseeded store.
|
|
25
|
+
*/
|
|
26
|
+
forget(key: K): void
|
|
27
|
+
/** Drop every key's store — a wholesale reset of the family. */
|
|
28
|
+
clear(): void
|
|
29
|
+
/** Number of live keys — for tests/diagnostics. */
|
|
30
|
+
size(): number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Options for a family's keyed store. */
|
|
34
|
+
export interface FamilyOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Drop a key's store automatically once it has no live subscribers (checked on
|
|
37
|
+
* the next microtask, so a same-commit re-subscribe — e.g. a key that just moved
|
|
38
|
+
* position in a list — cancels the eviction). This bounds an otherwise-unbounded
|
|
39
|
+
* family without explicit `Tombstone`s, but it also DISCARDS the key's value —
|
|
40
|
+
* re-`at`-ing a dropped key reseeds from `initial`. Use only for keys whose value
|
|
41
|
+
* is disposable when nothing renders them (search hits, transient rows); for a
|
|
42
|
+
* source-of-truth entity that may be temporarily off-screen (a virtualized list),
|
|
43
|
+
* keep this off and reclaim with an explicit reducer `Tombstone`. Default: off.
|
|
44
|
+
*/
|
|
45
|
+
readonly evictWhenUnused?: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const makeFamilyStore = <K, V>(
|
|
49
|
+
seed: (key: K) => V,
|
|
50
|
+
scheduler: Scheduler,
|
|
51
|
+
options: FamilyOptions = {},
|
|
52
|
+
): FamilyStore<K, V> => {
|
|
53
|
+
const entries = new Map<K, Store<V>>()
|
|
54
|
+
const evictWhenUnused = options.evictWhenUnused === true
|
|
55
|
+
// Live subscriber count per key — maintained only when eviction is on.
|
|
56
|
+
const subscribers = new Map<K, number>()
|
|
57
|
+
|
|
58
|
+
// Wrap a store's `subscribe` to ref-count, evicting the key when it falls idle.
|
|
59
|
+
// `get`/`set`/`getVersion` are delegated unchanged, so the loop writes and the
|
|
60
|
+
// host reads see the same cell; only subscribe/unsubscribe are instrumented.
|
|
61
|
+
const refCounted = (key: K, store: Store<V>): Store<V> => ({
|
|
62
|
+
...store,
|
|
63
|
+
subscribe: (listener) => {
|
|
64
|
+
subscribers.set(key, (subscribers.get(key) ?? 0) + 1)
|
|
65
|
+
const off = store.subscribe(listener)
|
|
66
|
+
const released = { done: false }
|
|
67
|
+
return () => {
|
|
68
|
+
if (released.done) return
|
|
69
|
+
released.done = true
|
|
70
|
+
off()
|
|
71
|
+
const remaining = (subscribers.get(key) ?? 1) - 1
|
|
72
|
+
if (remaining > 0) {
|
|
73
|
+
subscribers.set(key, remaining)
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
subscribers.delete(key)
|
|
77
|
+
queueMicrotask(() => {
|
|
78
|
+
if ((subscribers.get(key) ?? 0) === 0) entries.delete(key)
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
at: (key) => {
|
|
86
|
+
const existing = entries.get(key)
|
|
87
|
+
if (existing !== undefined) return existing
|
|
88
|
+
const base = makeStore(seed(key), scheduler)
|
|
89
|
+
const created = evictWhenUnused ? refCounted(key, base) : base
|
|
90
|
+
entries.set(key, created)
|
|
91
|
+
return created
|
|
92
|
+
},
|
|
93
|
+
forget: (key) => {
|
|
94
|
+
entries.delete(key)
|
|
95
|
+
subscribers.delete(key)
|
|
96
|
+
},
|
|
97
|
+
clear: () => {
|
|
98
|
+
entries.clear()
|
|
99
|
+
subscribers.clear()
|
|
100
|
+
},
|
|
101
|
+
size: () => entries.size,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface StateFamilyManifest<N extends string, K, V> extends Manifest {
|
|
106
|
+
readonly kind: 'StateFamily'
|
|
107
|
+
readonly name: N
|
|
108
|
+
readonly key: Schema.Schema<K, any>
|
|
109
|
+
readonly value: Schema.Schema<V, any>
|
|
110
|
+
readonly title?: string
|
|
111
|
+
readonly description?: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface StateFamilyClass<out N extends string, in out K, in out V> {
|
|
115
|
+
/** Instance carries the key phantom so `Family['Key']` resolves in `keyOf` types. */
|
|
116
|
+
new (): { readonly Key: K }
|
|
117
|
+
readonly manifest: StateFamilyManifest<N, K, V>
|
|
118
|
+
readonly store: Context.Tag<FamilyStore<K, V>, FamilyStore<K, V>>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type AnyFamily = StateFamilyClass<string, any, any>
|
|
122
|
+
export type FamilyKey<F> = F extends StateFamilyClass<any, infer K, any> ? K : never
|
|
123
|
+
export type FamilyValue<F> = F extends StateFamilyClass<any, any, infer V> ? V : never
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Normalized keyed state: one logical State per key, backed by a single family
|
|
127
|
+
* store. Scales with the collection without re-rendering unrelated entries.
|
|
128
|
+
*/
|
|
129
|
+
export const make = <const N extends string, K, V>(
|
|
130
|
+
name: N,
|
|
131
|
+
key: Schema.Schema<K, any>,
|
|
132
|
+
value: Schema.Schema<V, any>,
|
|
133
|
+
options: { readonly title?: string; readonly description?: string } = {},
|
|
134
|
+
): StateFamilyClass<N, K, V> => {
|
|
135
|
+
const identifier = `reform/family/${name}`
|
|
136
|
+
claimStateTag(identifier)
|
|
137
|
+
const store = Context.GenericTag<FamilyStore<K, V>, FamilyStore<K, V>>(identifier)
|
|
138
|
+
const manifest: StateFamilyManifest<N, K, V> = {
|
|
139
|
+
kind: 'StateFamily',
|
|
140
|
+
name,
|
|
141
|
+
key,
|
|
142
|
+
value,
|
|
143
|
+
...(options.title !== undefined ? { title: options.title } : {}),
|
|
144
|
+
...(options.description !== undefined ? { description: options.description } : {}),
|
|
145
|
+
}
|
|
146
|
+
return definitionClass<StateFamilyClass<N, K, V>>({ manifest, store })
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Read one entry by key — `StateFamily.read(ItemUi, id)`. Inside a render it
|
|
151
|
+
* subscribes that key's slice; elsewhere it just snapshots (D5).
|
|
152
|
+
*/
|
|
153
|
+
export const read = <N extends string, K, V>(
|
|
154
|
+
family: StateFamilyClass<N, K, V>,
|
|
155
|
+
key: K,
|
|
156
|
+
): Effect.Effect<V, never, FamilyStore<K, V>> =>
|
|
157
|
+
Effect.flatMap(family.store, (fs) => readTracked(fs.at(key)))
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Allocate a family's keyed store — `StateFamily.live(ItemUi, seed)`. The seed
|
|
161
|
+
* is a plain value or a factory; a factory receives the key, so a new entry can
|
|
162
|
+
* derive its starting value from its own id (`(id) => ({ ...blank, id }))`).
|
|
163
|
+
*/
|
|
164
|
+
export const live = <N extends string, K, V>(
|
|
165
|
+
family: StateFamilyClass<N, K, V>,
|
|
166
|
+
initial: V | ((key: K) => V),
|
|
167
|
+
options: FamilyOptions = {},
|
|
168
|
+
): Layer.Layer<FamilyStore<K, V>> => {
|
|
169
|
+
const seed: (key: K) => V = typeof initial === 'function' ? (initial as (key: K) => V) : () => initial
|
|
170
|
+
return Layer.effect(
|
|
171
|
+
family.store,
|
|
172
|
+
Effect.map(resolveScheduler, (scheduler) => makeFamilyStore<K, V>(seed, scheduler, options)),
|
|
173
|
+
)
|
|
174
|
+
}
|