@aomex/common-service 3.30.6 → 3.30.8-alpha.131cf1c.0
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/CHANGELOG.md +8 -0
- package/dist/index.d.ts +75 -34
- package/dist/index.js +149 -49
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.30.7](https://github.com/aomex/aomex/compare/v3.30.6...v3.30.7) (2026-11-01)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @aomex/common-service
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [3.30.6](https://github.com/aomex/aomex/compare/v3.30.5...v3.30.6) (2026-10-19)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @aomex/common-service
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,81 @@
|
|
|
1
|
+
import { IsUnion, PromisifyFunction } from "@aomex/internal-tools";
|
|
2
|
+
export declare abstract class Service {
|
|
3
|
+
protected get displayName(): string;
|
|
4
|
+
/**
|
|
5
|
+
* 懒加载后、首次对外调用前自动触发。同一次 combine 内对单个服务只执行一次。
|
|
6
|
+
*
|
|
7
|
+
* 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。
|
|
8
|
+
*/
|
|
9
|
+
protected onInit(): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* 进程/应用关停时由 WebApp、ConsoleApp 自动触发。
|
|
12
|
+
*
|
|
13
|
+
* 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。
|
|
14
|
+
*/
|
|
15
|
+
protected onDestroy(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
type ServiceCtor = abstract new (...args: any) => Service;
|
|
18
|
+
/** 生命周期钩子名:按名字剔除,与是否 protected 无关 */
|
|
19
|
+
type ServiceLifecycle = 'onInit' | 'onDestroy';
|
|
1
20
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* ```typescript
|
|
5
|
-
export const services = await combineServices({...});
|
|
6
|
-
|
|
7
|
-
declare module '@aomex/common' {
|
|
8
|
-
type T = typeof services;
|
|
9
|
-
export interface CombinedServices extends T {}
|
|
10
|
-
}
|
|
11
|
-
* ```
|
|
21
|
+
* 对外服务视图:仅方法,且全部 Promise 化;字段与生命周期钩子不可见。
|
|
22
|
+
* `onInit` / `onDestroy` 即使被 override 成 public 也不会出现在类型上。
|
|
12
23
|
*/
|
|
13
|
-
|
|
14
|
-
}
|
|
24
|
+
export type PromisifiedService<T> = [T] extends [never] ? {} : { [K in keyof T as T[K] extends ((...args: any) => any) ? K extends ServiceLifecycle ? never : K : never]: PromisifyFunction<T[K]>; };
|
|
25
|
+
type ServiceCtorKeys<M> = { [K in keyof M]: M[K] extends ServiceCtor ? K : never; }[keyof M];
|
|
26
|
+
/** 模块导出中恰好一个 Service 构造器;否则 never */
|
|
27
|
+
export type UniqueServiceCtor<M> = ServiceCtorKeys<M> extends (infer K) ? [K] extends [never] ? never : IsUnion<K> extends true ? never : K extends keyof M ? M[K] extends ServiceCtor ? M[K] : never : never : never;
|
|
28
|
+
/** 对 import / then 解析后的值推断构造器 */
|
|
29
|
+
export type CtorFromResolved<R> = R extends ServiceCtor ? R : R extends {
|
|
30
|
+
default: infer D;
|
|
31
|
+
} ? D extends ServiceCtor ? D : UniqueServiceCtor<R> : UniqueServiceCtor<R>;
|
|
15
32
|
/**
|
|
16
|
-
*
|
|
33
|
+
* loader:必须是异步工厂(如 `() => import('./x.service.js')`),不允许直接传 Class。
|
|
17
34
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
export type ServiceLoader = () => Promise<unknown>;
|
|
36
|
+
/** 用于 combineServices 的自反约束,避免上下文类型把 import/then 污染成 any */
|
|
37
|
+
export type EnsureServiceLoaders<T> = { [K in keyof T]: T[K] extends ServiceLoader ? T[K] : never; };
|
|
38
|
+
export type InferLoaderCtor<L> = L extends (() => Promise<infer R>) ? CtorFromResolved<R> : never;
|
|
39
|
+
export type InferLoaderInstance<L> = [InferLoaderCtor<L>] extends [never] ? never : InferLoaderCtor<L> extends ServiceCtor ? InstanceType<InferLoaderCtor<L>> : never;
|
|
40
|
+
/** `combineServices` 返回的服务袋(精确推断用 `CombinedServicesOf`) */
|
|
41
|
+
export type CombinedServices = Record<string, unknown>;
|
|
42
|
+
export type CombinedServicesOf<T> = { readonly [K in keyof T]: PromisifiedService<InferLoaderInstance<T[K]>>; };
|
|
43
|
+
type CombineServicesOptions<T> = {
|
|
44
|
+
/**
|
|
45
|
+
* 在 `combineServices` 返回前预先完成 `onInit` 的服务 key(如 cache)。
|
|
46
|
+
* 未列出的服务仍保持首次方法调用时懒加载。
|
|
47
|
+
*/
|
|
48
|
+
eager?: NoInfer<keyof T & string>[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 组合服务并返回懒加载代理。
|
|
52
|
+
*
|
|
53
|
+
* - loader 一律为异步工厂:`() => import('./xxx.service.js')`(不允许直接传 Class)
|
|
54
|
+
* - 工厂返回值解析:整体是 Class → `default` → 恰好一个 Service 导出
|
|
55
|
+
* - 对外仅方法,且一律返回 Promise;类内部 `this.xxx()` 仍为真实同步/异步语义
|
|
56
|
+
* - `options.eager`:启动时预先 `onInit` 指定服务(基础设施如 cache)
|
|
57
|
+
* - 生命周期销毁:把返回值交给 `WebApp` / `ConsoleApp` 的 `services`,勿手调销毁 API
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* export const services = await combineServices(
|
|
62
|
+
* {
|
|
63
|
+
* cache: () => import('./cache.service.js'),
|
|
64
|
+
* article: () => import('./article.service.js'),
|
|
65
|
+
* },
|
|
66
|
+
* { eager: ['cache'] },
|
|
67
|
+
* );
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare const combineServices: <T extends EnsureServiceLoaders<T>>(loaders: T, options?: CombineServicesOptions<T>) => Promise<CombinedServicesOf<T>>;
|
|
35
71
|
/**
|
|
36
|
-
*
|
|
72
|
+
* 销毁已加载实例(触发 `onDestroy`)。仅接受数组;幂等。
|
|
73
|
+
*
|
|
74
|
+
* 业务代码不要调用:把 `combineServices` 结果交给 `WebApp` / `ConsoleApp` 的 `services`,由框架在关停时调用。
|
|
75
|
+
*
|
|
76
|
+
* 触发时机:
|
|
77
|
+
* - `WebApp`:`SIGINT` / `SIGTERM` → `server.close()`(超时则 `closeAllConnections`)后再销毁;手动 `server.close()` 时也会销毁
|
|
78
|
+
* - `ConsoleApp`:进程 `beforeExit`,以及 `SIGINT` / `SIGTERM`
|
|
37
79
|
*/
|
|
38
|
-
declare const
|
|
39
|
-
|
|
40
|
-
export { type CombinedServices, Service, combineServices, destroyServices };
|
|
80
|
+
export declare const internalDestroyServices: (services: CombinedServices[]) => Promise<void>;
|
|
81
|
+
export type { CombineServicesOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,57 +1,157 @@
|
|
|
1
1
|
// src/service.ts
|
|
2
2
|
var Service = class {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
3
|
+
get displayName() {
|
|
4
|
+
const className = this.constructor.name;
|
|
5
|
+
if (className.endsWith("Service")) return className.slice(0, -7);
|
|
6
|
+
return className;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 懒加载后、首次对外调用前自动触发。同一次 combine 内对单个服务只执行一次。
|
|
10
|
+
*
|
|
11
|
+
* 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。
|
|
12
|
+
*/
|
|
13
|
+
async onInit() {}
|
|
14
|
+
/**
|
|
15
|
+
* 进程/应用关停时由 WebApp、ConsoleApp 自动触发。
|
|
16
|
+
*
|
|
17
|
+
* 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。
|
|
18
|
+
*/
|
|
19
|
+
async onDestroy() {}
|
|
20
|
+
};
|
|
21
|
+
// src/resolve-ctor.ts
|
|
22
|
+
const isServiceCtor = (value) => {
|
|
23
|
+
return typeof value === "function" && "prototype" in value && value.prototype instanceof Service;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 解析工厂返回值:整体是 Class → default → 导出中唯一的 Service 子类。
|
|
27
|
+
* (动态 import 的结果类型挡不住空模块/多导出,需运行时判定。)
|
|
28
|
+
*/
|
|
29
|
+
const resolveCtor = (resolved, key) => {
|
|
30
|
+
if (isServiceCtor(resolved)) return resolved;
|
|
31
|
+
if (resolved !== null && typeof resolved === "object") {
|
|
32
|
+
const mod = resolved;
|
|
33
|
+
if (isServiceCtor(mod["default"])) return mod["default"];
|
|
34
|
+
const ctors = Object.values(mod).filter(isServiceCtor);
|
|
35
|
+
if (ctors.length === 1) return ctors[0];
|
|
36
|
+
}
|
|
37
|
+
throw new TypeError(`combineServices["${key}"]: expected a Service class, default export, or a module with exactly one Service class`);
|
|
38
|
+
};
|
|
39
|
+
const loadCtor = async (loader, key) => {
|
|
40
|
+
return resolveCtor(await loader(), key);
|
|
26
41
|
};
|
|
27
|
-
|
|
28
42
|
// src/combine-services.ts
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
/** WebApp / ConsoleApp 关停时由 internalDestroyServices 使用 */
|
|
44
|
+
const serviceRegistries = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const ensureInstance = async (key, registry) => {
|
|
46
|
+
const existing = registry.instances.get(key);
|
|
47
|
+
if (existing) return existing;
|
|
48
|
+
const pending = registry.pending.get(key);
|
|
49
|
+
if (pending) return pending;
|
|
50
|
+
const task = (async () => {
|
|
51
|
+
const instance = new (await (loadCtor(registry.loaders[key], key)))();
|
|
52
|
+
registry.instances.set(key, instance);
|
|
53
|
+
try {
|
|
54
|
+
await instance["onInit"]();
|
|
55
|
+
} catch (e) {
|
|
56
|
+
registry.instances.delete(key);
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
return instance;
|
|
60
|
+
})();
|
|
61
|
+
registry.pending.set(key, task);
|
|
62
|
+
try {
|
|
63
|
+
return await task;
|
|
64
|
+
} finally {
|
|
65
|
+
registry.pending.delete(key);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const createServiceProxy = (key, registry) => {
|
|
69
|
+
return new Proxy({}, {
|
|
70
|
+
get(_target, prop) {
|
|
71
|
+
if (typeof prop === "symbol") return;
|
|
72
|
+
if (prop === "then") return;
|
|
73
|
+
return async (...args) => {
|
|
74
|
+
const instance = await ensureInstance(key, registry);
|
|
75
|
+
return Reflect.apply(Reflect.get(instance, prop, instance), instance, args);
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
set() {
|
|
79
|
+
return false;
|
|
80
|
+
},
|
|
81
|
+
deleteProperty() {
|
|
82
|
+
return false;
|
|
83
|
+
},
|
|
84
|
+
defineProperty() {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* 组合服务并返回懒加载代理。
|
|
91
|
+
*
|
|
92
|
+
* - loader 一律为异步工厂:`() => import('./xxx.service.js')`(不允许直接传 Class)
|
|
93
|
+
* - 工厂返回值解析:整体是 Class → `default` → 恰好一个 Service 导出
|
|
94
|
+
* - 对外仅方法,且一律返回 Promise;类内部 `this.xxx()` 仍为真实同步/异步语义
|
|
95
|
+
* - `options.eager`:启动时预先 `onInit` 指定服务(基础设施如 cache)
|
|
96
|
+
* - 生命周期销毁:把返回值交给 `WebApp` / `ConsoleApp` 的 `services`,勿手调销毁 API
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* export const services = await combineServices(
|
|
101
|
+
* {
|
|
102
|
+
* cache: () => import('./cache.service.js'),
|
|
103
|
+
* article: () => import('./article.service.js'),
|
|
104
|
+
* },
|
|
105
|
+
* { eager: ['cache'] },
|
|
106
|
+
* );
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
const combineServices = async (loaders, options) => {
|
|
110
|
+
const services = {};
|
|
111
|
+
const registry = {
|
|
112
|
+
loaders: { ...loaders },
|
|
113
|
+
instances: /* @__PURE__ */ new Map(),
|
|
114
|
+
pending: /* @__PURE__ */ new Map()
|
|
115
|
+
};
|
|
116
|
+
for (const key of Object.keys(loaders)) services[key] = createServiceProxy(key, registry);
|
|
117
|
+
Object.freeze(services);
|
|
118
|
+
serviceRegistries.set(services, registry);
|
|
119
|
+
for (const key of options?.eager ?? []) await ensureInstance(key, registry);
|
|
120
|
+
return services;
|
|
42
121
|
};
|
|
43
|
-
|
|
44
122
|
// src/destroy-services.ts
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
123
|
+
const destroyed = /* @__PURE__ */ new WeakSet();
|
|
124
|
+
const destroying = /* @__PURE__ */ new WeakMap();
|
|
125
|
+
/**
|
|
126
|
+
* 销毁已加载实例(触发 `onDestroy`)。仅接受数组;幂等。
|
|
127
|
+
*
|
|
128
|
+
* 业务代码不要调用:把 `combineServices` 结果交给 `WebApp` / `ConsoleApp` 的 `services`,由框架在关停时调用。
|
|
129
|
+
*
|
|
130
|
+
* 触发时机:
|
|
131
|
+
* - `WebApp`:`SIGINT` / `SIGTERM` → `server.close()`(超时则 `closeAllConnections`)后再销毁;手动 `server.close()` 时也会销毁
|
|
132
|
+
* - `ConsoleApp`:进程 `beforeExit`,以及 `SIGINT` / `SIGTERM`
|
|
133
|
+
*/
|
|
134
|
+
const internalDestroyServices = async (services) => {
|
|
135
|
+
await Promise.all(services.map(async (item) => {
|
|
136
|
+
if (destroyed.has(item)) return;
|
|
137
|
+
const pending = destroying.get(item);
|
|
138
|
+
if (pending) return pending;
|
|
139
|
+
const task = (async () => {
|
|
140
|
+
try {
|
|
141
|
+
const registry = serviceRegistries.get(item);
|
|
142
|
+
if (!registry) return;
|
|
143
|
+
await Promise.all([...registry.instances.values()].map((service) => service["onDestroy"]()));
|
|
144
|
+
registry.instances.clear();
|
|
145
|
+
registry.pending.clear();
|
|
146
|
+
} finally {
|
|
147
|
+
destroyed.add(item);
|
|
148
|
+
destroying.delete(item);
|
|
149
|
+
}
|
|
150
|
+
})();
|
|
151
|
+
destroying.set(item, task);
|
|
152
|
+
return task;
|
|
153
|
+
}));
|
|
56
154
|
};
|
|
155
|
+
export { Service, combineServices, internalDestroyServices };
|
|
156
|
+
|
|
57
157
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/service.ts","../src/combine-services.ts","../src/destroy-services.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/service.ts","../src/resolve-ctor.ts","../src/combine-services.ts","../src/destroy-services.ts"],"sourcesContent":["export abstract class Service {\n protected get displayName() {\n const className = this.constructor.name;\n if (className.endsWith('Service')) {\n return className.slice(0, -7);\n }\n return className;\n }\n\n /**\n * 懒加载后、首次对外调用前自动触发。同一次 combine 内对单个服务只执行一次。\n *\n * 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。\n */\n protected async onInit(): Promise<void> {}\n\n /**\n * 进程/应用关停时由 WebApp、ConsoleApp 自动触发。\n *\n * 请保持 `protected`;即便误写成 public,也不会出现在 `services.xxx` 的类型上。\n */\n protected async onDestroy(): Promise<void> {}\n}\n","import { Service } from './service';\n\nexport type ServiceCtor = new () => Service;\n\nexport const isServiceCtor = (value: unknown): value is ServiceCtor => {\n return (\n typeof value === 'function' &&\n 'prototype' in value &&\n value.prototype instanceof Service\n );\n};\n\n/**\n * 解析工厂返回值:整体是 Class → default → 导出中唯一的 Service 子类。\n * (动态 import 的结果类型挡不住空模块/多导出,需运行时判定。)\n */\nexport const resolveCtor = (resolved: unknown, key: string): ServiceCtor => {\n if (isServiceCtor(resolved)) {\n return resolved;\n }\n\n if (resolved !== null && typeof resolved === 'object') {\n const mod = resolved as Record<string, unknown>;\n\n if (isServiceCtor(mod['default'])) {\n return mod['default'];\n }\n\n // 与 UniqueServiceCtor 一致:恰好一个才选用,0 个或多个都不选\n const ctors = Object.values(mod).filter(isServiceCtor);\n if (ctors.length === 1) {\n return ctors[0]!;\n }\n }\n\n throw new TypeError(\n `combineServices[\"${key}\"]: expected a Service class, default export, or a module with exactly one Service class`,\n );\n};\n\nexport const loadCtor = async (\n loader: () => Promise<unknown>,\n key: string,\n): Promise<ServiceCtor> => {\n return resolveCtor(await loader(), key);\n};\n","import { loadCtor } from './resolve-ctor';\nimport type { Service } from './service';\nimport type {\n CombinedServices,\n CombinedServicesOf,\n EnsureServiceLoaders,\n ServiceLoader,\n} from './types';\n\nexport type {\n CombinedServices,\n CombinedServicesOf,\n EnsureServiceLoaders,\n PromisifiedService,\n ServiceLoader,\n} from './types';\n\nexport interface ServiceRegistry {\n loaders: Record<string, ServiceLoader>;\n instances: Map<string, Service>;\n pending: Map<string, Promise<Service>>;\n}\n\n/** WebApp / ConsoleApp 关停时由 internalDestroyServices 使用 */\nexport const serviceRegistries = new WeakMap<CombinedServices, ServiceRegistry>();\n\nconst ensureInstance = async (\n key: string,\n registry: ServiceRegistry,\n): Promise<Service> => {\n const existing = registry.instances.get(key);\n if (existing) return existing;\n\n const pending = registry.pending.get(key);\n if (pending) return pending;\n\n const task = (async () => {\n const Ctor = await loadCtor(registry.loaders[key]!, key);\n const instance = new Ctor();\n // 先登记再 onInit,避免钩子内互调同 key 死锁\n registry.instances.set(key, instance);\n try {\n await instance['onInit']();\n } catch (e) {\n registry.instances.delete(key);\n throw e;\n }\n return instance;\n })();\n\n registry.pending.set(key, task);\n try {\n return await task;\n } finally {\n registry.pending.delete(key);\n }\n};\n\nconst createServiceProxy = (key: string, registry: ServiceRegistry): object => {\n return new Proxy(\n {},\n {\n get(_target, prop) {\n // Symbol 键(如 iterator / toStringTag)不当作服务方法,避免被包成 async 函数\n if (typeof prop === 'symbol') {\n return undefined;\n }\n // 避免被当成 Thenable\n if (prop === 'then') {\n return undefined;\n }\n\n return async (...args: unknown[]) => {\n const instance = await ensureInstance(key, registry);\n // 非方法:类型已剔除字段;运行时由 Reflect.apply 自然抛错即可\n return Reflect.apply(Reflect.get(instance, prop, instance), instance, args);\n };\n },\n set() {\n return false;\n },\n deleteProperty() {\n return false;\n },\n defineProperty() {\n return false;\n },\n },\n );\n};\n\nexport type CombineServicesOptions<T> = {\n /**\n * 在 `combineServices` 返回前预先完成 `onInit` 的服务 key(如 cache)。\n * 未列出的服务仍保持首次方法调用时懒加载。\n */\n eager?: NoInfer<keyof T & string>[];\n};\n\n/**\n * 组合服务并返回懒加载代理。\n *\n * - loader 一律为异步工厂:`() => import('./xxx.service.js')`(不允许直接传 Class)\n * - 工厂返回值解析:整体是 Class → `default` → 恰好一个 Service 导出\n * - 对外仅方法,且一律返回 Promise;类内部 `this.xxx()` 仍为真实同步/异步语义\n * - `options.eager`:启动时预先 `onInit` 指定服务(基础设施如 cache)\n * - 生命周期销毁:把返回值交给 `WebApp` / `ConsoleApp` 的 `services`,勿手调销毁 API\n *\n * @example\n * ```ts\n * export const services = await combineServices(\n * {\n * cache: () => import('./cache.service.js'),\n * article: () => import('./article.service.js'),\n * },\n * { eager: ['cache'] },\n * );\n * ```\n */\nexport const combineServices = async <T extends EnsureServiceLoaders<T>>(\n loaders: T,\n options?: CombineServicesOptions<T>,\n): Promise<CombinedServicesOf<T>> => {\n const services: CombinedServices = {};\n const registry: ServiceRegistry = {\n loaders: { ...loaders },\n instances: new Map(),\n pending: new Map(),\n };\n\n for (const key of Object.keys(loaders)) {\n services[key] = createServiceProxy(key, registry);\n }\n\n Object.freeze(services);\n serviceRegistries.set(services, registry);\n\n for (const key of options?.eager ?? []) {\n await ensureInstance(key, registry);\n }\n\n return services as CombinedServicesOf<T>;\n};\n","import type { CombinedServices } from './types';\nimport { serviceRegistries } from './combine-services';\n\nconst destroyed = new WeakSet<object>();\nconst destroying = new WeakMap<object, Promise<void>>();\n\n/**\n * 销毁已加载实例(触发 `onDestroy`)。仅接受数组;幂等。\n *\n * 业务代码不要调用:把 `combineServices` 结果交给 `WebApp` / `ConsoleApp` 的 `services`,由框架在关停时调用。\n *\n * 触发时机:\n * - `WebApp`:`SIGINT` / `SIGTERM` → `server.close()`(超时则 `closeAllConnections`)后再销毁;手动 `server.close()` 时也会销毁\n * - `ConsoleApp`:进程 `beforeExit`,以及 `SIGINT` / `SIGTERM`\n */\nexport const internalDestroyServices = async (\n services: CombinedServices[],\n): Promise<void> => {\n await Promise.all(\n services.map(async (item) => {\n if (destroyed.has(item)) return;\n\n const pending = destroying.get(item);\n if (pending) return pending;\n\n const task = (async () => {\n try {\n const registry = serviceRegistries.get(item);\n if (!registry) return;\n\n await Promise.all(\n [...registry.instances.values()].map((service) => service['onDestroy']()),\n );\n registry.instances.clear();\n registry.pending.clear();\n } finally {\n dest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I,GAAG;EAEzB,MAAM,UAAU,WAAW,IAAI,IAAI;EACnC,IAAI,SAAS,OAAO;EAEpB,MAAM,QAAQ,YAAY;GACxB,IAAI;IACF,MAAM,WAAW,kBAAkB,IAAI,IAAI;IAC3C,IAAI,CAAC,UAAU;IAEf,MAAM,QAAQ,IACZ,CAAC,GAAG,SAAS,UAAU,OAAO,CAAC,CAAC,CAAC,KAAK,YAAY,QAAQ,YAAY,CAAC,CAAC,CAC1E;IACA,SAAS,UAAU,MAAM;IACzB,SAAS,QAAQ,MAAM;GACzB,UAAU;IACR,UAAU,IAAI,IAAI;IAClB,WAAW,OAAO,IAAI;GACxB;EACF,EAAA,CAAG;EAEH,WAAW,IAAI,MAAM,IAAI;EACzB,OAAO;CACT,CAAC,CACH;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/common-service",
|
|
3
|
-
"version": "3.30.
|
|
3
|
+
"version": "3.30.8-alpha.131cf1c.0",
|
|
4
4
|
"description": "aomex服务层",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,5 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://aomex.js.org",
|
|
29
29
|
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@aomex/internal-tools": "3.30.8-alpha.131cf1c.0"
|
|
32
|
+
},
|
|
30
33
|
"scripts": {}
|
|
31
34
|
}
|