-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodependency.js
165 lines (146 loc) · 4.44 KB
/
nodependency.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function () {
var lifeCycles = {};
var instances = {};
function LifeCycle ( key ) {
if(typeof lifeCycles[key] !== 'undefined') {
return lifeCycles[key];
}
var lifeCycleProto = {
resolveFactory : function ( key ) {
if(this.name === "singleton") {
if(typeof this.cache[key] === 'undefined') {
this.cache[key] = _resolveDependencies(this.factories[key].factoryMethod)();
}
return this.cache[key];
} else if (this.name === "transient") {
return _resolveDependencies(this.factories[key].factoryMethod)();
}
if(typeof this.cache[key] !== 'undefined') {
return this.cache[key];
} else {
var valueToCache = _resolveDependencies(this.factories[key].factoryMethod)();
this.cache[key] = valueToCache;
return valueToCache;
}
},
factory : function ( key, fact ) {
this.factories[key] = new Factory({
name: key,
factoryMethod: fact,
lifeCycle: this
});
return this;
},
expire: function () {
this.cache = {};
return this;
}
}
var thisLifeCycle = Object.create(lifeCycleProto, {
name: { writable: false, value: key },
factories : { writable: true, value: {} },
instances: { writable: true, value: {} },
cache: { writable: true, value: {} }
});
lifeCycles[key] = thisLifeCycle;
return thisLifeCycle;
}
function Factory ( options ) {
return Object.create({}, {
name: { writable: false, value: options.name },
lifeCycle : { writable: false, value: options.lifeCycle },
factoryMethod: {
get: function () {
if(typeof options.arguments !== 'undefined') {
return options.factoryMethod.apply(options.factoryMethod, options.arguments);
}
return options.factoryMethod;
}
}
});
}
function _getFactory ( key ) {
for (var lc in lifeCycles) {
if(lifeCycles.hasOwnProperty(lc)) {
var lifeCycle = lifeCycles[lc];
if(typeof lifeCycle.factories[key] !== 'undefined') {
return lifeCycle.factories[key];
}
}
}
}
function _getInstance ( key ) {
if (typeof instances[key] !== 'undefined') {
return instances[key];
}
}
function _resolveItem ( key, type ) {
if(type === 'instance') {
return _getInstance(key);
} else if (type === 'factory') {
var resolvedItem = _resolveDependencies(_getFactory(key).factoryMethod);
return resolvedItem();
}
throw new Error('Dependency not found: ' + key);
}
function _resolveDependencies ( method ) {
var methodString = method.toString()
, commentRegex = /\/\*.*?\*\//
, paramRegex = /\bfunction\s*\S*\s*?\(\s*([^)]+)/i;
methodString = methodString.replace(commentRegex, "");
var matches = paramRegex.exec(methodString);
if(!matches || matches.length === 0) {
return method;
}
var dependencies = matches[1].split(',').map(function(item) {
return item.trim();
});
var replacements = [];
dependencies.forEach(function(item) {
if(typeof _getFactory(item) !== 'undefined') {
replacements.push(_resolveItem(item, 'factory'));
}
var instance = _getInstance(item);
if(typeof instance !== 'undefined') {
replacements.push(instance);
}
});
return function () {
return method.apply(method, replacements);
}
}
var injectorProto = {
lifeCycle : (function() {
function F(args) {
return LifeCycle.apply(this, args);
}
F.prototype = LifeCycle.prototype;
return function () {
return new F(arguments);
}
})(),
factory : function ( key, fact ) {
if(typeof fact === 'undefined') {
var factory = _getFactory(key);
return factory.lifeCycle.resolveFactory(key);
}
return LifeCycle('transient').factory(key, fact);
},
instance : function (key, instance) {
if(typeof instance === 'undefined') {
return _getInstance(key);
}
instances[key] = instance;
},
inject: function ( method ) {
var injected = _resolveDependencies(method);
return injected;
}
}
var injector = Object.create(injectorProto);
if (typeof module !== "undefined" && typeof require !== "undefined") {
module.exports = injector;
} else {
window["h"] = injector;
}
})();