forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-logic.ts
143 lines (134 loc) · 3.66 KB
/
ui-logic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as fromPairs from 'lodash/fromPairs'
export type EventProcessor<Dependencies> = (
args: EventProcessorArgs<Dependencies>,
) => EventProcessorResult
export interface EventProcessorArgs<Dependencies> {
state: any
event: { type: string; [key: string]: any }
dependencies: Dependencies
}
export interface EventProcessorResult {
updateState?: StateUpdates
dispatch?: EventDispatch | EventDispatch[]
actions?: EventDispatch | EventDispatch[]
}
export interface StateUpdates {
[key: string]: any
}
export interface EventDispatch {
type: string
args?: { [key: string]: any }
}
export interface ActionMap {
[key: string]: () => any
}
export function compositeEventProcessor<Dependencies = null>(processors: {
[type: string]: EventProcessor<Dependencies>
}): EventProcessor<Dependencies> {
return (args: EventProcessorArgs<Dependencies>) => {
const processor = processors[args.event.type]
if (!processor) {
throw new Error(
`No event processor found for event ${args.event.type}`,
)
}
return processor(args)
}
}
export function handleEvent<Dependencies = null>({
eventProcessor,
state,
setState,
props,
event,
dependencies,
actions,
}: {
eventProcessor: EventProcessor<Dependencies>
state: { [key: string]: any }
setState: (...args: any[]) => void
props: { [prop: string]: any }
event: EventDispatch
dependencies: Dependencies
actions: ActionMap
}) {
const result = eventProcessor({ state, event, dependencies })
if (result.updateState) {
setState(result.updateState)
}
if (result.dispatch) {
_doDispatch(props, result.dispatch, 'event')
}
if (result.actions) {
_doDispatch(actions, result.actions, 'action')
}
}
export function reactEventHandler<Dependencies extends object = null>(
component,
eventProcessor,
{
actions = {},
dependencies = null,
}: { actions?: ActionMap; dependencies?: Dependencies } = {},
) {
return event => {
handleEvent({
eventProcessor,
state: component.state,
props: component.props,
setState: component.setState.bind(component),
event,
actions,
dependencies,
})
}
}
export function _doDispatch(
handlers,
dispatch: EventDispatch | EventDispatch[],
type: string,
) {
if (!(dispatch instanceof Array)) {
dispatch = [dispatch]
}
for (const dispatched of dispatch) {
const handler = handlers[dispatched.type]
if (!handler) {
console.error(
`Dispatched ${type} without handler: ${dispatched.type}`,
)
}
const args = dispatched.args ? Object.values(dispatched.args) : []
handler(...args)
}
}
export function fakeState(initial) {
const state = { ...initial }
const setState = updates => {
Object.assign(state, updates)
}
return { state, setState }
}
export function fakeEventProps(eventNames) {
const events = { log: [] }
const props = fromPairs(
eventNames.map(eventName => [
eventName,
(...args) => events.log.push({ event: eventName, args }),
]),
)
return { events, props }
}
export function setupUiLogicTest({
inititalState,
eventNames,
eventProcessor,
}) {
const { state, setState } = fakeState(inititalState)
const { props, events } = fakeEventProps(eventNames)
const trigger = reactEventHandler(
{ props, state, setState },
eventProcessor,
)
return { state, setState, props, events, trigger }
}