UNPKG

1.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Container to be used by this library for inversion control. If container was not implicitly set then by default
5 * container simply creates a new instance of the given class.
6 */
7var defaultContainer = new (/** @class */ (function () {
8 function class_1() {
9 this.instances = [];
10 }
11 class_1.prototype.get = function (someClass) {
12 var instance = this.instances.find(function (i) { return i.type === someClass; });
13 if (!instance) {
14 instance = { type: someClass, object: new someClass() };
15 this.instances.push(instance);
16 }
17 return instance.object;
18 };
19 return class_1;
20}()))();
21var userContainer;
22var userContainerOptions;
23/**
24 * Sets container to be used by this library.
25 */
26function useContainer(iocContainer, options) {
27 userContainer = iocContainer;
28 userContainerOptions = options;
29}
30exports.useContainer = useContainer;
31/**
32 * Gets the IOC container used by this library.
33 */
34function getFromContainer(someClass) {
35 if (userContainer) {
36 try {
37 var instance = userContainer.get(someClass);
38 if (instance)
39 return instance;
40 if (!userContainerOptions || !userContainerOptions.fallback)
41 return instance;
42 }
43 catch (error) {
44 if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
45 throw error;
46 }
47 }
48 return defaultContainer.get(someClass);
49}
50exports.getFromContainer = getFromContainer;
51
52//# sourceMappingURL=container.js.map