-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFeatureConnector.tsx
More file actions
112 lines (88 loc) · 3.4 KB
/
Copy pathFeatureConnector.tsx
File metadata and controls
112 lines (88 loc) · 3.4 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
import React from 'react';
import { bind } from 'decko';
import * as R from 'ramda';
import { injectable } from 'inversify';
import { Omit, GetProps, SubSet } from '_helpers';
import { inject, TYPES, IocTypes } from './configure/ioc';
import { IFeatureEntry } from './types';
type FeatureLoader = () => Promise<IFeatureEntry>;
interface IState {
mounted: boolean;
}
const bundles = new Map<FeatureLoader, IFeatureEntry>();
// tslint:disable:max-line-length
function featureConnect<L extends Record<string, FeatureLoader>>(loaders: L, preloader?: React.ReactChild):
<Props extends { [K in keyof L]: any }>(WrappedComponent: React.ComponentType<Props>) => React.ComponentType<Omit<Props, keyof L>> {
return <Props extends { [K in keyof L]: any }>(
WrappedComponent: React.ComponentType<Props>,
): React.ComponentClass<Omit<Props, keyof L>> => {
@injectable()
class FeatureConnector extends React.PureComponent<Props, IState> {
public state: IState = { mounted: false };
@inject(TYPES.connectEntryToStore)
private connectFeatureToStore!: IocTypes[typeof TYPES.connectEntryToStore];
public async bootstrap() {
await this.load();
}
public componentDidMount() {
if (!this.isAllBundlesLoaded()) {
this.load();
}
this.setState({ mounted: true });
}
public componentWillUnmount() {
this.saveBundle = null;
this.setState({ mounted: false });
}
public render() {
if (!this.isAllBundlesLoaded()) {
return preloader || null;
} else {
return <WrappedComponent {...this.getBundles()} {...this.props} />;
}
}
@bind
private async load() {
const keys: Array<keyof L> = Object.keys(loaders);
await Promise.all(
keys.map((key) => {
return loaders[key]().then(bundle => {
bundle.reduxEntry && this.connectFeatureToStore(bundle.reduxEntry);
if (this.saveBundle) {
this.saveBundle(bundle, key);
}
});
}),
);
this.saveBundle && this.state.mounted && this.forceUpdate();
}
private saveBundle: null | ((bundle: IFeatureEntry, key: keyof L) => void) = (bundle, key) => {
bundles.set(loaders[key], bundle);
}
private getBundles(): Record<string, IFeatureEntry> {
return R.map(value => bundles.get(value) || {}, loaders);
}
@bind
private isAllBundlesLoaded(): boolean {
return Object.keys(loaders).every(key => Boolean(bundles.get(loaders[key])));
}
}
return FeatureConnector;
};
}
type IFeatureEntryWithContainers<C extends Record<string, React.ComponentType<any>>> =
SubSet<IFeatureEntry, { containers: C }>;
export function getAsyncContainer<C extends Record<string, React.ComponentType<any>>, K extends keyof C>(
loader: () => Promise<IFeatureEntryWithContainers<C>>, componentName: K,
): React.ComponentClass<GetProps<C[K]>> {
interface IRenderProps {
_entry?: IFeatureEntryWithContainers<C>;
}
function render({ _entry, ...props }: IRenderProps) {
if (!_entry || !_entry.containers) { return null; }
const Container: React.ComponentType<any> = _entry.containers[componentName];
return <Container {...props} />;
}
return featureConnect({ _entry: loader })(render) as React.ComponentClass<GetProps<C[K]>>;
}
export default featureConnect;