-
Notifications
You must be signed in to change notification settings - Fork 1
/
module-loader.ts
37 lines (33 loc) · 1.26 KB
/
module-loader.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
import { ComponentType } from 'react'
import { combineReducers, ReducersMapObject, Store } from 'redux'
type ModuleType = {
View: ComponentType
reducers: ReducersMapObject
}
/**
* Provides a load() method that imports an ES6 module.
* The module must have a 'View' property that contains a React component and optionally a map of reducers.
* The reducers map is added to the overall set of reducers of the central Redux store.
* The React component is returned to the caller, which uses React.lazy to render it.
*/
export class ModuleLoader {
private readonly store: Store
private readonly reducers: ReducersMapObject
constructor(store: Store, reducers: ReducersMapObject) {
this.store = store
this.reducers = reducers
}
load = (path: string) => async () => {
const module: ModuleType = await import(path)
console.log('Loaded module', module)
if (module.reducers) {
// Note: this.reducers is changed in place
Object.assign(this.reducers, module.reducers)
this.store.replaceReducer(combineReducers(this.reducers))
}
return {
// React.lazy() expects a default export referring to a React component
default: module.View
}
}
}