composed-di 0.1.6 → 0.2.6
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/ServiceScope.d.ts +6 -0
- package/dist/ServiceScope.d.ts.map +1 -0
- package/dist/ServiceScope.js +11 -0
- package/dist/ServiceScope.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/serviceFactory.d.ts +8 -6
- package/dist/serviceFactory.d.ts.map +1 -1
- package/dist/serviceFactory.js +9 -10
- package/dist/serviceFactory.js.map +1 -1
- package/dist/serviceModule.d.ts +12 -0
- package/dist/serviceModule.d.ts.map +1 -1
- package/dist/serviceModule.js +16 -0
- package/dist/serviceModule.js.map +1 -1
- package/package.json +1 -1
- package/src/ServiceFactory.ts +14 -14
- package/src/ServiceModule.ts +19 -0
- package/src/ServiceScope.ts +7 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceScope.d.ts","sourceRoot":"","sources":["../src/ServiceScope.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAY;IAGX,QAAQ,CAAC,IAAI,EAAE,MAAM;IAFjC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEH,IAAI,EAAE,MAAM;CAGlC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceScope = void 0;
|
|
4
|
+
class ServiceScope {
|
|
5
|
+
constructor(name) {
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.symbol = Symbol(name);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ServiceScope = ServiceScope;
|
|
11
|
+
//# sourceMappingURL=ServiceScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceScope.js","sourceRoot":"","sources":["../src/ServiceScope.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAGvB,YAAqB,IAAY;QAAZ,SAAI,GAAJ,IAAI,CAAQ;QAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF;AAND,oCAMC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,SAAS,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./ServiceKey"), exports);
|
|
18
18
|
__exportStar(require("./ServiceModule"), exports);
|
|
19
19
|
__exportStar(require("./ServiceFactory"), exports);
|
|
20
|
+
__exportStar(require("./ServiceScope"), exports);
|
|
20
21
|
__exportStar(require("./utils"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,kDAAgC;AAChC,mDAAiC;AACjC,0CAAuB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,kDAAgC;AAChC,mDAAiC;AACjC,iDAA8B;AAC9B,0CAAuB"}
|
package/dist/serviceFactory.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ServiceKey } from './ServiceKey';
|
|
2
|
+
import { ServiceScope } from './ServiceScope';
|
|
2
3
|
type ServiceType<T> = T extends ServiceKey<infer U> ? U : never;
|
|
3
4
|
type DependencyTypes<T extends readonly ServiceKey<unknown>[]> = {
|
|
4
5
|
[K in keyof T]: ServiceType<T[K]>;
|
|
@@ -6,13 +7,15 @@ type DependencyTypes<T extends readonly ServiceKey<unknown>[]> = {
|
|
|
6
7
|
export declare abstract class ServiceFactory<const T, const D extends readonly ServiceKey<unknown>[] = []> {
|
|
7
8
|
abstract provides: ServiceKey<T>;
|
|
8
9
|
abstract dependsOn: D;
|
|
9
|
-
abstract
|
|
10
|
-
abstract
|
|
10
|
+
abstract scope?: ServiceScope;
|
|
11
|
+
abstract initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
12
|
+
abstract dispose?: () => void;
|
|
11
13
|
/**
|
|
12
14
|
* Creates a singleton service factory that ensures a single instance of the provided service is initialized
|
|
13
|
-
* and used throughout
|
|
15
|
+
* and used throughout the scope lifecycle.
|
|
14
16
|
*/
|
|
15
|
-
static singleton<const T, const D extends readonly ServiceKey<unknown>[] = []>({ provides, dependsOn, initialize, dispose, }: {
|
|
17
|
+
static singleton<const T, const D extends readonly ServiceKey<unknown>[] = []>({ scope, provides, dependsOn, initialize, dispose, }: {
|
|
18
|
+
scope?: ServiceScope;
|
|
16
19
|
provides: ServiceKey<T>;
|
|
17
20
|
dependsOn?: D;
|
|
18
21
|
initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
@@ -22,11 +25,10 @@ export declare abstract class ServiceFactory<const T, const D extends readonly S
|
|
|
22
25
|
* Creates a one-shot service factory that initializes a new instance of the provided service
|
|
23
26
|
* every time it is requested.
|
|
24
27
|
*/
|
|
25
|
-
static oneShot<const T, const D extends readonly ServiceKey<unknown>[] = []>({ provides, dependsOn, initialize,
|
|
28
|
+
static oneShot<const T, const D extends readonly ServiceKey<unknown>[] = []>({ provides, dependsOn, initialize, }: {
|
|
26
29
|
provides: ServiceKey<T>;
|
|
27
30
|
dependsOn: D;
|
|
28
31
|
initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
29
|
-
dispose?: (instance: T) => void;
|
|
30
32
|
}): ServiceFactory<T, D>;
|
|
31
33
|
}
|
|
32
34
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceFactory.d.ts","sourceRoot":"","sources":["../src/ServiceFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"ServiceFactory.d.ts","sourceRoot":"","sources":["../src/ServiceFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,KAAK,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAGhE,KAAK,eAAe,CAAC,CAAC,SAAS,SAAS,UAAU,CAAC,OAAO,CAAC,EAAE,IAAI;KAC9D,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAClC,CAAC;AAEF,8BAAsB,cAAc,CAClC,KAAK,CAAC,CAAC,EACP,KAAK,CAAC,CAAC,SAAS,SAAS,UAAU,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE;IAEnD,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IACjC,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;IACtB,QAAQ,CAAC,KAAK,CAAC,EAAE,YAAY,CA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13,25 +13,25 @@ exports.ServiceFactory = void 0;
|
|
|
13
13
|
class ServiceFactory {
|
|
14
14
|
/**
|
|
15
15
|
* Creates a singleton service factory that ensures a single instance of the provided service is initialized
|
|
16
|
-
* and used throughout
|
|
16
|
+
* and used throughout the scope lifecycle.
|
|
17
17
|
*/
|
|
18
|
-
static singleton({ provides, dependsOn = [], initialize, dispose = () => { }, }) {
|
|
18
|
+
static singleton({ scope, provides, dependsOn = [], initialize, dispose = () => { }, }) {
|
|
19
19
|
let instance;
|
|
20
20
|
return {
|
|
21
|
+
scope,
|
|
21
22
|
provides,
|
|
22
23
|
dependsOn,
|
|
23
24
|
initialize(...dependencies) {
|
|
24
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
if (instance) {
|
|
26
|
-
|
|
26
|
+
if (instance === undefined) {
|
|
27
|
+
instance = yield initialize(...dependencies);
|
|
27
28
|
}
|
|
28
|
-
instance = yield initialize(...dependencies);
|
|
29
29
|
return instance;
|
|
30
30
|
});
|
|
31
31
|
},
|
|
32
|
-
dispose(
|
|
33
|
-
if (instance
|
|
34
|
-
dispose(
|
|
32
|
+
dispose() {
|
|
33
|
+
if (instance !== undefined) {
|
|
34
|
+
dispose(instance);
|
|
35
35
|
instance = undefined;
|
|
36
36
|
}
|
|
37
37
|
},
|
|
@@ -41,12 +41,11 @@ class ServiceFactory {
|
|
|
41
41
|
* Creates a one-shot service factory that initializes a new instance of the provided service
|
|
42
42
|
* every time it is requested.
|
|
43
43
|
*/
|
|
44
|
-
static oneShot({ provides, dependsOn, initialize,
|
|
44
|
+
static oneShot({ provides, dependsOn, initialize, }) {
|
|
45
45
|
return {
|
|
46
46
|
provides,
|
|
47
47
|
dependsOn,
|
|
48
48
|
initialize,
|
|
49
|
-
dispose,
|
|
50
49
|
};
|
|
51
50
|
}
|
|
52
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceFactory.js","sourceRoot":"","sources":["../src/ServiceFactory.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"ServiceFactory.js","sourceRoot":"","sources":["../src/ServiceFactory.ts"],"names":[],"mappings":";;;;;;;;;;;;AAWA,MAAsB,cAAc;IAUlC;;;OAGG;IACH,MAAM,CAAC,SAAS,CAGd,EACA,KAAK,EACL,QAAQ,EACR,SAAS,GAAG,EAAkB,EAC9B,UAAU,EACV,OAAO,GAAG,GAAG,EAAE,GAAE,CAAC,GAOnB;QACC,IAAI,QAAuB,CAAC;QAE5B,OAAO;YACL,KAAK;YACL,QAAQ;YACR,SAAS;YACH,UAAU,CAAC,GAAG,YAAgC;;oBAClD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;wBAC3B,QAAQ,GAAG,MAAM,UAAU,CAAC,GAAG,YAAY,CAAC,CAAC;oBAC/C,CAAC;oBAED,OAAO,QAAQ,CAAC;gBAClB,CAAC;aAAA;YACD,OAAO;gBACL,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;oBAC3B,OAAO,CAAC,QAAQ,CAAC,CAAC;oBAClB,QAAQ,GAAG,SAAS,CAAC;gBACvB,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,OAAO,CAA+D,EAC3E,QAAQ,EACR,SAAS,EACT,UAAU,GAKX;QACC,OAAO;YACL,QAAQ;YACR,SAAS;YACT,UAAU;SACX,CAAC;IACJ,CAAC;CACF;AAvED,wCAuEC"}
|
package/dist/serviceModule.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { ServiceKey } from './ServiceKey';
|
|
2
2
|
import { ServiceFactory } from './ServiceFactory';
|
|
3
|
+
import { ServiceScope } from './ServiceScope';
|
|
3
4
|
type GenericFactory = ServiceFactory<unknown, readonly ServiceKey<unknown>[]>;
|
|
4
5
|
export declare class ServiceModule {
|
|
5
6
|
readonly factories: GenericFactory[];
|
|
6
7
|
private constructor();
|
|
7
8
|
get<T>(key: ServiceKey<T>): Promise<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Disposes of service factories within the specified scope or all factories if no scope is provided.
|
|
11
|
+
*
|
|
12
|
+
* This method is useful for cleaning up resources and instances held by service factories,
|
|
13
|
+
* such as singleton factories, as they may hold database connections or other resources that need to be released.
|
|
14
|
+
*
|
|
15
|
+
* @param {ServiceScope} [scope] The scope to filter the factories to be disposed.
|
|
16
|
+
* If not provided, all factories are disposed of.
|
|
17
|
+
* @return {void} No return value.
|
|
18
|
+
*/
|
|
19
|
+
dispose(scope?: ServiceScope): void;
|
|
8
20
|
/**
|
|
9
21
|
* Creates a new ServiceModule instance by aggregating and deduplicating a list of
|
|
10
22
|
* ServiceModule or GenericFactory instances.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceModule.d.ts","sourceRoot":"","sources":["../src/ServiceModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"ServiceModule.d.ts","sourceRoot":"","sources":["../src/ServiceModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,KAAK,cAAc,GAAG,cAAc,CAAC,OAAO,EAAE,SAAS,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AAG9E,qBAAa,aAAa;IACJ,QAAQ,CAAC,SAAS,EAAE,cAAc,EAAE;IAAxD,OAAO;IAOM,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAqBnD;;;;;;;;;OASG;IACI,OAAO,CAAC,KAAK,CAAC,EAAE,YAAY;IAQnC;;;;;;;;;OASG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,aAAa,GAAG,cAAc,CAAC,EAAE,GAAG,aAAa;CAcxE"}
|
package/dist/serviceModule.js
CHANGED
|
@@ -35,6 +35,22 @@ class ServiceModule {
|
|
|
35
35
|
return factory.initialize(...dependencies);
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Disposes of service factories within the specified scope or all factories if no scope is provided.
|
|
40
|
+
*
|
|
41
|
+
* This method is useful for cleaning up resources and instances held by service factories,
|
|
42
|
+
* such as singleton factories, as they may hold database connections or other resources that need to be released.
|
|
43
|
+
*
|
|
44
|
+
* @param {ServiceScope} [scope] The scope to filter the factories to be disposed.
|
|
45
|
+
* If not provided, all factories are disposed of.
|
|
46
|
+
* @return {void} No return value.
|
|
47
|
+
*/
|
|
48
|
+
dispose(scope) {
|
|
49
|
+
const factories = scope
|
|
50
|
+
? this.factories.filter((f) => f.scope === scope)
|
|
51
|
+
: this.factories;
|
|
52
|
+
factories.forEach((factory) => { var _a; return (_a = factory.dispose) === null || _a === void 0 ? void 0 : _a.call(factory); });
|
|
53
|
+
}
|
|
38
54
|
/**
|
|
39
55
|
* Creates a new ServiceModule instance by aggregating and deduplicating a list of
|
|
40
56
|
* ServiceModule or GenericFactory instances.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceModule.js","sourceRoot":"","sources":["../src/ServiceModule.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"ServiceModule.js","sourceRoot":"","sources":["../src/ServiceModule.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,MAAa,aAAa;IACxB,YAA6B,SAA2B;QAA3B,cAAS,GAAT,SAAS,CAAkB;QACtD,SAAS,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,0BAA0B,CAAC,OAAO,CAAC,CAAC;YACpC,wBAAwB,CAAC,OAAO,EAAE,IAAI,CAA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AAG,OAAO,CAAC,QAAQ,CAAC,IAAI,uCAAuC,cAAc,EAAE,CAChF,CAAC;AACJ,CAAC;AAED,SAAS,YAAY,CAAC,GAAe,EAAE,SAA2B;IAChE,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,WAAC,OAAA,CAAA,MAAA,OAAO,CAAC,QAAQ,0CAAE,MAAM,OAAK,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,MAAM,CAAA,CAAA,EAAA,CAAC,CAAC;AAC/E,CAAC;AAED,SAAS,UAAU,CACjB,GAAkB,EAClB,OAA+B;;IAE/B,OAAO,CAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,0CAAE,MAAM,OAAK,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,MAAM,CAAA,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
package/src/ServiceFactory.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ServiceKey } from './ServiceKey';
|
|
2
|
+
import { ServiceScope } from './ServiceScope';
|
|
2
3
|
|
|
3
4
|
// Helper types to extract the type from ServiceKey
|
|
4
5
|
type ServiceType<T> = T extends ServiceKey<infer U> ? U : never;
|
|
@@ -14,24 +15,25 @@ export abstract class ServiceFactory<
|
|
|
14
15
|
> {
|
|
15
16
|
abstract provides: ServiceKey<T>;
|
|
16
17
|
abstract dependsOn: D;
|
|
17
|
-
|
|
18
|
-
abstract initialize(...dependencies: DependencyTypes<D>)
|
|
19
|
-
|
|
20
|
-
abstract dispose(instance: T): void;
|
|
18
|
+
abstract scope?: ServiceScope;
|
|
19
|
+
abstract initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
20
|
+
abstract dispose?: () => void;
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Creates a singleton service factory that ensures a single instance of the provided service is initialized
|
|
24
|
-
* and used throughout
|
|
24
|
+
* and used throughout the scope lifecycle.
|
|
25
25
|
*/
|
|
26
26
|
static singleton<
|
|
27
27
|
const T,
|
|
28
28
|
const D extends readonly ServiceKey<unknown>[] = [],
|
|
29
29
|
>({
|
|
30
|
+
scope,
|
|
30
31
|
provides,
|
|
31
32
|
dependsOn = [] as unknown as D,
|
|
32
33
|
initialize,
|
|
33
34
|
dispose = () => {},
|
|
34
35
|
}: {
|
|
36
|
+
scope?: ServiceScope;
|
|
35
37
|
provides: ServiceKey<T>;
|
|
36
38
|
dependsOn?: D;
|
|
37
39
|
initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
@@ -40,18 +42,19 @@ export abstract class ServiceFactory<
|
|
|
40
42
|
let instance: T | undefined;
|
|
41
43
|
|
|
42
44
|
return {
|
|
45
|
+
scope,
|
|
43
46
|
provides,
|
|
44
47
|
dependsOn,
|
|
45
48
|
async initialize(...dependencies: DependencyTypes<D>): Promise<T> {
|
|
46
|
-
if (instance) {
|
|
47
|
-
|
|
49
|
+
if (instance === undefined) {
|
|
50
|
+
instance = await initialize(...dependencies);
|
|
48
51
|
}
|
|
49
|
-
|
|
52
|
+
|
|
50
53
|
return instance;
|
|
51
54
|
},
|
|
52
|
-
dispose(
|
|
53
|
-
if (instance
|
|
54
|
-
dispose(
|
|
55
|
+
dispose(): void {
|
|
56
|
+
if (instance !== undefined) {
|
|
57
|
+
dispose(instance);
|
|
55
58
|
instance = undefined;
|
|
56
59
|
}
|
|
57
60
|
},
|
|
@@ -66,18 +69,15 @@ export abstract class ServiceFactory<
|
|
|
66
69
|
provides,
|
|
67
70
|
dependsOn,
|
|
68
71
|
initialize,
|
|
69
|
-
dispose = () => {},
|
|
70
72
|
}: {
|
|
71
73
|
provides: ServiceKey<T>;
|
|
72
74
|
dependsOn: D;
|
|
73
75
|
initialize: (...dependencies: DependencyTypes<D>) => T | Promise<T>;
|
|
74
|
-
dispose?: (instance: T) => void;
|
|
75
76
|
}): ServiceFactory<T, D> {
|
|
76
77
|
return {
|
|
77
78
|
provides,
|
|
78
79
|
dependsOn,
|
|
79
80
|
initialize,
|
|
80
|
-
dispose,
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
}
|
package/src/ServiceModule.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ServiceKey } from './ServiceKey';
|
|
2
2
|
import { ServiceFactory } from './ServiceFactory';
|
|
3
|
+
import { ServiceScope } from './ServiceScope';
|
|
3
4
|
|
|
4
5
|
type GenericFactory = ServiceFactory<unknown, readonly ServiceKey<unknown>[]>;
|
|
5
6
|
type GenericKey = ServiceKey<unknown>;
|
|
@@ -33,6 +34,24 @@ export class ServiceModule {
|
|
|
33
34
|
return factory.initialize(...dependencies);
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Disposes of service factories within the specified scope or all factories if no scope is provided.
|
|
39
|
+
*
|
|
40
|
+
* This method is useful for cleaning up resources and instances held by service factories,
|
|
41
|
+
* such as singleton factories, as they may hold database connections or other resources that need to be released.
|
|
42
|
+
*
|
|
43
|
+
* @param {ServiceScope} [scope] The scope to filter the factories to be disposed.
|
|
44
|
+
* If not provided, all factories are disposed of.
|
|
45
|
+
* @return {void} No return value.
|
|
46
|
+
*/
|
|
47
|
+
public dispose(scope?: ServiceScope) {
|
|
48
|
+
const factories = scope
|
|
49
|
+
? this.factories.filter((f) => f.scope === scope)
|
|
50
|
+
: this.factories;
|
|
51
|
+
|
|
52
|
+
factories.forEach((factory) => factory.dispose?.());
|
|
53
|
+
}
|
|
54
|
+
|
|
36
55
|
/**
|
|
37
56
|
* Creates a new ServiceModule instance by aggregating and deduplicating a list of
|
|
38
57
|
* ServiceModule or GenericFactory instances.
|