forked from blambo/heroin-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroin.js
411 lines (347 loc) · 10.1 KB
/
heroin.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(function() {
function map(list, func) {
var res = Array(list.length);
var len = list.length;
for (var i = 0; i < len; i++) {
res[i] = func(list[i]);
}
return res;
}
function filter(list, func) {
var res = [];
var len = list.length;
for (var i = 0; i < len; i++) {
var val = list[i];
if (func(val)) {
res.push(val);
}
}
return res;
}
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function funcParams(func) {
if (typeof func !== 'function') {
throw new Error('Not a function: ' + func);
}
var mapped = map(
/\(([\s\S]*?)\)/.exec(func)[1].replace(/\/\*.*\*\//g, '').split(','),
trim);
return filter(mapped, function(a) {
return a;
});
}
function inheritObject(fromObj) {
var F;
F = function() {};
F.prototype = fromObj;
return new F;
}
function Injector(parent, values, providers) {
var me;
this.parent = parent;
this.values = values || {};
this.providers = providers || {};
me = this;
this.values.$injector = this;
this.values.di = this;
this.values.make = function(ctor, extraArgs) {
return new Injector(me, extraArgs).instantiate(ctor);
};
this.values.invoke = function(func, self, extraArgs) {
return me.invoke(func, self, extraArgs);
};
this.make = this.values.make;
this.make.__proto__ = this;
this._evaluating = {};
// Use the parent's annotate function if present, otherwise funcParams.
// E.g. if the ultimate ancestor is angular's injector, we will automatically
// support other annotate strategies such as $inject property or array method.
this.annotate = (parent && parent.annotate) || funcParams;
}
Injector.funcParams = funcParams;
Injector.transformingAnnotator = function(mapFunc, chainedAnnotator) {
chainedAnnotator = chainedAnnotator || funcParams;
return function(fn) {
var names = chainedAnnotator.call(this, fn)
var l = names.length;
for (var i = 0; i < l; i++) {
names[i] = mapFunc.call(this, names[i]);
}
return names;
};
};
Injector.prototype.child = function(/* mappings... */) {
var child = new Injector(this);
var l = arguments.length;
for (var i = 0; i < l; i++) {
child.load(arguments[i]);
}
return child;
};
// Resolution
Injector.prototype.get = function(name, optional) {
var maybeVal;
maybeVal = this.localGet(name);
if (maybeVal !== void 0) {
return maybeVal;
}
if (this.parent) {
return this.parent.get(name, optional);
}
if (!optional) {
throw new Error('Unresolved dependency: ' + name);
}
return void 0;
};
Injector.prototype.invoke = function(func, self, extraArgs) {
if (typeof func !== 'function') {
throw new Error("Invalid func: " + func);
}
return func.apply(self, this.resolveArgs(func, extraArgs, false));
};
/* Invoke with new child scope, putting the provided args in the child scope */
Injector.prototype.convoke = function(func, self, extraArgs) {
return new Injector(this, extraArgs).invoke(func, self, {});
};
Injector.prototype.instantiate = function(ctor, extraArgs) {
if (!ctor) {
throw new Error("Constructor not provided");
}
function Dummy() {};
Dummy.prototype = ctor.prototype;
var instance = new Dummy;
this.invoke(ctor, instance, extraArgs);
return instance;
};
Injector.prototype.resolve = function(funcOrName) {
if (typeof funcOrName === 'function') {
return this.invoke(funcOrName);
} else {
return this.get(funcOrName);
}
};
function insertArgs(incomplete, remaining) {
var len = incomplete.length;
var index = 0;
var args = incomplete.slice(); // copy
for (var i = 0; i < len; i++) {
if (args[i] === void 0) {
args[i] = remaining[index++];
}
}
len = remaining.length;
while (index < len) {
args.push(remaining[index++]);
}
return args;
}
Injector.prototype.curry = function(func, self, extraArgs) {
var curried, me = this;
func = this.asFunction(func);
return function(/* args... */) {
if (!curried) {
curried = me.resolveArgs(func, extraArgs, true);
}
return func.apply(self, insertArgs(curried, arguments));
};
// TODO: same as factory: extra curry.
};
// method on func returned from factory & curry
// used to curry more things on.
function curryMore(moreExtra) {
var extras = {};
for (var k in this._extraArgs) {
extras[k] = this._extraArgs[k];
}
for (var k in moreExtra) {
extras[k] = moreExtra[k];
}
return this._di[this._type](this._ctor, extras);
}
Injector.prototype.factory = function(ctor, extraArgs) {
var curried, me = this;
ctor = this.asFunction(ctor);
function Dummy() {};
Dummy.prototype = ctor.prototype;
var factoryFunc = function(/* args... */) {
if (!curried) {
curried = me.resolveArgs(ctor, extraArgs, true);
}
var instance = new Dummy;
ctor.apply(instance, insertArgs(curried, arguments));
return instance;
};
factoryFunc._ctor = ctor;
factoryFunc._extraArgs = extraArgs;
factoryFunc._di = this;
factoryFunc._type = 'factory';
factoryFunc.curry = curryMore;
return factoryFunc;
};
// Registration
Injector.prototype.value = function(name, val) {
this.values[this.checkNotRegistered(name)] = val;
return this;
};
Injector.prototype.provider = function(name, val) {
this.providers[this.checkNotRegistered(name)] = val;
return this;
};
/** Delegates to either .value or .provider depending on arg type */
Injector.prototype.register = function(name, valOrFunc) {
if (typeof valOrFunc === 'function') {
return this.provider(name, valOrFunc);
} else {
return this.value(name, valOrFunc);
}
};
Injector.prototype.load = function(map) {
for (var k in map) {
this.register(k, map[k]);
}
return this;
};
// Helpers
Injector.prototype.resolveArgs = function(func, extras, optional) {
var names = this.getArgs(func);
var args = Array(names.length);
var len = names.length;
if (extras) {
for (var i = 0; i < len; i++) {
var name = names[i];
args[i] = extras.hasOwnProperty(name) ?
extras[name] : this.get(name, optional);
}
} else {
for (var i = 0; i < len; i++) {
args[i] = this.get(names[i], optional);
}
}
return args;
};
/** Caching wrapper around annotate() */
Injector.prototype.getArgs = function(func) {
return func.$hxp || (func.$hxp = this.annotate(func));
}
/**
* func - function or name of function
*/
Injector.prototype.asFunction = function(func) {
if (!func) {
throw new Error("Function not provided");
}
if (typeof func === 'string') {
func = this.rawGet(func);
if (!func) {
throw new Error('Unknown dep name ' + func);
}
if (typeof func !== 'function') {
throw new Error('Dep ' + func + ' does not resolve to a function');
}
}
return func;
};
Injector.prototype.checkNotRegistered = function(name) {
if (this.localHas(name)) {
throw new Error("Already registered " + name +
" - create a child injector to override a dependency");
}
return name;
};
Injector.prototype.localGet = function(name) {
if (this.values.hasOwnProperty(name)) {
return this.values[name];
}
if (this.providers[name]) {
if (this._evaluating[name]) {
throw new Error("Circular dependency on " + name);
}
this._evaluating[name] = true;
var ret = this.values[name] = this.invoke(this.providers[name], null);
delete this._evaluating[name];
if (ret === void 0) {
throw new Error('provider returned undefined for ' + name);
}
return ret;
}
return void 0;
};
/**
* Gets the "raw" dep info, so, for values, the value, but
* for providers, the actual provider. Does not resolve anything.
*/
Injector.prototype.rawGet = function(name) {
var val = this.localRawGet(name);
if (this.parent) {
return this.parent.rawGet(name);
}
return void 0;
};
Injector.prototype.localRawGet = function(name) {
// Try providers first, because we cache vals,
// so this way we get the original raw value always.
var val = this.providers[name];
if (val !== void 0) {
return val;
}
val = this.values[name];
if (val !== void 0) {
return val;
}
return void 0;
};
Injector.prototype.has = function(name) {
return this.localHas(name) || (this.parent ? this.parent.has(name) : false);
};
Injector.prototype.localHas = function(name) {
return this.localRawGet(name) !== void 0;
};
Injector.providerOfDep = function(name) {
return function($injector) {
return $injector.get(name);
};
};
/**
* Helper to map dependency names
*
* e.g. registering mapped providers of
* { foo: 'bar' } means that if someone has
* "foo" as their input, then it will resolve
* to 'bar'. This is not baked into the injector,
* the mechanism is provided by this helper function.
*/
Injector.mappedProviders = function(mappings) {
var k, providers, v;
providers = {};
for (k in mappings) {
v = mappings[k];
providers[k] = Injector.providerOfDep(v);
}
return providers;
};
Injector.wrapAngular = function($injector) {
function Adaptor() {
this.get = function(name, optional) {
if (optional) {
return this.has(name) ? $injector.get(name) : void 0;
} else {
return $injector.get(name);
}
};
// Won't support getting raw factories/providers, but
// it's the best we can do.
this.rawGet = function(name) {
return this.get(name, true);
}
}
Adaptor.prototype = $injector;
return new Injector(new Adaptor);
};
if (typeof exports === 'undefined') {
this['heroin'] = {Injector: Injector};
} else {
module.exports = Injector;
}
})();