@playfast/reform 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -55
- package/package.json +17 -17
- package/src/boundary/boundary.test.ts +62 -16
- package/src/boundary/boundary.ts +141 -32
- package/src/calc/asyncCalc.invalidate.test.ts +516 -18
- package/src/calc/asyncCalc.test.ts +207 -175
- package/src/calc/asyncCalc.ts +168 -39
- package/src/calc/calc.test.ts +3 -5
- package/src/calc/calc.ts +44 -18
- package/src/calc/calcFamily.test.ts +80 -22
- package/src/calc/calcFamily.ts +56 -25
- package/src/calc/compose.ts +1 -14
- package/src/channel/channel.ts +128 -11
- package/src/compose/composition.test.ts +19 -0
- package/src/compose/composition.ts +346 -62
- package/src/compose/props.ts +13 -6
- package/src/compose/provide.ts +187 -46
- package/src/compose/slot.ts +156 -19
- package/src/compose/structure.test.ts +6 -3
- package/src/compose/structure.ts +106 -13
- package/src/compose/ui.test.ts +19 -3
- package/src/compose/ui.ts +147 -54
- package/src/compose/ui.typecheck.ts +40 -4
- package/src/definition/definition.ts +22 -9
- package/src/event/event.fromSource.test.ts +172 -0
- package/src/event/event.test.ts +10 -0
- package/src/event/event.ts +102 -18
- package/src/feature/feature.mount.test.ts +123 -56
- package/src/feature/feature.test.ts +67 -63
- package/src/feature/feature.ts +1317 -197
- package/src/feature/feature.typecheck.ts +253 -61
- package/src/graph/closure.ts +403 -0
- package/src/index.ts +171 -245
- package/src/internal/bucketCache.ts +39 -0
- package/src/internal/capture.ts +9 -4
- package/src/internal/env.ts +1 -3
- package/src/internal/errors.ts +21 -10
- package/src/internal/queryDriver.ts +120 -15
- package/src/internal/queryDriverStore.ts +8 -5
- package/src/internal/queryDriverTypes.ts +3 -1
- package/src/internal/reuse.test.ts +10 -3
- package/src/internal/reuse.ts +5 -2
- package/src/internal/scheduler.ts +4 -1
- package/src/internal/sources.ts +25 -11
- package/src/internal/track.ts +3 -2
- package/src/internal.ts +222 -0
- package/src/namespace/namespace.ts +46 -25
- package/src/procedure/procedure.ts +38 -11
- package/src/reducer/reducer.ts +78 -34
- package/src/remote/remoteState.test.ts +515 -339
- package/src/remote/remoteState.ts +572 -107
- package/src/remote/remoteState.typecheck.ts +47 -23
- package/src/runtime/appRuntime.activation.test.ts +100 -0
- package/src/runtime/appRuntime.test.ts +157 -105
- package/src/runtime/appRuntime.ts +394 -33
- package/src/runtime/eventBudget.test.ts +1 -4
- package/src/runtime/eventBudget.ts +9 -8
- package/src/runtime/hardening.test.ts +4 -9
- package/src/runtime/instrumentation.test.ts +174 -192
- package/src/runtime/instrumentation.ts +6 -11
- package/src/runtime/loop.test.ts +36 -0
- package/src/runtime/loop.ts +69 -40
- package/src/scene/featureScene.test.ts +4 -2
- package/src/scene/scene.ts +234 -64
- package/src/scene/seedScene.test.ts +69 -86
- package/src/state/state.nominal.typecheck.ts +68 -0
- package/src/state/state.test.ts +17 -0
- package/src/state/state.ts +79 -26
- package/src/state/stateFamily.test.ts +14 -0
- package/src/state/stateFamily.ts +55 -22
- package/src/state/stateGroup.ts +53 -17
- package/src/state/token.ts +40 -10
- package/src/synced/syncedStore.ts +46 -23
- package/src/testkit/flight.testkit.ts +75 -0
- package/src/wire/tree.test.ts +8 -4
- package/src/wire/triggers.test.ts +6 -2
- package/src/wire/triggers.ts +14 -10
- package/src/calc/asyncCalcDefinitions.ts +0 -48
- package/src/channel/procedureRegistry.ts +0 -90
- package/src/feature/featureBinding.ts +0 -48
- package/src/internal/ctx.ts +0 -9
- package/src/remote/remoteStateDefinition.ts +0 -135
- package/src/remote/remoteStateLayers.ts +0 -118
- package/src/remote/remoteStateLiveTypes.ts +0 -59
- package/src/remote/remoteStateSend.ts +0 -64
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Effect, Layer, Schema as S } from
|
|
2
|
-
import { expect, it } from
|
|
1
|
+
import { Effect, Layer, Schema as S } from 'effect'
|
|
2
|
+
import { expect, it } from '@effect/vitest'
|
|
3
3
|
import {
|
|
4
4
|
Composition,
|
|
5
5
|
Engine,
|
|
@@ -10,90 +10,82 @@ import {
|
|
|
10
10
|
publish,
|
|
11
11
|
Reducer,
|
|
12
12
|
type RenderEnv,
|
|
13
|
-
type Scene,
|
|
14
13
|
scene,
|
|
15
14
|
seedScene,
|
|
16
15
|
State,
|
|
17
16
|
StateGroup,
|
|
18
17
|
Ui,
|
|
19
18
|
ui,
|
|
20
|
-
} from
|
|
21
|
-
import { CurrentSeedOverrides } from
|
|
19
|
+
} from '../index'
|
|
20
|
+
import { CurrentSeedOverrides } from '../internal/seeds'
|
|
22
21
|
|
|
23
|
-
class CountState extends State.make(
|
|
22
|
+
class CountState extends State.make('count', S.Number) {}
|
|
24
23
|
class MiniStates extends StateGroup.make(CountState) {}
|
|
25
24
|
|
|
26
|
-
class Bumped extends Event.make(
|
|
25
|
+
class Bumped extends Event.make('Bumped', S.Struct({})) {}
|
|
27
26
|
|
|
28
|
-
class BumpReducer extends Reducer.make(
|
|
27
|
+
class BumpReducer extends Reducer.make('BumpReducer', {
|
|
29
28
|
states: [CountState],
|
|
30
29
|
events: [Bumped],
|
|
31
30
|
}) {}
|
|
32
|
-
const BumpReducerLive = Reducer.live(BumpReducer, (n) => n + 1)
|
|
31
|
+
const BumpReducerLive = Reducer.live(BumpReducer, (n) => n + 1)
|
|
33
32
|
|
|
34
|
-
class CounterUi extends ui(
|
|
35
|
-
class Counter extends Composition.make(
|
|
36
|
-
title:
|
|
33
|
+
class CounterUi extends ui('Counter')<{ props: { count: number } }>() {}
|
|
34
|
+
class Counter extends Composition.make('Counter', {
|
|
35
|
+
title: 'Counter',
|
|
37
36
|
states: [MiniStates],
|
|
38
37
|
events: [Bumped],
|
|
39
38
|
ui: CounterUi,
|
|
40
|
-
}) {}
|
|
39
|
+
})<Counter>() {}
|
|
41
40
|
const CounterLive = Composition.live(Counter, function* () {
|
|
42
|
-
const count = yield* StateGroup.select(MiniStates,
|
|
43
|
-
return mount({ props: { count }, slots: {} })
|
|
44
|
-
})
|
|
41
|
+
const count = yield* StateGroup.select(MiniStates, 'count')
|
|
42
|
+
return mount({ props: { count }, slots: {} })
|
|
43
|
+
})
|
|
45
44
|
|
|
46
45
|
const presentations = provide(
|
|
47
46
|
CounterUi,
|
|
48
47
|
Ui.make(CounterUi, () => null),
|
|
49
|
-
)
|
|
50
|
-
const Views = CounterLive.pipe(Layer.provideMerge(presentations))
|
|
48
|
+
)
|
|
49
|
+
const Views = CounterLive.pipe(Layer.provideMerge(presentations))
|
|
51
50
|
const Logic = Layer.mergeAll(BumpReducerLive).pipe(
|
|
52
|
-
Layer.provideMerge(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
);
|
|
56
|
-
const MiniApp = Views.pipe(Layer.provideMerge(Logic));
|
|
51
|
+
Layer.provideMerge(Layer.mergeAll(Engine, StateGroup.live(MiniStates, { count: 1 }))),
|
|
52
|
+
)
|
|
53
|
+
const MiniApp = Views.pipe(Layer.provideMerge(Logic))
|
|
57
54
|
|
|
58
|
-
const CounterScene = scene(Counter, { provide: [MiniApp] })
|
|
55
|
+
const CounterScene = scene(Counter, { provide: [MiniApp] })
|
|
59
56
|
|
|
60
57
|
const overlayRawSeeds = (
|
|
61
|
-
base:
|
|
58
|
+
base: typeof CounterScene,
|
|
62
59
|
seeds: Readonly<Record<string, unknown>>,
|
|
63
|
-
):
|
|
60
|
+
): typeof CounterScene => ({
|
|
64
61
|
...base,
|
|
65
62
|
provide: base.provide.map(Layer.locally(CurrentSeedOverrides, seeds)),
|
|
66
|
-
})
|
|
63
|
+
})
|
|
67
64
|
|
|
68
65
|
const headlessEnv: RenderEnv = {
|
|
69
66
|
props: {},
|
|
70
67
|
tracker: { add: () => {} },
|
|
71
|
-
}
|
|
68
|
+
}
|
|
72
69
|
|
|
73
70
|
const isCountProps = (props: unknown): props is { readonly count: number } =>
|
|
74
|
-
typeof props ===
|
|
75
|
-
props !== null &&
|
|
76
|
-
"count" in props &&
|
|
77
|
-
typeof props.count === "number";
|
|
71
|
+
typeof props === 'object' && props !== null && 'count' in props && typeof props.count === 'number'
|
|
78
72
|
|
|
79
|
-
const makeHarness = (s:
|
|
80
|
-
const layer = s.provide.reduce((a, b) => Layer.merge(a, b))
|
|
73
|
+
const makeHarness = (s: typeof CounterScene) => {
|
|
74
|
+
const layer = s.provide.reduce((a, b) => Layer.merge(a, b))
|
|
81
75
|
const read = Effect.flatMap(Counter.tag, (c) =>
|
|
82
76
|
Composition.render(c, headlessEnv).pipe(
|
|
83
77
|
Effect.flatMap((frame) => {
|
|
84
78
|
if (!isStructure(frame)) {
|
|
85
|
-
return Effect.dieMessage(
|
|
79
|
+
return Effect.dieMessage('Counter must render a Structure')
|
|
86
80
|
}
|
|
87
81
|
return isCountProps(frame.props)
|
|
88
82
|
? Effect.succeed(frame.props.count)
|
|
89
|
-
: Effect.dieMessage(
|
|
90
|
-
"Counter Structure props must include numeric count",
|
|
91
|
-
);
|
|
83
|
+
: Effect.dieMessage('Counter Structure props must include numeric count')
|
|
92
84
|
}),
|
|
93
85
|
),
|
|
94
|
-
)
|
|
95
|
-
return { layer, read }
|
|
96
|
-
}
|
|
86
|
+
)
|
|
87
|
+
return { layer, read }
|
|
88
|
+
}
|
|
97
89
|
|
|
98
90
|
const waitUntil = <A, R>(
|
|
99
91
|
read: Effect.Effect<A, never, R>,
|
|
@@ -103,54 +95,45 @@ const waitUntil = <A, R>(
|
|
|
103
95
|
Effect.flatMap(read, (a) =>
|
|
104
96
|
pred(a) || rounds <= 0
|
|
105
97
|
? Effect.succeed(a)
|
|
106
|
-
: Effect.flatMap(Effect.yieldNow(), () =>
|
|
107
|
-
|
|
108
|
-
),
|
|
109
|
-
);
|
|
98
|
+
: Effect.flatMap(Effect.yieldNow(), () => waitUntil(read, pred, rounds - 1)),
|
|
99
|
+
)
|
|
110
100
|
|
|
111
|
-
it.scopedLive(
|
|
112
|
-
const { layer, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
101
|
+
it.scopedLive('a seeded scene boots its store from the override', () => {
|
|
102
|
+
const { layer, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
113
103
|
return Effect.gen(function* () {
|
|
114
|
-
expect(yield* read).toBe(5)
|
|
115
|
-
}).pipe(Effect.provide(layer))
|
|
116
|
-
})
|
|
104
|
+
expect(yield* read).toBe(5)
|
|
105
|
+
}).pipe(Effect.provide(layer))
|
|
106
|
+
})
|
|
117
107
|
|
|
118
|
-
it.scopedLive(
|
|
119
|
-
const { layer, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
108
|
+
it.scopedLive('the live reducer drives the SAME overridden store', () => {
|
|
109
|
+
const { layer, read } = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
110
|
+
return Effect.gen(function* () {
|
|
111
|
+
expect(yield* read).toBe(5)
|
|
112
|
+
yield* publish('High', Event.construct(Bumped, {}))
|
|
113
|
+
const next = yield* waitUntil(read, (n) => n === 6)
|
|
114
|
+
expect(next).toBe(6)
|
|
115
|
+
}).pipe(Effect.provide(layer))
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it.scopedLive('an absent key leaves the authored seed in place', () => {
|
|
119
|
+
const { layer, read } = makeHarness(overlayRawSeeds(CounterScene, { other: 1 }))
|
|
120
120
|
return Effect.gen(function* () {
|
|
121
|
-
expect(yield* read).toBe(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
it.scopedLive("an absent key leaves the authored seed in place", () => {
|
|
129
|
-
const { layer, read } = makeHarness(
|
|
130
|
-
overlayRawSeeds(CounterScene, { other: 1 }),
|
|
131
|
-
);
|
|
121
|
+
expect(yield* read).toBe(1)
|
|
122
|
+
}).pipe(Effect.provide(layer))
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it.scopedLive('a schema-invalid override falls back to the authored seed', () => {
|
|
126
|
+
const { layer, read } = makeHarness(overlayRawSeeds(CounterScene, { count: 'not-a-number' }))
|
|
132
127
|
return Effect.gen(function* () {
|
|
133
|
-
expect(yield* read).toBe(1)
|
|
134
|
-
}).pipe(Effect.provide(layer))
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
it.scopedLive(
|
|
138
|
-
|
|
139
|
-
()
|
|
140
|
-
const { layer, read } = makeHarness(
|
|
141
|
-
overlayRawSeeds(CounterScene, { count: "not-a-number" }),
|
|
142
|
-
);
|
|
143
|
-
return Effect.gen(function* () {
|
|
144
|
-
expect(yield* read).toBe(1);
|
|
145
|
-
}).pipe(Effect.provide(layer));
|
|
146
|
-
},
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
it.scopedLive("seeding does not leak into the original scene", () => {
|
|
150
|
-
const seeded = makeHarness(seedScene(CounterScene, { count: 5 }));
|
|
151
|
-
const original = makeHarness(CounterScene);
|
|
128
|
+
expect(yield* read).toBe(1)
|
|
129
|
+
}).pipe(Effect.provide(layer))
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it.scopedLive('seeding does not leak into the original scene', () => {
|
|
133
|
+
const seeded = makeHarness(seedScene(CounterScene, { count: 5 }))
|
|
134
|
+
const original = makeHarness(CounterScene)
|
|
152
135
|
return Effect.gen(function* () {
|
|
153
|
-
expect(yield* seeded.read.pipe(Effect.provide(seeded.layer))).toBe(5)
|
|
154
|
-
expect(yield* original.read.pipe(Effect.provide(original.layer))).toBe(1)
|
|
155
|
-
})
|
|
156
|
-
})
|
|
136
|
+
expect(yield* seeded.read.pipe(Effect.provide(seeded.layer))).toBe(5)
|
|
137
|
+
expect(yield* original.read.pipe(Effect.provide(original.layer))).toBe(1)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Layer, Schema as S } from 'effect'
|
|
2
|
+
import type { ProvidedBy } from '../feature/feature'
|
|
3
|
+
import * as State from './state'
|
|
4
|
+
import * as StateFamily from './stateFamily'
|
|
5
|
+
|
|
6
|
+
type Equal<Left, Right> =
|
|
7
|
+
(<Value>() => Value extends Left ? 1 : 2) extends <Value>() => Value extends Right ? 1 : 2
|
|
8
|
+
? true
|
|
9
|
+
: false
|
|
10
|
+
type Expect<Value extends true> = Value
|
|
11
|
+
|
|
12
|
+
class FirstState extends State.make('nominal.first', S.Number) {}
|
|
13
|
+
class SecondState extends State.make('nominal.second', S.Number) {}
|
|
14
|
+
class FirstFamily extends StateFamily.make('nominal.firstFamily', S.String, S.Number) {}
|
|
15
|
+
class SecondFamily extends StateFamily.make('nominal.secondFamily', S.String, S.Number) {}
|
|
16
|
+
|
|
17
|
+
const FirstStateLive = State.live(FirstState, 0)
|
|
18
|
+
const SecondStateLive = State.live(SecondState, 0)
|
|
19
|
+
const FirstFamilyLive = StateFamily.live(FirstFamily, 0)
|
|
20
|
+
const SecondFamilyLive = StateFamily.live(SecondFamily, 0)
|
|
21
|
+
|
|
22
|
+
type StateLiveKeepsDefinitionIdentity = Expect<
|
|
23
|
+
Equal<Layer.Layer.Success<typeof FirstStateLive>, State.StateStore<'nominal.first', number>>
|
|
24
|
+
>
|
|
25
|
+
type FamilyLiveKeepsDefinitionIdentity = Expect<
|
|
26
|
+
Equal<
|
|
27
|
+
Layer.Layer.Success<typeof FirstFamilyLive>,
|
|
28
|
+
StateFamily.StateFamilyStore<'nominal.firstFamily', string, number>
|
|
29
|
+
>
|
|
30
|
+
>
|
|
31
|
+
type WrongStateDoesNotCloseRequirement = Expect<
|
|
32
|
+
Equal<
|
|
33
|
+
Exclude<State.StateStore<'nominal.first', number>, Layer.Layer.Success<typeof SecondStateLive>>,
|
|
34
|
+
State.StateStore<'nominal.first', number>
|
|
35
|
+
>
|
|
36
|
+
>
|
|
37
|
+
type WrongFamilyDoesNotCloseRequirement = Expect<
|
|
38
|
+
Equal<
|
|
39
|
+
Exclude<
|
|
40
|
+
StateFamily.StateFamilyStore<'nominal.firstFamily', string, number>,
|
|
41
|
+
Layer.Layer.Success<typeof SecondFamilyLive>
|
|
42
|
+
>,
|
|
43
|
+
StateFamily.StateFamilyStore<'nominal.firstFamily', string, number>
|
|
44
|
+
>
|
|
45
|
+
>
|
|
46
|
+
type ProvidedByKeepsStateIdentity = Expect<
|
|
47
|
+
Equal<ProvidedBy<readonly [typeof FirstState]>, State.StateStore<'nominal.first', number>>
|
|
48
|
+
>
|
|
49
|
+
type ProvidedByKeepsFamilyIdentity = Expect<
|
|
50
|
+
Equal<
|
|
51
|
+
ProvidedBy<readonly [typeof FirstFamily]>,
|
|
52
|
+
StateFamily.StateFamilyStore<'nominal.firstFamily', string, number>
|
|
53
|
+
>
|
|
54
|
+
>
|
|
55
|
+
|
|
56
|
+
type StateIdentityProofs = [
|
|
57
|
+
StateLiveKeepsDefinitionIdentity,
|
|
58
|
+
FamilyLiveKeepsDefinitionIdentity,
|
|
59
|
+
WrongStateDoesNotCloseRequirement,
|
|
60
|
+
WrongFamilyDoesNotCloseRequirement,
|
|
61
|
+
ProvidedByKeepsStateIdentity,
|
|
62
|
+
ProvidedByKeepsFamilyIdentity,
|
|
63
|
+
]
|
|
64
|
+
declare const stateIdentityProofs: StateIdentityProofs
|
|
65
|
+
|
|
66
|
+
void stateIdentityProofs
|
|
67
|
+
|
|
68
|
+
export {}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { expect, it } from '@effect/vitest'
|
|
2
|
+
import { Effect, Layer, Schema as S } from 'effect'
|
|
3
|
+
import { State } from '../index'
|
|
4
|
+
|
|
5
|
+
it.live('same-shaped states resolve their own stores', () => {
|
|
6
|
+
class First extends State.make('state.nominal.first', S.Number) {}
|
|
7
|
+
class Second extends State.make('state.nominal.second', S.Number) {}
|
|
8
|
+
const Live = Layer.mergeAll(State.live(First, 1), State.live(Second, 2))
|
|
9
|
+
|
|
10
|
+
return Effect.gen(function* () {
|
|
11
|
+
const first = yield* First
|
|
12
|
+
const second = yield* Second
|
|
13
|
+
|
|
14
|
+
expect(first).toBe(1)
|
|
15
|
+
expect(second).toBe(2)
|
|
16
|
+
}).pipe(Effect.provide(Live))
|
|
17
|
+
})
|
package/src/state/state.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context, Effect, FiberRef, Layer, Option, Schema } from 'effect'
|
|
1
|
+
import { Context, Effect, FiberRef, Layer, Option, type ParseResult, Schema } from 'effect'
|
|
2
2
|
import type { Effect as EffectType } from 'effect/Effect'
|
|
3
3
|
import { type Manifest, yieldableClass } from '../definition/definition'
|
|
4
4
|
import { resolveScheduler } from '../internal/scheduler'
|
|
@@ -7,7 +7,6 @@ import { resolveInstrumentation, stateUpdateHook } from '../runtime/instrumentat
|
|
|
7
7
|
import { claimStateTag } from '../internal/stateRegistry'
|
|
8
8
|
import { makeStore, type Store } from '../internal/store'
|
|
9
9
|
import { readTracked } from '../internal/track'
|
|
10
|
-
import type { AnySchema, AnyValue } from '../internal/variance'
|
|
11
10
|
|
|
12
11
|
export interface StateOptionsExternalApi {
|
|
13
12
|
readonly title?: string
|
|
@@ -15,34 +14,72 @@ export interface StateOptionsExternalApi {
|
|
|
15
14
|
}
|
|
16
15
|
export type StateOptions = StateOptionsExternalApi
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
// Full Schema parameters are invariant; heterogeneous manifests expose only their AST.
|
|
18
|
+
export interface StateSchemaReflection {
|
|
19
|
+
readonly ast: Schema.Schema<unknown, unknown, unknown>['ast']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StateManifestExternalApi<N extends string> extends Manifest {
|
|
19
23
|
readonly kind: 'State'
|
|
20
24
|
readonly name: N
|
|
21
|
-
readonly schema:
|
|
25
|
+
readonly schema: StateSchemaReflection
|
|
22
26
|
readonly title?: string
|
|
23
27
|
readonly description?: string
|
|
24
28
|
}
|
|
25
|
-
export type StateManifest<N extends string
|
|
29
|
+
export type StateManifest<N extends string> = StateManifestExternalApi<N>
|
|
30
|
+
|
|
31
|
+
export const StateStoreTypeId: unique symbol = Symbol.for('reform/StateStore')
|
|
32
|
+
export type StateStoreTypeId = typeof StateStoreTypeId
|
|
33
|
+
|
|
34
|
+
export interface StateStore<N extends string, in out A> extends Store<A> {
|
|
35
|
+
readonly [StateStoreTypeId]: N
|
|
36
|
+
}
|
|
26
37
|
|
|
27
|
-
export
|
|
38
|
+
export type StateCapture<Result> = <N extends string, A>(state: StateClass<N, A>) => Result
|
|
39
|
+
|
|
40
|
+
export interface AnyState {
|
|
28
41
|
new (): {}
|
|
29
|
-
readonly manifest: StateManifest<
|
|
30
|
-
readonly
|
|
42
|
+
readonly manifest: StateManifest<string>
|
|
43
|
+
readonly capture: <Result>(visit: StateCapture<Result>) => Result
|
|
31
44
|
}
|
|
32
45
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
interface StateStatics<N extends string, A> {
|
|
47
|
+
readonly manifest: StateManifest<N>
|
|
48
|
+
// Subclasses change the class function name; this preserves the authored source name.
|
|
49
|
+
readonly name: N
|
|
50
|
+
readonly capture: <Result>(visit: StateCapture<Result>) => Result
|
|
51
|
+
readonly store: Context.Tag<StateStore<N, A>, Store<A>>
|
|
52
|
+
readonly decodeUnknown: (input: unknown) => Effect.Effect<A, ParseResult.ParseError, never>
|
|
53
|
+
readonly decodeUnknownSync: (input: unknown) => A
|
|
54
|
+
readonly decodeUnknownOption: (input: unknown) => Option.Option<A>
|
|
55
|
+
}
|
|
36
56
|
|
|
37
|
-
export
|
|
57
|
+
export interface StateClass<N extends string, A>
|
|
58
|
+
extends EffectType<A, never, StateStore<N, A>>, StateStatics<N, A> {
|
|
59
|
+
new (): {}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type StateDefinition<
|
|
63
|
+
N extends string,
|
|
64
|
+
A,
|
|
65
|
+
StateSchema extends Schema.Schema.AnyNoContext,
|
|
66
|
+
> = StateClass<N, A> & {
|
|
67
|
+
readonly manifest: StateManifest<N> & { readonly schema: StateSchema }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type StateValue<S> = S extends StateClass<infer _N, infer A> ? A : never
|
|
71
|
+
export type StateName<S> = S extends StateClass<infer N, infer _A> ? N : never
|
|
72
|
+
|
|
73
|
+
export const make = <const N extends string, StateSchema extends Schema.Schema.AnyNoContext>(
|
|
38
74
|
name: N,
|
|
39
|
-
schema:
|
|
75
|
+
schema: StateSchema,
|
|
40
76
|
options: StateOptions = {},
|
|
41
|
-
):
|
|
77
|
+
): StateDefinition<N, Schema.Schema.Type<StateSchema>, StateSchema> => {
|
|
78
|
+
type A = Schema.Schema.Type<StateSchema>
|
|
42
79
|
const identifier = `reform/state/${name}`
|
|
43
80
|
claimStateTag(identifier)
|
|
44
|
-
const store = Context.GenericTag<
|
|
45
|
-
const manifest: StateManifest<N
|
|
81
|
+
const store = Context.GenericTag<StateStore<N, A>, Store<A>>(identifier)
|
|
82
|
+
const manifest: StateManifest<N> & { readonly schema: StateSchema } = {
|
|
46
83
|
kind: 'State',
|
|
47
84
|
name,
|
|
48
85
|
schema,
|
|
@@ -50,25 +87,41 @@ export const make = <const N extends string, A>(
|
|
|
50
87
|
...(options.description !== undefined ? { description: options.description } : {}),
|
|
51
88
|
}
|
|
52
89
|
const read = Effect.flatMap(store, readTracked)
|
|
53
|
-
|
|
90
|
+
const state: StateDefinition<N, A, StateSchema> = yieldableClass<
|
|
91
|
+
A,
|
|
92
|
+
never,
|
|
93
|
+
StateStore<N, A>,
|
|
94
|
+
StateStatics<N, A> & {
|
|
95
|
+
readonly manifest: StateManifest<N> & { readonly schema: StateSchema }
|
|
96
|
+
}
|
|
97
|
+
>(read, {
|
|
98
|
+
manifest,
|
|
99
|
+
name,
|
|
100
|
+
store,
|
|
101
|
+
decodeUnknown: Schema.decodeUnknown(schema),
|
|
102
|
+
decodeUnknownSync: Schema.decodeUnknownSync(schema),
|
|
103
|
+
decodeUnknownOption: Schema.decodeUnknownOption(schema),
|
|
104
|
+
capture: <Result>(visit: StateCapture<Result>): Result => visit(state),
|
|
105
|
+
})
|
|
106
|
+
return state
|
|
54
107
|
}
|
|
55
108
|
|
|
56
|
-
const resolveSeed = <
|
|
57
|
-
state:
|
|
58
|
-
initial:
|
|
59
|
-
): Effect.Effect<
|
|
109
|
+
const resolveSeed = <N extends string, A>(
|
|
110
|
+
state: StateClass<N, A>,
|
|
111
|
+
initial: A,
|
|
112
|
+
): Effect.Effect<A, never, never> =>
|
|
60
113
|
Effect.map(FiberRef.get(CurrentSeedOverrides), (overrides) => {
|
|
61
114
|
if (!(state.manifest.name in overrides)) {
|
|
62
115
|
return initial
|
|
63
116
|
}
|
|
64
|
-
const override =
|
|
117
|
+
const override = state.decodeUnknownOption(overrides[state.manifest.name])
|
|
65
118
|
return Option.getOrElse(override, () => initial)
|
|
66
119
|
})
|
|
67
120
|
|
|
68
|
-
export const live = <
|
|
69
|
-
state:
|
|
70
|
-
initial:
|
|
71
|
-
): Layer.Layer<
|
|
121
|
+
export const live = <N extends string, A>(
|
|
122
|
+
state: StateClass<N, A>,
|
|
123
|
+
initial: A,
|
|
124
|
+
): Layer.Layer<StateStore<N, A>> =>
|
|
72
125
|
Layer.effect(
|
|
73
126
|
state.store,
|
|
74
127
|
Effect.all([resolveScheduler, resolveSeed(state, initial), resolveInstrumentation]).pipe(
|
|
@@ -4,6 +4,20 @@ import { Engine, Event, publish, Reducer, StateFamily } from '../index'
|
|
|
4
4
|
|
|
5
5
|
const tick = Effect.sleep(Duration.millis(1))
|
|
6
6
|
|
|
7
|
+
it.live('same-shaped families resolve their own stores', () => {
|
|
8
|
+
class First extends StateFamily.make('family.nominal.first', S.String, S.Number) {}
|
|
9
|
+
class Second extends StateFamily.make('family.nominal.second', S.String, S.Number) {}
|
|
10
|
+
const Live = Layer.mergeAll(StateFamily.live(First, 1), StateFamily.live(Second, 2))
|
|
11
|
+
|
|
12
|
+
return Effect.gen(function* () {
|
|
13
|
+
const first = yield* First.store
|
|
14
|
+
const second = yield* Second.store
|
|
15
|
+
|
|
16
|
+
expect(first.at('key').get()).toBe(1)
|
|
17
|
+
expect(second.at('key').get()).toBe(2)
|
|
18
|
+
}).pipe(Effect.provide(Live))
|
|
19
|
+
})
|
|
20
|
+
|
|
7
21
|
it.live("writing one key does not notify another key's subscribers", () => {
|
|
8
22
|
class Items extends StateFamily.make('items', S.String, S.Number) {}
|
|
9
23
|
const TestLayer = StateFamily.live(Items, 0)
|
package/src/state/stateFamily.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Context, Effect, Layer, Option } from 'effect'
|
|
1
|
+
import { Context, Effect, Layer, Option, Schema } from 'effect'
|
|
2
2
|
import { type Manifest, definitionClass } from '../definition/definition'
|
|
3
3
|
import { resolveScheduler, type Scheduler } from '../internal/scheduler'
|
|
4
4
|
import { claimStateTag } from '../internal/stateRegistry'
|
|
5
5
|
import { resolveInstrumentation, stateUpdateHook } from '../runtime/instrumentation'
|
|
6
6
|
import { makeStore, type Store } from '../internal/store'
|
|
7
7
|
import { readTracked } from '../internal/track'
|
|
8
|
-
import type { AnySchema, AnyValue } from '../internal/variance'
|
|
9
8
|
|
|
10
9
|
export const Tombstone: unique symbol = Symbol.for('reform/StateFamily/Tombstone')
|
|
11
10
|
export type Tombstone = typeof Tombstone
|
|
@@ -84,41 +83,66 @@ const makeFamilyStore = <K, V>(
|
|
|
84
83
|
}
|
|
85
84
|
}
|
|
86
85
|
|
|
87
|
-
|
|
86
|
+
// Full Schema parameters are invariant; heterogeneous manifests expose only their AST.
|
|
87
|
+
export interface FamilySchemaReflection {
|
|
88
|
+
readonly ast: Schema.Schema<unknown, unknown, unknown>['ast']
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface StateFamilyManifestExternalApi<N extends string> extends Manifest {
|
|
88
92
|
readonly kind: 'StateFamily'
|
|
89
93
|
readonly name: N
|
|
90
|
-
readonly key:
|
|
91
|
-
readonly value:
|
|
94
|
+
readonly key: FamilySchemaReflection
|
|
95
|
+
readonly value: FamilySchemaReflection
|
|
92
96
|
readonly title?: string
|
|
93
97
|
readonly description?: string
|
|
94
98
|
}
|
|
95
|
-
export type StateFamilyManifest<N extends string
|
|
99
|
+
export type StateFamilyManifest<N extends string> = StateFamilyManifestExternalApi<N>
|
|
100
|
+
|
|
101
|
+
export const StateFamilyStoreTypeId: unique symbol = Symbol.for('reform/StateFamilyStore')
|
|
102
|
+
export type StateFamilyStoreTypeId = typeof StateFamilyStoreTypeId
|
|
103
|
+
|
|
104
|
+
export interface StateFamilyStore<N extends string, in out K, in out V> extends FamilyStore<K, V> {
|
|
105
|
+
readonly [StateFamilyStoreTypeId]: N
|
|
106
|
+
}
|
|
96
107
|
|
|
97
|
-
export
|
|
108
|
+
export type StateFamilyCapture<Result> = <N extends string, K, V>(
|
|
109
|
+
family: StateFamilyClass<N, K, V>,
|
|
110
|
+
) => Result
|
|
111
|
+
|
|
112
|
+
export interface AnyFamily {
|
|
113
|
+
new (): {}
|
|
114
|
+
readonly manifest: StateFamilyManifest<string>
|
|
115
|
+
readonly capture: <Result>(visit: StateFamilyCapture<Result>) => Result
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface StateFamilyClass<N extends string, K, V> {
|
|
98
119
|
new (): { readonly Key: K }
|
|
99
|
-
readonly manifest: StateFamilyManifest<N
|
|
100
|
-
readonly
|
|
120
|
+
readonly manifest: StateFamilyManifest<N>
|
|
121
|
+
readonly capture: <Result>(visit: StateFamilyCapture<Result>) => Result
|
|
122
|
+
readonly store: Context.Tag<StateFamilyStore<N, K, V>, FamilyStore<K, V>>
|
|
123
|
+
readonly decodeKeySync: (input: unknown) => K
|
|
124
|
+
readonly decodeValueSync: (input: unknown) => V
|
|
101
125
|
}
|
|
102
126
|
|
|
103
|
-
export type
|
|
104
|
-
export type FamilyKey<F> = F extends StateFamilyClass<
|
|
105
|
-
export type FamilyValue<F> = F extends StateFamilyClass<
|
|
127
|
+
export type FamilyName<F> = F extends StateFamilyClass<infer N, infer _K, infer _V> ? N : never
|
|
128
|
+
export type FamilyKey<F> = F extends StateFamilyClass<infer _N, infer K, infer _V> ? K : never
|
|
129
|
+
export type FamilyValue<F> = F extends StateFamilyClass<infer _N, infer _K, infer V> ? V : never
|
|
106
130
|
|
|
107
131
|
export interface StateFamilyMakeOptionsExternalApi {
|
|
108
132
|
readonly title?: string
|
|
109
133
|
readonly description?: string
|
|
110
134
|
}
|
|
111
135
|
|
|
112
|
-
export const make = <const N extends string, K, V>(
|
|
136
|
+
export const make = <const N extends string, K, KeyEncoded, V, ValueEncoded>(
|
|
113
137
|
name: N,
|
|
114
|
-
key:
|
|
115
|
-
valueSchema:
|
|
138
|
+
key: Schema.Schema<K, KeyEncoded, never>,
|
|
139
|
+
valueSchema: Schema.Schema<V, ValueEncoded, never>,
|
|
116
140
|
options: StateFamilyMakeOptionsExternalApi = {},
|
|
117
141
|
): StateFamilyClass<N, K, V> => {
|
|
118
142
|
const identifier = `reform/family/${name}`
|
|
119
143
|
claimStateTag(identifier)
|
|
120
|
-
const store = Context.GenericTag<
|
|
121
|
-
const manifest: StateFamilyManifest<N
|
|
144
|
+
const store = Context.GenericTag<StateFamilyStore<N, K, V>, FamilyStore<K, V>>(identifier)
|
|
145
|
+
const manifest: StateFamilyManifest<N> = {
|
|
122
146
|
kind: 'StateFamily',
|
|
123
147
|
name,
|
|
124
148
|
key,
|
|
@@ -126,22 +150,31 @@ export const make = <const N extends string, K, V>(
|
|
|
126
150
|
...(options.title !== undefined ? { title: options.title } : {}),
|
|
127
151
|
...(options.description !== undefined ? { description: options.description } : {}),
|
|
128
152
|
}
|
|
129
|
-
|
|
153
|
+
const family: StateFamilyClass<N, K, V> = definitionClass<StateFamilyClass<N, K, V>>({
|
|
154
|
+
manifest,
|
|
155
|
+
store,
|
|
156
|
+
decodeKeySync: Schema.decodeUnknownSync(key),
|
|
157
|
+
decodeValueSync: Schema.decodeUnknownSync(valueSchema),
|
|
158
|
+
capture: <Result>(visit: StateFamilyCapture<Result>): Result => visit(family),
|
|
159
|
+
})
|
|
160
|
+
return family
|
|
130
161
|
}
|
|
131
162
|
|
|
132
163
|
export const read = <N extends string, K, V>(
|
|
133
164
|
family: StateFamilyClass<N, K, V>,
|
|
134
165
|
key: K,
|
|
135
|
-
): Effect.Effect<V, never,
|
|
166
|
+
): Effect.Effect<V, never, StateFamilyStore<N, K, V>> =>
|
|
136
167
|
Effect.flatMap(family.store, (familyStore) => readTracked(familyStore.at(key)))
|
|
137
168
|
|
|
138
169
|
export const live = <N extends string, K, V>(
|
|
139
170
|
family: StateFamilyClass<N, K, V>,
|
|
140
171
|
initial: V | ((key: K) => V),
|
|
141
172
|
options: FamilyOptions = {},
|
|
142
|
-
): Layer.Layer<
|
|
143
|
-
|
|
144
|
-
|
|
173
|
+
): Layer.Layer<StateFamilyStore<N, K, V>> => {
|
|
174
|
+
const seed = (key: K): V =>
|
|
175
|
+
typeof initial === 'function'
|
|
176
|
+
? family.decodeValueSync(Reflect.apply(initial, undefined, [key]))
|
|
177
|
+
: initial
|
|
145
178
|
return Layer.effect(
|
|
146
179
|
family.store,
|
|
147
180
|
Effect.zipWith(resolveScheduler, resolveInstrumentation, (scheduler, instrumentation) =>
|