-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFlask.ts
54 lines (44 loc) · 1.46 KB
/
Flask.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
export abstract class Flask {
private static containerSettings:any = {};
private static singletonObjects:any = {};
private static isInitialized:boolean = false;
public static inject(type: any) {
return {
to: (fieldName:string)=>{
Flask.containerSettings[fieldName] = type;
}
}
}
public static initialize() {
//if (Flask.isInitialized)
// return;
Flask.createSingletonObjects();
Flask.isInitialized = true;
}
public static reinitialize() {
Flask.createSingletonObjects();
}
private static createSingletonObjects(){
let injectQueue = [];
for (let fieldName in Flask.containerSettings){
//if (Flask.singletonObjects[fieldName] === undefined){
let objType:any = Flask.containerSettings[fieldName];
let newObj;
if (typeof objType === 'function')
newObj = new objType();
else
newObj = objType;
injectQueue.push(newObj);
Flask.singletonObjects[fieldName] = newObj;
//}
}
for (let i=0;i< injectQueue.length;i++)
if (injectQueue[i].inject !== undefined)
injectQueue[i].inject();
}
public inject(){
for (let fieldName in Flask.singletonObjects){
this[fieldName] = Flask.singletonObjects[fieldName];
}
}
}