-
Notifications
You must be signed in to change notification settings - Fork 726
/
Copy pathcontainer.ts
381 lines (305 loc) · 14.4 KB
/
container.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
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
import { Binding } from "../bindings/binding";
import * as ERROR_MSGS from "../constants/error_msgs";
import { BindingScopeEnum, TargetTypeEnum } from "../constants/literal_types";
import * as METADATA_KEY from "../constants/metadata_keys";
import { interfaces } from "../interfaces/interfaces";
import { MetadataReader } from "../planning/metadata_reader";
import { createMockRequest, getBindingDictionary, plan } from "../planning/planner";
import { resolve } from "../resolution/resolver";
import { BindingToSyntax } from "../syntax/binding_to_syntax";
import { id } from "../utils/id";
import { getServiceIdentifierAsString } from "../utils/serialization";
import { ContainerSnapshot } from "./container_snapshot";
import { Lookup } from "./lookup";
class Container implements interfaces.Container {
public id: number;
public parent: interfaces.Container | null;
public readonly options: interfaces.ContainerOptions;
private _middleware: interfaces.Next | null;
private _bindingDictionary: interfaces.Lookup<interfaces.Binding<any>>;
private _snapshots: interfaces.ContainerSnapshot[];
private _metadataReader: interfaces.MetadataReader;
public static merge(container1: interfaces.Container, container2: interfaces.Container): interfaces.Container {
const container = new Container();
const bindingDictionary: interfaces.Lookup<interfaces.Binding<any>> = getBindingDictionary(container);
const bindingDictionary1: interfaces.Lookup<interfaces.Binding<any>> = getBindingDictionary(container1);
const bindingDictionary2: interfaces.Lookup<interfaces.Binding<any>> = getBindingDictionary(container2);
function copyDictionary(
origin: interfaces.Lookup<interfaces.Binding<any>>,
destination: interfaces.Lookup<interfaces.Binding<any>>
) {
origin.traverse((key, value) => {
value.forEach((binding) => {
destination.add(binding.serviceIdentifier, binding.clone());
});
});
}
copyDictionary(bindingDictionary1, bindingDictionary);
copyDictionary(bindingDictionary2, bindingDictionary);
return container;
}
public constructor(containerOptions?: interfaces.ContainerOptions) {
const options = containerOptions || {};
if (typeof options !== "object") {
throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_MUST_BE_AN_OBJECT}`);
}
if (options.defaultScope === undefined) {
options.defaultScope = BindingScopeEnum.Transient;
} else if (
options.defaultScope !== BindingScopeEnum.Singleton &&
options.defaultScope !== BindingScopeEnum.Transient &&
options.defaultScope !== BindingScopeEnum.Request
) {
throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_DEFAULT_SCOPE}`);
}
if (options.autoBindInjectable === undefined) {
options.autoBindInjectable = false;
} else if (
typeof options.autoBindInjectable !== "boolean"
) {
throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_AUTO_BIND_INJECTABLE}`);
}
if (options.skipBaseClassChecks === undefined) {
options.skipBaseClassChecks = false;
} else if (
typeof options.skipBaseClassChecks !== "boolean"
) {
throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_SKIP_BASE_CHECK}`);
}
this.options = {
autoBindInjectable: options.autoBindInjectable,
defaultScope: options.defaultScope,
skipBaseClassChecks: options.skipBaseClassChecks
};
this.id = id();
this._bindingDictionary = new Lookup<interfaces.Binding<any>>();
this._snapshots = [];
this._middleware = null;
this.parent = null;
this._metadataReader = new MetadataReader();
}
public load(...modules: interfaces.ContainerModule[]) {
const getHelpers = this._getContainerModuleHelpersFactory();
for (const currentModule of modules) {
const containerModuleHelpers = getHelpers(currentModule.id);
currentModule.registry(
containerModuleHelpers.bindFunction,
containerModuleHelpers.unbindFunction,
containerModuleHelpers.isboundFunction,
containerModuleHelpers.rebindFunction
);
}
}
public async loadAsync(...modules: interfaces.AsyncContainerModule[]) {
const getHelpers = this._getContainerModuleHelpersFactory();
for (const currentModule of modules) {
const containerModuleHelpers = getHelpers(currentModule.id);
await currentModule.registry(
containerModuleHelpers.bindFunction,
containerModuleHelpers.unbindFunction,
containerModuleHelpers.isboundFunction,
containerModuleHelpers.rebindFunction
);
}
}
public unload(...modules: interfaces.ContainerModule[]): void {
const conditionFactory = (expected: any) => (item: interfaces.Binding<any>): boolean =>
item.moduleId === expected;
modules.forEach((module) => {
const condition = conditionFactory(module.id);
this._bindingDictionary.removeByCondition(condition);
});
}
// Registers a type binding
public bind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T> {
const scope = this.options.defaultScope || BindingScopeEnum.Transient;
const binding = new Binding<T>(serviceIdentifier, scope);
this._bindingDictionary.add(serviceIdentifier, binding);
return new BindingToSyntax<T>(binding);
}
public rebind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T> {
this.unbind(serviceIdentifier);
return this.bind(serviceIdentifier);
}
// Removes a type binding from the registry by its key
public unbind(serviceIdentifier: interfaces.ServiceIdentifier<any>): void {
try {
this._bindingDictionary.remove(serviceIdentifier);
} catch (e) {
throw new Error(`${ERROR_MSGS.CANNOT_UNBIND} ${getServiceIdentifierAsString(serviceIdentifier)}`);
}
}
// Removes all the type bindings from the registry
public unbindAll(): void {
this._bindingDictionary = new Lookup<Binding<any>>();
}
// Allows to check if there are bindings available for serviceIdentifier
public isBound(serviceIdentifier: interfaces.ServiceIdentifier<any>): boolean {
let bound = this._bindingDictionary.hasKey(serviceIdentifier);
if (!bound && this.parent) {
bound = this.parent.isBound(serviceIdentifier);
}
return bound;
}
public isBoundNamed(serviceIdentifier: interfaces.ServiceIdentifier<any>, named: string | number | symbol): boolean {
return this.isBoundTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
}
// Check if a binding with a complex constraint is available without throwing a error. Ancestors are also verified.
public isBoundTagged(serviceIdentifier: interfaces.ServiceIdentifier<any>, key: string | number | symbol, value: any): boolean {
let bound = false;
// verify if there are bindings available for serviceIdentifier on current binding dictionary
if (this._bindingDictionary.hasKey(serviceIdentifier)) {
const bindings = this._bindingDictionary.get(serviceIdentifier);
const request = createMockRequest(this, serviceIdentifier, key, value);
bound = bindings.some((b) => b.constraint(request));
}
// verify if there is a parent container that could solve the request
if (!bound && this.parent) {
bound = this.parent.isBoundTagged(serviceIdentifier, key, value);
}
return bound;
}
public snapshot(): void {
this._snapshots.push(ContainerSnapshot.of(this._bindingDictionary.clone(), this._middleware));
}
public restore(): void {
const snapshot = this._snapshots.pop();
if (snapshot === undefined) {
throw new Error(ERROR_MSGS.NO_MORE_SNAPSHOTS_AVAILABLE);
}
this._bindingDictionary = snapshot.bindings;
this._middleware = snapshot.middleware;
}
public createChild(containerOptions?: interfaces.ContainerOptions): Container {
const child = new Container(containerOptions || this.options);
child.parent = this;
return child;
}
public applyMiddleware(...middlewares: interfaces.Middleware[]): void {
const initial: interfaces.Next = (this._middleware) ? this._middleware : this._planAndResolve();
this._middleware = middlewares.reduce(
(prev, curr) => curr(prev),
initial);
}
public applyCustomMetadataReader(metadataReader: interfaces.MetadataReader) {
this._metadataReader = metadataReader;
}
// Resolves a dependency by its runtime identifier
// The runtime identifier must be associated with only one binding
// use getAll when the runtime identifier is associated with multiple bindings
public get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T {
return this._get<T>(false, false, TargetTypeEnum.Variable, serviceIdentifier) as T;
}
public getTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: any): T {
return this._get<T>(false, false, TargetTypeEnum.Variable, serviceIdentifier, key, value) as T;
}
public getNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T {
return this.getTagged<T>(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
}
// Resolves a dependency by its runtime identifier
// The runtime identifier can be associated with one or multiple bindings
public getAll<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T[] {
return this._get<T>(true, true, TargetTypeEnum.Variable, serviceIdentifier) as T[];
}
public getAllTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string | number | symbol, value: any): T[] {
return this._get<T>(false, true, TargetTypeEnum.Variable, serviceIdentifier, key, value) as T[];
}
public getAllNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string | number | symbol): T[] {
return this.getAllTagged<T>(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
}
public resolve<T>(constructorFunction: interfaces.Newable<T>) {
const tempContainer = this.createChild();
tempContainer.bind<T>(constructorFunction).toSelf();
return tempContainer.get<T>(constructorFunction);
}
private _getContainerModuleHelpersFactory() {
const setModuleId = (bindingToSyntax: any, moduleId: number) => {
bindingToSyntax._binding.moduleId = moduleId;
};
const getBindFunction = (moduleId: number) =>
(serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
const _bind = this.bind.bind(this);
const bindingToSyntax = _bind(serviceIdentifier);
setModuleId(bindingToSyntax, moduleId);
return bindingToSyntax;
};
const getUnbindFunction = (moduleId: number) =>
(serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
const _unbind = this.unbind.bind(this);
_unbind(serviceIdentifier);
};
const getIsboundFunction = (moduleId: number) =>
(serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
const _isBound = this.isBound.bind(this);
return _isBound(serviceIdentifier);
};
const getRebindFunction = (moduleId: number) =>
(serviceIdentifier: interfaces.ServiceIdentifier<any>) => {
const _rebind = this.rebind.bind(this);
const bindingToSyntax = _rebind(serviceIdentifier);
setModuleId(bindingToSyntax, moduleId);
return bindingToSyntax;
};
return (mId: number) => ({
bindFunction: getBindFunction(mId),
isboundFunction: getIsboundFunction(mId),
rebindFunction: getRebindFunction(mId),
unbindFunction: getUnbindFunction(mId)
});
}
// Prepares arguments required for resolution and
// delegates resolution to _middleware if available
// otherwise it delegates resolution to _planAndResolve
private _get<T>(
avoidConstraints: boolean,
isMultiInject: boolean,
targetType: interfaces.TargetType,
serviceIdentifier: interfaces.ServiceIdentifier<any>,
key?: string | number | symbol,
value?: any
): (T | T[]) {
let result: (T | T[]) | null = null;
const defaultArgs: interfaces.NextArgs = {
avoidConstraints,
contextInterceptor: (context: interfaces.Context) => context,
isMultiInject,
key,
serviceIdentifier,
targetType,
value
};
if (this._middleware) {
result = this._middleware(defaultArgs);
if (result === undefined || result === null) {
throw new Error(ERROR_MSGS.INVALID_MIDDLEWARE_RETURN);
}
} else {
result = this._planAndResolve<T>()(defaultArgs);
}
return result;
}
// Planner creates a plan and Resolver resolves a plan
// one of the jobs of the Container is to links the Planner
// with the Resolver and that is what this function is about
private _planAndResolve<T>(): (args: interfaces.NextArgs) => (T | T[]) {
return (args: interfaces.NextArgs) => {
// create a plan
let context = plan(
this._metadataReader,
this,
args.isMultiInject,
args.targetType,
args.serviceIdentifier,
args.key,
args.value,
args.avoidConstraints
);
// apply context interceptor
context = args.contextInterceptor(context);
// resolve plan
const result = resolve<T>(context);
return result;
};
}
}
export { Container };