@playfast/reform-proof 0.1.0 → 1.1.1
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 +1 -1
- package/src/assertions.ts +65 -0
- package/src/driverTypes.ts +19 -0
- package/src/engine.ts +8 -520
- package/src/errors.ts +0 -14
- package/src/execution.ts +100 -0
- package/src/facade.ts +258 -0
- package/src/index.ts +53 -294
- package/src/product.ts +77 -0
- package/src/runner.ts +1 -17
- package/src/runtimeFacade.ts +269 -0
- package/src/runtimeTreeDispose.ts +21 -0
- package/src/runtimeTreeDriver.ts +285 -0
- package/src/runtimeTreeFacade.ts +134 -0
- package/src/runtimeTreeSettle.ts +34 -0
- package/src/runtimeTreeTypes.ts +99 -0
- package/src/sink.ts +73 -0
- package/src/structure-events.test.ts +0 -11
- package/src/structure-locators.test.ts +0 -23
- package/src/treeBindings.ts +121 -0
- package/src/typed-seams.test.ts +0 -11
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { Array as Arr, Effect, Option, Record as Rec } from 'effect'
|
|
2
|
+
import {
|
|
3
|
+
Composition,
|
|
4
|
+
type CompositionClass,
|
|
5
|
+
isStructure,
|
|
6
|
+
type RenderEnv,
|
|
7
|
+
type RuntimeHandle,
|
|
8
|
+
type SlotClass,
|
|
9
|
+
type Trigger,
|
|
10
|
+
} from '@playfast/reform'
|
|
11
|
+
import { AssertionFailed, UnknownAction, UnknownSlot } from './errors'
|
|
12
|
+
import { matchProps } from './assertions'
|
|
13
|
+
import { capturesFor, fingerprint } from './facade'
|
|
14
|
+
import {
|
|
15
|
+
keyed,
|
|
16
|
+
SETTLE_MAX_RENDERS,
|
|
17
|
+
SETTLE_STEP,
|
|
18
|
+
settleDrain,
|
|
19
|
+
type Sink,
|
|
20
|
+
uiNameOf,
|
|
21
|
+
} from './sink'
|
|
22
|
+
import {
|
|
23
|
+
childComposition,
|
|
24
|
+
driveStructure,
|
|
25
|
+
type Mounted,
|
|
26
|
+
type NodeRef,
|
|
27
|
+
type SettleProgress,
|
|
28
|
+
slotsOf,
|
|
29
|
+
} from './treeBindings'
|
|
30
|
+
|
|
31
|
+
export interface MountedFacade {
|
|
32
|
+
readonly props: Effect.Effect<unknown, never, never>
|
|
33
|
+
readonly expectProps: (partial: unknown) => Effect.Effect<void, never, never>
|
|
34
|
+
readonly actions: Record<string, (payload: unknown) => Effect.Effect<unknown, never, never>>
|
|
35
|
+
readonly slots: Record<string, MountedSlotFacade>
|
|
36
|
+
readonly frame: Effect.Effect<void, never, never>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface MountedSlotFacade extends MountedFacade {
|
|
40
|
+
readonly first: Effect.Effect<MountedFacade, never, never>
|
|
41
|
+
readonly at: (index: number) => Effect.Effect<MountedFacade, never, never>
|
|
42
|
+
readonly all: Effect.Effect<ReadonlyArray<MountedFacade>, never, never>
|
|
43
|
+
readonly byKey: (key: string) => Effect.Effect<MountedFacade, never, never>
|
|
44
|
+
readonly where: (
|
|
45
|
+
predicate: (props: unknown) => boolean,
|
|
46
|
+
) => Effect.Effect<ReadonlyArray<MountedFacade>, never, never>
|
|
47
|
+
readonly count: Effect.Effect<number, never, never>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const collectRuntimeBindings = (
|
|
51
|
+
root: CompositionClass<unknown>,
|
|
52
|
+
runtime: RuntimeHandle,
|
|
53
|
+
): Map<SlotClass, CompositionClass<unknown>> => {
|
|
54
|
+
const bindings = new Map<SlotClass, CompositionClass<unknown>>()
|
|
55
|
+
const walk = (comp: CompositionClass<unknown>): void => {
|
|
56
|
+
Rec.values(slotsOf(comp)).forEach((slotClass) => {
|
|
57
|
+
if (bindings.has(slotClass)) {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
const child = childComposition(runtime.read(slotClass.tag))
|
|
61
|
+
bindings.set(slotClass, child)
|
|
62
|
+
walk(child)
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
walk(root)
|
|
66
|
+
return bindings
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const renderMountedRuntime = (
|
|
70
|
+
runtime: RuntimeHandle,
|
|
71
|
+
mounted: Mounted,
|
|
72
|
+
bindings: ReadonlyMap<SlotClass, CompositionClass<unknown>>,
|
|
73
|
+
sink: Sink,
|
|
74
|
+
): ReadonlyArray<Mounted> => {
|
|
75
|
+
const service = runtime.read(mounted.comp.tag)
|
|
76
|
+
const env: RenderEnv = { props: mounted.props, tracker: { add: () => {} } }
|
|
77
|
+
const endRenderSpan = sink.instrumentation.uiRendered(uiNameOf(mounted.comp))
|
|
78
|
+
const frame = Effect.runSync(Composition.render(service, env))
|
|
79
|
+
endRenderSpan()
|
|
80
|
+
if (!isStructure(frame)) {
|
|
81
|
+
return Effect.runSync(
|
|
82
|
+
Effect.dieMessage(
|
|
83
|
+
`reform-proof: composition ${uiNameOf(mounted.comp)} did not return a Structure`,
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
return driveStructure(mounted.comp, frame, mounted.key, sink, bindings)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const renderLevelRuntime = (
|
|
91
|
+
runtime: RuntimeHandle,
|
|
92
|
+
frontier: ReadonlyArray<Mounted>,
|
|
93
|
+
bindings: ReadonlyMap<SlotClass, CompositionClass<unknown>>,
|
|
94
|
+
sink: Sink,
|
|
95
|
+
): void => {
|
|
96
|
+
if (frontier.length === 0) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
const levels = frontier.map((mounted) => renderMountedRuntime(runtime, mounted, bindings, sink))
|
|
100
|
+
renderLevelRuntime(runtime, levels.flat(), bindings, sink)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const renderTreeRuntime = (
|
|
104
|
+
runtime: RuntimeHandle,
|
|
105
|
+
root: CompositionClass<unknown>,
|
|
106
|
+
bindings: ReadonlyMap<SlotClass, CompositionClass<unknown>>,
|
|
107
|
+
sink: Sink,
|
|
108
|
+
): Effect.Effect<void, never, never> =>
|
|
109
|
+
Effect.sync(() => {
|
|
110
|
+
sink.reset()
|
|
111
|
+
renderLevelRuntime(runtime, [{ comp: root, props: {}, key: Option.none() }], bindings, sink)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
const settleTreeRuntime = Effect.fn('settleTreeRuntime')(function* (
|
|
115
|
+
runtime: RuntimeHandle,
|
|
116
|
+
root: CompositionClass<unknown>,
|
|
117
|
+
bindings: ReadonlyMap<SlotClass, CompositionClass<unknown>>,
|
|
118
|
+
sink: Sink,
|
|
119
|
+
): Effect.fn.Return<void, never, never> {
|
|
120
|
+
yield* settleDrain
|
|
121
|
+
yield* renderTreeRuntime(runtime, root, bindings, sink)
|
|
122
|
+
yield* Effect.iterate(
|
|
123
|
+
{
|
|
124
|
+
previous: fingerprint(sink),
|
|
125
|
+
remaining: SETTLE_MAX_RENDERS,
|
|
126
|
+
stable: false,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
while: (progress: SettleProgress) => !progress.stable && progress.remaining > 0,
|
|
130
|
+
body: (progress) =>
|
|
131
|
+
Effect.gen(function* () {
|
|
132
|
+
yield* settleDrain
|
|
133
|
+
yield* Effect.sleep(SETTLE_STEP)
|
|
134
|
+
yield* renderTreeRuntime(runtime, root, bindings, sink)
|
|
135
|
+
const current = fingerprint(sink)
|
|
136
|
+
return {
|
|
137
|
+
previous: current,
|
|
138
|
+
remaining: progress.remaining - 1,
|
|
139
|
+
stable: current === progress.previous,
|
|
140
|
+
}
|
|
141
|
+
}),
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const makeMountedFacade = (
|
|
147
|
+
runtime: RuntimeHandle,
|
|
148
|
+
root: CompositionClass<unknown>,
|
|
149
|
+
sink: Sink,
|
|
150
|
+
dispatched: Set<string>,
|
|
151
|
+
): {
|
|
152
|
+
readonly facade: MountedFacade
|
|
153
|
+
readonly settle: Effect.Effect<void, never, never>
|
|
154
|
+
} => {
|
|
155
|
+
const bindings = collectRuntimeBindings(root, runtime)
|
|
156
|
+
const settle = settleTreeRuntime(runtime, root, bindings, sink)
|
|
157
|
+
const rerender = renderTreeRuntime(runtime, root, bindings, sink)
|
|
158
|
+
|
|
159
|
+
const triggerOf = (ref: NodeRef, event: string): Effect.Effect<Trigger<unknown>, never, never> =>
|
|
160
|
+
Option.match(Option.fromNullable(capturesFor(sink, ref.name)[ref.index]?.events[event]), {
|
|
161
|
+
onNone: () =>
|
|
162
|
+
Effect.dieMessage(
|
|
163
|
+
new UnknownAction({
|
|
164
|
+
composition: ref.name,
|
|
165
|
+
action: event,
|
|
166
|
+
rendered: capturesFor(sink, ref.name).length,
|
|
167
|
+
}).message,
|
|
168
|
+
),
|
|
169
|
+
onSome: Effect.succeed,
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
const propsFor = (ref: NodeRef): Effect.Effect<unknown, never, never> =>
|
|
173
|
+
Effect.map(rerender, () => capturesFor(sink, ref.name)[ref.index]?.props)
|
|
174
|
+
|
|
175
|
+
const nodeFacade = (ref: NodeRef, comp: CompositionClass<unknown>): MountedFacade => ({
|
|
176
|
+
props: propsFor(ref),
|
|
177
|
+
expectProps: (partial) =>
|
|
178
|
+
propsFor(ref).pipe(
|
|
179
|
+
Effect.flatMap((props) => {
|
|
180
|
+
const mismatch = matchProps({ actual: props, expected: partial })
|
|
181
|
+
return mismatch === undefined
|
|
182
|
+
? Effect.void
|
|
183
|
+
: Effect.dieMessage(new AssertionFailed({ detail: mismatch }).message)
|
|
184
|
+
}),
|
|
185
|
+
),
|
|
186
|
+
actions: keyed(
|
|
187
|
+
(event) => (payload: unknown) =>
|
|
188
|
+
rerender.pipe(
|
|
189
|
+
Effect.flatMap(() => triggerOf(ref, event)),
|
|
190
|
+
Effect.flatMap((trigger) =>
|
|
191
|
+
Effect.sync(() => {
|
|
192
|
+
dispatched.add(event)
|
|
193
|
+
trigger(payload)
|
|
194
|
+
}),
|
|
195
|
+
),
|
|
196
|
+
Effect.flatMap(() => settle),
|
|
197
|
+
Effect.flatMap(() => propsFor(ref)),
|
|
198
|
+
),
|
|
199
|
+
),
|
|
200
|
+
slots: keyed((slotName) => slotFacade(comp, slotName)),
|
|
201
|
+
frame: settle,
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
const slotFacade = (parent: CompositionClass<unknown>, slotName: string): MountedSlotFacade => {
|
|
205
|
+
const slotClass = slotsOf(parent)[slotName]
|
|
206
|
+
const child = slotClass && bindings.get(slotClass)
|
|
207
|
+
if (child === undefined) {
|
|
208
|
+
return Effect.runSync(
|
|
209
|
+
Effect.dieMessage(new UnknownSlot({ parent: uiNameOf(parent), slot: slotName }).message),
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
const childName = uiNameOf(child)
|
|
213
|
+
const atIndex = (index: number): Effect.Effect<MountedFacade, never, never> =>
|
|
214
|
+
Effect.as(rerender, nodeFacade({ name: childName, index }, child))
|
|
215
|
+
const first = nodeFacade({ name: childName, index: 0 }, child)
|
|
216
|
+
const indexOfKey = (key: string): Option.Option<number> =>
|
|
217
|
+
Arr.findFirstIndex(capturesFor(sink, childName), (capture) => capture.key === key)
|
|
218
|
+
return {
|
|
219
|
+
first: atIndex(0),
|
|
220
|
+
at: atIndex,
|
|
221
|
+
all: Effect.map(rerender, () =>
|
|
222
|
+
capturesFor(sink, childName).map((_capture, index) =>
|
|
223
|
+
nodeFacade({ name: childName, index }, child),
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
byKey: (key) =>
|
|
227
|
+
Effect.flatMap(rerender, () =>
|
|
228
|
+
Option.match(indexOfKey(key), {
|
|
229
|
+
onNone: () =>
|
|
230
|
+
Effect.dieMessage(
|
|
231
|
+
new UnknownSlot({
|
|
232
|
+
parent: uiNameOf(parent),
|
|
233
|
+
slot: `${slotName}[key=${key}]`,
|
|
234
|
+
}).message,
|
|
235
|
+
),
|
|
236
|
+
onSome: (index) => Effect.succeed(nodeFacade({ name: childName, index }, child)),
|
|
237
|
+
}),
|
|
238
|
+
),
|
|
239
|
+
where: (predicate) =>
|
|
240
|
+
Effect.map(rerender, () =>
|
|
241
|
+
capturesFor(sink, childName).flatMap((capture, index) =>
|
|
242
|
+
predicate(capture.props) ? [nodeFacade({ name: childName, index }, child)] : [],
|
|
243
|
+
),
|
|
244
|
+
),
|
|
245
|
+
count: Effect.map(rerender, () => capturesFor(sink, childName).length),
|
|
246
|
+
props: first.props,
|
|
247
|
+
expectProps: first.expectProps,
|
|
248
|
+
actions: first.actions,
|
|
249
|
+
slots: first.slots,
|
|
250
|
+
frame: first.frame,
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
facade: nodeFacade({ name: uiNameOf(root), index: 0 }, root),
|
|
256
|
+
settle,
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export const makeRuntimeHandleFacade = (
|
|
261
|
+
runtime: RuntimeHandle,
|
|
262
|
+
root: CompositionClass<unknown>,
|
|
263
|
+
sink: Sink,
|
|
264
|
+
dispatched: Set<string>,
|
|
265
|
+
): {
|
|
266
|
+
readonly facade: MountedFacade
|
|
267
|
+
readonly settle: Effect.Effect<void, never, never>
|
|
268
|
+
} => makeMountedFacade(runtime, root, sink, dispatched)
|
|
269
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Array as Arr, Order } from 'effect'
|
|
2
|
+
import type { TreeFeatureMount } from './runtimeTreeTypes'
|
|
3
|
+
|
|
4
|
+
interface DisposeTreeMountsOptions {
|
|
5
|
+
readonly status: { disposed: boolean }
|
|
6
|
+
readonly mounts: Map<string, TreeFeatureMount>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const disposeTreeMounts = ({ status, mounts }: DisposeTreeMountsOptions): void => {
|
|
10
|
+
if (status.disposed) {
|
|
11
|
+
return
|
|
12
|
+
}
|
|
13
|
+
status.disposed = true
|
|
14
|
+
Arr.sortWith(Array.from(mounts.values()), (record) => -record.depth, Order.number).forEach(
|
|
15
|
+
(record) => {
|
|
16
|
+
record.attempt += 1
|
|
17
|
+
record.dispose()
|
|
18
|
+
},
|
|
19
|
+
)
|
|
20
|
+
mounts.clear()
|
|
21
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { Array as Arr, Effect, Match, Option, Order, Record as Rec, Schema as S } from 'effect'
|
|
2
|
+
import {
|
|
3
|
+
Composition,
|
|
4
|
+
type CompositionClass,
|
|
5
|
+
isFeatureBinding,
|
|
6
|
+
isStructure,
|
|
7
|
+
type RenderEnv,
|
|
8
|
+
type RuntimeHandle,
|
|
9
|
+
} from '@playfast/reform'
|
|
10
|
+
import { eventsOf, type Sink, uiNameOf } from './sink'
|
|
11
|
+
import { slotsOf, structureFills } from './treeBindings'
|
|
12
|
+
import { disposeTreeMounts } from './runtimeTreeDispose'
|
|
13
|
+
import { makeTreeSettle } from './runtimeTreeSettle'
|
|
14
|
+
import {
|
|
15
|
+
encodeIdentityPart,
|
|
16
|
+
type FeatureRecordInput,
|
|
17
|
+
fillEntries,
|
|
18
|
+
type MountIdentityInput,
|
|
19
|
+
type RenderCompositionInput,
|
|
20
|
+
type TreeDriver,
|
|
21
|
+
type TreeFeatureMount,
|
|
22
|
+
type TreeNode,
|
|
23
|
+
type TreeRenderState,
|
|
24
|
+
treeFingerprint,
|
|
25
|
+
} from './runtimeTreeTypes'
|
|
26
|
+
|
|
27
|
+
const encodeUnknown = S.encodeSync(S.parseJson(S.Unknown))
|
|
28
|
+
|
|
29
|
+
export const makeTreeDriver = (
|
|
30
|
+
runtime: RuntimeHandle,
|
|
31
|
+
root: CompositionClass<unknown>,
|
|
32
|
+
sink: Sink,
|
|
33
|
+
): TreeDriver => {
|
|
34
|
+
const mounts = new Map<string, TreeFeatureMount>()
|
|
35
|
+
const runtimeIds = new WeakMap<RuntimeHandle, number>()
|
|
36
|
+
const bindingIds = new WeakMap<object, number>()
|
|
37
|
+
const sequence = { runtime: 0, binding: 0 }
|
|
38
|
+
const status = { disposed: false }
|
|
39
|
+
|
|
40
|
+
const identity = (
|
|
41
|
+
identities: WeakMap<object, number>,
|
|
42
|
+
subject: object,
|
|
43
|
+
next: () => number,
|
|
44
|
+
): number => {
|
|
45
|
+
const current = identities.get(subject)
|
|
46
|
+
if (current !== undefined) {
|
|
47
|
+
return current
|
|
48
|
+
}
|
|
49
|
+
const created = next()
|
|
50
|
+
identities.set(subject, created)
|
|
51
|
+
return created
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const mountId = ({ parent, parentPath, slotName, key, binding }: MountIdentityInput): string => {
|
|
55
|
+
const runtimeId = identity(runtimeIds, parent, () => ++sequence.runtime)
|
|
56
|
+
const bindingId = identity(bindingIds, binding, () => ++sequence.binding)
|
|
57
|
+
return [String(runtimeId), parentPath, slotName, String(bindingId), key]
|
|
58
|
+
.map(encodeIdentityPart)
|
|
59
|
+
.join('')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const startMount = (record: TreeFeatureMount): void => {
|
|
63
|
+
record.attempt += 1
|
|
64
|
+
const attempt = record.attempt
|
|
65
|
+
if (record.binding.strategy === 'default') {
|
|
66
|
+
record.state = { _tag: 'Live', runtime: record.parent }
|
|
67
|
+
record.binding.boot.forEach((event) => record.parent.dispatch('High', event))
|
|
68
|
+
record.dispose = () => {}
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
record.state = { _tag: 'Loading' }
|
|
72
|
+
record.dispose = record.parent.mountFeature(record.binding, {
|
|
73
|
+
props: record.props,
|
|
74
|
+
onLive: (childRuntime) => {
|
|
75
|
+
if (status.disposed || record.attempt !== attempt || mounts.get(record.id) !== record) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
record.state = { _tag: 'Live', runtime: childRuntime }
|
|
79
|
+
},
|
|
80
|
+
onFailed: (error) => {
|
|
81
|
+
if (status.disposed || record.attempt !== attempt || mounts.get(record.id) !== record) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
record.state = { _tag: 'Failed', error }
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const retryMount = (record: TreeFeatureMount): void => {
|
|
90
|
+
if (status.disposed || mounts.get(record.id) !== record || record.state._tag !== 'Failed') {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
record.dispose()
|
|
94
|
+
startMount(record)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const featureRecord = ({
|
|
98
|
+
id,
|
|
99
|
+
parent,
|
|
100
|
+
binding,
|
|
101
|
+
depth,
|
|
102
|
+
props,
|
|
103
|
+
}: FeatureRecordInput): TreeFeatureMount => {
|
|
104
|
+
const current = mounts.get(id)
|
|
105
|
+
if (current !== undefined && current.binding === binding && current.parent === parent) {
|
|
106
|
+
return current
|
|
107
|
+
}
|
|
108
|
+
if (current !== undefined) {
|
|
109
|
+
current.attempt += 1
|
|
110
|
+
current.dispose()
|
|
111
|
+
}
|
|
112
|
+
const created: TreeFeatureMount = {
|
|
113
|
+
id,
|
|
114
|
+
binding,
|
|
115
|
+
parent,
|
|
116
|
+
depth,
|
|
117
|
+
props,
|
|
118
|
+
attempt: 0,
|
|
119
|
+
state: { _tag: 'Loading' },
|
|
120
|
+
dispose: () => {},
|
|
121
|
+
}
|
|
122
|
+
mounts.set(id, created)
|
|
123
|
+
startMount(created)
|
|
124
|
+
return created
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const renderComposition = ({
|
|
128
|
+
runtime: currentRuntime,
|
|
129
|
+
comp,
|
|
130
|
+
props,
|
|
131
|
+
key,
|
|
132
|
+
path,
|
|
133
|
+
depth,
|
|
134
|
+
seen,
|
|
135
|
+
}: RenderCompositionInput): TreeNode => {
|
|
136
|
+
const service = currentRuntime.read(comp.tag)
|
|
137
|
+
const env: RenderEnv = { props, tracker: { add: () => {} } }
|
|
138
|
+
const endRenderSpan = sink.instrumentation.uiRendered(uiNameOf(comp))
|
|
139
|
+
const frame = Effect.runSync(Composition.render(service, env))
|
|
140
|
+
endRenderSpan()
|
|
141
|
+
if (!isStructure(frame)) {
|
|
142
|
+
return Effect.runSync(
|
|
143
|
+
Effect.dieMessage(`reform-proof: composition ${uiNameOf(comp)} did not return a Structure`),
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
sink.api.record({
|
|
147
|
+
name: uiNameOf(comp),
|
|
148
|
+
props: frame.props,
|
|
149
|
+
events: eventsOf(frame),
|
|
150
|
+
...(Option.isSome(key) ? { key: key.value } : {}),
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const fills = structureFills(frame)
|
|
154
|
+
const renderedSlots = Rec.map(slotsOf(comp), (slotClass, slotName) => {
|
|
155
|
+
const fill = fills[slotName]
|
|
156
|
+
if (fill === undefined) {
|
|
157
|
+
return []
|
|
158
|
+
}
|
|
159
|
+
const child = currentRuntime.read(slotClass.tag)
|
|
160
|
+
return fillEntries(slotName, fill).flatMap((entry) => {
|
|
161
|
+
const childPath = `${path}${encodeIdentityPart(slotName)}${encodeIdentityPart(entry.key)}`
|
|
162
|
+
if (!isFeatureBinding(child)) {
|
|
163
|
+
return [
|
|
164
|
+
renderComposition({
|
|
165
|
+
runtime: currentRuntime,
|
|
166
|
+
comp: child,
|
|
167
|
+
props: entry.props,
|
|
168
|
+
key: Option.some(entry.key),
|
|
169
|
+
path: childPath,
|
|
170
|
+
depth: depth + 1,
|
|
171
|
+
seen,
|
|
172
|
+
}),
|
|
173
|
+
]
|
|
174
|
+
}
|
|
175
|
+
const id = mountId({
|
|
176
|
+
parent: currentRuntime,
|
|
177
|
+
parentPath: path,
|
|
178
|
+
slotName,
|
|
179
|
+
key: entry.key,
|
|
180
|
+
binding: child,
|
|
181
|
+
})
|
|
182
|
+
seen.add(id)
|
|
183
|
+
const record = featureRecord({
|
|
184
|
+
id,
|
|
185
|
+
parent: currentRuntime,
|
|
186
|
+
binding: child,
|
|
187
|
+
depth: depth + 1,
|
|
188
|
+
props: entry.props,
|
|
189
|
+
})
|
|
190
|
+
return Match.value(record.state).pipe(
|
|
191
|
+
Match.when({ _tag: 'Live' }, (live) => [
|
|
192
|
+
renderComposition({
|
|
193
|
+
runtime: live.runtime,
|
|
194
|
+
comp: child.composition,
|
|
195
|
+
props: entry.props,
|
|
196
|
+
key: Option.some(entry.key),
|
|
197
|
+
path: childPath,
|
|
198
|
+
depth: depth + 1,
|
|
199
|
+
seen,
|
|
200
|
+
}),
|
|
201
|
+
]),
|
|
202
|
+
Match.when({ _tag: 'Failed' }, (failed) => {
|
|
203
|
+
if (child.placeholder === undefined) {
|
|
204
|
+
return []
|
|
205
|
+
}
|
|
206
|
+
return [
|
|
207
|
+
renderComposition({
|
|
208
|
+
runtime: currentRuntime,
|
|
209
|
+
comp: child.placeholder.failed,
|
|
210
|
+
props: { error: failed.error, retry: () => retryMount(record) },
|
|
211
|
+
key: Option.some(entry.key),
|
|
212
|
+
path: `${childPath}/failed`,
|
|
213
|
+
depth: depth + 1,
|
|
214
|
+
seen,
|
|
215
|
+
}),
|
|
216
|
+
]
|
|
217
|
+
}),
|
|
218
|
+
Match.when({ _tag: 'Loading' }, () => {
|
|
219
|
+
if (child.placeholder === undefined) {
|
|
220
|
+
return []
|
|
221
|
+
}
|
|
222
|
+
return [
|
|
223
|
+
renderComposition({
|
|
224
|
+
runtime: currentRuntime,
|
|
225
|
+
comp: child.placeholder.loading,
|
|
226
|
+
props: entry.props,
|
|
227
|
+
key: Option.some(entry.key),
|
|
228
|
+
path: `${childPath}/loading`,
|
|
229
|
+
depth: depth + 1,
|
|
230
|
+
seen,
|
|
231
|
+
}),
|
|
232
|
+
]
|
|
233
|
+
}),
|
|
234
|
+
Match.exhaustive,
|
|
235
|
+
)
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
path,
|
|
241
|
+
depth,
|
|
242
|
+
key,
|
|
243
|
+
comp,
|
|
244
|
+
props: frame.props,
|
|
245
|
+
events: eventsOf(frame),
|
|
246
|
+
slots: renderedSlots,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const disposeStale = (seen: ReadonlySet<string>): void => {
|
|
251
|
+
Arr.sortWith(
|
|
252
|
+
Array.from(mounts.values()).filter((record) => !seen.has(record.id)),
|
|
253
|
+
(record) => -record.depth,
|
|
254
|
+
Order.number,
|
|
255
|
+
).forEach((record) => {
|
|
256
|
+
mounts.delete(record.id)
|
|
257
|
+
record.attempt += 1
|
|
258
|
+
record.dispose()
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const render = (): TreeRenderState => {
|
|
263
|
+
sink.reset()
|
|
264
|
+
const seen = new Set<string>()
|
|
265
|
+
const rootNode = renderComposition({
|
|
266
|
+
runtime,
|
|
267
|
+
comp: root,
|
|
268
|
+
props: {},
|
|
269
|
+
key: Option.none(),
|
|
270
|
+
path: 'root',
|
|
271
|
+
depth: 0,
|
|
272
|
+
seen,
|
|
273
|
+
})
|
|
274
|
+
disposeStale(seen)
|
|
275
|
+
return {
|
|
276
|
+
root: rootNode,
|
|
277
|
+
fingerprint: encodeUnknown(treeFingerprint(rootNode)),
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const settle = makeTreeSettle(render)
|
|
282
|
+
const dispose = (): void => disposeTreeMounts({ status, mounts })
|
|
283
|
+
|
|
284
|
+
return { render, settle, dispose }
|
|
285
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Array as Arr, Effect, Option } from 'effect'
|
|
2
|
+
import type { CompositionClass, RuntimeHandle } from '@playfast/reform'
|
|
3
|
+
import { AssertionFailed, UnknownAction, UnknownSlot } from './errors'
|
|
4
|
+
import { matchProps } from './assertions'
|
|
5
|
+
import { makeTreeDriver } from './runtimeTreeDriver'
|
|
6
|
+
import type { RuntimeTreeFacade, TreeNode } from './runtimeTreeTypes'
|
|
7
|
+
import { keyed, type Sink, uiNameOf } from './sink'
|
|
8
|
+
import type { MountedFacade, MountedSlotFacade } from './runtimeFacade'
|
|
9
|
+
|
|
10
|
+
export type { RuntimeTreeFacade } from './runtimeTreeTypes'
|
|
11
|
+
|
|
12
|
+
export const makeRuntimeTreeFacade = (
|
|
13
|
+
runtime: RuntimeHandle,
|
|
14
|
+
root: CompositionClass<unknown>,
|
|
15
|
+
sink: Sink,
|
|
16
|
+
dispatched: Set<string>,
|
|
17
|
+
): RuntimeTreeFacade => {
|
|
18
|
+
const driver = makeTreeDriver(runtime, root, sink)
|
|
19
|
+
const currentRoot = (): TreeNode => driver.render().root
|
|
20
|
+
|
|
21
|
+
const nodeFacade = (resolve: () => TreeNode): MountedFacade => ({
|
|
22
|
+
props: Effect.sync(() => resolve().props),
|
|
23
|
+
expectProps: (partial) =>
|
|
24
|
+
Effect.sync(() => {
|
|
25
|
+
const mismatch = matchProps({
|
|
26
|
+
actual: resolve().props,
|
|
27
|
+
expected: partial,
|
|
28
|
+
})
|
|
29
|
+
if (mismatch !== undefined) {
|
|
30
|
+
return Effect.runSync(
|
|
31
|
+
Effect.dieMessage(new AssertionFailed({ detail: mismatch }).message),
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
}),
|
|
35
|
+
actions: keyed(
|
|
36
|
+
(event) => (payload: unknown) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const node = resolve()
|
|
39
|
+
const trigger = node.events[event]
|
|
40
|
+
if (trigger === undefined) {
|
|
41
|
+
return yield* Effect.dieMessage(
|
|
42
|
+
new UnknownAction({
|
|
43
|
+
composition: uiNameOf(node.comp),
|
|
44
|
+
action: event,
|
|
45
|
+
rendered: 1,
|
|
46
|
+
}).message,
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
dispatched.add(event)
|
|
50
|
+
trigger(payload)
|
|
51
|
+
yield* driver.settle
|
|
52
|
+
return resolve().props
|
|
53
|
+
}),
|
|
54
|
+
),
|
|
55
|
+
slots: keyed((slotName) => slotFacade(resolve, slotName)),
|
|
56
|
+
frame: driver.settle,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const slotFacade = (resolveParent: () => TreeNode, slotName: string): MountedSlotFacade => {
|
|
60
|
+
const children = (): ReadonlyArray<TreeNode> => {
|
|
61
|
+
const parent = resolveParent()
|
|
62
|
+
const resolved = parent.slots[slotName]
|
|
63
|
+
if (resolved === undefined) {
|
|
64
|
+
return Effect.runSync(
|
|
65
|
+
Effect.dieMessage(
|
|
66
|
+
new UnknownSlot({ parent: uiNameOf(parent.comp), slot: slotName }).message,
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
return resolved
|
|
71
|
+
}
|
|
72
|
+
const childAt = (index: number): MountedFacade =>
|
|
73
|
+
nodeFacade(() => children()[index] ?? missingChild(index))
|
|
74
|
+
const childByKey = (key: string): MountedFacade =>
|
|
75
|
+
nodeFacade(() => {
|
|
76
|
+
return Option.getOrElse(
|
|
77
|
+
Arr.findFirst(children(), (candidate) => Option.contains(candidate.key, key)),
|
|
78
|
+
() => missingKey(key),
|
|
79
|
+
)
|
|
80
|
+
})
|
|
81
|
+
const missingChild = (index: number): never => {
|
|
82
|
+
const parent = resolveParent()
|
|
83
|
+
return Effect.runSync(
|
|
84
|
+
Effect.dieMessage(
|
|
85
|
+
new UnknownSlot({
|
|
86
|
+
parent: uiNameOf(parent.comp),
|
|
87
|
+
slot: `${slotName}[${index}]`,
|
|
88
|
+
}).message,
|
|
89
|
+
),
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
const missingKey = (key: string): never => {
|
|
93
|
+
const parent = resolveParent()
|
|
94
|
+
return Effect.runSync(
|
|
95
|
+
Effect.dieMessage(
|
|
96
|
+
new UnknownSlot({
|
|
97
|
+
parent: uiNameOf(parent.comp),
|
|
98
|
+
slot: `${slotName}[key=${key}]`,
|
|
99
|
+
}).message,
|
|
100
|
+
),
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
const first = childAt(0)
|
|
104
|
+
return {
|
|
105
|
+
first: Effect.sync(() => childAt(0)),
|
|
106
|
+
at: (index) => Effect.sync(() => childAt(index)),
|
|
107
|
+
all: Effect.sync(() => children().map((_child, index) => childAt(index))),
|
|
108
|
+
byKey: (key) =>
|
|
109
|
+
Effect.sync(() => {
|
|
110
|
+
const child = Arr.findFirst(children(), (candidate) =>
|
|
111
|
+
Option.contains(candidate.key, key),
|
|
112
|
+
)
|
|
113
|
+
return Option.isNone(child) ? missingKey(key) : childByKey(key)
|
|
114
|
+
}),
|
|
115
|
+
where: (predicate) =>
|
|
116
|
+
Effect.sync(() =>
|
|
117
|
+
children().flatMap((child, index) => (predicate(child.props) ? [childAt(index)] : [])),
|
|
118
|
+
),
|
|
119
|
+
count: Effect.sync(() => children().length),
|
|
120
|
+
props: first.props,
|
|
121
|
+
expectProps: first.expectProps,
|
|
122
|
+
actions: first.actions,
|
|
123
|
+
slots: first.slots,
|
|
124
|
+
frame: driver.settle,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
facade: nodeFacade(currentRoot),
|
|
130
|
+
settle: driver.settle,
|
|
131
|
+
dispose: driver.dispose,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|