/
index.js
126 lines (108 loc) · 3.24 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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function() {
return (root.Container = factory());
});
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Container = factory();
}
}(this, function(exports, b) {
var _isArray = Array.isArray || function(obj) {
return toString.call(obj) === "[object Array]";
};
function _noop() {}
function _isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}
function _construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
function _validate(name, registration) {
var i = 0,
dep, deps = registration.dependencies,
len;
if (deps && (len = deps.length)) {
for (; i < len; i++) {
if ((dep = this._factories[deps[i]]) && !!~dep.dependencies.indexOf(name)) {
throw new Error(name + ' registration failed because of circular dependency with "' + deps[i] + '"');
}
}
}
}
function _provide(registration) {
var
len, instance, providedInstance,
i = 0,
args = [],
deps = registration.dependencies,
cfg = registration.configure,
factory = registration.factory;
if (deps && (len = deps.length)) {
for (; i < len; i++) {
args.push(this.resolve(deps[i]));
}
}
instance = !_isFunction(factory) ? factory : (factory.apply(null, args) || _construct(factory, args));
if (instance.$get) {
cfg(instance);
providedInstance = instance.$get.apply(instance, args);
}
return providedInstance || instance;
}
function Container(log) {
this._instances = {};
this._factories = {};
this._log = (log || _noop).bind(null, 'di');
}
Container.prototype.register = function register(name, factory, dependencies, configure) {
if (name in this._factories) {
console.warn(name + ' already registered in container');
} else {
if (_isArray(factory)) {
dependencies = factory;
factory = dependencies.pop();
}
if (_isFunction(dependencies)) {
configure = dependencies;
dependencies = null;
}
var registration = {
factory: factory,
configure: configure || _noop,
dependencies: dependencies || factory.$inject || []
};
_validate.call(this, name, registration);
this._factories[name] = registration;
this._log('register', name);
}
};
Container.prototype.resolve = function resolve(name) {
if (name in this._instances) {
this._log('resolve', name);
return this._instances[name];
} else if (name in this._factories) {
this._log('provide', name);
return (this._instances[name] = _provide.call(this, this._factories[name]));
} else {
throw new Error(name + ' is not registered in container');
}
};
Container.prototype.Mixin = function() {
var
deps = arguments || [],
mixin = {
services: {}
};
for (var i = 0, len = deps.length; i < len; i++) {
mixin.services[deps[i]] = this.resolve(deps[i]);
}
return mixin;
};
return Container;
}));