-
Notifications
You must be signed in to change notification settings - Fork 115
/
DynamicModuleLoader.tsx
189 lines (164 loc) · 6.1 KB
/
DynamicModuleLoader.tsx
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
import * as React from "react";
//@ts-ignore // ReactReduxContext is not officially exported
import { Provider, ReactReduxContext } from "react-redux";
import {
IDynamicallyAddedModule,
IModuleStore,
IModuleTuple,
} from "redux-dynamic-modules-core";
export interface IDynamicModuleLoaderProps {
/** Modules that need to be dynamically registerd */
modules: IModuleTuple;
/**
* Set this flag to indicate that this component is being rendered in 'Strict Mode'
* React 'StrictMode' does not allow constructor side-effects, so we defer adding modules to componentDidMount
* when this flag is set.
* This has the effect of adding a second render.
*/
strictMode?: boolean;
/** Optional callback which returns a store instance. This would be called if no store could be loaded from th e context. */
createStore?: () => IModuleStore<any>;
}
/**
* The DynamicModuleLoader adds a way to register a module on mount
* When this component is initialized, the reducer and saga from the module passed as props will be registered with the system
* On unmount, they will be unregistered
*/
export class DynamicModuleLoader extends React.Component<
IDynamicModuleLoaderProps
> {
public render() {
return (
<ReactReduxContext.Consumer>
{reactReduxContext => (
<DynamicModuleLoaderImpl
{...this.props}
reactReduxContext={reactReduxContext}
/>
)}
</ReactReduxContext.Consumer>
);
}
}
interface IDynamicModuleLoaderImplProps extends IDynamicModuleLoaderProps {
/** The react-redux context passed from the <Provider> component */
reactReduxContext?: { store: IModuleStore<any> };
}
interface IDynamicModuleLoaderImplState {
/** Is the DML component ready to render.
* If strictMode is set to false, this will be set to true initially
* If strict mode is set to true, this will be set after the first render completes
*/
readyToRender: boolean;
}
class DynamicModuleLoaderImpl extends React.Component<
IDynamicModuleLoaderImplProps,
IDynamicModuleLoaderImplState
> {
/** The modules that were added from this loader */
private _addedModules?: IDynamicallyAddedModule;
/** Flag that indicates we need to create a store/provider because a parent store was not provided */
private _providerInitializationNeeded: boolean = false;
/** The module store, derived from context */
private _store: IModuleStore<any>;
constructor(props: IDynamicModuleLoaderImplProps) {
super(props);
if (props.reactReduxContext == null) {
const message =
"Tried to render DynamicModuleLoader, but no ReactReduxContext was provided";
console.error(message);
throw new Error(message);
}
this._store = props.reactReduxContext
? props.reactReduxContext.store
: undefined;
// We are not in strict mode, let's add the modules ASAP
if (!this.props.strictMode) {
this._addModules();
this.state = { readyToRender: true };
} else {
// We are in strict mode, so have to wait for CDM to add modules.
// Thus, we cannot render the children at this time
this.state = { readyToRender: false };
}
}
public render(): React.ReactNode {
if (this.state.readyToRender) {
if (this._providerInitializationNeeded) {
return (
<Provider store={this._store}>
{/* We just rendered the provider, so now we need to render
DML again. This one will add the modules */}
<DynamicModuleLoader {...this.props} />
</Provider>
);
}
return (
<>
{this._renderLoader()}
<AddedModulesCleanup cleanup={this._cleanup} />
</>
);
}
return null;
}
/**
* Render a Redux provider
*/
private _renderLoader(): React.ReactNode {
return this.props.children
? typeof this.props.children === "function"
? this.props.children()
: this.props.children
: null;
}
private _addModules(): void {
const { createStore, modules } = this.props;
if (!this._store) {
// If we need to create a store, do that here. We will skip adding the modules and render DML again
if (createStore) {
this._store = createStore();
this._providerInitializationNeeded = true;
} else {
throw new Error(
"Store could not be resolved from React context"
);
}
} else {
// Add the modules here
this._addedModules = this._store.addModules(modules);
}
}
public componentDidMount() {
if (this.props.strictMode) {
this._addModules();
this.setState({ readyToRender: true });
}
}
/**
* Unregister sagas and reducers
*/
private _cleanup = () => {
if (this._addedModules) {
this._addedModules.remove();
this._addedModules = undefined;
}
}
}
interface IAddedModulesCleanupProps {
cleanup: () => any;
}
/**
* This component is rendered as the last child of DynamicModuleLoaderImpl
* so react runs willUnmount on connected(react-redux) children before this
* cleanup and allows them to unsubscribe from store before dynamic reducers
* removing (and avoid errors in selectors)
*/
class AddedModulesCleanup extends React.Component<IAddedModulesCleanupProps> {
public render() {
return null;
}
public componentWillUnmount() {
this.props.cleanup();
}
}