1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getFromContainer = exports.useContainer = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const defaultContainer = new (class {
|
9 | constructor() {
|
10 | this.instances = [];
|
11 | }
|
12 | get(someClass) {
|
13 | let instance = this.instances.find(instance => instance.type === someClass);
|
14 | if (!instance) {
|
15 | instance = { type: someClass, object: new someClass() };
|
16 | this.instances.push(instance);
|
17 | }
|
18 | return instance.object;
|
19 | }
|
20 | })();
|
21 | let userContainer;
|
22 | let userContainerOptions;
|
23 |
|
24 |
|
25 |
|
26 | function useContainer(iocContainer, options) {
|
27 | userContainer = iocContainer;
|
28 | userContainerOptions = options;
|
29 | }
|
30 | exports.useContainer = useContainer;
|
31 |
|
32 |
|
33 |
|
34 | function getFromContainer(someClass) {
|
35 | if (userContainer) {
|
36 | try {
|
37 | const 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 | }
|
50 | exports.getFromContainer = getFromContainer;
|
51 |
|
\ | No newline at end of file |