@tachybase/di 1.6.3-alpha.3 → 1.6.8-alpha.1
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/lib/container-instance.class.js +12 -1
- package/lib/error/cannot-inject-value.error.js +1 -1
- package/package.json +11 -2
- package/src/__tests__/container-registry.test.ts +1 -1
- package/src/__tests__/lifecycle.test.ts +9 -4
- package/src/container-instance.class.ts +19 -1
- package/src/error/cannot-inject-value.error.ts +1 -1
|
@@ -166,6 +166,17 @@ const _ContainerInstance = class _ContainerInstance {
|
|
|
166
166
|
if (Array.isArray(identifierOrIdentifierArray)) {
|
|
167
167
|
identifierOrIdentifierArray.forEach((id) => this.remove(id));
|
|
168
168
|
} else {
|
|
169
|
+
const multiGroup = this.multiServiceIds.get(identifierOrIdentifierArray);
|
|
170
|
+
if (multiGroup) {
|
|
171
|
+
multiGroup.tokens.forEach((maskedToken) => {
|
|
172
|
+
const serviceMetadata2 = this.metadataMap.get(maskedToken);
|
|
173
|
+
if (serviceMetadata2) {
|
|
174
|
+
this.disposeServiceInstance(serviceMetadata2);
|
|
175
|
+
this.metadataMap.delete(maskedToken);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this.multiServiceIds.delete(identifierOrIdentifierArray);
|
|
179
|
+
}
|
|
169
180
|
const serviceMetadata = this.metadataMap.get(identifierOrIdentifierArray);
|
|
170
181
|
if (serviceMetadata) {
|
|
171
182
|
this.disposeServiceInstance(serviceMetadata);
|
|
@@ -323,7 +334,7 @@ const _ContainerInstance = class _ContainerInstance {
|
|
|
323
334
|
this.throwIfDisposed();
|
|
324
335
|
const shouldResetValue = force || !!serviceMetadata.type || !!serviceMetadata.factory;
|
|
325
336
|
if (shouldResetValue) {
|
|
326
|
-
if (
|
|
337
|
+
if ((serviceMetadata == null ? void 0 : serviceMetadata.value) != null && typeof serviceMetadata.value["dispose"] === "function") {
|
|
327
338
|
try {
|
|
328
339
|
serviceMetadata.value.dispose();
|
|
329
340
|
} catch (error) {
|
|
@@ -29,7 +29,7 @@ const _CannotInjectValueError = class _CannotInjectValueError extends Error {
|
|
|
29
29
|
this.name = "CannotInjectValueError";
|
|
30
30
|
}
|
|
31
31
|
get message() {
|
|
32
|
-
return `Cannot inject value into "${this.target.
|
|
32
|
+
return `Cannot inject value into "${this.target.name}.${this.propertyName}". Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value.`;
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
__name(_CannotInjectValueError, "CannotInjectValueError");
|
package/package.json
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tachybase/di",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.8-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
|
+
"homepage": "https://github.com/tegojs/tego#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/tegojs/tego/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/tegojs/tego",
|
|
12
|
+
"directory": "packages/di"
|
|
13
|
+
},
|
|
5
14
|
"license": "Apache-2.0",
|
|
6
15
|
"main": "./lib/index.js",
|
|
7
16
|
"types": "./lib/index.d.ts",
|
|
8
17
|
"dependencies": {
|
|
9
|
-
"@tachybase/utils": "1.6.
|
|
18
|
+
"@tachybase/utils": "1.6.8-alpha.1"
|
|
10
19
|
}
|
|
11
20
|
}
|
|
@@ -96,7 +96,7 @@ describe('ContainerRegistry', () => {
|
|
|
96
96
|
container1 = new ContainerInstance('remove-test');
|
|
97
97
|
await ContainerRegistry.removeContainer(container1);
|
|
98
98
|
|
|
99
|
-
expect(async () => {
|
|
99
|
+
await expect(async () => {
|
|
100
100
|
await ContainerRegistry.removeContainer(container1);
|
|
101
101
|
}).rejects.toThrow('No container is registered with the given ID.');
|
|
102
102
|
});
|
|
@@ -142,11 +142,12 @@ describe('Service Lifecycle and Cleanup', () => {
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
145
|
+
// Singleton services are stored in the default Container
|
|
146
|
+
Container.set({ type: TestService, scope: 'singleton' });
|
|
147
|
+
const instance1 = Container.get(TestService);
|
|
148
|
+
const instance2 = Container.get(TestService);
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
Container.remove(TestService);
|
|
150
151
|
|
|
151
152
|
expect(disposeCount).toBe(1);
|
|
152
153
|
});
|
|
@@ -202,6 +203,7 @@ describe('Service Lifecycle and Cleanup', () => {
|
|
|
202
203
|
container.set({ id: 'multi-service', type: TestService, multiple: true });
|
|
203
204
|
container.set({ id: 'multi-service', type: TestService, multiple: true });
|
|
204
205
|
|
|
206
|
+
// Must get the services to create instances
|
|
205
207
|
const services = container.getMany('multi-service');
|
|
206
208
|
|
|
207
209
|
container.remove('multi-service');
|
|
@@ -228,6 +230,9 @@ describe('Service Lifecycle and Cleanup', () => {
|
|
|
228
230
|
container.set({ id: 'multi-service', type: Service1, multiple: true });
|
|
229
231
|
container.set({ id: 'multi-service', type: Service2, multiple: true });
|
|
230
232
|
|
|
233
|
+
// Must get the services to create instances
|
|
234
|
+
const services = container.getMany('multi-service');
|
|
235
|
+
|
|
231
236
|
container.remove('multi-service');
|
|
232
237
|
|
|
233
238
|
expect(service1Disposed).toBe(true);
|
|
@@ -236,6 +236,21 @@ export class ContainerInstance {
|
|
|
236
236
|
if (Array.isArray(identifierOrIdentifierArray)) {
|
|
237
237
|
identifierOrIdentifierArray.forEach((id) => this.remove(id));
|
|
238
238
|
} else {
|
|
239
|
+
// Handle multiple services (registered with multiple: true)
|
|
240
|
+
const multiGroup = this.multiServiceIds.get(identifierOrIdentifierArray);
|
|
241
|
+
if (multiGroup) {
|
|
242
|
+
// Remove all masked services
|
|
243
|
+
multiGroup.tokens.forEach((maskedToken) => {
|
|
244
|
+
const serviceMetadata = this.metadataMap.get(maskedToken);
|
|
245
|
+
if (serviceMetadata) {
|
|
246
|
+
this.disposeServiceInstance(serviceMetadata);
|
|
247
|
+
this.metadataMap.delete(maskedToken);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
this.multiServiceIds.delete(identifierOrIdentifierArray);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Handle single service
|
|
239
254
|
const serviceMetadata = this.metadataMap.get(identifierOrIdentifierArray);
|
|
240
255
|
|
|
241
256
|
if (serviceMetadata) {
|
|
@@ -471,7 +486,10 @@ export class ContainerInstance {
|
|
|
471
486
|
|
|
472
487
|
if (shouldResetValue) {
|
|
473
488
|
/** If we wound a function named destroy we call it without any params. */
|
|
474
|
-
if (
|
|
489
|
+
if (
|
|
490
|
+
serviceMetadata?.value != null &&
|
|
491
|
+
typeof (serviceMetadata.value as Record<string, unknown>)['dispose'] === 'function'
|
|
492
|
+
) {
|
|
475
493
|
try {
|
|
476
494
|
(serviceMetadata.value as { dispose: CallableFunction }).dispose();
|
|
477
495
|
} catch (error) {
|
|
@@ -8,7 +8,7 @@ export class CannotInjectValueError extends Error {
|
|
|
8
8
|
|
|
9
9
|
get message(): string {
|
|
10
10
|
return (
|
|
11
|
-
`Cannot inject value into "${this.target.
|
|
11
|
+
`Cannot inject value into "${this.target.name}.${this.propertyName}". ` +
|
|
12
12
|
`Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value.`
|
|
13
13
|
);
|
|
14
14
|
}
|