-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.ts
76 lines (66 loc) · 1.75 KB
/
Registry.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
65
66
67
68
69
70
71
72
73
74
75
76
import {RegistryInterface} from "@enhavo/core/RegistryInterface";
import RegistryEntry from "@enhavo/core/RegistryEntry";
export default class Registry implements RegistryInterface
{
private entries: RegistryEntry[] = [];
private get(name: string): RegistryEntry
{
for(let entry of this.entries) {
if(entry.getName() == name) {
return entry;
}
}
throw 'Entry with name "'+name+'" does not exist in registry';
}
getFactory(name: string): object
{
return this.get(name).getFactory();
}
getComponent(name: string): object
{
return this.get(name).getComponent();
}
register(entry: RegistryEntry): RegistryInterface
{
if(this.has(entry.getName())) {
this.deleteEntry(entry.getName());
}
this.entries.push(entry);
return this;
}
private deleteEntry(name: string)
{
for (let i in this.entries) {
if(this.entries[i].getName() == name) {
this.entries.splice(parseInt(i), 1);
break;
}
}
}
has(name: string): boolean
{
for(let entry of this.entries) {
if(entry.getName() == name) {
return true;
}
}
return false;
}
getComponents(): Component[]
{
let components = [];
for(let entry of this.entries) {
components.push(new Component(entry.getName(), entry.getComponent()));
}
return components;
}
}
export class Component
{
public name: string;
public component: object;
constructor(name: string, component: object) {
this.name = name;
this.component = component;
}
}