-
-
Notifications
You must be signed in to change notification settings - Fork 635
/
connectAdvanced.ts
409 lines (346 loc) · 12.3 KB
/
connectAdvanced.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { VNodeFlags } from 'inferno-vnode-flags';
import { type Dispatch, type Store } from 'redux';
import {
Component,
createComponentVNode,
type InfernoNode,
normalizeProps,
} from 'inferno';
import { Subscription } from '../utils/Subscription';
import { hoistStaticProperties } from 'inferno-shared';
let hotReloadingVersion = 0;
const noop = (): void => {};
const makeSelectorStateful = (
sourceSelector: (state: any, props: any) => any,
store: Store<any>,
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
) => {
// wrap the selector in an object that tracks its results between runs.
const selector = {
error: null as Error | null,
props: {} as any,
run: function runComponentSelector(props) {
try {
const nextProps = sourceSelector(store.getState(), props);
if (nextProps !== selector.props || selector.error) {
selector.shouldComponentUpdate = true;
selector.props = nextProps;
selector.error = null;
}
} catch (e) {
selector.shouldComponentUpdate = true;
selector.error = e;
}
},
shouldComponentUpdate: false,
};
return selector;
};
export interface IConnectOptions {
displayName: string;
/**
* the func used to compute this HOC's displayName from the wrapped component's displayName.
* probably overridden by wrapper functions such as connect().
*
* @memberOf IConnectOptions
*/
getDisplayName: (name: string) => string;
/**
* shown in error messages.
* probably overridden by wrapper functions such as connect()
*
* @type {string}
* @memberOf IConnectOptions
*/
methodName: string;
/**
* if defined, the name of the property passed to the wrapped element indicating the number of
* calls to render. useful for watching in react devtools for unnecessary re-renders.
*
* @type {(string | null)}
* @memberOf IConnectOptions
*/
renderCountProp: string | null;
/**
* determines whether this HOC subscribes to store changes.
*
* @type {boolean}
* @memberOf IConnectOptions
*/
shouldHandleStateChanges: boolean;
/**
* the key of props/context to get the store.
*
* @type {string}
* @memberOf IConnectOptions
*/
storeKey: string;
/**
* if true, the wrapped element is exposed by this HOC via the getWrappedInstance() function.
*
* @type {boolean}
* @memberOf IConnectOptions
*/
withRef: boolean;
initMapStateToProps?: any;
initMapDispatchToProps?: any;
initMergeProps?: any;
pure?: any;
areStatesEqual?: any;
areOwnPropsEqual?: any;
areStatePropsEqual?: any;
areMergedPropsEqual?: any;
WrappedComponent?: any;
wrappedComponentName: string;
}
// TODO: This should be typed better. Spesifically, the output and input props should be generic.
export type SelectorFactory = (
dispatch: Dispatch<any>,
options: IConnectOptions,
) => (state: any, props: any) => any;
// TODO: Move
const invariant = (test: boolean, error: string): void => {
if (!test) {
throw new Error(error);
}
};
function getDefaultName(name): string {
return `ConnectAdvanced(${name})`;
}
export function connectAdvanced(
selectorFactory: SelectorFactory,
{
getDisplayName = getDefaultName,
methodName = 'connectAdvanced',
renderCountProp = null,
shouldHandleStateChanges = true,
storeKey = 'store',
withRef = false,
...connectOptions
}: Partial<IConnectOptions>,
// eslint-disable-next-line @typescript-eslint/ban-types
): Function {
const subscriptionKey = storeKey + 'Subscription';
const version = hotReloadingVersion++;
// eslint-disable-next-line @typescript-eslint/ban-types
const wrapWithConnect = <T extends Function>(WrappedComponent: T): any => {
invariant(
typeof WrappedComponent === 'function',
`You must pass a component to the function returned by ` +
`connect. Instead received ${WrappedComponent as any}`,
);
const wrappedComponentName: string =
(WrappedComponent as any).displayName ||
WrappedComponent.name ||
'Component';
const displayName = getDisplayName(wrappedComponentName);
const selectorFactoryOptions: IConnectOptions = {
...connectOptions,
WrappedComponent,
displayName,
getDisplayName,
methodName,
renderCountProp,
shouldHandleStateChanges,
storeKey,
withRef,
wrappedComponentName,
};
class Connect<P, S> extends Component<P, S> {
/* eslint-disable */
// @ts-ignore
public state: {};
/* eslint-enable */
public static displayName = displayName;
public static WrappedComponent = WrappedComponent;
public version: number;
private renderCount: number;
private readonly propsMode: boolean;
private store: Store<any> | null;
private notifyNestedSubs: (() => void) | null;
private subscription: Subscription | null;
private wrappedInstance: any;
private selector: {
error: Error | null;
shouldComponentUpdate: boolean;
props: any;
run: (props: any) => void;
};
constructor(props: P, context?: any) {
super(props, context);
this.version = version;
this.state = {};
this.renderCount = 0;
this.store = this.props[storeKey] || this.context[storeKey];
this.propsMode = Boolean(props[storeKey]);
this.setWrappedInstance = this.setWrappedInstance.bind(this);
invariant(
!!this.store,
`Could not find "${storeKey}" in either the context or ` +
`props of "${displayName}". ` +
`Either wrap the root component in a <Provider>, ` +
`or explicitly pass "${storeKey}" as a prop to "${displayName}".`,
);
this.initSelector();
this.initSubscription();
}
public getChildContext(): Record<string, any> {
// If this component received store from props, its subscription should be transparent
// to any descendants receiving store+subscription from context; it passes along
// subscription passed to it. Otherwise, it shadows the parent subscription, which allows
// Connect to control ordering of notifications to flow top-down.
const subscription = this.propsMode ? null : this.subscription;
return {
[subscriptionKey]: subscription || this.context[subscriptionKey],
};
}
public componentWillMount(): void {
if (!shouldHandleStateChanges || this.$SSR) {
return;
}
this.subscription!.trySubscribe();
this.selector.run(this.props);
}
public componentWillReceiveProps(nextProps): void {
this.selector.run(nextProps);
}
public shouldComponentUpdate(): boolean {
return this.selector.shouldComponentUpdate;
}
public componentWillUnmount(): void {
if (this.subscription) {
this.subscription.tryUnsubscribe();
}
// these are just to guard against extra memory leakage if a parent element doesn't
// dereference this instance properly, such as an async callback that never finishes
this.subscription = null;
this.notifyNestedSubs = noop;
this.store = null;
this.selector.run = noop;
this.selector.shouldComponentUpdate = false;
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
public getWrappedInstance() {
invariant(
withRef,
`To access the wrapped instance, you need to specify ` +
`{ withRef: true } in the options argument of the ${methodName}() call.`,
);
return this.wrappedInstance;
}
private setWrappedInstance(ref): void {
this.wrappedInstance = ref;
}
public initSelector(): void {
const sourceSelector = selectorFactory(
this.store!.dispatch,
selectorFactoryOptions,
);
this.selector = makeSelectorStateful(sourceSelector, this.store!);
this.selector.run(this.props);
}
public initSubscription(): void {
if (!shouldHandleStateChanges) {
return;
}
// parentSub's source should match where store came from: props vs. context. A component
// connected to the store via props shouldn't use subscription from context, or vice versa.
const parentSub = (this.propsMode ? this.props : this.context)[
subscriptionKey
];
this.subscription = new Subscription(
this.store!,
parentSub,
this.onStateChange.bind(this),
);
// `notifyNestedSubs` is duplicated to handle the case where the component is unmounted in
// the middle of the notification loop, where `this.subscription` will then be null. An
// extra null check every change can be avoided by copying the method onto `this` and then
// replacing it with a no-op on unmount. This can probably be avoided if Subscription's
// listeners logic is changed to not call listeners that have been unsubscribed in the
// middle of the notification loop.
this.notifyNestedSubs = this.subscription.notifyNestedSubs.bind(
this.subscription,
);
}
private onStateChange(): void {
this.selector.run(this.props);
if (!this.selector.shouldComponentUpdate) {
this.notifyNestedSubs!();
} else {
// eslint-disable-next-line @typescript-eslint/unbound-method
this.componentDidUpdate = this.notifyNestedSubsOnComponentDidUpdate;
this.setState({});
}
}
private notifyNestedSubsOnComponentDidUpdate(): void {
// `componentDidUpdate` is conditionally implemented when `onStateChange` determines it
// needs to notify nested subs. Once called, it unimplements itself until further state
// changes occur. Doing it this way vs having a permanent `componentDidMount` that does
// a boolean check every time avoids an extra method call most of the time, resulting
// in some perf boost.
this.componentDidUpdate = undefined;
this.notifyNestedSubs!();
}
public isSubscribed(): boolean {
return Boolean(this.subscription?.isSubscribed());
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
private addExtraProps(props: any) {
if (!renderCountProp) {
return props;
}
// make a shallow copy so that fields added don't leak to the original selector.
// this is especially important for 'ref' since that's a reference back to the component
// instance. a singleton memoized selector would then be holding a reference to the
// instance, preventing the instance from being garbage collected, and that would be bad
const withExtras = { ...props };
if (renderCountProp) {
withExtras[renderCountProp] = this.renderCount++;
}
if (this.propsMode && this.subscription) {
withExtras[subscriptionKey] = this.subscription;
}
return withExtras;
}
public render(): InfernoNode {
const selector = this.selector;
selector.shouldComponentUpdate = false;
if (selector.error) {
throw selector.error;
} else {
return normalizeProps(
createComponentVNode(
VNodeFlags.ComponentUnknown,
WrappedComponent,
this.addExtraProps(selector.props),
null,
// eslint-disable-next-line @typescript-eslint/unbound-method
withRef ? this.setWrappedInstance : null,
),
);
}
}
}
if (process.env.NODE_ENV !== 'production') {
(Connect.prototype as any).componentWillUpdate =
function componentWillUpdate() {
if (this.version !== version) {
// We are hot reloading!
this.version = version;
this.initSelector();
if (this.subscription) {
this.subscription.tryUnsubscribe();
}
this.initSubscription();
if (shouldHandleStateChanges) {
this.subscription.trySubscribe();
}
}
};
}
hoistStaticProperties(Connect, WrappedComponent);
return Connect;
};
return wrapWithConnect;
}