@noego/ioc 0.0.5 → 0.0.7
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.
- package/dist/cjs/framework/decorators/Component.d.ts +10 -0
- package/dist/cjs/framework/decorators/Component.js +25 -0
- package/dist/cjs/framework/decorators/Component.js.map +1 -0
- package/dist/cjs/framework/decorators/Inject.d.ts +7 -0
- package/dist/cjs/framework/decorators/Inject.js +22 -0
- package/dist/cjs/framework/decorators/Inject.js.map +1 -0
- package/dist/cjs/framework/decorators/index.d.ts +3 -0
- package/dist/cjs/framework/decorators/index.js +24 -0
- package/dist/cjs/framework/decorators/index.js.map +1 -0
- package/dist/cjs/framework/decorators/metadataKeys.d.ts +3 -0
- package/dist/cjs/framework/decorators/metadataKeys.js +8 -0
- package/dist/cjs/framework/decorators/metadataKeys.js.map +1 -0
- package/dist/cjs/framework/errors/ContainerErrors.d.ts +33 -0
- package/dist/cjs/framework/errors/ContainerErrors.js +52 -0
- package/dist/cjs/framework/errors/ContainerErrors.js.map +1 -0
- package/dist/cjs/framework/implementation/Container.d.ts +31 -0
- package/dist/cjs/framework/implementation/Container.js +448 -0
- package/dist/cjs/framework/implementation/Container.js.map +1 -0
- package/dist/cjs/framework/implementation/Container.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/Container.test.js +184 -0
- package/dist/cjs/framework/implementation/Container.test.js.map +1 -0
- package/dist/cjs/framework/implementation/ContainerAsync.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/ContainerAsync.test.js +178 -0
- package/dist/cjs/framework/implementation/ContainerAsync.test.js.map +1 -0
- package/dist/cjs/framework/implementation/ContainerBasic.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/ContainerBasic.test.js +101 -0
- package/dist/cjs/framework/implementation/ContainerBasic.test.js.map +1 -0
- package/dist/cjs/framework/implementation/ContainerDecorators.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/ContainerDecorators.test.js +517 -0
- package/dist/cjs/framework/implementation/ContainerDecorators.test.js.map +1 -0
- package/dist/cjs/framework/implementation/ContainerExtension.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/ContainerExtension.test.js +169 -0
- package/dist/cjs/framework/implementation/ContainerExtension.test.js.map +1 -0
- package/dist/cjs/framework/implementation/ContainerLifetimes.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/ContainerLifetimes.test.js +154 -0
- package/dist/cjs/framework/implementation/ContainerLifetimes.test.js.map +1 -0
- package/dist/cjs/framework/implementation/DecoratorSupport.d.ts +31 -0
- package/dist/cjs/framework/implementation/DecoratorSupport.js +96 -0
- package/dist/cjs/framework/implementation/DecoratorSupport.js.map +1 -0
- package/dist/cjs/framework/implementation/LoadAs.d.ts +5 -0
- package/dist/cjs/framework/implementation/LoadAs.js +10 -0
- package/dist/cjs/framework/implementation/LoadAs.js.map +1 -0
- package/dist/cjs/framework/implementation/Parameter.d.ts +33 -0
- package/dist/cjs/framework/implementation/Parameter.js +87 -0
- package/dist/cjs/framework/implementation/Parameter.js.map +1 -0
- package/dist/cjs/framework/implementation/Parameter.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/Parameter.test.js +19 -0
- package/dist/cjs/framework/implementation/Parameter.test.js.map +1 -0
- package/dist/cjs/framework/implementation/SimpleDecoratorTest.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/SimpleDecoratorTest.test.js +78 -0
- package/dist/cjs/framework/implementation/SimpleDecoratorTest.test.js.map +1 -0
- package/dist/cjs/framework/implementation/SingletonTest.test.d.ts +1 -0
- package/dist/cjs/framework/implementation/SingletonTest.test.js +85 -0
- package/dist/cjs/framework/implementation/SingletonTest.test.js.map +1 -0
- package/dist/cjs/framework/interface/Container.d.ts +22 -0
- package/dist/cjs/framework/interface/Container.js +3 -0
- package/dist/cjs/framework/interface/Container.js.map +1 -0
- package/dist/esm/framework/decorators/Component.d.ts +10 -0
- package/dist/esm/framework/decorators/Component.js +21 -0
- package/dist/esm/framework/decorators/Component.js.map +1 -0
- package/dist/esm/framework/decorators/Inject.d.ts +7 -0
- package/dist/esm/framework/decorators/Inject.js +19 -0
- package/dist/esm/framework/decorators/Inject.js.map +1 -0
- package/dist/esm/framework/decorators/index.d.ts +3 -0
- package/dist/esm/framework/decorators/index.js +5 -0
- package/dist/esm/framework/decorators/index.js.map +1 -0
- package/dist/esm/framework/decorators/metadataKeys.d.ts +3 -0
- package/dist/esm/framework/decorators/metadataKeys.js +5 -0
- package/dist/esm/framework/decorators/metadataKeys.js.map +1 -0
- package/dist/esm/framework/errors/ContainerErrors.d.ts +33 -0
- package/dist/esm/framework/errors/ContainerErrors.js +44 -0
- package/dist/esm/framework/errors/ContainerErrors.js.map +1 -0
- package/dist/esm/framework/implementation/Container.d.ts +31 -0
- package/dist/esm/framework/implementation/Container.js +432 -0
- package/dist/esm/framework/implementation/Container.js.map +1 -0
- package/dist/esm/framework/implementation/Container.test.d.ts +1 -0
- package/dist/esm/framework/implementation/Container.test.js +173 -0
- package/dist/esm/framework/implementation/Container.test.js.map +1 -0
- package/dist/esm/framework/implementation/ContainerAsync.test.d.ts +1 -0
- package/dist/esm/framework/implementation/ContainerAsync.test.js +151 -0
- package/dist/esm/framework/implementation/ContainerAsync.test.js.map +1 -0
- package/dist/esm/framework/implementation/ContainerBasic.test.d.ts +1 -0
- package/dist/esm/framework/implementation/ContainerBasic.test.js +90 -0
- package/dist/esm/framework/implementation/ContainerBasic.test.js.map +1 -0
- package/dist/esm/framework/implementation/ContainerDecorators.test.d.ts +1 -0
- package/dist/esm/framework/implementation/ContainerDecorators.test.js +498 -0
- package/dist/esm/framework/implementation/ContainerDecorators.test.js.map +1 -0
- package/dist/esm/framework/implementation/ContainerExtension.test.d.ts +1 -0
- package/dist/esm/framework/implementation/ContainerExtension.test.js +158 -0
- package/dist/esm/framework/implementation/ContainerExtension.test.js.map +1 -0
- package/dist/esm/framework/implementation/ContainerLifetimes.test.d.ts +1 -0
- package/dist/esm/framework/implementation/ContainerLifetimes.test.js +143 -0
- package/dist/esm/framework/implementation/ContainerLifetimes.test.js.map +1 -0
- package/dist/esm/framework/implementation/DecoratorSupport.d.ts +31 -0
- package/dist/esm/framework/implementation/DecoratorSupport.js +87 -0
- package/dist/esm/framework/implementation/DecoratorSupport.js.map +1 -0
- package/dist/esm/framework/implementation/LoadAs.d.ts +5 -0
- package/dist/esm/framework/implementation/LoadAs.js +7 -0
- package/dist/esm/framework/implementation/LoadAs.js.map +1 -0
- package/dist/esm/framework/implementation/Parameter.d.ts +33 -0
- package/dist/esm/framework/implementation/Parameter.js +49 -0
- package/dist/esm/framework/implementation/Parameter.js.map +1 -0
- package/dist/esm/framework/implementation/Parameter.test.d.ts +1 -0
- package/dist/esm/framework/implementation/Parameter.test.js +17 -0
- package/dist/esm/framework/implementation/Parameter.test.js.map +1 -0
- package/dist/esm/framework/implementation/SimpleDecoratorTest.test.d.ts +1 -0
- package/dist/esm/framework/implementation/SimpleDecoratorTest.test.js +67 -0
- package/dist/esm/framework/implementation/SimpleDecoratorTest.test.js.map +1 -0
- package/dist/esm/framework/implementation/SingletonTest.test.d.ts +1 -0
- package/dist/esm/framework/implementation/SingletonTest.test.js +74 -0
- package/dist/esm/framework/implementation/SingletonTest.test.js.map +1 -0
- package/dist/esm/framework/interface/Container.d.ts +22 -0
- package/dist/esm/framework/interface/Container.js +2 -0
- package/dist/esm/framework/interface/Container.js.map +1 -0
- package/package.json +11 -1
- package/readme.md +9 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import 'reflect-metadata'; // Ensure polyfill is loaded conceptually
|
|
2
|
+
import { LoadAs } from "./LoadAs";
|
|
3
|
+
import { Parameter } from "./Parameter";
|
|
4
|
+
import { NotAClassDefinitionError, ParameterNotFoundError, DependencyNotFoundError, InstanceNotCreatedError, CircularDependencyError } from "../errors/ContainerErrors";
|
|
5
|
+
import { getScope, getComponentOptions, // Use helper
|
|
6
|
+
getResolvedDependencies // Use helper
|
|
7
|
+
} from './DecoratorSupport'; // Assume helpers exist or create them
|
|
8
|
+
var ContainerRouting;
|
|
9
|
+
(function (ContainerRouting) {
|
|
10
|
+
ContainerRouting["FUNCTIONAL"] = "FUNCTIONAL";
|
|
11
|
+
ContainerRouting["CLASS"] = "CLASS";
|
|
12
|
+
// No need for COMPONENT route, just check metadata if CLASS/FUNCTIONAL fails
|
|
13
|
+
})(ContainerRouting || (ContainerRouting = {}));
|
|
14
|
+
export class Container {
|
|
15
|
+
constructor() {
|
|
16
|
+
// Routing and definition maps
|
|
17
|
+
this._routing = new Map();
|
|
18
|
+
this._functionDefinition = new Map();
|
|
19
|
+
this._classDefinition = new Map();
|
|
20
|
+
// Storage maps
|
|
21
|
+
this._functionStorageSingleton = new Map();
|
|
22
|
+
this._functionStorageScoped = new Map();
|
|
23
|
+
this._classStorageSingleton = new Map();
|
|
24
|
+
this._classStorageScoped = new Map();
|
|
25
|
+
}
|
|
26
|
+
// Registration methods
|
|
27
|
+
registerClass(classDefinition, options) {
|
|
28
|
+
// Basic validation
|
|
29
|
+
if (typeof classDefinition !== 'function' || !classDefinition.prototype) {
|
|
30
|
+
console.warn(`[IOC] Attempted to register non-class: ${String(classDefinition)}`);
|
|
31
|
+
// Optional: throw error instead?
|
|
32
|
+
// throw new NotAClassDefinitionError(`Attempted to register non-class: ${String(classDefinition)}`);
|
|
33
|
+
return; // Don't proceed if not a class constructor
|
|
34
|
+
}
|
|
35
|
+
// Always clear previous definitions for this exact class constructor
|
|
36
|
+
this.clearDefinitions(classDefinition);
|
|
37
|
+
// Always set the routing type so 'isClassDefinition' works
|
|
38
|
+
this._routing.set(classDefinition, ContainerRouting.CLASS);
|
|
39
|
+
// Store manual options *only if they are actually provided and have content*
|
|
40
|
+
if (options && Object.keys(options).length > 0) {
|
|
41
|
+
this._classDefinition.set(classDefinition, options);
|
|
42
|
+
// console.log(`[IOC RegClass] Manually registered ${classDefinition.name} with options:`, options);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// console.log(`[IOC RegClass] Manually registered ${classDefinition.name} with no specific options (will use decorators if present).`);
|
|
46
|
+
// If no options are provided, we don't store anything in _classDefinition,
|
|
47
|
+
// effectively clearing any previous manual config and letting decorators take over if present.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
registerFunction(label, func, options = {}) {
|
|
51
|
+
this.clearDefinitions(label);
|
|
52
|
+
this._routing.set(label, ContainerRouting.FUNCTIONAL);
|
|
53
|
+
this._functionDefinition.set(label, { function: func, options });
|
|
54
|
+
// console.log(`[IOC RegFunc] Registered function with label ${String(label)} and options:`, options);
|
|
55
|
+
}
|
|
56
|
+
// Instance resolution methods - Simplify to just call loadInstance
|
|
57
|
+
instance(classDefinition, params = []) {
|
|
58
|
+
// Basic check upfront
|
|
59
|
+
if (typeof classDefinition !== 'function' || !classDefinition.prototype) {
|
|
60
|
+
throw new NotAClassDefinitionError(`Cannot resolve instance for non-class: ${String(classDefinition)}`);
|
|
61
|
+
}
|
|
62
|
+
return this.loadInstance(classDefinition, params);
|
|
63
|
+
}
|
|
64
|
+
get(label, param = []) {
|
|
65
|
+
// Just delegate to loadInstance, which handles routing and auto-registration
|
|
66
|
+
return this.loadInstance(label, param);
|
|
67
|
+
}
|
|
68
|
+
extend() {
|
|
69
|
+
const childContainer = createContainer();
|
|
70
|
+
// Copy routing and definition maps (share the registration knowledge)
|
|
71
|
+
childContainer._routing = new Map(this._routing);
|
|
72
|
+
childContainer._functionDefinition = new Map(this._functionDefinition);
|
|
73
|
+
childContainer._classDefinition = new Map(this._classDefinition);
|
|
74
|
+
// Share singleton storage (singletons are shared across all containers)
|
|
75
|
+
childContainer._functionStorageSingleton = this._functionStorageSingleton;
|
|
76
|
+
childContainer._classStorageSingleton = this._classStorageSingleton;
|
|
77
|
+
// Do NOT share scoped storage (new container = new scope)
|
|
78
|
+
// Child gets its own empty scoped storage
|
|
79
|
+
// console.log('[IOC Extend] Created child container.');
|
|
80
|
+
return childContainer;
|
|
81
|
+
}
|
|
82
|
+
// Private helper methods
|
|
83
|
+
clearDefinitions(label) {
|
|
84
|
+
this._routing.delete(label);
|
|
85
|
+
this._functionDefinition.delete(label);
|
|
86
|
+
// Only delete from classDefinition if the label is actually a function constructor
|
|
87
|
+
if (typeof label === 'function') {
|
|
88
|
+
this._classDefinition.delete(label);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Checks if the label is *currently* registered as a class
|
|
92
|
+
isRegisteredAsClass(label) {
|
|
93
|
+
return this._routing.get(label) === ContainerRouting.CLASS;
|
|
94
|
+
}
|
|
95
|
+
// Checks if the label is *currently* registered as a function
|
|
96
|
+
isRegisteredAsFunction(label) {
|
|
97
|
+
return this._routing.get(label) === ContainerRouting.FUNCTIONAL;
|
|
98
|
+
}
|
|
99
|
+
hasInstance(label) {
|
|
100
|
+
const storageType = this.getStorageType(label); // This checks decorators if needed
|
|
101
|
+
if (!storageType) {
|
|
102
|
+
// Transient instances are never stored
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
// Determine if it's treated as a class (registered or decorated) *after* getting type
|
|
106
|
+
const isClass = this.isClassType(label); // Use a helper that checks routing OR decorator
|
|
107
|
+
if (isClass) {
|
|
108
|
+
return storageType === 'singleton'
|
|
109
|
+
? this._classStorageSingleton.has(label)
|
|
110
|
+
: this._classStorageScoped.has(label);
|
|
111
|
+
}
|
|
112
|
+
else { // Must be a function if not a class
|
|
113
|
+
return storageType === 'singleton'
|
|
114
|
+
? this._functionStorageSingleton.has(label)
|
|
115
|
+
: this._functionStorageScoped.has(label);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Helper to determine if something should be treated as a class (registered or decorated)
|
|
119
|
+
isClassType(label) {
|
|
120
|
+
if (this.isRegisteredAsClass(label)) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
// If not explicitly registered as a class, check if it's a function with @Component
|
|
124
|
+
if (typeof label === 'function' && label.prototype && !this.isRegisteredAsFunction(label)) {
|
|
125
|
+
return getComponentOptions(label) !== null;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
getInstance(label) {
|
|
130
|
+
const storageType = this.getStorageType(label); // Checks decorators if needed
|
|
131
|
+
if (!storageType) {
|
|
132
|
+
// This should ideally not be reached if called after hasInstance check
|
|
133
|
+
console.error(`[IOC GetInstance] Attempted to get instance for non-stored type: ${String(label)}`);
|
|
134
|
+
throw new InstanceNotCreatedError();
|
|
135
|
+
}
|
|
136
|
+
const isClass = this.isClassType(label); // Check routing OR decorator
|
|
137
|
+
if (isClass) {
|
|
138
|
+
return storageType === 'singleton'
|
|
139
|
+
? this._classStorageSingleton.get(label)
|
|
140
|
+
: this._classStorageScoped.get(label);
|
|
141
|
+
}
|
|
142
|
+
else { // Must be a function
|
|
143
|
+
return storageType === 'singleton'
|
|
144
|
+
? this._functionStorageSingleton.get(label)
|
|
145
|
+
: this._functionStorageScoped.get(label);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
setInstance(label, instance, storageType) {
|
|
149
|
+
const isClass = this.isClassType(label); // Determine type based on routing OR decorator
|
|
150
|
+
if (isClass) {
|
|
151
|
+
if (storageType === 'singleton') {
|
|
152
|
+
this._classStorageSingleton.set(label, instance);
|
|
153
|
+
}
|
|
154
|
+
else if (storageType == 'scoped') { // storageType === 'scoped'
|
|
155
|
+
this._classStorageScoped.set(label, instance);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else { // Must be a function
|
|
159
|
+
if (storageType === 'singleton') {
|
|
160
|
+
this._functionStorageSingleton.set(label, instance);
|
|
161
|
+
}
|
|
162
|
+
else if (storageType === "scoped") { // storageType === 'scoped'
|
|
163
|
+
this._functionStorageScoped.set(label, instance);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
getStorageType(label) {
|
|
168
|
+
let manualLoadAs;
|
|
169
|
+
let decoratorScope;
|
|
170
|
+
const isPotentialClass = typeof label === 'function' && label.prototype;
|
|
171
|
+
// Get manual registration options if available
|
|
172
|
+
if (this.isRegisteredAsClass(label)) { // Use helper
|
|
173
|
+
manualLoadAs = this._classDefinition.get(label)?.loadAs;
|
|
174
|
+
}
|
|
175
|
+
else if (this.isRegisteredAsFunction(label)) { // Use helper
|
|
176
|
+
manualLoadAs = this._functionDefinition.get(label)?.options?.loadAs;
|
|
177
|
+
}
|
|
178
|
+
// Get decorator scope only if it's potentially a class *and not explicitly registered as a function*
|
|
179
|
+
if (isPotentialClass && !this.isRegisteredAsFunction(label)) {
|
|
180
|
+
decoratorScope = getScope(label); // Uses getComponentOptions internally
|
|
181
|
+
}
|
|
182
|
+
// Determine final scope: Manual > Decorator > Default (Transient)
|
|
183
|
+
const finalScope = manualLoadAs ?? decoratorScope ?? LoadAs.Transient;
|
|
184
|
+
if (finalScope === LoadAs.Singleton) {
|
|
185
|
+
return 'singleton';
|
|
186
|
+
}
|
|
187
|
+
else if (finalScope === LoadAs.Scoped) {
|
|
188
|
+
return 'scoped';
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
return 'transient'; // Transient
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Central resolution logic
|
|
195
|
+
async loadInstance(label, params = [], resolutionChain = [] // Track resolution path
|
|
196
|
+
) {
|
|
197
|
+
// --- Circular Dependency Check ---
|
|
198
|
+
const currentLabelStr = (typeof label === 'function' && label.name) ? label.name : String(label);
|
|
199
|
+
if (resolutionChain.includes(currentLabelStr)) {
|
|
200
|
+
console.error(`[IOC LoadInstance] Circular dependency detected: ${[...resolutionChain, currentLabelStr].join(' -> ')}`);
|
|
201
|
+
throw new CircularDependencyError([...resolutionChain, currentLabelStr]);
|
|
202
|
+
}
|
|
203
|
+
const nextResolutionChain = [...resolutionChain, currentLabelStr];
|
|
204
|
+
// --- End Circular Dependency Check ---
|
|
205
|
+
// --- Handle Parameters ---
|
|
206
|
+
if (label instanceof Parameter) {
|
|
207
|
+
try {
|
|
208
|
+
// Pass a *copy* of params to avoid side effects in resolveParameterValue
|
|
209
|
+
return Promise.resolve(this.resolveParameterValue(label, [...params]));
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
// console.error(`[IOC LoadInstance] Error resolving parameter ${param.toString()}:`, e);
|
|
213
|
+
return Promise.reject(e);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
// --- End Handle Parameters ---
|
|
217
|
+
// --- Check Explicit Registrations ---
|
|
218
|
+
if (this.isRegisteredAsClass(label)) {
|
|
219
|
+
// console.log(`[IOC LoadInstance] Resolving explicitly registered class: ${currentLabelStr}`);
|
|
220
|
+
return this.loadClassInstance(label, params, nextResolutionChain);
|
|
221
|
+
}
|
|
222
|
+
if (this.isRegisteredAsFunction(label)) {
|
|
223
|
+
// console.log(`[IOC LoadInstance] Resolving explicitly registered function: ${currentLabelStr}`);
|
|
224
|
+
return this.loadFunctionInstance(label, params, nextResolutionChain);
|
|
225
|
+
}
|
|
226
|
+
// --- End Check Explicit Registrations ---
|
|
227
|
+
// --- Attempt Auto-Registration/Resolution for Decorated Classes ---
|
|
228
|
+
if (typeof label === 'function' && label.prototype) {
|
|
229
|
+
const componentOptions = getComponentOptions(label); // Check for @Component
|
|
230
|
+
if (componentOptions !== null) {
|
|
231
|
+
// console.log(`[IOC LoadInstance] Auto-resolving @Component class: ${currentLabelStr}`);
|
|
232
|
+
// Set routing *now* so subsequent internal checks work correctly
|
|
233
|
+
if (!this._routing.has(label)) { // Only set if not already set
|
|
234
|
+
this._routing.set(label, ContainerRouting.CLASS);
|
|
235
|
+
// console.log(`[IOC LoadInstance] Set routing for auto-resolved class: ${currentLabelStr}`);
|
|
236
|
+
}
|
|
237
|
+
// Proceed to load it as a class
|
|
238
|
+
return this.loadClassInstance(label, params, nextResolutionChain);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// --- End Auto-Registration ---
|
|
242
|
+
// --- Dependency Not Found ---
|
|
243
|
+
console.error(`[IOC LoadInstance] Dependency not found and cannot be resolved: ${currentLabelStr}`);
|
|
244
|
+
throw new DependencyNotFoundError(currentLabelStr);
|
|
245
|
+
}
|
|
246
|
+
resolveParameterValue(param, params) {
|
|
247
|
+
// Find the *last* provided value for the parameter
|
|
248
|
+
const valueContainer = [...params].reverse().find((p) => p.belongsTo(param));
|
|
249
|
+
if (!valueContainer) {
|
|
250
|
+
// console.error(`[IOC ResolveParam] Parameter not found: ${param.toString()}`);
|
|
251
|
+
throw new ParameterNotFoundError(param.toString());
|
|
252
|
+
}
|
|
253
|
+
// console.log(`[IOC ResolveParam] Resolved parameter ${param.toString()}`);
|
|
254
|
+
return valueContainer.value;
|
|
255
|
+
}
|
|
256
|
+
loadClassInstance(classDefinition, // Should be a class constructor here
|
|
257
|
+
params, resolutionChain) {
|
|
258
|
+
const storageType = this.getStorageType(classDefinition);
|
|
259
|
+
const className = classDefinition.name || '[Anonymous Class]';
|
|
260
|
+
// Check storage first (handle stored promises)
|
|
261
|
+
if (storageType && this.hasInstance(classDefinition)) {
|
|
262
|
+
const instance = this.getInstance(classDefinition);
|
|
263
|
+
// console.log(`[IOC LoadClass] Found existing ${storageType} instance for ${className}. IsPromise: ${isPromise(instance)}`);
|
|
264
|
+
return instance;
|
|
265
|
+
}
|
|
266
|
+
// console.log(`[IOC LoadClass] Creating new ${storageType || 'transient'} instance for ${className}`);
|
|
267
|
+
// --- Determine Dependencies ---
|
|
268
|
+
const manualOptions = this._classDefinition.get(classDefinition);
|
|
269
|
+
let dependenciesToResolve;
|
|
270
|
+
if (manualOptions?.param && Array.isArray(manualOptions.param)) {
|
|
271
|
+
dependenciesToResolve = manualOptions.param;
|
|
272
|
+
// console.log(`[IOC LoadClass] Using manual dependencies for ${className}`);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
// Use decorator-derived dependencies
|
|
276
|
+
dependenciesToResolve = getResolvedDependencies(classDefinition);
|
|
277
|
+
// console.log(`[IOC LoadClass] Using decorator/reflected dependencies for ${className}:`, dependenciesToResolve.map(d => typeof d === 'function' ? d.name : String(d)));
|
|
278
|
+
}
|
|
279
|
+
// --- End Determine Dependencies ---
|
|
280
|
+
// --- Resolve Dependencies ---
|
|
281
|
+
// Create a *copy* of params to pass down for dependency resolution
|
|
282
|
+
const paramsCopy = [...params];
|
|
283
|
+
const resolutionPromises = dependenciesToResolve.map(dependencyIdentifier => this.loadInstance(dependencyIdentifier, paramsCopy, resolutionChain) // Recursive call handles auto-registration
|
|
284
|
+
);
|
|
285
|
+
let finalInstance = Promise.all(resolutionPromises)
|
|
286
|
+
.catch((error) => {
|
|
287
|
+
console.error(`[IOC LoadClass] Failed to resolve one or more dependencies for ${className}:`, error);
|
|
288
|
+
// Clean up potential partially stored promise if this was going to be singleton/scoped
|
|
289
|
+
if (storageType && this.isRegisteredAsClass(classDefinition)) { // Check if it was actually *being* stored
|
|
290
|
+
if (storageType === 'singleton')
|
|
291
|
+
this._classStorageSingleton.delete(classDefinition);
|
|
292
|
+
if (storageType === 'scoped')
|
|
293
|
+
this._classStorageScoped.delete(classDefinition);
|
|
294
|
+
}
|
|
295
|
+
throw error; // Re-throw dependency resolution error
|
|
296
|
+
}).then(async (resolvedDependencies) => {
|
|
297
|
+
// --- Instantiate Class ---
|
|
298
|
+
// Store the promise *before* starting instantiation for concurrent requests
|
|
299
|
+
let instancePromise;
|
|
300
|
+
if (storageType && !this.hasInstance(classDefinition)) { // Check again in case of race condition?
|
|
301
|
+
// Create the promise wrapper immediately
|
|
302
|
+
instancePromise = (async () => {
|
|
303
|
+
try {
|
|
304
|
+
// console.log(`[IOC LoadClass] Instantiating ${className} with ${resolvedDependencies.length} args.`);
|
|
305
|
+
const instance = new classDefinition(...resolvedDependencies);
|
|
306
|
+
// Overwrite the stored promise with the actual instance *after* it's created
|
|
307
|
+
this.setInstance(classDefinition, instance, storageType); // Use the correct storageType
|
|
308
|
+
// console.log(`[IOC LoadClass] Stored actual ${storageType} instance for ${className}`);
|
|
309
|
+
return instance;
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
console.error(`[IOC LoadClass] Error during instantiation of ${className}: ${err.message}`, err.stack);
|
|
313
|
+
// Clean up the stored promise on failure
|
|
314
|
+
if (storageType === 'singleton')
|
|
315
|
+
this._classStorageSingleton.delete(classDefinition);
|
|
316
|
+
if (storageType === 'scoped')
|
|
317
|
+
this._classStorageScoped.delete(classDefinition);
|
|
318
|
+
throw err; // Re-throw instantiation error
|
|
319
|
+
}
|
|
320
|
+
})();
|
|
321
|
+
// console.log(`[IOC LoadClass] Storing promise for concurrent requests (${storageType}) for ${className}`);
|
|
322
|
+
this.setInstance(classDefinition, instancePromise, storageType);
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
// If already has instance (from storage check) or is transient, just instantiate directly
|
|
326
|
+
instancePromise = (async () => {
|
|
327
|
+
try {
|
|
328
|
+
// console.log(`[IOC LoadClass] Instantiating transient ${className} with ${resolvedDependencies.length} args.`);
|
|
329
|
+
const instance = new classDefinition(...resolvedDependencies);
|
|
330
|
+
// No storing for transient
|
|
331
|
+
return instance;
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
console.error(`[IOC LoadClass] Error during instantiation of transient ${className}: ${err.message}`, err.stack);
|
|
335
|
+
throw err; // Re-throw instantiation error
|
|
336
|
+
}
|
|
337
|
+
})();
|
|
338
|
+
}
|
|
339
|
+
// --- End Instantiate Class ---
|
|
340
|
+
// Await and return the final instance (either newly created or from the stored promise)
|
|
341
|
+
return await instancePromise;
|
|
342
|
+
});
|
|
343
|
+
if (storageType && storageType !== 'transient') {
|
|
344
|
+
this.setInstance(classDefinition, finalInstance, storageType); // Store the promise
|
|
345
|
+
}
|
|
346
|
+
return finalInstance;
|
|
347
|
+
}
|
|
348
|
+
loadFunctionInstance(funcLabel, params, resolutionChain) {
|
|
349
|
+
const functionConfig = this._functionDefinition.get(funcLabel); // Assumed to exist by caller
|
|
350
|
+
const storageType = this.getStorageType(funcLabel); // Only uses manual options for functions
|
|
351
|
+
const labelStr = String(funcLabel);
|
|
352
|
+
// Check storage first
|
|
353
|
+
if (storageType && this.hasInstance(funcLabel)) {
|
|
354
|
+
const instance = this.getInstance(funcLabel);
|
|
355
|
+
// console.log(`[IOC LoadFunc] Found existing ${storageType} instance for ${labelStr}. IsPromise: ${isPromise(instance)}`);
|
|
356
|
+
return instance;
|
|
357
|
+
}
|
|
358
|
+
// console.log(`[IOC LoadFunc] Creating new ${storageType || 'transient'} instance for ${labelStr}`);
|
|
359
|
+
const ioc_func = functionConfig.function;
|
|
360
|
+
const options = functionConfig.options;
|
|
361
|
+
const dependenciesToResolve = options?.param || [];
|
|
362
|
+
// --- Resolve Dependencies ---
|
|
363
|
+
const paramsCopy = [...params]; // Create a copy
|
|
364
|
+
const resolutionPromises = dependenciesToResolve.map(dependencyIdentifier => this.loadInstance(dependencyIdentifier, paramsCopy, resolutionChain));
|
|
365
|
+
let finalInstance = Promise.all(resolutionPromises).catch((error) => {
|
|
366
|
+
console.error(`[IOC LoadFunc] Failed to resolve dependencies for function ${labelStr}:`, error);
|
|
367
|
+
// Clean up potential partial storage
|
|
368
|
+
if (storageType && this.isRegisteredAsFunction(funcLabel)) {
|
|
369
|
+
if (storageType === 'singleton')
|
|
370
|
+
this._functionStorageSingleton.delete(funcLabel);
|
|
371
|
+
if (storageType === 'scoped')
|
|
372
|
+
this._functionStorageScoped.delete(funcLabel);
|
|
373
|
+
}
|
|
374
|
+
throw error;
|
|
375
|
+
}).then(async (resolvedArgs) => {
|
|
376
|
+
let instancePromise;
|
|
377
|
+
if (storageType && !this.hasInstance(funcLabel)) {
|
|
378
|
+
instancePromise = (async () => {
|
|
379
|
+
try {
|
|
380
|
+
// console.log(`[IOC LoadFunc] Invoking ${labelStr} with ${resolvedArgs.length} args.`);
|
|
381
|
+
const result = await ioc_func.apply(null, resolvedArgs); // Await in case function is async
|
|
382
|
+
// Overwrite stored promise with actual result
|
|
383
|
+
this.setInstance(funcLabel, result, storageType);
|
|
384
|
+
// console.log(`[IOC LoadFunc] Stored actual ${storageType} instance for ${labelStr}`);
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
console.error(`[IOC LoadFunc] Error executing function ${labelStr}: ${err.message}`, err.stack);
|
|
389
|
+
// Clean up stored promise on failure
|
|
390
|
+
if (storageType === 'singleton')
|
|
391
|
+
this._functionStorageSingleton.delete(funcLabel);
|
|
392
|
+
if (storageType === 'scoped')
|
|
393
|
+
this._functionStorageScoped.delete(funcLabel);
|
|
394
|
+
throw err;
|
|
395
|
+
}
|
|
396
|
+
})();
|
|
397
|
+
// console.log(`[IOC LoadFunc] Storing promise for concurrent requests (${storageType}) for ${labelStr}`);
|
|
398
|
+
this.setInstance(funcLabel, instancePromise, storageType);
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
// Transient or already resolved from storage check
|
|
402
|
+
instancePromise = (async () => {
|
|
403
|
+
try {
|
|
404
|
+
// console.log(`[IOC LoadFunc] Invoking transient ${labelStr} with ${resolvedArgs.length} args.`);
|
|
405
|
+
const result = await ioc_func.apply(null, resolvedArgs); // Await in case function is async
|
|
406
|
+
// No storing for transient
|
|
407
|
+
return result;
|
|
408
|
+
}
|
|
409
|
+
catch (err) {
|
|
410
|
+
console.error(`[IOC LoadFunc] Error executing transient function ${labelStr}: ${err.message}`, err.stack);
|
|
411
|
+
throw err;
|
|
412
|
+
}
|
|
413
|
+
})();
|
|
414
|
+
}
|
|
415
|
+
// --- End Invoke Function ---
|
|
416
|
+
return await instancePromise; // Return the final (awaited) result
|
|
417
|
+
});
|
|
418
|
+
if (storageType && storageType !== 'transient') {
|
|
419
|
+
this.setInstance(funcLabel, finalInstance, storageType); // Store the promise
|
|
420
|
+
}
|
|
421
|
+
return finalInstance;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// Helper functions
|
|
425
|
+
export function isPromise(obj) {
|
|
426
|
+
// Simplified check - more robust check might be needed depending on environment
|
|
427
|
+
return !!obj && typeof obj.then === 'function';
|
|
428
|
+
}
|
|
429
|
+
export function createContainer() {
|
|
430
|
+
return new Container();
|
|
431
|
+
}
|
|
432
|
+
//# sourceMappingURL=Container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Container.js","sourceRoot":"","sources":["../../../../framework/implementation/Container.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC,CAAC,yCAAyC;AAEpE,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,SAAS,EAAkB,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACxK,OAAO,EACH,QAAQ,EACR,mBAAmB,EAAE,aAAa;AAClC,uBAAuB,CAAC,aAAa;EACxC,MAAM,oBAAoB,CAAC,CAAC,sCAAsC;AAEnE,IAAK,gBAIJ;AAJD,WAAK,gBAAgB;IACjB,6CAAyB,CAAA;IACzB,mCAAe,CAAA;IACf,6EAA6E;AACjF,CAAC,EAJI,gBAAgB,KAAhB,gBAAgB,QAIpB;AAID,MAAM,OAAO,SAAS;IAAtB;QACI,8BAA8B;QACtB,aAAQ,GAA+B,IAAI,GAAG,EAAE,CAAC;QACjD,wBAAmB,GAA2C,IAAI,GAAG,EAAE,CAAC;QACxE,qBAAgB,GAA6C,IAAI,GAAG,EAAE,CAAC;QAE/E,eAAe;QACP,8BAAyB,GAAkB,IAAI,GAAG,EAAE,CAAC;QACrD,2BAAsB,GAAkB,IAAI,GAAG,EAAE,CAAC;QAClD,2BAAsB,GAAuB,IAAI,GAAG,EAAE,CAAC;QACvD,wBAAmB,GAAuB,IAAI,GAAG,EAAE,CAAC;IA6bhE,CAAC;IA3bG,uBAAuB;IACvB,aAAa,CAAI,eAA0C,EAAE,OAAmC;QAC5F,mBAAmB;QACnB,IAAI,OAAO,eAAe,KAAK,UAAU,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,CAAC;YACrE,OAAO,CAAC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wBACF,kGAAkG;wBACjG,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,kCAAkC;wBAC3F,2BAA2B;wBAC3B,OAAO,MAAM,CAAC;oBAClB,CAAC;oBAAC,OAAO,GAAQ,EAAE,CAAC;wBAChB,OAAO,CAAC,KAAK,CAAC,qDAAqD,QAAQ,KAAK,GAAG,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;wBAC1G,MAAM,GAAG,CAAC;oBACd,CAAC;gBACN,CAAC,CAAC,EAAE,CAAC;YACV,CAAC;YACD,8BAA8B;YAE9B,OAAO,MAAM,eAAe,CAAC,CAAC,oCAAoC;QAClE,CAAC,CAAC,CAAC;QAEH,IAAG,WAAW,IAAI,WAAW,KAAK,WAAW,EAAC,CAAC;YAC3C,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC,CAAC,oBAAoB;QACjF,CAAC;QAED,OAAO,aAAa,CAAC;IACzB,CAAC;CACJ;AAED,mBAAmB;AACnB,MAAM,UAAU,SAAS,CAAC,GAAQ;IAC9B,gFAAgF;IAChF,OAAO,CAAC,CAAC,GAAG,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,UAAU,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,eAAe;IAC3B,OAAO,IAAI,SAAS,EAAE,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { describe } from "node:test";
|
|
2
|
+
import { createContainer } from "./Container";
|
|
3
|
+
import { Parameter } from "./Parameter";
|
|
4
|
+
import { LoadAs } from "./LoadAs";
|
|
5
|
+
describe('Container', () => {
|
|
6
|
+
describe('Transient', () => {
|
|
7
|
+
describe('registerClass', () => {
|
|
8
|
+
const container = createContainer();
|
|
9
|
+
class Test {
|
|
10
|
+
}
|
|
11
|
+
class Test2 {
|
|
12
|
+
constructor(test) {
|
|
13
|
+
this.test = test;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
beforeAll(() => {
|
|
17
|
+
container.registerClass(Test);
|
|
18
|
+
container.registerClass(Test2, { param: [Test] });
|
|
19
|
+
});
|
|
20
|
+
it('should be able to register a class', async () => {
|
|
21
|
+
const instance = await container.instance(Test);
|
|
22
|
+
expect(instance).toBeInstanceOf(Test);
|
|
23
|
+
});
|
|
24
|
+
it('should be able to register a class with dependencies', async () => {
|
|
25
|
+
const instance = await container.instance(Test2);
|
|
26
|
+
expect(instance).toBeInstanceOf(Test2);
|
|
27
|
+
expect(instance.test).toBeInstanceOf(Test);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe('registerFunction', () => {
|
|
31
|
+
const container = createContainer();
|
|
32
|
+
function loadTest() {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
function loadTest2() {
|
|
36
|
+
return { "test2": true };
|
|
37
|
+
}
|
|
38
|
+
beforeAll(() => {
|
|
39
|
+
container.registerFunction('test', loadTest);
|
|
40
|
+
container.registerFunction('test2', loadTest2);
|
|
41
|
+
});
|
|
42
|
+
it('should be able to register a function', async () => {
|
|
43
|
+
const instance = await container.get('test');
|
|
44
|
+
expect(instance).toEqual({});
|
|
45
|
+
});
|
|
46
|
+
it('should load different instances', async () => {
|
|
47
|
+
const instance = await container.get('test');
|
|
48
|
+
const instance2 = await container.get('test');
|
|
49
|
+
expect(instance).not.toBe(instance2);
|
|
50
|
+
});
|
|
51
|
+
it('should be able to register a function', async () => {
|
|
52
|
+
const instance = await container.get('test2');
|
|
53
|
+
expect(instance).toEqual({ "test2": true });
|
|
54
|
+
});
|
|
55
|
+
it('should load different instances', async () => {
|
|
56
|
+
const instance = await container.get('test2');
|
|
57
|
+
const instance2 = await container.get('test2');
|
|
58
|
+
expect(instance).not.toBe(instance2);
|
|
59
|
+
});
|
|
60
|
+
it('should check that test and test2 are not equal', async () => {
|
|
61
|
+
const instance1 = await container.get('test');
|
|
62
|
+
const instance2 = await container.get('test2');
|
|
63
|
+
expect(instance1).toBeTruthy();
|
|
64
|
+
expect(instance2).toBeTruthy();
|
|
65
|
+
expect(instance1).not.toBe(instance2);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
describe('instantiate a class object using parameters', async () => {
|
|
69
|
+
const container = createContainer();
|
|
70
|
+
class User {
|
|
71
|
+
constructor(id) {
|
|
72
|
+
this.id = id;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const USER_ID = Parameter.create();
|
|
76
|
+
beforeAll(() => {
|
|
77
|
+
container.registerClass(User, { param: [USER_ID] });
|
|
78
|
+
});
|
|
79
|
+
it("should be able to instantiate an object using parameters", async () => {
|
|
80
|
+
const user = await container.instance(User, [USER_ID.value(1)]);
|
|
81
|
+
expect(user).toBeInstanceOf(User);
|
|
82
|
+
expect(user.id).toBe(1);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe('instantiate a function using parameters', async () => {
|
|
86
|
+
const container = createContainer();
|
|
87
|
+
function loadUser(id) {
|
|
88
|
+
return { id };
|
|
89
|
+
}
|
|
90
|
+
const USER_ID = Parameter.create();
|
|
91
|
+
beforeAll(() => {
|
|
92
|
+
container.registerFunction('user', loadUser, { param: [USER_ID] });
|
|
93
|
+
});
|
|
94
|
+
it("should be able to instantiate an object using parameters", async () => {
|
|
95
|
+
const user = await container.get('user', [USER_ID.value(1)]);
|
|
96
|
+
expect(user).toEqual({ id: 1 });
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('a class should be able to be overridden with a function setup', () => {
|
|
100
|
+
const container = createContainer();
|
|
101
|
+
class Test {
|
|
102
|
+
}
|
|
103
|
+
beforeAll(() => {
|
|
104
|
+
container.registerClass(Test);
|
|
105
|
+
container.registerFunction(Test, () => ({}));
|
|
106
|
+
});
|
|
107
|
+
it('should be able to override a class with a function and get as instance', async () => {
|
|
108
|
+
const instance = await container.instance(Test);
|
|
109
|
+
expect(instance).toEqual({});
|
|
110
|
+
});
|
|
111
|
+
it('should be able to override a class with a function', async () => {
|
|
112
|
+
const instance = await container.get(Test);
|
|
113
|
+
expect(instance).toEqual({});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
describe('Singleton', () => {
|
|
118
|
+
describe('registerClass', () => {
|
|
119
|
+
const container = createContainer();
|
|
120
|
+
class Test {
|
|
121
|
+
}
|
|
122
|
+
beforeAll(() => {
|
|
123
|
+
container.registerClass(Test, { loadAs: LoadAs.Singleton });
|
|
124
|
+
});
|
|
125
|
+
it('should be able to register a class', async () => {
|
|
126
|
+
const instance = await container.instance(Test);
|
|
127
|
+
expect(instance).toBeInstanceOf(Test);
|
|
128
|
+
const instance2 = await container.instance(Test);
|
|
129
|
+
expect(instance).toBe(instance2);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
describe("registerNestedClass", () => {
|
|
133
|
+
it('should be able to register a nested class', async () => {
|
|
134
|
+
const container = createContainer();
|
|
135
|
+
class Parent {
|
|
136
|
+
constructor(test) {
|
|
137
|
+
this.test = test;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
class Child {
|
|
141
|
+
constructor() {
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
container.registerClass(Parent, { param: [Child] });
|
|
145
|
+
container.registerClass(Child, { loadAs: LoadAs.Singleton });
|
|
146
|
+
const parent1 = await container.instance(Parent);
|
|
147
|
+
const parent2 = await container.instance(Parent);
|
|
148
|
+
expect(parent1).not.toBe(parent2);
|
|
149
|
+
const equal = parent1.test === parent2.test;
|
|
150
|
+
expect(equal).toBeTruthy();
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
describe('Scoped', () => {
|
|
155
|
+
describe('registerClass', () => {
|
|
156
|
+
const container = createContainer();
|
|
157
|
+
class Test {
|
|
158
|
+
}
|
|
159
|
+
beforeAll(() => {
|
|
160
|
+
container.registerClass(Test, { loadAs: LoadAs.Scoped });
|
|
161
|
+
});
|
|
162
|
+
it('should be able extend a container and all scoped instances are created new', async () => {
|
|
163
|
+
const instance = await container.instance(Test);
|
|
164
|
+
const newContainer = container.extend();
|
|
165
|
+
const instance2 = await newContainer.instance(Test);
|
|
166
|
+
const instance3 = await container.instance(Test);
|
|
167
|
+
expect(instance).not.toBe(instance2);
|
|
168
|
+
expect(instance).toBe(instance3);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
//# sourceMappingURL=Container.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Container.test.js","sourceRoot":"","sources":["../../../../framework/implementation/Container.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IAEvB,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;QAEvB,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;YAG3B,MAAM,SAAS,GAAG,eAAe,EAAE,CAAA;YAEnC,MAAM,IAAI;aAET;YAED,MAAM,KAAK;gBACP,YAAmB,IAAS;oBAAT,SAAI,GAAJ,IAAI,CAAK;gBAC5B,CAAC;aACJ;YAED,SAAS,CAAC,GAAG,EAAE;gBACX,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;gBAC7B,SAAS,CAAC,aAAa,CAAC,KAAK,EAAC,EAAC,KAAK,EAAC,CAAC,IAAI,CAAC,EAAC,CAAC,CAAA;YACjD,CAAC,CAAC,CAAA;YAEF,EAAE,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;gBAChD,MAAM,Q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