@playfast/reform-proof 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.
@@ -1,121 +0,0 @@
1
- import { Effect, Match, Option, Record as Rec } from 'effect'
2
- import {
3
- type CompositionClass,
4
- isFeatureBinding,
5
- type SlotChild,
6
- type SlotClass,
7
- type SlotFill,
8
- type Structure,
9
- type UiContract,
10
- } from '@playfast/reform'
11
- import { eventsOf, type Sink, uiNameOf } from './sink'
12
-
13
- export interface Mounted {
14
- readonly comp: CompositionClass<unknown>
15
- readonly props: unknown
16
- readonly key: Option.Option<string>
17
- }
18
-
19
- export interface NodeRef {
20
- readonly name: string
21
- readonly index: number
22
- }
23
-
24
- export interface SettleProgress {
25
- readonly previous: string
26
- readonly remaining: number
27
- readonly stable: boolean
28
- }
29
-
30
- export interface PumpStep {
31
- readonly input: unknown
32
- readonly index: number
33
- }
34
-
35
- export const childComposition = (child: SlotChild): CompositionClass<unknown> =>
36
- isFeatureBinding(child) ? child.composition : child
37
-
38
- export const slotsOf = (comp: CompositionClass<unknown>): Record<string, SlotClass> =>
39
- Option.getOrElse(Option.fromNullable(comp.manifest.slots), () => ({}))
40
-
41
- const isSlotFill = (candidate: unknown): candidate is SlotFill<unknown> =>
42
- typeof candidate === 'object' &&
43
- candidate !== null &&
44
- '_tag' in candidate &&
45
- (candidate._tag === 'Each' || candidate._tag === 'One' || candidate._tag === 'Absent')
46
-
47
- export const structureFills = (
48
- structure: Structure<UiContract>,
49
- ): Record<string, SlotFill<unknown>> =>
50
- Rec.fromEntries(
51
- Rec.toEntries(structure.slots).flatMap(([slotName, fillValue]) =>
52
- isSlotFill(fillValue) ? [[slotName, fillValue] as const] : [],
53
- ),
54
- )
55
-
56
- export const collectBindings: (
57
- root: CompositionClass<unknown>,
58
- ) => Effect.Effect<Map<SlotClass, CompositionClass<unknown>>, never, SlotChild> = Effect.fn(
59
- 'collectBindings',
60
- )(function* (
61
- root: CompositionClass<unknown>,
62
- ): Effect.fn.Return<Map<SlotClass, CompositionClass<unknown>>, never, SlotChild> {
63
- const bindings = new Map<SlotClass, CompositionClass<unknown>>()
64
- const walk: (comp: CompositionClass<unknown>) => Effect.Effect<void, never, SlotChild> = Effect.fn(
65
- 'collectBindings.walk',
66
- )(function* (comp: CompositionClass<unknown>): Effect.fn.Return<void, never, SlotChild> {
67
- yield* Effect.forEach(Rec.values(slotsOf(comp)), (slotClass) =>
68
- Effect.gen(function* () {
69
- if (bindings.has(slotClass)) {
70
- return
71
- }
72
- const child = childComposition(yield* slotClass.tag)
73
- bindings.set(slotClass, child)
74
- yield* walk(child)
75
- }),
76
- )
77
- })
78
- yield* walk(root)
79
- return bindings
80
- })
81
-
82
- const enqueueFill = (
83
- slotName: string,
84
- fill: SlotFill<unknown>,
85
- child: CompositionClass<unknown>,
86
- ): ReadonlyArray<Mounted> =>
87
- Match.value(fill).pipe(
88
- Match.when({ _tag: 'Each' }, (each) =>
89
- each.items.map((entry) => ({
90
- comp: child,
91
- props: entry.props,
92
- key: Option.some(entry.key),
93
- })),
94
- ),
95
- Match.when({ _tag: 'One' }, (oneFill) => [
96
- { comp: child, props: oneFill.props, key: Option.some(`${slotName}.0`) },
97
- ]),
98
- Match.when({ _tag: 'Absent' }, () => []),
99
- Match.exhaustive,
100
- )
101
-
102
- export const driveStructure = (
103
- comp: CompositionClass<unknown>,
104
- structure: Structure<UiContract>,
105
- key: Option.Option<string>,
106
- sink: Sink,
107
- bindings: ReadonlyMap<SlotClass, CompositionClass<unknown>>,
108
- ): ReadonlyArray<Mounted> => {
109
- sink.api.record({
110
- name: uiNameOf(comp),
111
- props: structure.props,
112
- events: eventsOf(structure),
113
- ...(Option.isSome(key) ? { key: key.value } : {}),
114
- })
115
- const fills = structureFills(structure)
116
- return Rec.toEntries(slotsOf(comp)).flatMap(([slotName, slotClass]) => {
117
- const child = bindings.get(slotClass)
118
- const fill = fills[slotName]
119
- return child === undefined || fill === undefined ? [] : enqueueFill(slotName, fill, child)
120
- })
121
- }