-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
135 lines (105 loc) · 2.91 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
class Container {
constructor(parentContainer) {
this._parentContainer = parentContainer;
this._classes = new Map();
this._factories = new Map();
this._instances = new Map();
this._values = new Map();
this._uniqueNames = new Set();
}
classesGet(name){
return this._classes.get(name) || this._parentContainer && this._parentContainer.classesGet(name);
}
factoriesGet(name){
return this._factories.get(name) || this._parentContainer && this._parentContainer.factoriesGet(name);
}
instancesGet(name){
return this._instances.get(name) || this._parentContainer && this._parentContainer.instancesGet(name);
}
valuesGet(name){
return this._values.get(name) || this._parentContainer && this._parentContainer.valuesGet(name);
}
registerClass(name, Class, isSingleton = false) {
this._setName(name);
this._classes.set(name, {Class, isSingleton});
}
registerFactory(name, factory) {
this._setName(name);
this._factories.set(name, factory);
}
registerValue(name, value) {
this._setName(name);
this._values.set(name, value);
}
getInstance(name) {
const instance = this.instancesGet(name);
if(instance) {
return instance;
}
const value = this.valuesGet(name);
if(value) {
return value;
}
const classItem = this.classesGet(name);
if(classItem) {
const args = this._getConstructorArgNames(classItem.Class);
const dependencies = this._getDependencies(args);
const instance = new classItem.Class(...dependencies);
if(classItem.isSingleton) {
this._instances.set(name, instance);
}
return instance;
}
const factory = this.factoriesGet(name);
if(factory) {
const args = this._getFunctionArgNames(factory);
const dependencies = this._getDependencies(args);
return factory(...dependencies);
}
throw new Error('Dependency ' + name + ' not found');
}
checkDependencies() {
this._uniqueNames.forEach((name) => {
try{
this.getInstance(name);
} catch(e) {
throw new Error('Dependency "' + name + '" fail: ' + e.message);
}
});
if(this._parentContainer){
this._parentContainer.checkDependencies();
}
}
checkDuplicateName(name){
if(this._uniqueNames.has(name)) {
throw new Error('Duplicate dependency name:' + name);
}
}
_setName(name) {
this.checkDuplicateName(name);
this._uniqueNames.add(name);
}
_getDependencies(args) {
return args.map((argName) => {
return this.getInstance(argName);
});
}
_getFunctionArgNames(func) {
const regex = /^(?:function)?\s*[^\(]*\(\s*([^\)]*)\)(?:=>)?/m;
return this._parseArgs(func, regex);
};
_getConstructorArgNames(cls) {
const regex = /constructor\s*[^\(]*\(\s*([^\)]*)\)/m;
return this._parseArgs(cls, regex);
};
_parseArgs(method, regex) {
const matched = method.toString().match(regex);
const args = (matched) ? matched[1].trim().split(/[\s,]+/) : [''];
if(args[0] === '') {
return [];
}
return args;
}
}
module.exports = Container;