feat(commands): add app state dispatcher foundation

This commit is contained in:
syntaxbullet
2026-07-03 09:28:06 +02:00
parent 697d12725d
commit a8dbd26474
11 changed files with 331 additions and 3 deletions

29
commands/dispatcher.ts Normal file
View File

@@ -0,0 +1,29 @@
import type { AppState } from "@editor/state";
import type { CommandContext } from "./command";
import type { CommandRegistry } from "./registry";
export type Dispatch = <TPayload>(commandId: string, payload: TPayload) => AppState;
export type CommandDispatcher = {
dispatch: Dispatch;
};
export function createCommandDispatcher(options: {
registry: CommandRegistry;
getState: () => AppState;
setState: (state: AppState) => void;
}): CommandDispatcher {
return {
dispatch(commandId, payload) {
const command = options.registry.get(commandId);
if (!command) {
throw new Error(`Unknown command: ${commandId}`);
}
const context: CommandContext = { state: options.getState() };
const nextState = command.execute(context, payload);
options.setState(nextState);
return nextState;
},
};
}