15.1 装配核心框架
前 14 章实现了编辑器的一块块子系统:模块注册、命令路由、事件总线、文档模型、撤销栈。子系统单独都能跑,但编辑器是它们组装在一起的样子——这一步叫装配。
EditorAssembly 把装配规则落成可测试的约束:
boot():按注册序启动全部模块,装配完成。shutdown():逆序停止——启动的先启,停止的后止,生命周期成对。applyEdit():改文档并广播document.changed——命令路径与事件路径在装配处交汇。- 未装配拒绝编辑:
boot()之前applyEdit返回 false,不让系统在未就绪时被调用。
装配的正确性不是「代码能跑」,而是三条可断言的性质:子系统互相可达、生命周期对称、未就绪拒绝服务。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 模块系统 | 2.1 模块注册与依赖 | 注册与依赖拓扑;本节做启动/停止的装配序 |
| 事件总线 | 5.1 事件总线与订阅 | 订阅/广播机制;本节把它接入文档变更 |
| 文档模型 | 7.1 场景文档操作 API | 文档增删改;本节把编辑路径与事件路径接通 |
| 场景编辑闭环 | 15.2 首个场景编辑闭环 | 装配出可用的框架;闭环把编辑到视图串完整 |
生命周期成对:boot 序 vs shutdown 逆序
装配最容易犯的错是不对称:启动按 A→B→C,停止也按 A→B→C——B 停了还依赖 C,C 停了还依赖 A。正确规则是停止逆序:shutdown:c → shutdown:b → shutdown:a。
测试第 2 条用日志钉死这个对称性:boot 记录 boot:a, boot:b, boot:c,shutdown 记录 shutdown:c, shutdown:b, shutdown:a。这不是教条——依赖方向决定了顺序:谁依赖谁,谁先启谁后停。装配序是依赖拓扑的运行时投影。
未就绪拒绝服务:装配的边界
applyEdit 在未装配时返回 false,不是防御式废话,是边界声明:装配完成之前,子系统不应被外部调用。这个「开关」让装配状态成为系统的一个显式属性——isBooted() 可查、可断言。
实践中这类边界拦截两类 bug:启动脚本漏了 boot()(系统悄悄半就绪)和 shutdown 后残留定时器还在调 applyEdit(系统假装还活着)。边界让「半就绪」从隐蔽故障变成立即失败。
对标模块装配
Unreal 区分构建依赖与运行时加载阶段,同一加载阶段内的模块顺序没有确定性保证。helio 本节使用调用方提供的注册顺序启动,正常停止时逆序关闭。此处用简化模块说明顺序契约,第 2 章已有依赖拓扑示例。 Unreal Engine Modules
可运行产物:6 条用例
下面 src/editor-assembly.ts 层级 L1,完整可编译,命令 node --test test/15-1-assemble-core-framework.test.ts:
// file: src/editor-assembly.ts
// tier: L1
// verify: node --test test/15-1-assemble-core-framework.test.ts
//
// 编辑器逻辑测试与自动化基础(第十四章)后的装配收口(第十五章):
// 装配核心框架。把前面章节的子系统(模块、事件总线、文档存储)装配成
// 一个整体:boot() 按注册序启动模块并启用装配,shutdown() 逆序停止,
// applyEdit() 改文档并广播 document.changed。装配的正确性是「子系统
// 互相可达 + 生命周期成对」:boot 的顺序与 shutdown 的逆序对称。
export interface BootableModule {
name: string;
boot(): void;
shutdown(): void;
}
export class EventBus {
#listeners = new Map<string, Set<() => void>>();
on(event: string, fn: () => void): void {
if (!this.#listeners.has(event)) this.#listeners.set(event, new Set());
this.#listeners.get(event)?.add(fn);
}
emit(event: string): void {
for (const fn of this.#listeners.get(event) ?? []) fn();
}
listenerCount(event: string): number {
return this.#listeners.get(event)?.size ?? 0;
}
}
export class EditorAssembly {
#modules: BootableModule[] = [];
#booted = false;
#bus = new EventBus();
#docs = new Map<string, Record<string, number>>();
register(m: BootableModule): 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(): EventBus { return this.#bus; }
docIds(): string[] { return [...this.#docs.keys()]; }
/** 改文档并广播 document.changed;未装配时拒绝。 */
applyEdit(docId: string, patch: Record<string, number>): boolean {
if (!this.#booted) return false;
const doc = this.#docs.get(docId) ?? {};
this.#docs.set(docId, { ...doc, ...patch });
this.#bus.emit('document.changed');
return true;
}
}
applyEdit 是装配的交汇点:改文档(文档模型的能力)+ 广播 document.changed(事件总线的能力)+ 未装配拒绝(装配状态的约束)——三条能力在同一条路径上被验证。boot 与 shutdown 用同一数组的正向与逆序遍历,对称一目了然。
可运行产物:6 条用例
下面是 test/15-1-assemble-core-framework.test.ts 层级 L1,命令 node --test test/15-1-assemble-core-framework.test.ts:
// file: test/15-1-assemble-core-framework.test.ts
// tier: L1
// verify: node --test test/15-1-assemble-core-framework.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { EditorAssembly, EventBus } from '../src/editor-assembly.ts';
test('装配后 isBooted 为 true,子系统可达', () => {
const asm = new EditorAssembly();
asm.boot();
assert.equal(asm.isBooted(), true);
assert.ok(asm.bus() instanceof EventBus);
});
test('boot 按注册序启动、shutdown 逆序停止', () => {
const asm = new EditorAssembly();
const log: string[] = [];
for (const name of ['a', 'b', 'c']) {
asm.register({ name, boot: () => log.push(`boot:${name}`), shutdown: () => log.push(`shutdown:${name}`) });
}
asm.boot();
asm.shutdown();
assert.deepEqual(log, ['boot:a', 'boot:b', 'boot:c', 'shutdown:c', 'shutdown:b', 'shutdown:a']);
});
test('事件总线订阅与广播', () => {
const asm = new EditorAssembly();
asm.boot();
let count = 0;
asm.bus().on('document.changed', () => { count += 1; });
asm.bus().emit('document.changed');
asm.bus().emit('document.changed');
assert.equal(count, 2);
});
test('applyEdit 改文档并广播 document.changed', () => {
const asm = new EditorAssembly();
asm.boot();
let count = 0;
asm.bus().on('document.changed', () => { count += 1; });
assert.equal(asm.applyEdit('level-1', { hp: 80 }), true);
assert.equal(asm.applyEdit('level-1', { gold: 5 }), true);
assert.deepEqual(asm.docIds(), ['level-1']);
assert.equal(count, 2);
});
test('未装配时 applyEdit 拒绝', () => {
const asm = new EditorAssembly(); // 未 boot
assert.equal(asm.applyEdit('level-1', { hp: 80 }), false);
assert.deepEqual(asm.docIds(), []);
});
test('同一事件多个监听器都收到', () => {
const asm = new EditorAssembly();
asm.boot();
let a = 0;
let b = 0;
asm.bus().on('document.changed', () => { a += 1; });
asm.bus().on('document.changed', () => { b += 1; });
asm.applyEdit('level-1', { hp: 1 });
assert.equal(a, 1);
assert.equal(b, 1);
assert.equal(asm.bus().listenerCount('document.changed'), 2);
});
本机实测(Node v26.5.0,node --test test/15-1-assemble-core-framework.test.ts):tests 6、pass 6、fail 0,耗时约 36 ms。
第二条验收生命周期对称:boot 正序、shutdown 逆序,日志逐项精确。第四条验收交汇:applyEdit 一次调用同时改文档、发事件。第五条验收边界:未 boot 的 applyEdit 返回 false、文档为空——半就绪状态立即失败。
小结
- 装配 = 子系统组装成整体,正确性是「互相可达 + 生命周期成对」。
- boot 按注册序、shutdown 逆序:装配序是依赖拓扑的运行时投影。
- 未装配拒绝编辑:边界让「半就绪」从隐蔽故障变成立即失败。
- 示例按注册顺序启动,正常停止时逆序关闭。
- 下一节走出装配,跑第一个真闭环:首个场景编辑闭环——编辑操作到视图刷新全链路。
阅读导航:上一节:14.3 编辑器 CI 快照回归 · 下一节:15.2 首个场景编辑闭环 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。