-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1-internal-injector.html
145 lines (126 loc) · 3.67 KB
/
1-internal-injector.html
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
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8"/>
<title>AngularJS Injector</title>
<script src="ngsrc/minErr.js"></script>
<script src="ngsrc/Angular.js"></script>
<script src="ngsrc/apis.js"></script>
<script src="ngsrc/auto/injector.js"></script>
</head>
<body>
<script>
var INSTANTIATING = {},
providerSuffix = 'Provider',
path = [];
function createInternalInjector(cache, factory) {
function getService(serviceName) {
if (cache.hasOwnProperty(serviceName)) {
if (cache[serviceName] === INSTANTIATING) {
throw $injectorMinErr('cdep', 'Circular dependency found: {0}', path.join(' <- '));
}
return cache[serviceName];
} else {
try {
path.unshift(serviceName);
cache[serviceName] = INSTANTIATING;
return cache[serviceName] = factory(serviceName);
} catch (err) {
if (cache[serviceName] === INSTANTIATING) {
delete cache[serviceName];
}
throw err;
} finally {
path.shift();
}
}
}
function invoke(fn, self, locals) {
var args = [],
$inject = annotate(fn),
length, i,
key;
for (i = 0, length = $inject.length; i < length; i++) {
key = $inject[i];
if (typeof key !== 'string') {
throw $injectorMinErr('itkn',
'Incorrect injection token! Expected service name as string, got {0}', key);
}
args.push(
locals && locals.hasOwnProperty(key)
? locals[key]
: getService(key)
);
}
if (!fn.$inject) {
// this means that we must be an array.
fn = fn[length];
}
// http://jsperf.com/angularjs-invoke-apply-vs-switch
// #5388
return fn.apply(self, args);
}
function instantiate(Type, locals) {
var Constructor = function () {
},
instance, returnedValue;
// Check if Type is annotated and use just the given function at n-1 as parameter
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
Constructor.prototype = (isArray(Type) ? Type[Type.length - 1] : Type).prototype;
instance = new Constructor();
returnedValue = invoke(Type, instance, locals);
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
}
return {
invoke: invoke,
instantiate: instantiate,
get: getService,
annotate: annotate,
has: function (name) {
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
}
};
}
var theCache = {
a: 'world',
b: function(value) { return value; },
c: function(a) { console.log(a); }
};
var injector = createInternalInjector(theCache, function(serviceName) {
console.log('Hi:', serviceName);
});
/// annotate
// inference
console.log(injector.annotate(function(a, b) {} )); // ['a', 'b']
// inline
console.log(injector.annotate(['a', 'b', function(a, b) {}]));
// $inject
var f = function(a, b) {};
f.$inject = ['a', 'b'];
console.log(injector.annotate(f));
/// get
console.log(injector.get('a'));
console.log(injector.get('b'));
/// invoke
injector.invoke(function(a) {
console.log(a);
}, null, {a: 'hihihi'});
injector.invoke(function(b) {
console.log(b('test'));
});
injector.invoke(function(c) {
injector.invoke(c);
});
/// instantiate
var s1 = injector.instantiate(function(b) {
var s = {};
s.test = function(v) {
console.log(b(v));
};
return s;
});
console.log(s1);
s1.test('test instantiate');
</script>
</body>
</html>