-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactBinding.ts
265 lines (232 loc) · 6.44 KB
/
ReactBinding.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
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
import * as React from "react";
import {mixinDecoratorFactory} from "../utils/mixinDecoratorFactory";
import {patchMethod} from "../utils/patchMethod";
import {
Signal,
Automaton,
Transition,
TransitionFunction,
} from "./Automaton";
/**
* @ignore
*/
export interface IAutomatonComponent<P = any, S = any> extends React.Component<P, S> {
automaton: Automaton;
transitions: Transition[];
}
/**
* This signal fires when the target React component is created.
* @see [[ReactAutomaton]]
*/
export const CREATE = "create";
/**
* This signal fires after the target React component is mounted.
* @see [[ReactAutomaton]]
*/
export const MOUNT = "mount";
/**
* This signal fires after the target React component is updated.
* @see [[ReactAutomaton]]
*/
export const UPDATE = "update";
/**
* This signal fires before the target React component is unmounted.
* @see [[ReactAutomaton]]
*/
export const UNMOUNT = "unmount";
/**
* This signal fires if the target React component catches error
* @see [[ReactAutomaton]]
* @see [[IAutomatonDecoratorParams]]
*/
export const CATCH = "catch";
/**
* Parameters of [[withAutomaton]] decorator.
*/
export interface IAutomatonDecoratorParams {
/**
* Determines whether or not [[withAutomaton]] should provide `componentDidCatch`
* lifecycle method.
*/
catching?: boolean;
/**
* If set to a string, [[withAutomaton]] will call setState on every transition and
* set state[mapToState] to the current [[State]] value
*/
mapToState?: string;
}
/**
* @ignore
*/
interface IReactEventHandlers {
[signal: string]: React.EventHandler<any>;
}
/**
* This class is used to extend [[Automaton]] functionality so it can be used with React components.
*
* [[ReactAutomaton]] always passes the host component instance to it's transition functions as
* the first argument.
*/
export class ReactAutomaton extends Automaton {
public mapToState: string;
private handlers: IReactEventHandlers = {};
constructor(private component: IAutomatonComponent) {
super(component.transitions || /* istanbul ignore next */ []);
}
/**
* This method is used by [[asSignal]] to produce signaling event handlers.
*
* @param signal
*/
public signal(signal: Signal): React.EventHandler<any> {
const key = String(signal);
if (!this.handlers[key]) {
this.handlers[key] = (event: React.SyntheticEvent) => {
this.transition(signal, event);
};
}
return this.handlers[key];
}
/**
* Performs a transition and calls setState if mapToState is enabled.
*
* @param signal
* @param args
*/
public transition(signal: Signal, ...args: any[]) {
super.transition(signal, ...args);
if (typeof this.mapToState !== "string") {
return;
}
if (this.component.state[this.mapToState] !== this.state) {
this.component.setState({
[this.mapToState]: this.state,
});
}
}
/**
* Calls transition implementation, passing the host component instance.
*
* @param implementation
* @param args
*/
protected doTransition(implementation: TransitionFunction, ...args: any[]) {
if (typeof implementation !== "function") {
return super.doTransition(implementation);
}
super.doTransition(
() => implementation(this.component, ...args), args,
);
}
}
/**
* @ignore
*/
const withAutomationCallbacks = {
onPatch<MixinBase, ResultType>(baseClass: MixinBase, params: IAutomatonDecoratorParams): void {
const {
catching,
} = params;
patchMethod(baseClass, "componentDidMount",
function() {
this.automaton.transition(MOUNT);
},
);
patchMethod(baseClass, "componentDidUpdate",
function(prevProps: any, prevState: any) {
this.automaton.transition(UPDATE, prevProps, prevState);
},
);
patchMethod(baseClass, "componentWillUnmount",
function() {
this.automaton.transition(UNMOUNT);
this.automaton.reset();
},
);
catching && patchMethod(baseClass, "componentDidCatch",
function(error: any, errorInfo: any) {
this.automaton.transition(CATCH, error, errorInfo);
},
);
},
onConstruct(self: IAutomatonComponent, params: IAutomatonDecoratorParams): void {
self.automaton = new ReactAutomaton(self);
self.automaton.transition(CREATE, self);
if (typeof params.mapToState === "string") {
if (!self.state) {
self.state = {};
}
// Dirty hack as we're still in constructor
(self.state as any)[params.mapToState] = self.automaton.state;
(self.automaton as ReactAutomaton).mapToState = params.mapToState;
}
},
};
/**
* React component decorator that enchants the provided class with an [[Automaton]] instance.
*
* [[withAutomaton]] patches some lifecycle methods of the provided component to dispatch
* corresponding signals. The signals dispatched are:
*
* - [[CREATE]], dispatched from the constructor
* - [[MOUNT]], dispatched from `componentDidMount`
* - [[UPDATE]], dispatched from `componentDidUpdate`
* - [[UNMOUNT]], dispatched from `componentWillUnmount`
* - [[CATCH]], dispatched from `componentDidCatch` and if `catching` parameter is set to true:
*
* ```jsx
*import {withAutomaton} from 'automatons';
*
*@withAutomaton({catching: true})
*class Button extends React.Component
* ```
*/
export const withAutomaton = mixinDecoratorFactory<
IAutomatonComponent, IAutomatonDecoratorParams
>(withAutomationCallbacks);
/**
* Returns the corresponding [[Automaton]] of the provided React component
*
* ```jsx
*import {withAutomaton, automatonOf} from 'automatons';
*
*@withAutomaton
*class Button extends React.Component {
* render() {
* return (
* <button onClick={() => automatonOf(this).transition('click')}/>
* );
* }
*}
* ```
*
* @param component - target component
*/
export function automatonOf(component: React.Component): ReactAutomaton {
return (component as any).automaton;
}
/**
* Creates an event handler callback that, on being called, dispatches a [[Signal]] on
* corresponding React component's [[Automaton]]
*
* ```jsx
*import {withAutomaton, asSignal} from 'automatons';
*
*@withAutomaton
*class Button extends React.Component {
* render() {
* return (
* <button onClick={asSignal(this, 'click')}/>
* );
* }
*}
* ```
*
* Handler instances produced by this function are memoized internally to avoid unnecessary re-renders.
*
* @param component - target React component
* @param signal - desired signal
*/
export function asSignal(component: React.Component, signal: Signal): React.EventHandler<any> {
return automatonOf(component).signal(signal);
}