-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
index.ts
335 lines (287 loc) · 9.99 KB
/
index.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @module Inferno-Component
*/ /** TypeDoc Comment */
// Make sure u use EMPTY_OBJ from 'inferno', otherwise it'll be a different reference
import { createVNode, EMPTY_OBJ, internal_DOMNodeMap, internal_patch, options, Props, VNode } from 'inferno';
import {
combineFrom,
ERROR_MSG,
isArray,
isBrowser,
isFunction,
isInvalid,
isNull,
isNullOrUndef,
isStringOrNumber,
NO_OP,
throwError
} from 'inferno-shared';
import VNodeFlags from 'inferno-vnode-flags';
let noOp = ERROR_MSG;
if (process.env.NODE_ENV !== 'production') {
noOp = 'Inferno Error: Can only update a mounted or mounting component. This usually means you called setState() or forceUpdate() on an unmounted component. This is a no-op.';
}
const componentCallbackQueue: Map<any, Function[]> = new Map();
export interface ComponentLifecycle<P, S> {
componentDidMount?(): void;
componentWillMount?(): void;
componentWillReceiveProps?(nextProps: P, nextContext: any): void;
shouldComponentUpdate?(nextProps: P, nextState: S, nextContext: any): boolean;
componentWillUpdate?(nextProps: P, nextState: S, nextContext: any): void;
componentDidUpdate?(prevProps: P, prevState: S, prevContext: any): void;
componentWillUnmount?(): void;
}
// when a components root VNode is also a component, we can run into issues
// this will recursively look for vNode.parentNode if the VNode is a component
function updateParentComponentVNodes(vNode: VNode, dom: Element) {
if (vNode.flags & VNodeFlags.Component) {
const parentVNode = vNode.parentVNode;
if (parentVNode) {
parentVNode.dom = dom;
updateParentComponentVNodes(parentVNode, dom);
}
}
}
const resolvedPromise = Promise.resolve();
function addToQueue(component: Component<any, any>, force: boolean, callback?: Function): void {
let queue: any = componentCallbackQueue.get(component);
if (queue === void 0) {
queue = [];
componentCallbackQueue.set(component, queue);
resolvedPromise.then(() => {
componentCallbackQueue.delete(component);
component._updating = true;
applyState(component, force, () => {
for (let i = 0, len = queue.length; i < len; i++) {
queue[ i ].call(component);
}
});
component._updating = false;
});
}
if (!isNullOrUndef(callback)) {
queue.push(
callback
);
}
}
function queueStateChanges<P, S>(component: Component<P, S>, newState: S, callback?: Function): void {
if (isFunction(newState)) {
newState = newState(component.state, component.props, component.context);
}
let pending = component._pendingState;
if (isNullOrUndef(pending)) {
component._pendingState = pending = newState;
} else {
for (const stateKey in newState) {
pending[ stateKey ] = newState[ stateKey ];
}
}
if (isBrowser && !component._pendingSetState && !component._blockRender) {
if (!component._updating) {
component._pendingSetState = true;
component._updating = true;
applyState(component, false, callback);
component._updating = false;
} else {
addToQueue(component, false, callback);
}
} else {
const state = component.state;
if (state === null) {
component.state = pending;
} else {
for (const key in pending) {
state[ key ] = pending[ key ];
}
}
component._pendingState = null;
if (!isNullOrUndef(callback) && component._blockRender) {
(component._lifecycle as any).addListener(callback.bind(component));
}
}
}
function applyState<P, S>(component: Component<P, S>, force: boolean, callback?: Function): void {
if (component._unmounted) {
return;
}
if (force || !component._blockRender) {
component._pendingSetState = false;
const pendingState = component._pendingState;
const prevState = component.state;
const nextState = combineFrom(prevState, pendingState) as any;
const props = component.props as P;
const context = component.context;
component._pendingState = null;
let nextInput = component._updateComponent(prevState as S, nextState, props, props, context, force, true);
let didUpdate = true;
if (isInvalid(nextInput)) {
nextInput = createVNode(VNodeFlags.Void, null);
} else if (nextInput === NO_OP) {
nextInput = component._lastInput;
didUpdate = false;
} else if (isStringOrNumber(nextInput)) {
nextInput = createVNode(VNodeFlags.Text, null, null, nextInput) as VNode;
} else if (isArray(nextInput)) {
if (process.env.NODE_ENV !== 'production') {
throwError('a valid Inferno VNode (or null) must be returned from a component render. You may have returned an array or an invalid object.');
}
throwError();
}
const lastInput = component._lastInput as VNode;
const vNode = component._vNode as VNode;
const parentDom = (lastInput.dom && lastInput.dom.parentNode) || (lastInput.dom = vNode.dom);
component._lastInput = nextInput as VNode;
if (didUpdate) {
let childContext;
if (!isNullOrUndef(component.getChildContext)) {
childContext = component.getChildContext();
}
if (isNullOrUndef(childContext)) {
childContext = component._childContext;
} else {
childContext = combineFrom(context, childContext as any);
}
const lifeCycle = component._lifecycle as any;
internal_patch(lastInput, nextInput as VNode, parentDom as Element, lifeCycle, childContext, component._isSVG, false);
lifeCycle.trigger();
if (!isNullOrUndef(component.componentDidUpdate)) {
component.componentDidUpdate(props, prevState as S, context);
}
if (!isNull(options.afterUpdate)) {
options.afterUpdate(vNode);
}
}
const dom = vNode.dom = (nextInput as VNode).dom as Element;
if (options.findDOMNodeEnabled) {
internal_DOMNodeMap.set(component, (nextInput as VNode).dom);
}
updateParentComponentVNodes(vNode, dom);
} else {
component.state = component._pendingState as any;
component._pendingState = null;
}
if (!isNullOrUndef(callback)) {
callback.call(component);
}
}
let alreadyWarned = false;
export default class Component<P, S> implements ComponentLifecycle<P, S> {
public static defaultProps: {};
public state: S|null = null;
public props: P & Props;
public context: any;
public _blockRender = false;
public _blockSetState = true;
public _pendingSetState = false;
public _pendingState: S|null = null;
public _lastInput: any = null;
public _vNode: VNode|null = null;
public _unmounted = false;
public _lifecycle = null;
public _childContext = null;
public _isSVG = false;
public _updating = true;
constructor(props?: P, context?: any) {
/** @type {object} */
this.props = props || (EMPTY_OBJ as P);
/** @type {object} */
this.context = context || EMPTY_OBJ; // context should not be mutable
}
// LifeCycle methods
public componentDidMount?(): void;
public componentWillMount?(): void;
public componentWillReceiveProps?(nextProps: P, nextContext: any): void;
public shouldComponentUpdate?(nextProps: P, nextState: S, nextContext: any): boolean;
public componentWillUpdate?(nextProps: P, nextState: S, nextContext: any): void;
public componentDidUpdate?(prevProps: P, prevState: S, prevContext: any): void;
public componentWillUnmount?(): void;
public getChildContext?(): void;
public forceUpdate(callback?: Function) {
if (this._unmounted || !isBrowser) {
return;
}
applyState(this, true, callback);
}
public setState(newState, callback?: Function) {
if (this._unmounted) {
return;
}
if (!this._blockSetState) {
queueStateChanges(this, newState, callback);
} else {
if (process.env.NODE_ENV !== 'production') {
throwError('cannot update state via setState() in componentWillUpdate() or constructor.');
}
throwError();
}
}
public setStateSync(newState) {
if (process.env.NODE_ENV !== 'production') {
if (!alreadyWarned) {
alreadyWarned = true;
// tslint:disable-next-line:no-console
console.warn('Inferno WARNING: setStateSync has been deprecated and will be removed in next release. Use setState instead.');
}
}
this.setState(newState);
}
public _updateComponent(prevState: S, nextState: S, prevProps: P & Props, nextProps: P & Props, context: any, force: boolean, fromSetState: boolean): VNode|string {
if (this._unmounted === true) {
if (process.env.NODE_ENV !== 'production') {
throwError(noOp);
}
throwError();
}
if ((prevProps !== nextProps || nextProps === EMPTY_OBJ) || prevState !== nextState || force) {
if (prevProps !== nextProps || nextProps === EMPTY_OBJ) {
if (!isNullOrUndef(this.componentWillReceiveProps) && !fromSetState) {
// keep a copy of state before componentWillReceiveProps
const beforeState = combineFrom(this.state) as any;
this._blockRender = true;
this.componentWillReceiveProps(nextProps, context);
this._blockRender = false;
const afterState = this.state;
if (beforeState !== afterState) {
// if state changed in componentWillReceiveProps, reassign the beforeState
this.state = beforeState;
// set the afterState as pending state so the change gets picked up below
this._pendingSetState = true;
this._pendingState = afterState;
}
}
if (this._pendingSetState) {
nextState = combineFrom(nextState, this._pendingState) as any;
this._pendingSetState = false;
this._pendingState = null;
}
}
/* Update if scu is not defined, or it returns truthy value or force */
if (force || isNullOrUndef(this.shouldComponentUpdate) || (this.shouldComponentUpdate && this.shouldComponentUpdate(nextProps, nextState, context))) {
if (!isNullOrUndef(this.componentWillUpdate)) {
this._blockSetState = true;
this.componentWillUpdate(nextProps, nextState, context);
this._blockSetState = false;
}
this.props = nextProps;
this.state = nextState;
this.context = context;
if (options.beforeRender) {
options.beforeRender(this);
}
const render = this.render(nextProps, nextState, context);
if (options.afterRender) {
options.afterRender(this);
}
return render;
} else {
this.props = nextProps;
this.state = nextState;
this.context = context;
}
}
return NO_OP;
}
// tslint:disable-next-line:no-empty
public render(nextProps?: P, nextState?, nextContext?): any {}
}