| Total Complexity | 2 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | import React from 'react'; |
||
| 7 | |||
| 8 | export default class ModuleLoader { |
||
| 9 | moduleState: IObserver<typeof React.Component | null>; |
||
| 10 | moduleNameState: IObserver<string>; |
||
| 11 | dictionary: ILoadedModuleDictionary; |
||
| 12 | pathToRoot: string; |
||
| 13 | |||
| 14 | constructor( |
||
| 15 | pathToRoot: string, |
||
| 16 | moduleNameState: IObserver<string>, |
||
| 17 | moduleState: IObserver<typeof React.Component | null> |
||
| 18 | ) { |
||
| 19 | 3 | this.moduleNameState = moduleNameState; |
|
| 20 | 3 | this.moduleState = moduleState; |
|
| 21 | 3 | this.dictionary = {}; |
|
| 22 | 3 | this.pathToRoot = pathToRoot; |
|
| 23 | |||
| 24 | 3 | this.moduleNameState.adapter.onChange = this.loadModule.bind(this); |
|
| 25 | } |
||
| 26 | |||
| 27 | async loadModule(oldValue: string, newValue: string) { |
||
| 28 | let module: typeof React.Component; |
||
| 29 | |||
| 30 | 3 | if (!this.dictionary.hasOwnProperty(newValue)) { |
|
| 31 | 2 | module = |
|
| 32 | ( |
||
| 33 | await import(this.pathToRoot + newValue + '.js') |
||
| 34 | ).default as typeof React.Component; |
||
| 35 | 2 | this.dictionary[newValue] = module; |
|
| 36 | } else { |
||
| 37 | 1 | module = this.dictionary[newValue]; |
|
| 38 | } |
||
| 39 | |||
| 40 | 3 | this.moduleState.value = module; |
|
| 41 | } |
||
| 42 | } |