/
ComponentRegistry.ts
64 lines (58 loc) · 2.37 KB
/
ComponentRegistry.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
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
import Component, {IComponentConstructor} from "./Component";
import ClearScreen from "./render/ClearScreen";
/**
* A Registry of Component classes.
*
* ComponentRegistry maintains a map from Component name
* to Component class constructors. Typically used through the [[Main.componentRegistry]]
*/
export default class ComponentRegistry {
private components: {[name: string]: IComponentConstructor} = {};
private tags: string[] = [];
/**
* construct a ComponentRegistry with an initial set of Component class
* constructors.
* @param componentClasses one or more Component class constructors
*/
constructor(componentClasses?: IComponentConstructor | IComponentConstructor[]) {
if (componentClasses) {
this.addComponent(componentClasses);
}
}
/**
* Add a Component class constructor into the registry. Each constructor is mapped
* to the static [[Component.componentName]] value of the class.
* @param componentClasses one or more Component class constructors to be added
*/
public addComponent(componentClasses: IComponentConstructor | IComponentConstructor[]) {
if (!Array.isArray(componentClasses)) {
componentClasses = [componentClasses];
}
componentClasses.forEach((componentClass) => {
const name = componentClass.getComponentName();
if (name in this.components) {
throw new Error(`Component ${name} already exists in the registry`);
}
this.components[name] = componentClass;
if (this.tags.indexOf(componentClass.getComponentTag()) === -1) {
this.tags.push(componentClass.getComponentTag());
}
});
}
/**
* Returns the Component class constructor mapped to the given name
* @param name componentName of the Component class to be retrieved
*/
public getComponentClass(name: string): IComponentConstructor {
return this.components[name];
}
/**
* Returns all components with given tag
* @param tag of component classes to be returned
*/
public getComponentClassesByTag(tag: string): IComponentConstructor[] {
return Object.keys(this.components)
.map((name) => this.components[name])
.filter((componentClass) => componentClass.getComponentTag() === tag);
}
}