forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
296 lines (273 loc) · 8.54 KB
/
index.ts
File metadata and controls
296 lines (273 loc) · 8.54 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { TApp, Configuration, EventType, Overmind } from 'overmind'
import {
// @ts-ignore
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
ClassicComponentClass,
Component,
ComponentClass,
createElement,
StatelessComponent,
// @ts-ignore
useEffect,
// @ts-ignore
useState,
useLayoutEffect,
} from 'react'
import { IMutation } from 'proxy-state-tree'
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
export type IReactComponent<P = any> =
| StatelessComponent<P>
| ComponentClass<P>
| ClassicComponentClass<P>
// Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766
type Omit<T, K extends keyof T> = Pick<
T,
({ [P in keyof T]: P } &
{ [P in K]: never } & { [x: string]: never; [x: number]: never })[keyof T]
>
export type TConnect<Config extends Configuration> = {
overmind: {
state: TApp<Config>['state']
actions: TApp<Config>['actions']
addMutationListener: (cb: (mutation: IMutation) => void) => () => void
}
}
let nextComponentId = 0
export const createHook = <A extends Overmind<Configuration>>(
overmind: A
): (() => {
state: A['state']
actions: A['actions']
addMutationListener: (cb: (mutation: IMutation) => void) => () => void
}) => {
let currentComponentInstanceId = 0
const {
ReactCurrentOwner,
} = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
const useCurrentComponent = () => {
return ReactCurrentOwner &&
ReactCurrentOwner.current &&
ReactCurrentOwner.current.elementType
? ReactCurrentOwner.current.elementType
: {}
}
return () => {
const component = useCurrentComponent()
const name = component.name
component.__componentId =
typeof component.__componentId === 'undefined'
? nextComponentId++
: component.__componentId
const [tree, updateComponent] = useState<any>(() =>
(overmind as any).proxyStateTree.getTrackStateTree()
)
if (IS_PRODUCTION) {
tree.track(() => {
updateComponent((state) => state)
})
useEffect(
() => () => {
;(overmind as any).proxyStateTree.disposeTree(tree)
},
[]
)
} else {
const [debugging] = useState<any>(() => ({
isFirstRender: true,
currentFlushId: 0,
componentInstanceId: currentComponentInstanceId++,
}))
tree.track((mutations, paths, flushId) => {
debugging.currentFlushId = flushId
updateComponent((state) => state)
})
useLayoutEffect(() => {
if (debugging.isFirstRender) {
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: component.__componentId,
componentInstanceId: debugging.componentInstanceId,
name,
paths: Array.from(tree.pathDependencies) as any,
})
debugging.isFirstRender = false
} else {
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: component.__componentId,
componentInstanceId: debugging.componentInstanceId,
name,
flushId: debugging.currentFlushId,
paths: Array.from(tree.pathDependencies as Set<string>),
})
}
})
useEffect(
() => () => {
;(overmind as any).proxyStateTree.disposeTree(tree)
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId: component.__componentId,
componentInstanceId: debugging.componentInstanceId,
name,
})
},
[]
)
}
return {
state: tree.state,
actions: overmind.actions,
addMutationListener: overmind.addMutationListener,
}
}
}
export const createConnect = <A extends Overmind<Configuration>>(
overmind: A
) => {
return <Props>(
component: IReactComponent<
Props & { overmind: { state: A['state']; actions: A['actions'] } }
>
): IReactComponent<Omit<Props & TConnect<A>, keyof TConnect<A>>> => {
let componentInstanceId = 0
const name = component.name
const populatedComponent = component as any
populatedComponent.__componentId =
typeof populatedComponent.__componentId === 'undefined'
? nextComponentId++
: populatedComponent.__componentId
const isClassComponent =
component.prototype && typeof component.prototype.render === 'function'
if (isClassComponent) {
const originalRender = component.prototype.render
component.prototype.render = function() {
this.props.overmind &&
this.props.overmind.tree.track(this.props.overmind.onUpdate)
return originalRender.call(this)
}
}
if (IS_PRODUCTION) {
class HOC extends Component {
tree = (overmind as any).proxyStateTree.getTrackStateTree()
state: {
overmind: any
}
constructor(props) {
super(props)
this.state = {
overmind: {
state: this.tree.state,
actions: overmind.actions,
addMutationListener: overmind.addMutationListener,
onUpdate: this.onUpdate,
tree: this.tree,
},
}
}
componentWillUnmount() {
;(overmind as any).proxyStateTree.disposeTree(this.tree)
}
onUpdate = () => {
this.setState({
overmind: {
state: this.tree.state,
actions: overmind.actions,
addMutationListener: overmind.addMutationListener,
onUpdate: this.onUpdate,
tree: this.tree,
},
})
}
render() {
if (isClassComponent) {
return createElement(component, {
...this.props,
overmind: this.state.overmind,
} as any)
}
this.tree.track(this.onUpdate)
return createElement(component, {
...this.props,
overmind: this.state.overmind,
} as any)
}
}
return HOC as any
} else {
class HOC extends Component {
tree = (overmind as any).proxyStateTree.getTrackStateTree()
componentInstanceId = componentInstanceId++
currentFlushId = 0
state: {
overmind: any
}
constructor(props) {
super(props)
this.state = {
overmind: {
state: this.tree.state,
actions: overmind.actions,
addMutationListener: overmind.addMutationListener,
onUpdate: this.onUpdate,
tree: this.tree,
},
}
}
componentDidMount() {
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: populatedComponent.__componentId,
componentInstanceId: this.componentInstanceId,
name,
paths: Array.from(this.tree.pathDependencies) as any,
})
}
componentDidUpdate() {
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: populatedComponent.__componentId,
componentInstanceId: this.componentInstanceId,
name,
flushId: this.currentFlushId,
paths: Array.from(this.tree.pathDependencies as Set<string>),
})
}
componentWillUnmount() {
;(overmind as any).proxyStateTree.disposeTree(this.tree)
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId: populatedComponent.__componentId,
componentInstanceId: this.componentInstanceId,
name,
})
}
onUpdate = (mutatons, paths, flushId) => {
this.currentFlushId = flushId
this.setState({
overmind: {
state: this.tree.state,
actions: overmind.actions,
addMutationListener: overmind.addMutationListener,
onUpdate: this.onUpdate,
tree: this.tree,
},
})
}
render() {
if (isClassComponent) {
return createElement(component, {
...this.props,
overmind: this.state.overmind,
} as any)
}
this.tree.track(this.onUpdate)
return createElement(component, {
...this.props,
overmind: this.state.overmind,
} as any)
}
}
Object.defineProperties(HOC, {
name: {
value: 'Connect' + (component.displayName || component.name || ''),
},
})
return HOC as any
}
}
}