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
200 lines (184 loc) · 6.39 KB
/
index.ts
File metadata and controls
200 lines (184 loc) · 6.39 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
import OvermindApp, { BaseApp, EventType } from 'overmind'
import * as React from 'react'
export type IReactComponent<P = any> =
| React.StatelessComponent<P>
| React.ComponentClass<P>
| React.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<App extends BaseApp> = {
app: {
state: App['state']
actions: App['actions']
reaction: (
name: string,
stateCb: (state: App['state']) => any,
Function
) => void
}
}
let nextComponentId = 0
export default <App extends OvermindApp<any>>(app: App) => {
return <Props>(
Component: IReactComponent<Props & TConnect<App>>
): IReactComponent<Omit<Props & TConnect<App>, keyof TConnect<App>>> => {
let componentInstanceId = 0
const isClassComponent =
Component.prototype && typeof Component.prototype.render === 'function'
if (isClassComponent) {
const originalRender = Component.prototype.render
const originalWillUnmount = Component.prototype.componentWillUnmount
Component.prototype.componentWillUnmount = function() {
this.__isUnmounting = true
if (this.__mutationListener) {
app.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId: this.__componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
})
this.__mutationListener.dispose()
}
originalWillUnmount && originalWillUnmount.call(this)
}
Component.prototype.render = function() {
if (typeof this.__componentId === 'undefined') {
this.__componentId = nextComponentId++
}
const trackId = app.trackState()
const value = originalRender.call(this)
const paths = app.clearTrackState(trackId)
if (this.__mutationListener) {
this.__mutationListener.update(paths)
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: this.__componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
} else {
app.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: this.__componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
this.__mutationListener = app.addMutationListener(
paths,
(flushId) => {
if (this.__isUnmounting) {
return
}
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: this.__componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
flushId,
})
this.forceUpdate()
}
)
}
return value
}
}
const klass = class extends React.PureComponent<
Omit<Props & TConnect<App>, keyof TConnect<App>>
> {
__mutationListener: any
__isUnmounting: boolean
__componentId: number
__componentInstanceId = componentInstanceId++
__reactionFactory = app.createReactionFactory(
Component.name || Component.displayName
)
componentWillUnmount() {
this.__isUnmounting = true
if (this.__mutationListener) {
app.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId: this.__componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
})
this.__mutationListener.dispose()
}
this.__reactionFactory.dispose()
}
renderStatelessComponent() {
const trackId = app.trackState()
const value = (Component as any)(
Object.assign({}, this.props, {
app: {
state: app.state,
actions: app.actions,
reaction: this.__reactionFactory.add,
__componentInstanceId: this.__componentInstanceId,
},
}),
this.context
)
const paths = app.clearTrackState(trackId)
if (this.__mutationListener) {
this.__mutationListener.update(paths)
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: this.__componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
} else {
app.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: this.__componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
this.__mutationListener = app.addMutationListener(
paths,
(flushId) => {
if (this.__isUnmounting) {
return
}
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: this.__componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
flushId,
})
this.forceUpdate()
}
)
}
return value
}
renderClassComponent() {
return React.createElement(Component, Object.assign({}, this.props, {
app: {
state: app.state,
actions: app.actions,
reaction: this.__reactionFactory.add,
__componentInstanceId: this.__componentInstanceId,
},
}) as any)
}
render() {
if (isClassComponent) {
return this.renderClassComponent()
}
if (typeof this.__componentId === 'undefined') {
this.__componentId = nextComponentId++
}
return this.renderStatelessComponent()
}
}
Object.defineProperties(klass, {
name: { value: Component.displayName || Component.name || '' },
})
return klass
}
}