createHistory()
从独立子入口创建异步 undo/redo 栈。
签名
ts
import {
createHistory,
createHistoryPlugin,
} from '@tripo3d/engine/history';
function createHistory(limit?: number): History;默认 limit 为 50,必须是有限正整数。
用法
ts
const history = createHistory(100);
await history.execute({
label: 'move',
redo: () => object.position.x = 1,
undo: () => object.position.x = 0,
});
await history.undo();
await history.redo();execute/undo/redo/transaction 都返回 Promise,同一时刻只允许一个 active operation。
transaction
ts
await history.transaction('batch', async (transaction) => {
await transaction.execute(first);
await transaction.execute(second);
});0 个 child 不提交,1 个直接提交,多于 1 个形成 redo 正序、undo 逆序的 composite。child 必须 await。
任一 child 的准入或执行失败都会使整个 transaction 失败;即使 executor 捕获该错误,transaction() 最终仍会以首个 child 错误 reject,且不提交任何 History entry。此前已成功执行的 child 不会被自动 undo,其业务副作用仍然存在。需要部分成功时应拆成多次顶层 execute();需要原子性时由业务命令负责显式 rollback。
Engine dispose 清栈
ts
const history = createHistory();
const engine = createEngine({
plugins: [createHistoryPlugin(history)],
});Plugin 只在 Engine dispose 时 history.clear();History identity 仍由业务持有。Engine core 没有 engine.history。