-
Notifications
You must be signed in to change notification settings - Fork 716
/
resolver.ts
270 lines (215 loc) · 9.46 KB
/
resolver.ts
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
import * as ERROR_MSGS from "../constants/error_msgs";
import { BindingScopeEnum, BindingTypeEnum } from "../constants/literal_types";
import { interfaces } from "../interfaces/interfaces";
import { getBindingDictionary } from "../planning/planner";
import { isPromise } from "../utils/async";
import { isStackOverflowExeption } from "../utils/exceptions";
import { getServiceIdentifierAsString } from "../utils/serialization";
import { resolveInstance } from "./instantiation";
type FactoryType = "toDynamicValue" | "toFactory" | "toAutoFactory" | "toProvider";
const invokeFactory = (
factoryType: FactoryType,
serviceIdentifier: interfaces.ServiceIdentifier<any>,
fn: () => any
) => {
try {
return fn();
} catch (error) {
if (isStackOverflowExeption(error)) {
throw new Error(
ERROR_MSGS.CIRCULAR_DEPENDENCY_IN_FACTORY(factoryType, serviceIdentifier.toString())
);
} else {
throw error;
}
}
};
const _resolveRequest = <T>(requestScope: interfaces.RequestScope) =>
(request: interfaces.Request): undefined | T | Promise<T> | (T | Promise<T>)[] => {
request.parentContext.setCurrentRequest(request);
const bindings = request.bindings;
const childRequests = request.childRequests;
const targetIsAnArray = request.target && request.target.isArray();
const targetParentIsNotAnArray = !request.parentRequest ||
!request.parentRequest.target ||
!request.target ||
!request.parentRequest.target.matchesArray(request.target.serviceIdentifier);
if (targetIsAnArray && targetParentIsNotAnArray) {
// Create an array instead of creating an instance
return childRequests.map((childRequest: interfaces.Request) => {
const _f = _resolveRequest(requestScope);
return _f(childRequest) as T | Promise<T>;
});
} else {
let result: undefined | T | Promise<T> | (T | Promise<T>)[];
if (request.target.isOptional() && bindings.length === 0) {
return undefined;
}
const binding = bindings[0];
const isSingleton = binding.scope === BindingScopeEnum.Singleton;
const isRequestSingleton = binding.scope === BindingScopeEnum.Request;
if (isSingleton && binding.activated) {
return binding.cache;
}
if (
isRequestSingleton &&
requestScope !== null &&
requestScope.has(binding.id)
) {
return requestScope.get(binding.id);
}
if (binding.type === BindingTypeEnum.ConstantValue) {
result = binding.cache;
binding.activated = true;
} else if (binding.type === BindingTypeEnum.Function) {
result = binding.cache;
binding.activated = true;
} else if (binding.type === BindingTypeEnum.Constructor) {
result = binding.implementationType as unknown as T;
} else if (binding.type === BindingTypeEnum.DynamicValue && binding.dynamicValue !== null) {
result = invokeFactory(
"toDynamicValue",
binding.serviceIdentifier,
() => (binding.dynamicValue as (context: interfaces.Context) => any)(request.parentContext)
);
} else if (binding.type === BindingTypeEnum.Factory && binding.factory !== null) {
result = invokeFactory(
"toFactory",
binding.serviceIdentifier,
() => (binding.factory as interfaces.FactoryCreator<any>)(request.parentContext)
);
} else if (binding.type === BindingTypeEnum.Provider && binding.provider !== null) {
result = invokeFactory(
"toProvider",
binding.serviceIdentifier,
() => (binding.provider as interfaces.Provider<any>)(request.parentContext)
);
} else if (binding.type === BindingTypeEnum.Instance && binding.implementationType !== null) {
result = resolveInstance(
binding,
binding.implementationType,
childRequests,
_resolveRequest(requestScope)
);
} else {
// The user probably created a binding but didn't finish it
// e.g. container.bind<T>("Something"); missing BindingToSyntax
const serviceIdentifier = getServiceIdentifierAsString(request.serviceIdentifier);
throw new Error(`${ERROR_MSGS.INVALID_BINDING_TYPE} ${serviceIdentifier}`);
}
if (isPromise(result)) {
result = result.then((resolved) => _onActivation(request, binding, resolved));
} else {
result = _onActivation(request, binding, result);
}
// store in cache if scope is singleton
if (isSingleton) {
binding.cache = result;
binding.activated = true;
if (isPromise(result)) {
result = result.catch((ex) => {
// allow binding to retry in future
binding.cache = null;
binding.activated = false;
throw ex;
});
}
}
if (
isRequestSingleton &&
requestScope !== null &&
!requestScope.has(binding.id)
) {
requestScope.set(binding.id, result);
}
return result;
}
};
function _onActivation<T>(request: interfaces.Request, binding: interfaces.Binding<T>, resolved: T): T | Promise<T> {
let result = _bindingActivation(request.parentContext, binding, resolved);
const containersIterator = _getContainersIterator(request.parentContext.container);
let container: interfaces.Container;
let containersIteratorResult = containersIterator.next();
do {
container = containersIteratorResult.value;
const context = request.parentContext;
const serviceIdentifier = request.serviceIdentifier;
const activationsIterator = _getContainerActivationsForService(container, serviceIdentifier);
if (isPromise(result)) {
result = _activateContainerAsync<T>(activationsIterator, context, result);
} else {
result = _activateContainer<T>(activationsIterator, context, result);
}
containersIteratorResult = containersIterator.next();
// make sure if we are currently on the container that owns the binding, not to keep looping down to child containers
} while (containersIteratorResult.done !== true && !getBindingDictionary(container).hasKey(request.serviceIdentifier));
return result;
}
const _bindingActivation = <T>(context: interfaces.Context, binding: interfaces.Binding<T>, previousResult: T): T | Promise<T> => {
let result: T | Promise<T>;
// use activation handler if available
if (typeof binding.onActivation === "function") {
result = binding.onActivation(context, previousResult);
} else {
result = previousResult;
}
return result;
}
const _activateContainer = <T> (
activationsIterator: Iterator<interfaces.BindingActivation<any>>,
context: interfaces.Context,
result: T,
): T | Promise<T> => {
let activation = activationsIterator.next();
while (!activation.done) {
result = activation.value(context, result);
if (isPromise<T>(result)) {
return _activateContainerAsync(activationsIterator, context, result);
}
activation = activationsIterator.next();
}
return result;
}
const _activateContainerAsync = async<T>(
activationsIterator: Iterator<interfaces.BindingActivation<any>>,
context: interfaces.Context,
resultPromise: Promise<T>,
): Promise<T> => {
let result = await resultPromise
let activation = activationsIterator.next();
while (!activation.done) {
result = await activation.value(context, result);
activation = activationsIterator.next();
}
return result;
}
const _getContainerActivationsForService = <T>(container: interfaces.Container, serviceIdentifier: interfaces.ServiceIdentifier<T>) => {
// smell accessing _activations, but similar pattern is done in planner.getBindingDictionary()
const activations = (container as any)._activations as interfaces.Lookup<interfaces.BindingActivation<any>>;
return activations.hasKey(serviceIdentifier) ? activations.get(serviceIdentifier).values() : [].values();
}
const _getContainersIterator = (container: interfaces.Container): Iterator<interfaces.Container> => {
const containersStack: interfaces.Container[] = [container];
let parent = container.parent;
while (parent !== null) {
containersStack.push(parent);
parent = parent.parent;
}
const getNextContainer: () => IteratorResult<interfaces.Container> = () => {
const nextContainer = containersStack.pop();
if (nextContainer !== undefined) {
return { done: false, value: nextContainer };
} else {
return { done: true, value: undefined };
}
};
const containersIterator: Iterator<interfaces.Container> = {
next: getNextContainer,
};
return containersIterator;
}
function resolve<T>(context: interfaces.Context): T | Promise<T> | (T | Promise<T>)[] {
const _f = _resolveRequest(context.plan.rootRequest.requestScope);
return _f(context.plan.rootRequest) as T | Promise<T> | (T | Promise<T>)[];
}
export { resolve };