inversify 6.0.2 → 6.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/.nyc_output/c29b2307-4e95-47d0-b220-9efcfa7ee49f.json +1 -0
  2. package/.nyc_output/processinfo/c29b2307-4e95-47d0-b220-9efcfa7ee49f.json +1 -0
  3. package/.nyc_output/processinfo/index.json +1 -1
  4. package/CHANGELOG.md +86 -81
  5. package/LICENSE +22 -22
  6. package/README.md +311 -309
  7. package/amd/annotation/decorator_utils.js +117 -114
  8. package/amd/annotation/inject.js +31 -27
  9. package/amd/annotation/inject_base.js +16 -17
  10. package/amd/annotation/injectable.js +40 -37
  11. package/amd/annotation/lazy_service_identifier.js +15 -15
  12. package/amd/annotation/multi_inject.js +31 -27
  13. package/amd/annotation/named.js +32 -29
  14. package/amd/annotation/optional.js +32 -29
  15. package/amd/annotation/post_construct.js +32 -28
  16. package/amd/annotation/pre_destroy.js +32 -28
  17. package/amd/annotation/property_event_decorator.js +16 -17
  18. package/amd/annotation/tagged.js +8 -9
  19. package/amd/annotation/target_name.js +35 -32
  20. package/amd/annotation/unmanaged.js +35 -32
  21. package/amd/bindings/binding.js +39 -39
  22. package/amd/bindings/binding_count.js +10 -10
  23. package/amd/constants/error_msgs.js +74 -64
  24. package/amd/constants/literal_types.js +28 -28
  25. package/amd/constants/metadata_keys.js +28 -28
  26. package/amd/container/container.js +723 -719
  27. package/amd/container/container_module.js +21 -21
  28. package/amd/container/container_snapshot.js +20 -20
  29. package/amd/container/lookup.js +131 -127
  30. package/amd/container/module_activation_store.js +53 -53
  31. package/amd/interfaces/interfaces.js +9 -10
  32. package/amd/inversify.js +59 -55
  33. package/amd/planning/context.js +19 -19
  34. package/amd/planning/metadata.js +45 -41
  35. package/amd/planning/metadata_reader.js +47 -43
  36. package/amd/planning/plan.js +13 -13
  37. package/amd/planning/planner.js +166 -163
  38. package/amd/planning/queryable_string.js +30 -30
  39. package/amd/planning/reflection_utils.js +161 -157
  40. package/amd/planning/request.js +26 -26
  41. package/amd/planning/target.js +105 -101
  42. package/amd/resolution/instantiation.js +214 -211
  43. package/amd/resolution/resolver.js +225 -222
  44. package/amd/scope/scope.js +93 -93
  45. package/amd/syntax/binding_in_syntax.js +24 -24
  46. package/amd/syntax/binding_in_when_on_syntax.js +75 -75
  47. package/amd/syntax/binding_on_syntax.js +20 -20
  48. package/amd/syntax/binding_to_syntax.js +109 -105
  49. package/amd/syntax/binding_when_on_syntax.js +65 -65
  50. package/amd/syntax/binding_when_syntax.js +102 -102
  51. package/amd/syntax/constraint_helpers.js +65 -61
  52. package/amd/utils/async.js +16 -17
  53. package/amd/utils/binding_utils.js +81 -77
  54. package/amd/utils/clonable.js +11 -12
  55. package/amd/utils/exceptions.js +46 -42
  56. package/amd/utils/factory_type.js +11 -11
  57. package/amd/utils/id.js +9 -10
  58. package/amd/utils/js.js +18 -19
  59. package/amd/utils/serialization.js +132 -129
  60. package/es/annotation/decorator_utils.js +88 -88
  61. package/es/annotation/inject.js +4 -4
  62. package/es/annotation/inject_base.js +14 -14
  63. package/es/annotation/injectable.js +13 -13
  64. package/es/annotation/lazy_service_identifier.js +10 -10
  65. package/es/annotation/multi_inject.js +4 -4
  66. package/es/annotation/named.js +7 -7
  67. package/es/annotation/optional.js +7 -7
  68. package/es/annotation/post_construct.js +5 -5
  69. package/es/annotation/pre_destroy.js +5 -5
  70. package/es/annotation/property_event_decorator.js +13 -13
  71. package/es/annotation/tagged.js +6 -6
  72. package/es/annotation/target_name.js +10 -10
  73. package/es/annotation/unmanaged.js +10 -10
  74. package/es/bindings/binding.js +36 -36
  75. package/es/bindings/binding_count.js +5 -5
  76. package/es/constants/error_msgs.js +62 -52
  77. package/es/constants/literal_types.js +21 -21
  78. package/es/constants/metadata_keys.js +23 -23
  79. package/es/container/container.js +707 -707
  80. package/es/container/container_module.js +17 -17
  81. package/es/container/container_snapshot.js +15 -15
  82. package/es/container/lookup.js +104 -104
  83. package/es/container/module_activation_store.js +49 -49
  84. package/es/interfaces/interfaces.js +5 -5
  85. package/es/inversify.js +25 -25
  86. package/es/planning/context.js +15 -15
  87. package/es/planning/metadata.js +17 -17
  88. package/es/planning/metadata_reader.js +19 -19
  89. package/es/planning/plan.js +8 -8
  90. package/es/planning/planner.js +147 -147
  91. package/es/planning/queryable_string.js +25 -25
  92. package/es/planning/reflection_utils.js +135 -135
  93. package/es/planning/request.js +22 -22
  94. package/es/planning/target.js +81 -81
  95. package/es/resolution/instantiation.js +190 -190
  96. package/es/resolution/resolver.js +205 -205
  97. package/es/scope/scope.js +88 -88
  98. package/es/syntax/binding_in_syntax.js +21 -21
  99. package/es/syntax/binding_in_when_on_syntax.js +73 -73
  100. package/es/syntax/binding_on_syntax.js +16 -16
  101. package/es/syntax/binding_to_syntax.js +84 -84
  102. package/es/syntax/binding_when_on_syntax.js +62 -62
  103. package/es/syntax/binding_when_syntax.js +99 -99
  104. package/es/syntax/constraint_helpers.js +35 -35
  105. package/es/utils/async.js +11 -11
  106. package/es/utils/binding_utils.js +53 -53
  107. package/es/utils/clonable.js +7 -7
  108. package/es/utils/exceptions.js +16 -16
  109. package/es/utils/factory_type.js +6 -6
  110. package/es/utils/id.js +5 -5
  111. package/es/utils/js.js +13 -13
  112. package/es/utils/serialization.js +100 -100
  113. package/es6/annotation/decorator_utils.js +112 -109
  114. package/es6/annotation/inject.js +30 -26
  115. package/es6/annotation/inject_base.js +17 -18
  116. package/es6/annotation/injectable.js +38 -35
  117. package/es6/annotation/lazy_service_identifier.js +12 -12
  118. package/es6/annotation/multi_inject.js +30 -26
  119. package/es6/annotation/named.js +32 -29
  120. package/es6/annotation/optional.js +32 -29
  121. package/es6/annotation/post_construct.js +31 -27
  122. package/es6/annotation/pre_destroy.js +31 -27
  123. package/es6/annotation/property_event_decorator.js +15 -16
  124. package/es6/annotation/tagged.js +8 -9
  125. package/es6/annotation/target_name.js +35 -32
  126. package/es6/annotation/unmanaged.js +35 -32
  127. package/es6/bindings/binding.js +38 -38
  128. package/es6/bindings/binding_count.js +8 -8
  129. package/es6/constants/error_msgs.js +62 -53
  130. package/es6/constants/literal_types.js +26 -26
  131. package/es6/constants/metadata_keys.js +26 -26
  132. package/es6/container/container.js +524 -520
  133. package/es6/container/container_module.js +18 -18
  134. package/es6/container/container_snapshot.js +15 -15
  135. package/es6/container/lookup.js +124 -120
  136. package/es6/container/module_activation_store.js +51 -51
  137. package/es6/interfaces/interfaces.js +7 -8
  138. package/es6/inversify.js +80 -76
  139. package/es6/planning/context.js +17 -17
  140. package/es6/planning/metadata.js +42 -38
  141. package/es6/planning/metadata_reader.js +42 -38
  142. package/es6/planning/plan.js +10 -10
  143. package/es6/planning/planner.js +173 -170
  144. package/es6/planning/queryable_string.js +27 -27
  145. package/es6/planning/reflection_utils.js +159 -155
  146. package/es6/planning/request.js +24 -24
  147. package/es6/planning/target.js +103 -99
  148. package/es6/resolution/instantiation.js +154 -151
  149. package/es6/resolution/resolver.js +190 -187
  150. package/es6/scope/scope.js +57 -57
  151. package/es6/syntax/binding_in_syntax.js +23 -23
  152. package/es6/syntax/binding_in_when_on_syntax.js +75 -75
  153. package/es6/syntax/binding_on_syntax.js +18 -18
  154. package/es6/syntax/binding_to_syntax.js +109 -105
  155. package/es6/syntax/binding_when_on_syntax.js +64 -64
  156. package/es6/syntax/binding_when_syntax.js +79 -79
  157. package/es6/syntax/constraint_helpers.js +62 -58
  158. package/es6/utils/async.js +14 -15
  159. package/es6/utils/binding_utils.js +72 -68
  160. package/es6/utils/clonable.js +9 -10
  161. package/es6/utils/exceptions.js +44 -40
  162. package/es6/utils/factory_type.js +9 -9
  163. package/es6/utils/id.js +7 -8
  164. package/es6/utils/js.js +15 -16
  165. package/es6/utils/serialization.js +129 -126
  166. package/lib/annotation/decorator_utils.d.ts +16 -16
  167. package/lib/annotation/decorator_utils.js +116 -113
  168. package/lib/annotation/inject.d.ts +2 -2
  169. package/lib/annotation/inject.js +30 -26
  170. package/lib/annotation/inject_base.d.ts +3 -3
  171. package/lib/annotation/inject_base.js +17 -18
  172. package/lib/annotation/injectable.d.ts +2 -2
  173. package/lib/annotation/injectable.js +38 -35
  174. package/lib/annotation/lazy_service_identifier.d.ts +7 -7
  175. package/lib/annotation/lazy_service_identifier.js +13 -13
  176. package/lib/annotation/multi_inject.d.ts +2 -2
  177. package/lib/annotation/multi_inject.js +30 -26
  178. package/lib/annotation/named.d.ts +2 -2
  179. package/lib/annotation/named.js +32 -29
  180. package/lib/annotation/optional.d.ts +2 -2
  181. package/lib/annotation/optional.js +32 -29
  182. package/lib/annotation/post_construct.d.ts +4 -4
  183. package/lib/annotation/post_construct.js +31 -27
  184. package/lib/annotation/pre_destroy.d.ts +4 -4
  185. package/lib/annotation/pre_destroy.js +31 -27
  186. package/lib/annotation/property_event_decorator.d.ts +4 -4
  187. package/lib/annotation/property_event_decorator.js +15 -16
  188. package/lib/annotation/tagged.d.ts +2 -2
  189. package/lib/annotation/tagged.js +8 -9
  190. package/lib/annotation/target_name.d.ts +3 -3
  191. package/lib/annotation/target_name.js +35 -32
  192. package/lib/annotation/unmanaged.d.ts +3 -3
  193. package/lib/annotation/unmanaged.js +35 -32
  194. package/lib/bindings/binding.d.ts +20 -20
  195. package/lib/bindings/binding.js +39 -39
  196. package/lib/bindings/binding_count.d.ts +5 -5
  197. package/lib/bindings/binding_count.js +8 -8
  198. package/lib/constants/error_msgs.d.ts +32 -32
  199. package/lib/constants/error_msgs.js +72 -62
  200. package/lib/constants/literal_types.d.ts +5 -5
  201. package/lib/constants/literal_types.js +26 -26
  202. package/lib/constants/metadata_keys.d.ts +13 -13
  203. package/lib/constants/metadata_keys.js +26 -26
  204. package/lib/container/container.d.ts +73 -73
  205. package/lib/container/container.js +733 -729
  206. package/lib/container/container_module.d.ts +11 -11
  207. package/lib/container/container_module.js +20 -20
  208. package/lib/container/container_snapshot.d.ts +10 -10
  209. package/lib/container/container_snapshot.js +18 -18
  210. package/lib/container/lookup.d.ts +16 -16
  211. package/lib/container/lookup.js +130 -126
  212. package/lib/container/module_activation_store.d.ts +10 -10
  213. package/lib/container/module_activation_store.js +52 -52
  214. package/lib/interfaces/interfaces.d.ts +284 -284
  215. package/lib/interfaces/interfaces.js +7 -8
  216. package/lib/inversify.d.ts +25 -25
  217. package/lib/inversify.js +80 -76
  218. package/lib/planning/context.d.ts +11 -11
  219. package/lib/planning/context.js +18 -18
  220. package/lib/planning/metadata.d.ts +8 -8
  221. package/lib/planning/metadata.js +43 -39
  222. package/lib/planning/metadata_reader.d.ts +6 -6
  223. package/lib/planning/metadata_reader.js +45 -41
  224. package/lib/planning/plan.d.ts +7 -7
  225. package/lib/planning/plan.js +11 -11
  226. package/lib/planning/planner.d.ts +5 -5
  227. package/lib/planning/planner.js +174 -171
  228. package/lib/planning/queryable_string.d.ts +11 -11
  229. package/lib/planning/queryable_string.js +28 -28
  230. package/lib/planning/reflection_utils.d.ts +5 -5
  231. package/lib/planning/reflection_utils.js +163 -159
  232. package/lib/planning/request.d.ts +14 -14
  233. package/lib/planning/request.js +25 -25
  234. package/lib/planning/target.d.ts +23 -23
  235. package/lib/planning/target.js +107 -103
  236. package/lib/resolution/instantiation.d.ts +3 -3
  237. package/lib/resolution/instantiation.js +215 -212
  238. package/lib/resolution/resolver.d.ts +3 -3
  239. package/lib/resolution/resolver.js +230 -227
  240. package/lib/scope/scope.d.ts +3 -3
  241. package/lib/scope/scope.js +93 -93
  242. package/lib/syntax/binding_in_syntax.d.ts +9 -9
  243. package/lib/syntax/binding_in_syntax.js +24 -24
  244. package/lib/syntax/binding_in_when_on_syntax.d.ts +29 -29
  245. package/lib/syntax/binding_in_when_on_syntax.js +76 -76
  246. package/lib/syntax/binding_on_syntax.d.ts +8 -8
  247. package/lib/syntax/binding_on_syntax.js +19 -19
  248. package/lib/syntax/binding_to_syntax.d.ts +18 -18
  249. package/lib/syntax/binding_to_syntax.js +110 -106
  250. package/lib/syntax/binding_when_on_syntax.d.ts +25 -25
  251. package/lib/syntax/binding_when_on_syntax.js +65 -65
  252. package/lib/syntax/binding_when_syntax.d.ts +21 -21
  253. package/lib/syntax/binding_when_syntax.js +102 -102
  254. package/lib/syntax/constraint_helpers.d.ts +6 -6
  255. package/lib/syntax/constraint_helpers.js +64 -60
  256. package/lib/tsconfig.tsbuildinfo +1 -1
  257. package/lib/utils/async.d.ts +3 -3
  258. package/lib/utils/async.js +14 -15
  259. package/lib/utils/binding_utils.d.ts +4 -4
  260. package/lib/utils/binding_utils.js +82 -78
  261. package/lib/utils/clonable.d.ts +3 -3
  262. package/lib/utils/clonable.js +9 -10
  263. package/lib/utils/exceptions.d.ts +2 -2
  264. package/lib/utils/exceptions.js +44 -40
  265. package/lib/utils/factory_type.d.ts +5 -5
  266. package/lib/utils/factory_type.js +9 -9
  267. package/lib/utils/id.d.ts +2 -2
  268. package/lib/utils/id.js +7 -8
  269. package/lib/utils/js.d.ts +1 -1
  270. package/lib/utils/js.js +16 -17
  271. package/lib/utils/serialization.d.ts +10 -10
  272. package/lib/utils/serialization.js +130 -127
  273. package/package.json +60 -61
  274. package/.nyc_output/16b95af9-19b9-451c-8475-9a8585677128.json +0 -1
  275. package/.nyc_output/processinfo/16b95af9-19b9-451c-8475-9a8585677128.json +0 -1
@@ -1,520 +1,524 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- };
30
- Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.Container = void 0;
32
- const binding_1 = require("../bindings/binding");
33
- const ERROR_MSGS = __importStar(require("../constants/error_msgs"));
34
- const literal_types_1 = require("../constants/literal_types");
35
- const METADATA_KEY = __importStar(require("../constants/metadata_keys"));
36
- const metadata_reader_1 = require("../planning/metadata_reader");
37
- const planner_1 = require("../planning/planner");
38
- const resolver_1 = require("../resolution/resolver");
39
- const binding_to_syntax_1 = require("../syntax/binding_to_syntax");
40
- const async_1 = require("../utils/async");
41
- const id_1 = require("../utils/id");
42
- const serialization_1 = require("../utils/serialization");
43
- const container_snapshot_1 = require("./container_snapshot");
44
- const lookup_1 = require("./lookup");
45
- const module_activation_store_1 = require("./module_activation_store");
46
- class Container {
47
- constructor(containerOptions) {
48
- const options = containerOptions || {};
49
- if (typeof options !== 'object') {
50
- throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_MUST_BE_AN_OBJECT}`);
51
- }
52
- if (options.defaultScope === undefined) {
53
- options.defaultScope = literal_types_1.BindingScopeEnum.Transient;
54
- }
55
- else if (options.defaultScope !== literal_types_1.BindingScopeEnum.Singleton &&
56
- options.defaultScope !== literal_types_1.BindingScopeEnum.Transient &&
57
- options.defaultScope !== literal_types_1.BindingScopeEnum.Request) {
58
- throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_DEFAULT_SCOPE}`);
59
- }
60
- if (options.autoBindInjectable === undefined) {
61
- options.autoBindInjectable = false;
62
- }
63
- else if (typeof options.autoBindInjectable !== 'boolean') {
64
- throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_AUTO_BIND_INJECTABLE}`);
65
- }
66
- if (options.skipBaseClassChecks === undefined) {
67
- options.skipBaseClassChecks = false;
68
- }
69
- else if (typeof options.skipBaseClassChecks !== 'boolean') {
70
- throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_SKIP_BASE_CHECK}`);
71
- }
72
- this.options = {
73
- autoBindInjectable: options.autoBindInjectable,
74
- defaultScope: options.defaultScope,
75
- skipBaseClassChecks: options.skipBaseClassChecks
76
- };
77
- this.id = (0, id_1.id)();
78
- this._bindingDictionary = new lookup_1.Lookup();
79
- this._snapshots = [];
80
- this._middleware = null;
81
- this._activations = new lookup_1.Lookup();
82
- this._deactivations = new lookup_1.Lookup();
83
- this.parent = null;
84
- this._metadataReader = new metadata_reader_1.MetadataReader();
85
- this._moduleActivationStore = new module_activation_store_1.ModuleActivationStore();
86
- }
87
- static merge(container1, container2, ...containers) {
88
- const container = new Container();
89
- const targetContainers = [container1, container2, ...containers]
90
- .map((targetContainer) => (0, planner_1.getBindingDictionary)(targetContainer));
91
- const bindingDictionary = (0, planner_1.getBindingDictionary)(container);
92
- function copyDictionary(origin, destination) {
93
- origin.traverse((_key, value) => {
94
- value.forEach((binding) => {
95
- destination.add(binding.serviceIdentifier, binding.clone());
96
- });
97
- });
98
- }
99
- targetContainers.forEach((targetBindingDictionary) => {
100
- copyDictionary(targetBindingDictionary, bindingDictionary);
101
- });
102
- return container;
103
- }
104
- load(...modules) {
105
- const getHelpers = this._getContainerModuleHelpersFactory();
106
- for (const currentModule of modules) {
107
- const containerModuleHelpers = getHelpers(currentModule.id);
108
- currentModule.registry(containerModuleHelpers.bindFunction, containerModuleHelpers.unbindFunction, containerModuleHelpers.isboundFunction, containerModuleHelpers.rebindFunction, containerModuleHelpers.unbindAsyncFunction, containerModuleHelpers.onActivationFunction, containerModuleHelpers.onDeactivationFunction);
109
- }
110
- }
111
- loadAsync(...modules) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- const getHelpers = this._getContainerModuleHelpersFactory();
114
- for (const currentModule of modules) {
115
- const containerModuleHelpers = getHelpers(currentModule.id);
116
- yield currentModule.registry(containerModuleHelpers.bindFunction, containerModuleHelpers.unbindFunction, containerModuleHelpers.isboundFunction, containerModuleHelpers.rebindFunction, containerModuleHelpers.unbindAsyncFunction, containerModuleHelpers.onActivationFunction, containerModuleHelpers.onDeactivationFunction);
117
- }
118
- });
119
- }
120
- unload(...modules) {
121
- modules.forEach((module) => {
122
- const deactivations = this._removeModuleBindings(module.id);
123
- this._deactivateSingletons(deactivations);
124
- this._removeModuleHandlers(module.id);
125
- });
126
- }
127
- unloadAsync(...modules) {
128
- return __awaiter(this, void 0, void 0, function* () {
129
- for (const module of modules) {
130
- const deactivations = this._removeModuleBindings(module.id);
131
- yield this._deactivateSingletonsAsync(deactivations);
132
- this._removeModuleHandlers(module.id);
133
- }
134
- });
135
- }
136
- bind(serviceIdentifier) {
137
- const scope = this.options.defaultScope || literal_types_1.BindingScopeEnum.Transient;
138
- const binding = new binding_1.Binding(serviceIdentifier, scope);
139
- this._bindingDictionary.add(serviceIdentifier, binding);
140
- return new binding_to_syntax_1.BindingToSyntax(binding);
141
- }
142
- rebind(serviceIdentifier) {
143
- this.unbind(serviceIdentifier);
144
- return this.bind(serviceIdentifier);
145
- }
146
- rebindAsync(serviceIdentifier) {
147
- return __awaiter(this, void 0, void 0, function* () {
148
- yield this.unbindAsync(serviceIdentifier);
149
- return this.bind(serviceIdentifier);
150
- });
151
- }
152
- unbind(serviceIdentifier) {
153
- if (this._bindingDictionary.hasKey(serviceIdentifier)) {
154
- const bindings = this._bindingDictionary.get(serviceIdentifier);
155
- this._deactivateSingletons(bindings);
156
- }
157
- this._removeServiceFromDictionary(serviceIdentifier);
158
- }
159
- unbindAsync(serviceIdentifier) {
160
- return __awaiter(this, void 0, void 0, function* () {
161
- if (this._bindingDictionary.hasKey(serviceIdentifier)) {
162
- const bindings = this._bindingDictionary.get(serviceIdentifier);
163
- yield this._deactivateSingletonsAsync(bindings);
164
- }
165
- this._removeServiceFromDictionary(serviceIdentifier);
166
- });
167
- }
168
- unbindAll() {
169
- this._bindingDictionary.traverse((_key, value) => {
170
- this._deactivateSingletons(value);
171
- });
172
- this._bindingDictionary = new lookup_1.Lookup();
173
- }
174
- unbindAllAsync() {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- const promises = [];
177
- this._bindingDictionary.traverse((_key, value) => {
178
- promises.push(this._deactivateSingletonsAsync(value));
179
- });
180
- yield Promise.all(promises);
181
- this._bindingDictionary = new lookup_1.Lookup();
182
- });
183
- }
184
- onActivation(serviceIdentifier, onActivation) {
185
- this._activations.add(serviceIdentifier, onActivation);
186
- }
187
- onDeactivation(serviceIdentifier, onDeactivation) {
188
- this._deactivations.add(serviceIdentifier, onDeactivation);
189
- }
190
- isBound(serviceIdentifier) {
191
- let bound = this._bindingDictionary.hasKey(serviceIdentifier);
192
- if (!bound && this.parent) {
193
- bound = this.parent.isBound(serviceIdentifier);
194
- }
195
- return bound;
196
- }
197
- isCurrentBound(serviceIdentifier) {
198
- return this._bindingDictionary.hasKey(serviceIdentifier);
199
- }
200
- isBoundNamed(serviceIdentifier, named) {
201
- return this.isBoundTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
202
- }
203
- isBoundTagged(serviceIdentifier, key, value) {
204
- let bound = false;
205
- if (this._bindingDictionary.hasKey(serviceIdentifier)) {
206
- const bindings = this._bindingDictionary.get(serviceIdentifier);
207
- const request = (0, planner_1.createMockRequest)(this, serviceIdentifier, key, value);
208
- bound = bindings.some((b) => b.constraint(request));
209
- }
210
- if (!bound && this.parent) {
211
- bound = this.parent.isBoundTagged(serviceIdentifier, key, value);
212
- }
213
- return bound;
214
- }
215
- snapshot() {
216
- this._snapshots.push(container_snapshot_1.ContainerSnapshot.of(this._bindingDictionary.clone(), this._middleware, this._activations.clone(), this._deactivations.clone(), this._moduleActivationStore.clone()));
217
- }
218
- restore() {
219
- const snapshot = this._snapshots.pop();
220
- if (snapshot === undefined) {
221
- throw new Error(ERROR_MSGS.NO_MORE_SNAPSHOTS_AVAILABLE);
222
- }
223
- this._bindingDictionary = snapshot.bindings;
224
- this._activations = snapshot.activations;
225
- this._deactivations = snapshot.deactivations;
226
- this._middleware = snapshot.middleware;
227
- this._moduleActivationStore = snapshot.moduleActivationStore;
228
- }
229
- createChild(containerOptions) {
230
- const child = new Container(containerOptions || this.options);
231
- child.parent = this;
232
- return child;
233
- }
234
- applyMiddleware(...middlewares) {
235
- const initial = (this._middleware) ? this._middleware : this._planAndResolve();
236
- this._middleware = middlewares.reduce((prev, curr) => curr(prev), initial);
237
- }
238
- applyCustomMetadataReader(metadataReader) {
239
- this._metadataReader = metadataReader;
240
- }
241
- get(serviceIdentifier) {
242
- const getArgs = this._getNotAllArgs(serviceIdentifier, false);
243
- return this._getButThrowIfAsync(getArgs);
244
- }
245
- getAsync(serviceIdentifier) {
246
- return __awaiter(this, void 0, void 0, function* () {
247
- const getArgs = this._getNotAllArgs(serviceIdentifier, false);
248
- return this._get(getArgs);
249
- });
250
- }
251
- getTagged(serviceIdentifier, key, value) {
252
- const getArgs = this._getNotAllArgs(serviceIdentifier, false, key, value);
253
- return this._getButThrowIfAsync(getArgs);
254
- }
255
- getTaggedAsync(serviceIdentifier, key, value) {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- const getArgs = this._getNotAllArgs(serviceIdentifier, false, key, value);
258
- return this._get(getArgs);
259
- });
260
- }
261
- getNamed(serviceIdentifier, named) {
262
- return this.getTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
263
- }
264
- getNamedAsync(serviceIdentifier, named) {
265
- return this.getTaggedAsync(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
266
- }
267
- getAll(serviceIdentifier) {
268
- const getArgs = this._getAllArgs(serviceIdentifier);
269
- return this._getButThrowIfAsync(getArgs);
270
- }
271
- getAllAsync(serviceIdentifier) {
272
- const getArgs = this._getAllArgs(serviceIdentifier);
273
- return this._getAll(getArgs);
274
- }
275
- getAllTagged(serviceIdentifier, key, value) {
276
- const getArgs = this._getNotAllArgs(serviceIdentifier, true, key, value);
277
- return this._getButThrowIfAsync(getArgs);
278
- }
279
- getAllTaggedAsync(serviceIdentifier, key, value) {
280
- const getArgs = this._getNotAllArgs(serviceIdentifier, true, key, value);
281
- return this._getAll(getArgs);
282
- }
283
- getAllNamed(serviceIdentifier, named) {
284
- return this.getAllTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
285
- }
286
- getAllNamedAsync(serviceIdentifier, named) {
287
- return this.getAllTaggedAsync(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
288
- }
289
- resolve(constructorFunction) {
290
- const isBound = this.isBound(constructorFunction);
291
- if (!isBound) {
292
- this.bind(constructorFunction).toSelf();
293
- }
294
- const resolved = this.get(constructorFunction);
295
- if (!isBound) {
296
- this.unbind(constructorFunction);
297
- }
298
- return resolved;
299
- }
300
- _preDestroy(constructor, instance) {
301
- var _a, _b;
302
- if (Reflect.hasMetadata(METADATA_KEY.PRE_DESTROY, constructor)) {
303
- const data = Reflect.getMetadata(METADATA_KEY.PRE_DESTROY, constructor);
304
- return (_b = (_a = instance)[data.value]) === null || _b === void 0 ? void 0 : _b.call(_a);
305
- }
306
- }
307
- _removeModuleHandlers(moduleId) {
308
- const moduleActivationsHandlers = this._moduleActivationStore.remove(moduleId);
309
- this._activations.removeIntersection(moduleActivationsHandlers.onActivations);
310
- this._deactivations.removeIntersection(moduleActivationsHandlers.onDeactivations);
311
- }
312
- _removeModuleBindings(moduleId) {
313
- return this._bindingDictionary.removeByCondition(binding => binding.moduleId === moduleId);
314
- }
315
- _deactivate(binding, instance) {
316
- const constructor = Object.getPrototypeOf(instance).constructor;
317
- try {
318
- if (this._deactivations.hasKey(binding.serviceIdentifier)) {
319
- const result = this._deactivateContainer(instance, this._deactivations.get(binding.serviceIdentifier).values());
320
- if ((0, async_1.isPromise)(result)) {
321
- return this._handleDeactivationError(result.then(() => this._propagateContainerDeactivationThenBindingAndPreDestroyAsync(binding, instance, constructor)), constructor);
322
- }
323
- }
324
- const propagateDeactivationResult = this._propagateContainerDeactivationThenBindingAndPreDestroy(binding, instance, constructor);
325
- if ((0, async_1.isPromise)(propagateDeactivationResult)) {
326
- return this._handleDeactivationError(propagateDeactivationResult, constructor);
327
- }
328
- }
329
- catch (ex) {
330
- if (ex instanceof Error) {
331
- throw new Error(ERROR_MSGS.ON_DEACTIVATION_ERROR(constructor.name, ex.message));
332
- }
333
- }
334
- }
335
- _handleDeactivationError(asyncResult, constructor) {
336
- return __awaiter(this, void 0, void 0, function* () {
337
- try {
338
- yield asyncResult;
339
- }
340
- catch (ex) {
341
- if (ex instanceof Error) {
342
- throw new Error(ERROR_MSGS.ON_DEACTIVATION_ERROR(constructor.name, ex.message));
343
- }
344
- }
345
- });
346
- }
347
- _deactivateContainer(instance, deactivationsIterator) {
348
- let deactivation = deactivationsIterator.next();
349
- while (deactivation.value) {
350
- const result = deactivation.value(instance);
351
- if ((0, async_1.isPromise)(result)) {
352
- return result.then(() => this._deactivateContainerAsync(instance, deactivationsIterator));
353
- }
354
- deactivation = deactivationsIterator.next();
355
- }
356
- }
357
- _deactivateContainerAsync(instance, deactivationsIterator) {
358
- return __awaiter(this, void 0, void 0, function* () {
359
- let deactivation = deactivationsIterator.next();
360
- while (deactivation.value) {
361
- yield deactivation.value(instance);
362
- deactivation = deactivationsIterator.next();
363
- }
364
- });
365
- }
366
- _getContainerModuleHelpersFactory() {
367
- const setModuleId = (bindingToSyntax, moduleId) => {
368
- bindingToSyntax._binding.moduleId = moduleId;
369
- };
370
- const getBindFunction = (moduleId) => (serviceIdentifier) => {
371
- const bindingToSyntax = this.bind(serviceIdentifier);
372
- setModuleId(bindingToSyntax, moduleId);
373
- return bindingToSyntax;
374
- };
375
- const getUnbindFunction = () => (serviceIdentifier) => {
376
- return this.unbind(serviceIdentifier);
377
- };
378
- const getUnbindAsyncFunction = () => (serviceIdentifier) => {
379
- return this.unbindAsync(serviceIdentifier);
380
- };
381
- const getIsboundFunction = () => (serviceIdentifier) => {
382
- return this.isBound(serviceIdentifier);
383
- };
384
- const getRebindFunction = (moduleId) => (serviceIdentifier) => {
385
- const bindingToSyntax = this.rebind(serviceIdentifier);
386
- setModuleId(bindingToSyntax, moduleId);
387
- return bindingToSyntax;
388
- };
389
- const getOnActivationFunction = (moduleId) => (serviceIdentifier, onActivation) => {
390
- this._moduleActivationStore.addActivation(moduleId, serviceIdentifier, onActivation);
391
- this.onActivation(serviceIdentifier, onActivation);
392
- };
393
- const getOnDeactivationFunction = (moduleId) => (serviceIdentifier, onDeactivation) => {
394
- this._moduleActivationStore.addDeactivation(moduleId, serviceIdentifier, onDeactivation);
395
- this.onDeactivation(serviceIdentifier, onDeactivation);
396
- };
397
- return (mId) => ({
398
- bindFunction: getBindFunction(mId),
399
- isboundFunction: getIsboundFunction(),
400
- onActivationFunction: getOnActivationFunction(mId),
401
- onDeactivationFunction: getOnDeactivationFunction(mId),
402
- rebindFunction: getRebindFunction(mId),
403
- unbindFunction: getUnbindFunction(),
404
- unbindAsyncFunction: getUnbindAsyncFunction()
405
- });
406
- }
407
- _getAll(getArgs) {
408
- return Promise.all(this._get(getArgs));
409
- }
410
- _get(getArgs) {
411
- const planAndResolveArgs = Object.assign(Object.assign({}, getArgs), { contextInterceptor: (context) => context, targetType: literal_types_1.TargetTypeEnum.Variable });
412
- if (this._middleware) {
413
- const middlewareResult = this._middleware(planAndResolveArgs);
414
- if (middlewareResult === undefined || middlewareResult === null) {
415
- throw new Error(ERROR_MSGS.INVALID_MIDDLEWARE_RETURN);
416
- }
417
- return middlewareResult;
418
- }
419
- return this._planAndResolve()(planAndResolveArgs);
420
- }
421
- _getButThrowIfAsync(getArgs) {
422
- const result = this._get(getArgs);
423
- if ((0, async_1.isPromiseOrContainsPromise)(result)) {
424
- throw new Error(ERROR_MSGS.LAZY_IN_SYNC(getArgs.serviceIdentifier));
425
- }
426
- return result;
427
- }
428
- _getAllArgs(serviceIdentifier) {
429
- const getAllArgs = {
430
- avoidConstraints: true,
431
- isMultiInject: true,
432
- serviceIdentifier,
433
- };
434
- return getAllArgs;
435
- }
436
- _getNotAllArgs(serviceIdentifier, isMultiInject, key, value) {
437
- const getNotAllArgs = {
438
- avoidConstraints: false,
439
- isMultiInject,
440
- serviceIdentifier,
441
- key,
442
- value,
443
- };
444
- return getNotAllArgs;
445
- }
446
- _planAndResolve() {
447
- return (args) => {
448
- let context = (0, planner_1.plan)(this._metadataReader, this, args.isMultiInject, args.targetType, args.serviceIdentifier, args.key, args.value, args.avoidConstraints);
449
- context = args.contextInterceptor(context);
450
- const result = (0, resolver_1.resolve)(context);
451
- return result;
452
- };
453
- }
454
- _deactivateIfSingleton(binding) {
455
- if (!binding.activated) {
456
- return;
457
- }
458
- if ((0, async_1.isPromise)(binding.cache)) {
459
- return binding.cache.then((resolved) => this._deactivate(binding, resolved));
460
- }
461
- return this._deactivate(binding, binding.cache);
462
- }
463
- _deactivateSingletons(bindings) {
464
- for (const binding of bindings) {
465
- const result = this._deactivateIfSingleton(binding);
466
- if ((0, async_1.isPromise)(result)) {
467
- throw new Error(ERROR_MSGS.ASYNC_UNBIND_REQUIRED);
468
- }
469
- }
470
- }
471
- _deactivateSingletonsAsync(bindings) {
472
- return __awaiter(this, void 0, void 0, function* () {
473
- yield Promise.all(bindings.map(b => this._deactivateIfSingleton(b)));
474
- });
475
- }
476
- _propagateContainerDeactivationThenBindingAndPreDestroy(binding, instance, constructor) {
477
- if (this.parent) {
478
- return this._deactivate.bind(this.parent)(binding, instance);
479
- }
480
- else {
481
- return this._bindingDeactivationAndPreDestroy(binding, instance, constructor);
482
- }
483
- }
484
- _propagateContainerDeactivationThenBindingAndPreDestroyAsync(binding, instance, constructor) {
485
- return __awaiter(this, void 0, void 0, function* () {
486
- if (this.parent) {
487
- yield this._deactivate.bind(this.parent)(binding, instance);
488
- }
489
- else {
490
- yield this._bindingDeactivationAndPreDestroyAsync(binding, instance, constructor);
491
- }
492
- });
493
- }
494
- _removeServiceFromDictionary(serviceIdentifier) {
495
- try {
496
- this._bindingDictionary.remove(serviceIdentifier);
497
- }
498
- catch (e) {
499
- throw new Error(`${ERROR_MSGS.CANNOT_UNBIND} ${(0, serialization_1.getServiceIdentifierAsString)(serviceIdentifier)}`);
500
- }
501
- }
502
- _bindingDeactivationAndPreDestroy(binding, instance, constructor) {
503
- if (typeof binding.onDeactivation === 'function') {
504
- const result = binding.onDeactivation(instance);
505
- if ((0, async_1.isPromise)(result)) {
506
- return result.then(() => this._preDestroy(constructor, instance));
507
- }
508
- }
509
- return this._preDestroy(constructor, instance);
510
- }
511
- _bindingDeactivationAndPreDestroyAsync(binding, instance, constructor) {
512
- return __awaiter(this, void 0, void 0, function* () {
513
- if (typeof binding.onDeactivation === 'function') {
514
- yield binding.onDeactivation(instance);
515
- }
516
- yield this._preDestroy(constructor, instance);
517
- });
518
- }
519
- }
520
- exports.Container = Container;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Container = void 0;
36
+ const binding_1 = require("../bindings/binding");
37
+ const ERROR_MSGS = __importStar(require("../constants/error_msgs"));
38
+ const literal_types_1 = require("../constants/literal_types");
39
+ const METADATA_KEY = __importStar(require("../constants/metadata_keys"));
40
+ const metadata_reader_1 = require("../planning/metadata_reader");
41
+ const planner_1 = require("../planning/planner");
42
+ const resolver_1 = require("../resolution/resolver");
43
+ const binding_to_syntax_1 = require("../syntax/binding_to_syntax");
44
+ const async_1 = require("../utils/async");
45
+ const id_1 = require("../utils/id");
46
+ const serialization_1 = require("../utils/serialization");
47
+ const container_snapshot_1 = require("./container_snapshot");
48
+ const lookup_1 = require("./lookup");
49
+ const module_activation_store_1 = require("./module_activation_store");
50
+ class Container {
51
+ static merge(container1, container2, ...containers) {
52
+ const container = new Container();
53
+ const targetContainers = [container1, container2, ...containers]
54
+ .map((targetContainer) => (0, planner_1.getBindingDictionary)(targetContainer));
55
+ const bindingDictionary = (0, planner_1.getBindingDictionary)(container);
56
+ function copyDictionary(origin, destination) {
57
+ origin.traverse((_key, value) => {
58
+ value.forEach((binding) => {
59
+ destination.add(binding.serviceIdentifier, binding.clone());
60
+ });
61
+ });
62
+ }
63
+ targetContainers.forEach((targetBindingDictionary) => {
64
+ copyDictionary(targetBindingDictionary, bindingDictionary);
65
+ });
66
+ return container;
67
+ }
68
+ constructor(containerOptions) {
69
+ const options = containerOptions || {};
70
+ if (typeof options !== 'object') {
71
+ throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_MUST_BE_AN_OBJECT}`);
72
+ }
73
+ if (options.defaultScope === undefined) {
74
+ options.defaultScope = literal_types_1.BindingScopeEnum.Transient;
75
+ }
76
+ else if (options.defaultScope !== literal_types_1.BindingScopeEnum.Singleton &&
77
+ options.defaultScope !== literal_types_1.BindingScopeEnum.Transient &&
78
+ options.defaultScope !== literal_types_1.BindingScopeEnum.Request) {
79
+ throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_DEFAULT_SCOPE}`);
80
+ }
81
+ if (options.autoBindInjectable === undefined) {
82
+ options.autoBindInjectable = false;
83
+ }
84
+ else if (typeof options.autoBindInjectable !== 'boolean') {
85
+ throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_AUTO_BIND_INJECTABLE}`);
86
+ }
87
+ if (options.skipBaseClassChecks === undefined) {
88
+ options.skipBaseClassChecks = false;
89
+ }
90
+ else if (typeof options.skipBaseClassChecks !== 'boolean') {
91
+ throw new Error(`${ERROR_MSGS.CONTAINER_OPTIONS_INVALID_SKIP_BASE_CHECK}`);
92
+ }
93
+ this.options = {
94
+ autoBindInjectable: options.autoBindInjectable,
95
+ defaultScope: options.defaultScope,
96
+ skipBaseClassChecks: options.skipBaseClassChecks
97
+ };
98
+ this.id = (0, id_1.id)();
99
+ this._bindingDictionary = new lookup_1.Lookup();
100
+ this._snapshots = [];
101
+ this._middleware = null;
102
+ this._activations = new lookup_1.Lookup();
103
+ this._deactivations = new lookup_1.Lookup();
104
+ this.parent = null;
105
+ this._metadataReader = new metadata_reader_1.MetadataReader();
106
+ this._moduleActivationStore = new module_activation_store_1.ModuleActivationStore();
107
+ }
108
+ load(...modules) {
109
+ const getHelpers = this._getContainerModuleHelpersFactory();
110
+ for (const currentModule of modules) {
111
+ const containerModuleHelpers = getHelpers(currentModule.id);
112
+ currentModule.registry(containerModuleHelpers.bindFunction, containerModuleHelpers.unbindFunction, containerModuleHelpers.isboundFunction, containerModuleHelpers.rebindFunction, containerModuleHelpers.unbindAsyncFunction, containerModuleHelpers.onActivationFunction, containerModuleHelpers.onDeactivationFunction);
113
+ }
114
+ }
115
+ loadAsync(...modules) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const getHelpers = this._getContainerModuleHelpersFactory();
118
+ for (const currentModule of modules) {
119
+ const containerModuleHelpers = getHelpers(currentModule.id);
120
+ yield currentModule.registry(containerModuleHelpers.bindFunction, containerModuleHelpers.unbindFunction, containerModuleHelpers.isboundFunction, containerModuleHelpers.rebindFunction, containerModuleHelpers.unbindAsyncFunction, containerModuleHelpers.onActivationFunction, containerModuleHelpers.onDeactivationFunction);
121
+ }
122
+ });
123
+ }
124
+ unload(...modules) {
125
+ modules.forEach((module) => {
126
+ const deactivations = this._removeModuleBindings(module.id);
127
+ this._deactivateSingletons(deactivations);
128
+ this._removeModuleHandlers(module.id);
129
+ });
130
+ }
131
+ unloadAsync(...modules) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ for (const module of modules) {
134
+ const deactivations = this._removeModuleBindings(module.id);
135
+ yield this._deactivateSingletonsAsync(deactivations);
136
+ this._removeModuleHandlers(module.id);
137
+ }
138
+ });
139
+ }
140
+ bind(serviceIdentifier) {
141
+ const scope = this.options.defaultScope || literal_types_1.BindingScopeEnum.Transient;
142
+ const binding = new binding_1.Binding(serviceIdentifier, scope);
143
+ this._bindingDictionary.add(serviceIdentifier, binding);
144
+ return new binding_to_syntax_1.BindingToSyntax(binding);
145
+ }
146
+ rebind(serviceIdentifier) {
147
+ this.unbind(serviceIdentifier);
148
+ return this.bind(serviceIdentifier);
149
+ }
150
+ rebindAsync(serviceIdentifier) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ yield this.unbindAsync(serviceIdentifier);
153
+ return this.bind(serviceIdentifier);
154
+ });
155
+ }
156
+ unbind(serviceIdentifier) {
157
+ if (this._bindingDictionary.hasKey(serviceIdentifier)) {
158
+ const bindings = this._bindingDictionary.get(serviceIdentifier);
159
+ this._deactivateSingletons(bindings);
160
+ }
161
+ this._removeServiceFromDictionary(serviceIdentifier);
162
+ }
163
+ unbindAsync(serviceIdentifier) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ if (this._bindingDictionary.hasKey(serviceIdentifier)) {
166
+ const bindings = this._bindingDictionary.get(serviceIdentifier);
167
+ yield this._deactivateSingletonsAsync(bindings);
168
+ }
169
+ this._removeServiceFromDictionary(serviceIdentifier);
170
+ });
171
+ }
172
+ unbindAll() {
173
+ this._bindingDictionary.traverse((_key, value) => {
174
+ this._deactivateSingletons(value);
175
+ });
176
+ this._bindingDictionary = new lookup_1.Lookup();
177
+ }
178
+ unbindAllAsync() {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ const promises = [];
181
+ this._bindingDictionary.traverse((_key, value) => {
182
+ promises.push(this._deactivateSingletonsAsync(value));
183
+ });
184
+ yield Promise.all(promises);
185
+ this._bindingDictionary = new lookup_1.Lookup();
186
+ });
187
+ }
188
+ onActivation(serviceIdentifier, onActivation) {
189
+ this._activations.add(serviceIdentifier, onActivation);
190
+ }
191
+ onDeactivation(serviceIdentifier, onDeactivation) {
192
+ this._deactivations.add(serviceIdentifier, onDeactivation);
193
+ }
194
+ isBound(serviceIdentifier) {
195
+ let bound = this._bindingDictionary.hasKey(serviceIdentifier);
196
+ if (!bound && this.parent) {
197
+ bound = this.parent.isBound(serviceIdentifier);
198
+ }
199
+ return bound;
200
+ }
201
+ isCurrentBound(serviceIdentifier) {
202
+ return this._bindingDictionary.hasKey(serviceIdentifier);
203
+ }
204
+ isBoundNamed(serviceIdentifier, named) {
205
+ return this.isBoundTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
206
+ }
207
+ isBoundTagged(serviceIdentifier, key, value) {
208
+ let bound = false;
209
+ if (this._bindingDictionary.hasKey(serviceIdentifier)) {
210
+ const bindings = this._bindingDictionary.get(serviceIdentifier);
211
+ const request = (0, planner_1.createMockRequest)(this, serviceIdentifier, key, value);
212
+ bound = bindings.some((b) => b.constraint(request));
213
+ }
214
+ if (!bound && this.parent) {
215
+ bound = this.parent.isBoundTagged(serviceIdentifier, key, value);
216
+ }
217
+ return bound;
218
+ }
219
+ snapshot() {
220
+ this._snapshots.push(container_snapshot_1.ContainerSnapshot.of(this._bindingDictionary.clone(), this._middleware, this._activations.clone(), this._deactivations.clone(), this._moduleActivationStore.clone()));
221
+ }
222
+ restore() {
223
+ const snapshot = this._snapshots.pop();
224
+ if (snapshot === undefined) {
225
+ throw new Error(ERROR_MSGS.NO_MORE_SNAPSHOTS_AVAILABLE);
226
+ }
227
+ this._bindingDictionary = snapshot.bindings;
228
+ this._activations = snapshot.activations;
229
+ this._deactivations = snapshot.deactivations;
230
+ this._middleware = snapshot.middleware;
231
+ this._moduleActivationStore = snapshot.moduleActivationStore;
232
+ }
233
+ createChild(containerOptions) {
234
+ const child = new Container(containerOptions || this.options);
235
+ child.parent = this;
236
+ return child;
237
+ }
238
+ applyMiddleware(...middlewares) {
239
+ const initial = (this._middleware) ? this._middleware : this._planAndResolve();
240
+ this._middleware = middlewares.reduce((prev, curr) => curr(prev), initial);
241
+ }
242
+ applyCustomMetadataReader(metadataReader) {
243
+ this._metadataReader = metadataReader;
244
+ }
245
+ get(serviceIdentifier) {
246
+ const getArgs = this._getNotAllArgs(serviceIdentifier, false);
247
+ return this._getButThrowIfAsync(getArgs);
248
+ }
249
+ getAsync(serviceIdentifier) {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ const getArgs = this._getNotAllArgs(serviceIdentifier, false);
252
+ return this._get(getArgs);
253
+ });
254
+ }
255
+ getTagged(serviceIdentifier, key, value) {
256
+ const getArgs = this._getNotAllArgs(serviceIdentifier, false, key, value);
257
+ return this._getButThrowIfAsync(getArgs);
258
+ }
259
+ getTaggedAsync(serviceIdentifier, key, value) {
260
+ return __awaiter(this, void 0, void 0, function* () {
261
+ const getArgs = this._getNotAllArgs(serviceIdentifier, false, key, value);
262
+ return this._get(getArgs);
263
+ });
264
+ }
265
+ getNamed(serviceIdentifier, named) {
266
+ return this.getTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
267
+ }
268
+ getNamedAsync(serviceIdentifier, named) {
269
+ return this.getTaggedAsync(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
270
+ }
271
+ getAll(serviceIdentifier) {
272
+ const getArgs = this._getAllArgs(serviceIdentifier);
273
+ return this._getButThrowIfAsync(getArgs);
274
+ }
275
+ getAllAsync(serviceIdentifier) {
276
+ const getArgs = this._getAllArgs(serviceIdentifier);
277
+ return this._getAll(getArgs);
278
+ }
279
+ getAllTagged(serviceIdentifier, key, value) {
280
+ const getArgs = this._getNotAllArgs(serviceIdentifier, true, key, value);
281
+ return this._getButThrowIfAsync(getArgs);
282
+ }
283
+ getAllTaggedAsync(serviceIdentifier, key, value) {
284
+ const getArgs = this._getNotAllArgs(serviceIdentifier, true, key, value);
285
+ return this._getAll(getArgs);
286
+ }
287
+ getAllNamed(serviceIdentifier, named) {
288
+ return this.getAllTagged(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
289
+ }
290
+ getAllNamedAsync(serviceIdentifier, named) {
291
+ return this.getAllTaggedAsync(serviceIdentifier, METADATA_KEY.NAMED_TAG, named);
292
+ }
293
+ resolve(constructorFunction) {
294
+ const isBound = this.isBound(constructorFunction);
295
+ if (!isBound) {
296
+ this.bind(constructorFunction).toSelf();
297
+ }
298
+ const resolved = this.get(constructorFunction);
299
+ if (!isBound) {
300
+ this.unbind(constructorFunction);
301
+ }
302
+ return resolved;
303
+ }
304
+ _preDestroy(constructor, instance) {
305
+ var _a, _b;
306
+ if (Reflect.hasMetadata(METADATA_KEY.PRE_DESTROY, constructor)) {
307
+ const data = Reflect.getMetadata(METADATA_KEY.PRE_DESTROY, constructor);
308
+ return (_b = (_a = instance)[data.value]) === null || _b === void 0 ? void 0 : _b.call(_a);
309
+ }
310
+ }
311
+ _removeModuleHandlers(moduleId) {
312
+ const moduleActivationsHandlers = this._moduleActivationStore.remove(moduleId);
313
+ this._activations.removeIntersection(moduleActivationsHandlers.onActivations);
314
+ this._deactivations.removeIntersection(moduleActivationsHandlers.onDeactivations);
315
+ }
316
+ _removeModuleBindings(moduleId) {
317
+ return this._bindingDictionary.removeByCondition(binding => binding.moduleId === moduleId);
318
+ }
319
+ _deactivate(binding, instance) {
320
+ const constructor = Object.getPrototypeOf(instance).constructor;
321
+ try {
322
+ if (this._deactivations.hasKey(binding.serviceIdentifier)) {
323
+ const result = this._deactivateContainer(instance, this._deactivations.get(binding.serviceIdentifier).values());
324
+ if ((0, async_1.isPromise)(result)) {
325
+ return this._handleDeactivationError(result.then(() => this._propagateContainerDeactivationThenBindingAndPreDestroyAsync(binding, instance, constructor)), constructor);
326
+ }
327
+ }
328
+ const propagateDeactivationResult = this._propagateContainerDeactivationThenBindingAndPreDestroy(binding, instance, constructor);
329
+ if ((0, async_1.isPromise)(propagateDeactivationResult)) {
330
+ return this._handleDeactivationError(propagateDeactivationResult, constructor);
331
+ }
332
+ }
333
+ catch (ex) {
334
+ if (ex instanceof Error) {
335
+ throw new Error(ERROR_MSGS.ON_DEACTIVATION_ERROR(constructor.name, ex.message));
336
+ }
337
+ }
338
+ }
339
+ _handleDeactivationError(asyncResult, constructor) {
340
+ return __awaiter(this, void 0, void 0, function* () {
341
+ try {
342
+ yield asyncResult;
343
+ }
344
+ catch (ex) {
345
+ if (ex instanceof Error) {
346
+ throw new Error(ERROR_MSGS.ON_DEACTIVATION_ERROR(constructor.name, ex.message));
347
+ }
348
+ }
349
+ });
350
+ }
351
+ _deactivateContainer(instance, deactivationsIterator) {
352
+ let deactivation = deactivationsIterator.next();
353
+ while (deactivation.value) {
354
+ const result = deactivation.value(instance);
355
+ if ((0, async_1.isPromise)(result)) {
356
+ return result.then(() => this._deactivateContainerAsync(instance, deactivationsIterator));
357
+ }
358
+ deactivation = deactivationsIterator.next();
359
+ }
360
+ }
361
+ _deactivateContainerAsync(instance, deactivationsIterator) {
362
+ return __awaiter(this, void 0, void 0, function* () {
363
+ let deactivation = deactivationsIterator.next();
364
+ while (deactivation.value) {
365
+ yield deactivation.value(instance);
366
+ deactivation = deactivationsIterator.next();
367
+ }
368
+ });
369
+ }
370
+ _getContainerModuleHelpersFactory() {
371
+ const setModuleId = (bindingToSyntax, moduleId) => {
372
+ bindingToSyntax._binding.moduleId = moduleId;
373
+ };
374
+ const getBindFunction = (moduleId) => (serviceIdentifier) => {
375
+ const bindingToSyntax = this.bind(serviceIdentifier);
376
+ setModuleId(bindingToSyntax, moduleId);
377
+ return bindingToSyntax;
378
+ };
379
+ const getUnbindFunction = () => (serviceIdentifier) => {
380
+ return this.unbind(serviceIdentifier);
381
+ };
382
+ const getUnbindAsyncFunction = () => (serviceIdentifier) => {
383
+ return this.unbindAsync(serviceIdentifier);
384
+ };
385
+ const getIsboundFunction = () => (serviceIdentifier) => {
386
+ return this.isBound(serviceIdentifier);
387
+ };
388
+ const getRebindFunction = (moduleId) => (serviceIdentifier) => {
389
+ const bindingToSyntax = this.rebind(serviceIdentifier);
390
+ setModuleId(bindingToSyntax, moduleId);
391
+ return bindingToSyntax;
392
+ };
393
+ const getOnActivationFunction = (moduleId) => (serviceIdentifier, onActivation) => {
394
+ this._moduleActivationStore.addActivation(moduleId, serviceIdentifier, onActivation);
395
+ this.onActivation(serviceIdentifier, onActivation);
396
+ };
397
+ const getOnDeactivationFunction = (moduleId) => (serviceIdentifier, onDeactivation) => {
398
+ this._moduleActivationStore.addDeactivation(moduleId, serviceIdentifier, onDeactivation);
399
+ this.onDeactivation(serviceIdentifier, onDeactivation);
400
+ };
401
+ return (mId) => ({
402
+ bindFunction: getBindFunction(mId),
403
+ isboundFunction: getIsboundFunction(),
404
+ onActivationFunction: getOnActivationFunction(mId),
405
+ onDeactivationFunction: getOnDeactivationFunction(mId),
406
+ rebindFunction: getRebindFunction(mId),
407
+ unbindFunction: getUnbindFunction(),
408
+ unbindAsyncFunction: getUnbindAsyncFunction()
409
+ });
410
+ }
411
+ _getAll(getArgs) {
412
+ return Promise.all(this._get(getArgs));
413
+ }
414
+ _get(getArgs) {
415
+ const planAndResolveArgs = Object.assign(Object.assign({}, getArgs), { contextInterceptor: (context) => context, targetType: literal_types_1.TargetTypeEnum.Variable });
416
+ if (this._middleware) {
417
+ const middlewareResult = this._middleware(planAndResolveArgs);
418
+ if (middlewareResult === undefined || middlewareResult === null) {
419
+ throw new Error(ERROR_MSGS.INVALID_MIDDLEWARE_RETURN);
420
+ }
421
+ return middlewareResult;
422
+ }
423
+ return this._planAndResolve()(planAndResolveArgs);
424
+ }
425
+ _getButThrowIfAsync(getArgs) {
426
+ const result = this._get(getArgs);
427
+ if ((0, async_1.isPromiseOrContainsPromise)(result)) {
428
+ throw new Error(ERROR_MSGS.LAZY_IN_SYNC(getArgs.serviceIdentifier));
429
+ }
430
+ return result;
431
+ }
432
+ _getAllArgs(serviceIdentifier) {
433
+ const getAllArgs = {
434
+ avoidConstraints: true,
435
+ isMultiInject: true,
436
+ serviceIdentifier,
437
+ };
438
+ return getAllArgs;
439
+ }
440
+ _getNotAllArgs(serviceIdentifier, isMultiInject, key, value) {
441
+ const getNotAllArgs = {
442
+ avoidConstraints: false,
443
+ isMultiInject,
444
+ serviceIdentifier,
445
+ key,
446
+ value,
447
+ };
448
+ return getNotAllArgs;
449
+ }
450
+ _planAndResolve() {
451
+ return (args) => {
452
+ let context = (0, planner_1.plan)(this._metadataReader, this, args.isMultiInject, args.targetType, args.serviceIdentifier, args.key, args.value, args.avoidConstraints);
453
+ context = args.contextInterceptor(context);
454
+ const result = (0, resolver_1.resolve)(context);
455
+ return result;
456
+ };
457
+ }
458
+ _deactivateIfSingleton(binding) {
459
+ if (!binding.activated) {
460
+ return;
461
+ }
462
+ if ((0, async_1.isPromise)(binding.cache)) {
463
+ return binding.cache.then((resolved) => this._deactivate(binding, resolved));
464
+ }
465
+ return this._deactivate(binding, binding.cache);
466
+ }
467
+ _deactivateSingletons(bindings) {
468
+ for (const binding of bindings) {
469
+ const result = this._deactivateIfSingleton(binding);
470
+ if ((0, async_1.isPromise)(result)) {
471
+ throw new Error(ERROR_MSGS.ASYNC_UNBIND_REQUIRED);
472
+ }
473
+ }
474
+ }
475
+ _deactivateSingletonsAsync(bindings) {
476
+ return __awaiter(this, void 0, void 0, function* () {
477
+ yield Promise.all(bindings.map(b => this._deactivateIfSingleton(b)));
478
+ });
479
+ }
480
+ _propagateContainerDeactivationThenBindingAndPreDestroy(binding, instance, constructor) {
481
+ if (this.parent) {
482
+ return this._deactivate.bind(this.parent)(binding, instance);
483
+ }
484
+ else {
485
+ return this._bindingDeactivationAndPreDestroy(binding, instance, constructor);
486
+ }
487
+ }
488
+ _propagateContainerDeactivationThenBindingAndPreDestroyAsync(binding, instance, constructor) {
489
+ return __awaiter(this, void 0, void 0, function* () {
490
+ if (this.parent) {
491
+ yield this._deactivate.bind(this.parent)(binding, instance);
492
+ }
493
+ else {
494
+ yield this._bindingDeactivationAndPreDestroyAsync(binding, instance, constructor);
495
+ }
496
+ });
497
+ }
498
+ _removeServiceFromDictionary(serviceIdentifier) {
499
+ try {
500
+ this._bindingDictionary.remove(serviceIdentifier);
501
+ }
502
+ catch (e) {
503
+ throw new Error(`${ERROR_MSGS.CANNOT_UNBIND} ${(0, serialization_1.getServiceIdentifierAsString)(serviceIdentifier)}`);
504
+ }
505
+ }
506
+ _bindingDeactivationAndPreDestroy(binding, instance, constructor) {
507
+ if (typeof binding.onDeactivation === 'function') {
508
+ const result = binding.onDeactivation(instance);
509
+ if ((0, async_1.isPromise)(result)) {
510
+ return result.then(() => this._preDestroy(constructor, instance));
511
+ }
512
+ }
513
+ return this._preDestroy(constructor, instance);
514
+ }
515
+ _bindingDeactivationAndPreDestroyAsync(binding, instance, constructor) {
516
+ return __awaiter(this, void 0, void 0, function* () {
517
+ if (typeof binding.onDeactivation === 'function') {
518
+ yield binding.onDeactivation(instance);
519
+ }
520
+ yield this._preDestroy(constructor, instance);
521
+ });
522
+ }
523
+ }
524
+ exports.Container = Container;