UNPKG

4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var ContainerInstance_1 = require("./ContainerInstance");
4/**
5 * Service container.
6 */
7var Container = /** @class */ (function () {
8 function Container() {
9 }
10 // -------------------------------------------------------------------------
11 // Public Static Methods
12 // -------------------------------------------------------------------------
13 /**
14 * Gets a separate container instance for the given instance id.
15 */
16 Container.of = function (instanceId) {
17 if (instanceId === undefined)
18 return this.globalInstance;
19 var container = this.instances.find(function (instance) { return instance.id === instanceId; });
20 if (!container) {
21 container = new ContainerInstance_1.ContainerInstance(instanceId);
22 this.instances.push(container);
23 }
24 return container;
25 };
26 /**
27 * Checks if the service with given name or type is registered service container.
28 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
29 */
30 Container.has = function (identifier) {
31 return this.globalInstance.has(identifier);
32 };
33 /**
34 * Retrieves the service with given name or type from the service container.
35 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
36 */
37 Container.get = function (identifier) {
38 return this.globalInstance.get(identifier);
39 };
40 /**
41 * Gets all instances registered in the container of the given service identifier.
42 * Used when service defined with multiple: true flag.
43 */
44 Container.getMany = function (id) {
45 return this.globalInstance.getMany(id);
46 };
47 /**
48 * Sets a value for the given type or service name in the container.
49 */
50 Container.set = function (identifierOrServiceMetadata, value) {
51 this.globalInstance.set(identifierOrServiceMetadata, value);
52 return this;
53 };
54 /**
55 * Removes services with a given service identifiers (tokens or types).
56 */
57 Container.remove = function () {
58 var ids = [];
59 for (var _i = 0; _i < arguments.length; _i++) {
60 ids[_i] = arguments[_i];
61 }
62 (_a = this.globalInstance).remove.apply(_a, ids);
63 return this;
64 var _a;
65 };
66 /**
67 * Completely resets the container by removing all previously registered services and handlers from it.
68 */
69 Container.reset = function (containerId) {
70 if (containerId) {
71 var instance = this.instances.find(function (instance) { return instance.id === containerId; });
72 if (instance) {
73 instance.reset();
74 this.instances.splice(this.instances.indexOf(instance), 1);
75 }
76 }
77 else {
78 this.globalInstance.reset();
79 this.instances.forEach(function (instance) { return instance.reset(); });
80 }
81 return this;
82 };
83 /**
84 * Registers a new handler.
85 */
86 Container.registerHandler = function (handler) {
87 this.handlers.push(handler);
88 return this;
89 };
90 /**
91 * Helper method that imports given services.
92 */
93 Container.import = function (services) {
94 return this;
95 };
96 // -------------------------------------------------------------------------
97 // Private Static Properties
98 // -------------------------------------------------------------------------
99 /**
100 * Global container instance.
101 */
102 Container.globalInstance = new ContainerInstance_1.ContainerInstance(undefined);
103 /**
104 * Other containers created using Container.of method.
105 */
106 Container.instances = [];
107 /**
108 * All registered handlers.
109 */
110 Container.handlers = [];
111 return Container;
112}());
113exports.Container = Container;
114
115//# sourceMappingURL=Container.js.map