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
193 lines (178 loc) · 5.98 KB
/
index.ts
File metadata and controls
193 lines (178 loc) · 5.98 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
import * as React from 'react'
import App, { ActionsCallback, ReactionsCallback, EventType } from 'overmind'
export * from 'overmind'
export type IReactComponent<P = any> =
| React.StatelessComponent<P>
| React.ComponentClass<P>
| React.ClassicComponentClass<P>
export type TConnect<State = {}, Actions = {}> = {
app: {
state: State
actions: Actions
reaction: (name: string, stateCb: (state: State) => any, Function) => void
}
}
// 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]
>
let nextComponentId = 0
export default class ReactApp<
State extends object,
Effects extends object,
Reactions extends ReactionsCallback<State, any>,
Actions extends
| {
[namespace: string]: ActionsCallback<State, Effects>
}
| ActionsCallback<State, Effects>
> extends App<State, Effects, Reactions, Actions> {
connect = <
Props,
ConnectedActions = Actions extends {
[namespace: string]: ActionsCallback<Effects, State>
}
? { [Namespace in keyof Actions]: ReturnType<Actions[Namespace]> }
: Actions extends ActionsCallback<Effects, State>
? ReturnType<Actions>
: any
>(
Component: IReactComponent<Props & TConnect<State, ConnectedActions>>
): IReactComponent<
Omit<
Props & TConnect<State, ConnectedActions>,
keyof TConnect<State, ConnectedActions>
>
> => {
const componentId = nextComponentId++
let componentInstanceId = 0
const instance = this
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() {
if (this.__mutationListener) {
instance.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
})
this.__mutationListener.dispose()
}
originalWillUnmount && originalWillUnmount.call(this)
}
Component.prototype.render = function() {
const trackId = instance.trackState()
const value = originalRender.call(this)
const paths = instance.clearTrackState(trackId)
if (this.__mutationListener) {
this.__mutationListener.update(paths)
} else {
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
this.__mutationListener = instance.addMutationListener(
paths,
(flushId) => {
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId,
componentInstanceId: this.props.app.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
flushId,
})
this.forceUpdate()
}
)
}
return value
}
}
return class extends React.PureComponent<
Omit<
Props & TConnect<State, ConnectedActions>,
keyof TConnect<State, ConnectedActions>
>
> {
__mutationListener: any
__componentId = componentId
__componentInstanceId = componentInstanceId++
__reactionFactory = instance.createReactionFactory(
Component.name || Component.displayName
)
componentWillUnmount() {
if (this.__mutationListener) {
instance.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
})
this.__mutationListener.dispose()
}
this.__reactionFactory.dispose()
}
renderStatelessComponent() {
const trackId = instance.trackState()
const value = (Component as any)(
Object.assign({}, this.props, {
app: {
state: instance.state,
actions: instance.actions,
reaction: this.__reactionFactory.add,
__componentInstanceId: this.__componentInstanceId,
},
}),
this.context
)
const paths = instance.clearTrackState(trackId)
if (this.__mutationListener) {
this.__mutationListener.update(paths)
} else {
instance.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId: this.__componentInstanceId,
name: Component.name || '',
paths: Array.from(paths),
})
this.__mutationListener = instance.addMutationListener(
paths,
(flushId) => {
instance.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
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: instance.state,
actions: instance.actions,
reaction: this.__reactionFactory.add,
__componentInstanceId: this.__componentInstanceId,
},
}) as any)
}
render() {
if (isClassComponent) {
return this.renderClassComponent()
}
return this.renderStatelessComponent()
}
}
}
}