Passed
Push — develop ( 9011bf...5a7fee )
by Endre
03:38
created

src/Application/ModuleLoader.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 42
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 42
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 2
mnd 1
bc 1
fnc 1
bpm 1
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A ModuleLoader.loadModule 0 15 2
1
import React from 'react';
2
import {IObserver} from '../Observer/Observer';
3
4
interface ILoadedModuleDictionary {
5
  [name: string]: typeof React.Component,
6
}
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
}