15.1 装配资产管线
前 14 章实现了资产管线的一块块子系统:资产数据库、导入器注册表、依赖图、派生缓存、构建清单。子系统单独都能跑,但资产管线是它们组装在一起的样子——这一步叫装配。
AssetPipeline 把装配规则落成可测试的约束:
boot():按注册序启动全部模块,装配完成。shutdown():逆序停止——启动的先启,停止的后止,生命周期成对。import(path, type):写入资产目录并广播asset.imported——导入路径与事件路径在装配处交汇。importWithDeps(path, type, deps):依赖未就绪则拒绝——装配处校验依赖先于被依赖。- 未装配拒绝导入:
boot()之前import返回 false,不让系统在未就绪时被调用。
装配的正确性不是「代码能跑」,而是三条可断言的性质:子系统互相可达、生命周期对称、未就绪拒绝服务。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 资产数据库 | 2.1 资产数据库扫描 | 数据库是模块之一;本节装配它 |
| 导入器注册表 | 3.1 导入器注册与分发 | 注册表是模块之一;本节装配它 |
| 依赖图 | 9.1 依赖图与传递依赖 | 依赖图是模块;本节用 importWithDeps 校验依赖 |
| 端到端闭环 | 15.2 端到端构建导出 | 装配出可用的管线;闭环把导入到交付串完整 |
生命周期成对:boot 序 vs shutdown 逆序
装配最容易犯的错是不对称:启动按 A→B→C,停止也按 A→B→C——B 停了还依赖 C,C 停了还依赖 A。正确规则是停止逆序:shutdown:c → shutdown:b → shutdown:a。
测试第 2 条用日志钉死这个对称性:boot 记录 boot:db, boot:registry, boot:graph,shutdown 记录 shutdown:graph, shutdown:registry, shutdown:db。这不是教条——依赖方向决定了顺序:谁依赖谁,谁先启谁后停。装配序是依赖拓扑的运行时投影。
未就绪拒绝服务:装配的边界
import 在未装配时返回 false,不是防御式废话,是边界声明:装配完成之前,子系统不应被外部调用。这个「开关」让装配状态成为系统的一个显式属性——isBooted() 可查、可断言。
实践中这类边界拦截两类 bug:启动脚本漏了 boot()(系统悄悄半就绪)和 shutdown 后残留定时器还在调 import(系统假装还活着)。边界让「半就绪」从隐蔽故障变成立即失败。
对标导入插件注册与装配
Godot 的编辑器插件通过 add_import_plugin() 和 remove_import_plugin() 注册及移除导入器。helio 的装配示例按预先给定的顺序启动组件,正常关闭时逆序停止。组件依赖解析和启动失败后的清理由宿主负责。 Godot EditorPlugin
可运行产物:6 条用例
下面 src/asset-pipeline.ts 层级 L1,完整可编译,命令 node --test test/15-1-asset-pipeline.test.ts:
// file: src/asset-pipeline.ts
// tier: L1
// verify: node --test test/15-1-asset-pipeline.test.ts
//
// 资产管线(第十五章):装配——一条可用的资产管线。
// 把前面章节的子系统(资产数据库、导入器、依赖图、构建清单)装配成整体:
// boot() 按注册序启动模块、shutdown() 逆序停止、import() 写入资产目录并
// 广播 asset.imported。装配的正确性是「子系统互相可达 + 生命周期成对 +
// 未就绪拒绝服务」:boot 之前 import 返回 false,半就绪立即失败。
export interface PipelineModule {
name: string;
boot(): void;
shutdown(): void;
}
export class PipelineBus {
#listeners = new Map<string, Set<(payload: unknown) => void>>();
on(event: string, fn: (payload: unknown) => void): void {
if (!this.#listeners.has(event)) this.#listeners.set(event, new Set());
this.#listeners.get(event)?.add(fn);
}
emit(event: string, payload: unknown): void {
for (const fn of this.#listeners.get(event) ?? []) fn(payload);
}
listenerCount(event: string): number {
return this.#listeners.get(event)?.size ?? 0;
}
}
export class AssetPipeline {
#modules: PipelineModule[] = [];
#booted = false;
#bus = new PipelineBus();
#assets = new Map<string, string>(); // path → type
register(m: PipelineModule): void {
this.#modules.push(m);
}
/** 按注册序启动全部模块,装配完成。 */
boot(): void {
for (const m of this.#modules) m.boot();
this.#booted = true;
}
/** 逆序停止全部模块。 */
shutdown(): void {
for (const m of [...this.#modules].reverse()) m.shutdown();
this.#booted = false;
}
isBooted(): boolean {
return this.#booted;
}
bus(): PipelineBus {
return this.#bus;
}
/** 资产目录:path → type。 */
assetTypes(): string[] {
return [...this.#assets.keys()];
}
typeOf(path: string): string | undefined {
return this.#assets.get(path);
}
/** 导入资产:写入目录并广播 asset.imported;未装配时拒绝。 */
import(path: string, type: string): boolean {
if (!this.#booted) return false;
this.#assets.set(path, type);
this.#bus.emit('asset.imported', { path, type });
return true;
}
/** 带依赖导入:依赖未就绪则拒绝——装配处校验依赖先于被依赖。 */
importWithDeps(path: string, type: string, deps: string[]): boolean {
if (!this.#booted) return false;
for (const d of deps) {
if (!this.#assets.has(d)) return false;
}
this.import(path, type);
return true;
}
}
import 是装配的交汇点:写资产目录(数据库的能力)+ 广播 asset.imported(事件总线的能力)+ 未装配拒绝(装配状态的约束)——三条能力在同一条路径上被验证。boot 与 shutdown 用同一数组的正向与逆序遍历,对称一目了然。importWithDeps 把依赖校验也收进装配:场景引用模型、模型引用纹理——纹理没导入,场景导入被拒绝。
可运行产物:6 条用例
下面是 test/15-1-asset-pipeline.test.ts 层级 L1,命令 node --test test/15-1-asset-pipeline.test.ts:
// file: test/15-1-asset-pipeline.test.ts
// tier: L1
// verify: node --test test/15-1-asset-pipeline.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { AssetPipeline, PipelineBus } from '../src/asset-pipeline.ts';
test('装配后 isBooted 为 true,子系统可达', () => {
const p = new AssetPipeline();
p.boot();
assert.equal(p.isBooted(), true);
assert.ok(p.bus() instanceof PipelineBus);
});
test('boot 按注册序启动、shutdown 逆序停止', () => {
const p = new AssetPipeline();
const log: string[] = [];
for (const name of ['db', 'registry', 'graph']) {
p.register({
name,
boot: () => log.push(`boot:${name}`),
shutdown: () => log.push(`shutdown:${name}`),
});
}
p.boot();
p.shutdown();
assert.deepEqual(log, [
'boot:db',
'boot:registry',
'boot:graph',
'shutdown:graph',
'shutdown:registry',
'shutdown:db',
]);
});
test('import 写入目录并广播 asset.imported', () => {
const p = new AssetPipeline();
p.boot();
const events: unknown[] = [];
p.bus().on('asset.imported', (e) => events.push(e));
assert.equal(p.import('tex', 'texture'), true);
assert.deepEqual(p.assetTypes(), ['tex']);
assert.equal(p.typeOf('tex'), 'texture');
assert.equal(events.length, 1);
});
test('importWithDeps:依赖就绪导入、未就绪拒绝', () => {
const p = new AssetPipeline();
p.boot();
p.import('tex', 'texture');
assert.equal(p.importWithDeps('model', 'model', ['tex']), true);
assert.equal(p.importWithDeps('scene', 'scene', ['missing']), false);
assert.deepEqual(p.assetTypes(), ['tex', 'model']);
});
test('未装配时 import 拒绝', () => {
const p = new AssetPipeline();
assert.equal(p.import('tex', 'texture'), false);
assert.equal(p.importWithDeps('scene', 'scene', []), false);
assert.deepEqual(p.assetTypes(), []);
});
test('同一事件多个监听器都收到', () => {
const p = new AssetPipeline();
p.boot();
let a = 0;
let b = 0;
p.bus().on('asset.imported', () => { a += 1; });
p.bus().on('asset.imported', () => { b += 1; });
p.import('tex', 'texture');
assert.equal(a, 1);
assert.equal(b, 1);
assert.equal(p.bus().listenerCount('asset.imported'), 2);
});
本机实测(Node v26.5.0,node --test test/15-1-asset-pipeline.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。
第二条验收生命周期对称:boot 正序、shutdown 逆序,日志逐项精确。第四条验收依赖校验:tex 就绪则 model 导入成功、missing 未就绪则 scene 被拒。第五条验收边界:未 boot 的 import 返回 false、目录为空——半就绪状态立即失败。
小结
- 装配 = 子系统组装成整体,正确性是「互相可达 + 生命周期成对」。
- boot 按注册序、shutdown 逆序:装配序是依赖拓扑的运行时投影。
importWithDeps让依赖校验收进装配:依赖先于被依赖。- 未装配拒绝导入:边界让「半就绪」从隐蔽故障变成立即失败。
- 下一节走出装配,跑端到端:端到端构建导出——从源资产到交付物清单的闭环。
阅读导航:上一节:14.3 流式区块 · 下一节:15.2 端到端构建导出 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。