@taicode/common-base 4.0.0-beta.21 → 4.0.0-beta.24
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/output/module/index.d.ts +3 -0
- package/output/module/index.d.ts.map +1 -0
- package/output/module/index.js +1 -0
- package/output/module/module.d.ts +94 -0
- package/output/module/module.d.ts.map +1 -0
- package/output/module/module.js +149 -0
- package/output/module/module.test.d.ts +2 -0
- package/output/module/module.test.d.ts.map +1 -0
- package/output/module/module.test.js +152 -0
- package/package.json +1 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/module/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { defineLayerPolicy, planModules, runModules } from './module.js';
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模块图内核:分层策略 + 声明序 + 依赖解析 + 生命周期顺序。
|
|
3
|
+
*
|
|
4
|
+
* 它只回答静态问题——「哪些层、层能引用哪些层、模块之间是否合法、按什么顺序构造与释放」——
|
|
5
|
+
* 不持有任何运行时(没有 logger、没有异步、没有构造 / 初始化回调),因此 server / web /
|
|
6
|
+
* worker 都能用同一份口径,也都能在单测里直接断言。
|
|
7
|
+
*
|
|
8
|
+
* 构造与释放的**执行**留给使用方:各运行时的阶段划分不同(server 要在 store 与 config
|
|
9
|
+
* 之间装 HTTP 契约、config 要按固定节拍独自刷新),内核只给出稳定的顺序。
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 一层的规则:能引用哪些层(含自身表示允许同层引用),以及本层能拿到哪些**环境输入**。
|
|
13
|
+
*
|
|
14
|
+
* 依赖视图(`stores` / `configs` …)不在这里:它们由模块自己的 `uses` 声明,内核只负责
|
|
15
|
+
* 把「环境输入」按层收口——`log` 之外还有什么,由使用方决定。
|
|
16
|
+
*/
|
|
17
|
+
export interface LayerRule {
|
|
18
|
+
readonly uses: readonly string[];
|
|
19
|
+
readonly inputs: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
export type LayerPolicy<Layer extends string = string> = Readonly<Record<Layer, LayerRule>>;
|
|
22
|
+
/** 依赖引用:内核只要求能读出 `name`,其余语义(能力 / 覆盖 / 身份)由 `planModules` 的 resolver 决定 */
|
|
23
|
+
export interface ModuleReference {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
}
|
|
26
|
+
/** 模块图的节点:层 + 层内唯一的 name + 它引用的节点 */
|
|
27
|
+
export interface ModuleNode<Layer extends string = string, Reference extends ModuleReference = ModuleReference> {
|
|
28
|
+
readonly layer: Layer;
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly uses: readonly Reference[];
|
|
31
|
+
}
|
|
32
|
+
export interface ModulePlan<Layer extends string, Node extends ModuleNode<Layer>> {
|
|
33
|
+
/** 层顺序:与策略的声明序一致 */
|
|
34
|
+
readonly order: readonly Layer[];
|
|
35
|
+
/** 每层内按声明序排列的节点 */
|
|
36
|
+
readonly layers: Readonly<Record<Layer, readonly Node[]>>;
|
|
37
|
+
/**
|
|
38
|
+
* 依赖边:节点 → 它直接引用的节点。
|
|
39
|
+
*
|
|
40
|
+
* 装配器可以据此做依赖驱动的并发调度:`create` 只等依赖的 `create`,`init` 只等依赖的
|
|
41
|
+
* `init`。按 `order` / `layers` 严格串行仍然安全,但不是唯一解。
|
|
42
|
+
*/
|
|
43
|
+
readonly dependencies: ReadonlyMap<Node, readonly Node[]>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 声明一份分层策略:**声明序就是依赖序**。
|
|
47
|
+
*
|
|
48
|
+
* - `uses` 只能引用同一份策略里的层;
|
|
49
|
+
* - 除自身(同层引用)外,被引用的层必须声明在本层之前——否则构造序与依赖序相反,
|
|
50
|
+
* 层间成环在定义期就不可能存在;
|
|
51
|
+
* - `inputs` 原样保留,交给使用方在构造 ctx 时消费。
|
|
52
|
+
*
|
|
53
|
+
* 层名与层数完全由使用方决定;内核不预置任何具体分层。
|
|
54
|
+
*/
|
|
55
|
+
export declare function defineLayerPolicy<const Policy extends LayerPolicy>(policy: Policy): Policy;
|
|
56
|
+
/**
|
|
57
|
+
* 校验模块图并给出构造 / 释放顺序。
|
|
58
|
+
*
|
|
59
|
+
* 不变式(每条都在这里兜底,不依赖调用方先校验):
|
|
60
|
+
* 1. 每个节点的 `layer` 都在策略里;
|
|
61
|
+
* 2. 同层内 `name` 唯一(跨层可以同名:视图按层聚合);
|
|
62
|
+
* 3. `uses` 的目标必须能解析到本次装配里的节点;
|
|
63
|
+
* 4. 目标层在本层 `uses` 里;
|
|
64
|
+
* 5. 同层只能引用**声明更早**的节点;
|
|
65
|
+
* 6. 依赖无环(策略经 `defineLayerPolicy` 校验后已不可能,这里仍兜底裸策略)。
|
|
66
|
+
*
|
|
67
|
+
* `resolve` 是**依赖策略**:引用是什么形状、能力如何落到接管单元上,都由使用方决定;
|
|
68
|
+
* 内核只消费解析结果,并在拿不到节点时按「不在本次装配里」报错。
|
|
69
|
+
*/
|
|
70
|
+
export declare function planModules<Layer extends string, Reference extends ModuleReference, Node extends ModuleNode<Layer, Reference>>(policy: LayerPolicy<Layer>, modules: readonly Node[], resolve: (reference: Reference, owner: Node) => Node | undefined): ModulePlan<Layer, Node>;
|
|
71
|
+
/** 生命周期方向:`setup` 正向、`teardown` 反向 */
|
|
72
|
+
export type LifecycleDirection = 'setup' | 'teardown';
|
|
73
|
+
/**
|
|
74
|
+
* 一个生命周期阶段:内核只决定**方向与顺序**,做什么由使用方提供。
|
|
75
|
+
*
|
|
76
|
+
* `setup` 按层序 + 层内声明序,`teardown` 按层逆序 + 层内逆序;`layers` 把阶段收窄到
|
|
77
|
+
* 指定层(teardown 时这个列表本身也逆序)。业务可以注册任意多个阶段(预热、停写、
|
|
78
|
+
* 关闭外部连接……),不需要改动内核。
|
|
79
|
+
*/
|
|
80
|
+
export interface LifecyclePhase<Node> {
|
|
81
|
+
/** 阶段名:只用于报错定位 */
|
|
82
|
+
readonly name: string;
|
|
83
|
+
readonly direction: LifecycleDirection;
|
|
84
|
+
/** 只作用于这些层;省略表示全部层 */
|
|
85
|
+
readonly layers?: readonly string[];
|
|
86
|
+
run(node: Node): void | Promise<void>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 按声明序**串行**驱动阶段;单个 `run` 失败即停(要不要吞错由 `run` 自己决定)。
|
|
90
|
+
*
|
|
91
|
+
* 内核不缓存任何运行期状态:阶段拿到的是节点本身,取值 / 记录由使用方完成。
|
|
92
|
+
*/
|
|
93
|
+
export declare function runModules<Layer extends string, Node extends ModuleNode<Layer>>(plan: ModulePlan<Layer, Node>, phases: readonly LifecyclePhase<Node>[]): Promise<void>;
|
|
94
|
+
//# sourceMappingURL=module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../source/module/module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;IAChC,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;CACnC;AAED,MAAM,MAAM,WAAW,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAA;AAE3F,2EAA2E;AAC3E,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CACtB;AAED,qCAAqC;AACrC,MAAM,WAAW,UAAU,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EAAE,SAAS,SAAS,eAAe,GAAG,eAAe;IAC5G,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,SAAS,SAAS,EAAE,CAAA;CACpC;AAED,MAAM,WAAW,UAAU,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,SAAS,UAAU,CAAC,KAAK,CAAC;IAC9E,oBAAoB;IACpB,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,CAAA;IAChC,mBAAmB;IACnB,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC,CAAC,CAAA;IACzD;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,EAAE,WAAW,CAAC,IAAI,EAAE,SAAS,IAAI,EAAE,CAAC,CAAA;CAC1D;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,CAAC,MAAM,SAAS,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAiB1F;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,KAAK,SAAS,MAAM,EAAE,SAAS,SAAS,eAAe,EAAE,IAAI,SAAS,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,SAAS,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,KAAK,IAAI,GAAG,SAAS,GAAG,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CA4D/Q;AAkCD,sCAAsC;AACtC,MAAM,MAAM,kBAAkB,GAAG,OAAO,GAAG,UAAU,CAAA;AAErD;;;;;;GAMG;AACH,MAAM,WAAW,cAAc,CAAC,IAAI;IAClC,kBAAkB;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,SAAS,EAAE,kBAAkB,CAAA;IACtC,sBAAsB;IACtB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACnC,GAAG,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACtC;AAED;;;;GAIG;AACH,wBAAsB,UAAU,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,SAAS,UAAU,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,SAAS,cAAc,CAAC,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAiB5K"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模块图内核:分层策略 + 声明序 + 依赖解析 + 生命周期顺序。
|
|
3
|
+
*
|
|
4
|
+
* 它只回答静态问题——「哪些层、层能引用哪些层、模块之间是否合法、按什么顺序构造与释放」——
|
|
5
|
+
* 不持有任何运行时(没有 logger、没有异步、没有构造 / 初始化回调),因此 server / web /
|
|
6
|
+
* worker 都能用同一份口径,也都能在单测里直接断言。
|
|
7
|
+
*
|
|
8
|
+
* 构造与释放的**执行**留给使用方:各运行时的阶段划分不同(server 要在 store 与 config
|
|
9
|
+
* 之间装 HTTP 契约、config 要按固定节拍独自刷新),内核只给出稳定的顺序。
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 声明一份分层策略:**声明序就是依赖序**。
|
|
13
|
+
*
|
|
14
|
+
* - `uses` 只能引用同一份策略里的层;
|
|
15
|
+
* - 除自身(同层引用)外,被引用的层必须声明在本层之前——否则构造序与依赖序相反,
|
|
16
|
+
* 层间成环在定义期就不可能存在;
|
|
17
|
+
* - `inputs` 原样保留,交给使用方在构造 ctx 时消费。
|
|
18
|
+
*
|
|
19
|
+
* 层名与层数完全由使用方决定;内核不预置任何具体分层。
|
|
20
|
+
*/
|
|
21
|
+
export function defineLayerPolicy(policy) {
|
|
22
|
+
const order = Object.keys(policy);
|
|
23
|
+
const position = new Map(order.map((layer, index) => [layer, index]));
|
|
24
|
+
for (const [layer, rule] of Object.entries(policy)) {
|
|
25
|
+
for (const dependency of rule.uses) {
|
|
26
|
+
const dependencyPosition = position.get(dependency);
|
|
27
|
+
if (dependencyPosition === undefined) {
|
|
28
|
+
throw new Error(`[module] layer "${layer}" 引用了未知 layer "${dependency}"`);
|
|
29
|
+
}
|
|
30
|
+
if (dependency !== layer && dependencyPosition > position.get(layer)) {
|
|
31
|
+
throw new Error(`[module] layer "${layer}" 引用了 "${dependency}",但后者声明在它之后:层顺序即依赖顺序`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return policy;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 校验模块图并给出构造 / 释放顺序。
|
|
39
|
+
*
|
|
40
|
+
* 不变式(每条都在这里兜底,不依赖调用方先校验):
|
|
41
|
+
* 1. 每个节点的 `layer` 都在策略里;
|
|
42
|
+
* 2. 同层内 `name` 唯一(跨层可以同名:视图按层聚合);
|
|
43
|
+
* 3. `uses` 的目标必须能解析到本次装配里的节点;
|
|
44
|
+
* 4. 目标层在本层 `uses` 里;
|
|
45
|
+
* 5. 同层只能引用**声明更早**的节点;
|
|
46
|
+
* 6. 依赖无环(策略经 `defineLayerPolicy` 校验后已不可能,这里仍兜底裸策略)。
|
|
47
|
+
*
|
|
48
|
+
* `resolve` 是**依赖策略**:引用是什么形状、能力如何落到接管单元上,都由使用方决定;
|
|
49
|
+
* 内核只消费解析结果,并在拿不到节点时按「不在本次装配里」报错。
|
|
50
|
+
*/
|
|
51
|
+
export function planModules(policy, modules, resolve) {
|
|
52
|
+
const order = Object.keys(policy);
|
|
53
|
+
const policyByLayer = policy;
|
|
54
|
+
const layers = {};
|
|
55
|
+
for (const layer of order) {
|
|
56
|
+
layers[layer] = [];
|
|
57
|
+
}
|
|
58
|
+
const declared = new Set();
|
|
59
|
+
for (const module of modules) {
|
|
60
|
+
if (!(module.layer in policyByLayer)) {
|
|
61
|
+
throw new Error(`[module] "${module.name}" 使用了未知的层 "${module.layer}"`);
|
|
62
|
+
}
|
|
63
|
+
const key = `${module.layer}:${module.name}`;
|
|
64
|
+
if (declared.has(key)) {
|
|
65
|
+
throw new Error(`[module] ${module.layer} 层里有重复的 name "${module.name}":视图按 name 聚合,重名等于静默覆盖`);
|
|
66
|
+
}
|
|
67
|
+
declared.add(key);
|
|
68
|
+
layers[module.layer].push(module);
|
|
69
|
+
}
|
|
70
|
+
const position = new Map();
|
|
71
|
+
for (const layer of order) {
|
|
72
|
+
layers[layer].forEach((node, index) => {
|
|
73
|
+
position.set(node, index);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const edges = new Map();
|
|
77
|
+
for (const layer of order) {
|
|
78
|
+
const allowed = policyByLayer[layer].uses;
|
|
79
|
+
for (const node of layers[layer]) {
|
|
80
|
+
const targets = [];
|
|
81
|
+
for (const reference of node.uses) {
|
|
82
|
+
const target = resolve(reference, node);
|
|
83
|
+
// resolver 也可能返回本次装配外的节点(例如按名字查到了别处的图):同样按「不在图里」处理
|
|
84
|
+
if (target === undefined || !position.has(target)) {
|
|
85
|
+
throw new Error(`[module] ${layer} "${node.name}" 引用的 "${reference.name}" 不在本次装配里`);
|
|
86
|
+
}
|
|
87
|
+
if (!allowed.includes(target.layer)) {
|
|
88
|
+
throw new Error(`[module] ${layer} "${node.name}" 不能引用 ${target.layer} "${target.name}":${layer} 层允许的层是 [${allowed.join(', ') || '—'}]`);
|
|
89
|
+
}
|
|
90
|
+
if (target.layer === layer && position.get(target) >= position.get(node)) {
|
|
91
|
+
throw new Error(`[module] ${layer} "${node.name}" 引用了同层的 "${target.name}",但后者声明在它之后:同层只能引用更早声明的模块`);
|
|
92
|
+
}
|
|
93
|
+
targets.push(target);
|
|
94
|
+
}
|
|
95
|
+
edges.set(node, targets);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
assertAcyclic(layers, order, edges);
|
|
99
|
+
return { order, layers, dependencies: edges };
|
|
100
|
+
}
|
|
101
|
+
/** 未解析的 `uses` 目标不会进图(上面已拦),这里只沿已解析的边走 */
|
|
102
|
+
function assertAcyclic(layers, order, edges) {
|
|
103
|
+
const state = new Map();
|
|
104
|
+
const stack = [];
|
|
105
|
+
const visit = (node) => {
|
|
106
|
+
const current = state.get(node);
|
|
107
|
+
if (current === 'done') {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (current === 'visiting') {
|
|
111
|
+
const start = stack.indexOf(node);
|
|
112
|
+
const path = [...stack.slice(start), node].map((entry) => entry.name);
|
|
113
|
+
throw new Error(`[module] 依赖存在环:${path.join(' -> ')}`);
|
|
114
|
+
}
|
|
115
|
+
state.set(node, 'visiting');
|
|
116
|
+
stack.push(node);
|
|
117
|
+
for (const target of edges.get(node) ?? []) {
|
|
118
|
+
visit(target);
|
|
119
|
+
}
|
|
120
|
+
stack.pop();
|
|
121
|
+
state.set(node, 'done');
|
|
122
|
+
};
|
|
123
|
+
for (const layer of order) {
|
|
124
|
+
for (const node of layers[layer]) {
|
|
125
|
+
visit(node);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 按声明序**串行**驱动阶段;单个 `run` 失败即停(要不要吞错由 `run` 自己决定)。
|
|
131
|
+
*
|
|
132
|
+
* 内核不缓存任何运行期状态:阶段拿到的是节点本身,取值 / 记录由使用方完成。
|
|
133
|
+
*/
|
|
134
|
+
export async function runModules(plan, phases) {
|
|
135
|
+
for (const phase of phases) {
|
|
136
|
+
const layers = phase.layers ?? plan.order;
|
|
137
|
+
const orderedLayers = phase.direction === 'setup' ? layers : [...layers].reverse();
|
|
138
|
+
for (const layer of orderedLayers) {
|
|
139
|
+
if (!plan.order.includes(layer)) {
|
|
140
|
+
throw new Error(`[module] 阶段 "${phase.name}" 引用了未知 layer "${layer}"`);
|
|
141
|
+
}
|
|
142
|
+
const declared = plan.layers[layer];
|
|
143
|
+
const nodes = phase.direction === 'setup' ? declared : [...declared].reverse();
|
|
144
|
+
for (const node of nodes) {
|
|
145
|
+
await phase.run(node);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.test.d.ts","sourceRoot":"","sources":["../../source/module/module.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { defineLayerPolicy, planModules, runModules } from './module.js';
|
|
3
|
+
const policy = defineLayerPolicy({
|
|
4
|
+
store: { uses: [], inputs: ['log', 'env'] },
|
|
5
|
+
config: { uses: ['store'], inputs: ['log', 'env'] },
|
|
6
|
+
client: { uses: ['store', 'config'], inputs: ['log'] },
|
|
7
|
+
service: { uses: ['store', 'config', 'client', 'service'], inputs: ['log'] },
|
|
8
|
+
});
|
|
9
|
+
function node(layer, name, uses = []) {
|
|
10
|
+
return { layer, name, uses };
|
|
11
|
+
}
|
|
12
|
+
/** 测试用的依赖策略:按对象身份解析(生产里会按名字 / 能力解析) */
|
|
13
|
+
function plan(target, modules) {
|
|
14
|
+
return planModules(target, modules, (reference) => modules.find((candidate) => candidate === reference));
|
|
15
|
+
}
|
|
16
|
+
describe('defineLayerPolicy', () => {
|
|
17
|
+
it('保留声明序', () => {
|
|
18
|
+
expect(Object.keys(policy)).toEqual(['store', 'config', 'client', 'service']);
|
|
19
|
+
});
|
|
20
|
+
it('保留每层的环境输入', () => {
|
|
21
|
+
expect(policy.store.inputs).toEqual(['log', 'env']);
|
|
22
|
+
expect(policy.client.inputs).toEqual(['log']);
|
|
23
|
+
});
|
|
24
|
+
it('拒绝引用未声明的层', () => {
|
|
25
|
+
expect(() => defineLayerPolicy({ store: { uses: ['db'], inputs: [] } })).toThrow('引用了未知 layer "db"');
|
|
26
|
+
});
|
|
27
|
+
it('拒绝引用声明在后面的层', () => {
|
|
28
|
+
expect(() => defineLayerPolicy({ api: { uses: ['store'], inputs: [] }, store: { uses: [], inputs: [] } })).toThrow('层顺序即依赖顺序');
|
|
29
|
+
});
|
|
30
|
+
it('允许引用自己(同层引用)', () => {
|
|
31
|
+
expect(() => defineLayerPolicy({ service: { uses: ['service'], inputs: [] }, api: { uses: ['service'], inputs: [] } })).not.toThrow();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe('planModules', () => {
|
|
35
|
+
it('按层与声明序给出构造顺序,释放序完全逆序', () => {
|
|
36
|
+
const database = node('store', 'database');
|
|
37
|
+
const redis = node('store', 'redis');
|
|
38
|
+
const settings = node('config', 'settings', [database]);
|
|
39
|
+
const account = node('client', 'account', [database, settings]);
|
|
40
|
+
const billing = node('service', 'billing', [database, settings, account]);
|
|
41
|
+
const report = node('service', 'report', [billing]);
|
|
42
|
+
const result = plan(policy, [database, redis, settings, account, billing, report]);
|
|
43
|
+
expect(result.order).toEqual(['store', 'config', 'client', 'service']);
|
|
44
|
+
expect(result.layers.store.map((entry) => entry.name)).toEqual(['database', 'redis']);
|
|
45
|
+
expect(result.layers.service.map((entry) => entry.name)).toEqual(['billing', 'report']);
|
|
46
|
+
expect(result.dependencies.get(billing)).toEqual([database, settings, account]);
|
|
47
|
+
expect(result.dependencies.get(database)).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
it('跨层可以同名,同层不行', () => {
|
|
50
|
+
expect(() => plan(policy, [node('store', 'A'), node('service', 'A')])).not.toThrow();
|
|
51
|
+
expect(() => plan(policy, [node('service', 'A'), node('service', 'A')])).toThrow('重复的 name "A"');
|
|
52
|
+
});
|
|
53
|
+
it('拒绝未知层', () => {
|
|
54
|
+
expect(() => plan(policy, [node('cache', 'A')])).toThrow('未知的层 "cache"');
|
|
55
|
+
});
|
|
56
|
+
it('拒绝引用不在本次装配里的模块', () => {
|
|
57
|
+
const database = node('store', 'database');
|
|
58
|
+
const missing = node('store', 'missing');
|
|
59
|
+
const service = node('service', 'billing', [missing]);
|
|
60
|
+
expect(() => plan(policy, [database, service])).toThrow('引用的 "missing" 不在本次装配里');
|
|
61
|
+
});
|
|
62
|
+
it('resolver 返回本次装配之外的节点时同样报错', () => {
|
|
63
|
+
const outside = node('store', 'outside');
|
|
64
|
+
const service = node('service', 'billing', [outside]);
|
|
65
|
+
expect(() => planModules(policy, [service], () => outside)).toThrow('引用的 "outside" 不在本次装配里');
|
|
66
|
+
});
|
|
67
|
+
it('拒绝越层引用', () => {
|
|
68
|
+
const settings = node('config', 'settings');
|
|
69
|
+
const service = node('service', 'billing', [settings]);
|
|
70
|
+
const store = node('store', 'database', [settings]);
|
|
71
|
+
expect(() => plan(policy, [settings, service, store])).toThrow('不能引用 config "settings"');
|
|
72
|
+
});
|
|
73
|
+
it('同层只能引用更早声明的模块', () => {
|
|
74
|
+
const first = node('service', 'first');
|
|
75
|
+
const second = node('service', 'second', [first]);
|
|
76
|
+
expect(() => plan(policy, [first, second])).not.toThrow();
|
|
77
|
+
expect(() => plan(policy, [second, first])).toThrow('同层只能引用更早声明的模块');
|
|
78
|
+
});
|
|
79
|
+
it('策略未经验证时,环仍会被拒绝', () => {
|
|
80
|
+
// 裸策略(未经 defineLayerPolicy):层顺序校验让合法的策略不可能成环,这里兜底
|
|
81
|
+
const cyclicPolicy = {
|
|
82
|
+
compute: { uses: ['compute', 'storage'], inputs: [] },
|
|
83
|
+
storage: { uses: ['compute'], inputs: [] },
|
|
84
|
+
};
|
|
85
|
+
const compute = node('compute', 'compute');
|
|
86
|
+
const storage = node('storage', 'storage', [compute]);
|
|
87
|
+
compute.uses.push(storage);
|
|
88
|
+
expect(() => plan(cyclicPolicy, [compute, storage])).toThrow('依赖存在环');
|
|
89
|
+
});
|
|
90
|
+
it('uses 指向另一个层的同名节点时按层解析', () => {
|
|
91
|
+
const storeA = node('store', 'A');
|
|
92
|
+
const serviceA = node('service', 'A', [storeA]);
|
|
93
|
+
const result = plan(policy, [storeA, serviceA]);
|
|
94
|
+
expect(result.layers.store[0].name).toBe('A');
|
|
95
|
+
expect(result.layers.service[0].uses[0]).toBe(storeA);
|
|
96
|
+
});
|
|
97
|
+
it('引用只要求能读出 name,其余语义交给 resolver', () => {
|
|
98
|
+
const database = { layer: 'store', name: 'database', uses: [] };
|
|
99
|
+
// 能力引用的 name 与目标 name 不同:能落到哪由 resolver 决定
|
|
100
|
+
const billing = { layer: 'service', name: 'billing', uses: [{ name: 'db', capability: true }] };
|
|
101
|
+
const result = planModules(policy, [database, billing], (reference) => (reference.capability ? database : undefined));
|
|
102
|
+
expect(result.layers.service[0].uses[0].name).toBe('db');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
describe('runModules', () => {
|
|
106
|
+
const database = node('store', 'database');
|
|
107
|
+
const redis = node('store', 'redis');
|
|
108
|
+
const settings = node('config', 'settings', [database]);
|
|
109
|
+
const billing = node('service', 'billing', [database, settings]);
|
|
110
|
+
const modulePlan = plan(policy, [database, redis, settings, billing]);
|
|
111
|
+
const collect = async (direction, layers) => {
|
|
112
|
+
const seen = [];
|
|
113
|
+
await runModules(modulePlan, [
|
|
114
|
+
{
|
|
115
|
+
name: direction,
|
|
116
|
+
direction,
|
|
117
|
+
...(layers === undefined ? {} : { layers }),
|
|
118
|
+
run: (entry) => {
|
|
119
|
+
seen.push(`${entry.layer}:${entry.name}`);
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
return seen;
|
|
124
|
+
};
|
|
125
|
+
it('setup 按层序 + 声明序,teardown 层与层内都逆序', async () => {
|
|
126
|
+
expect(await collect('setup')).toEqual(['store:database', 'store:redis', 'config:settings', 'service:billing']);
|
|
127
|
+
expect(await collect('teardown')).toEqual(['service:billing', 'config:settings', 'store:redis', 'store:database']);
|
|
128
|
+
});
|
|
129
|
+
it('layers 收窄阶段范围,teardown 时列表本身也逆序', async () => {
|
|
130
|
+
expect(await collect('setup', ['store', 'config'])).toEqual(['store:database', 'store:redis', 'config:settings']);
|
|
131
|
+
expect(await collect('teardown', ['store', 'config'])).toEqual(['config:settings', 'store:redis', 'store:database']);
|
|
132
|
+
});
|
|
133
|
+
it('拒绝未知 layer', async () => {
|
|
134
|
+
await expect(runModules(modulePlan, [{ name: 'bad', direction: 'setup', layers: ['cache'], run: () => { } }])).rejects.toThrow('未知 layer "cache"');
|
|
135
|
+
});
|
|
136
|
+
it('阶段按数组序串行,异步 run 会被等待', async () => {
|
|
137
|
+
const seen = [];
|
|
138
|
+
const single = plan(policy, [node('store', 'database')]);
|
|
139
|
+
await runModules(single, [
|
|
140
|
+
{
|
|
141
|
+
name: 'first',
|
|
142
|
+
direction: 'setup',
|
|
143
|
+
run: async () => {
|
|
144
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
145
|
+
seen.push('first');
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
{ name: 'second', direction: 'setup', run: () => { seen.push('second'); } },
|
|
149
|
+
]);
|
|
150
|
+
expect(seen).toEqual(['first', 'second']);
|
|
151
|
+
});
|
|
152
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taicode/common-base",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.24",
|
|
4
4
|
"author": "Alain",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"description": "",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"eslint-import-resolver-typescript": "^4.4.4",
|
|
31
31
|
"eslint-plugin-import": "^2.32.0",
|
|
32
32
|
"globals": "^16.5.0",
|
|
33
|
-
"mobx": "^6.15.0",
|
|
34
33
|
"typescript-eslint": "^8.56.1",
|
|
35
34
|
"vitest": "^3.2.4"
|
|
36
35
|
},
|