feat(commands): type command payload dispatch
This commit is contained in:
@@ -7,7 +7,7 @@ import { viewportPanCommand } from "./viewport";
|
|||||||
describe("command dispatcher", () => {
|
describe("command dispatcher", () => {
|
||||||
test("throws for unknown commands", () => {
|
test("throws for unknown commands", () => {
|
||||||
const store = createAppStore(createInitialAppState("Test"), createCommandRegistry([]));
|
const store = createAppStore(createInitialAppState("Test"), createCommandRegistry([]));
|
||||||
expect(() => store.dispatch("missing", undefined)).toThrow("Unknown command: missing");
|
expect(() => store.dispatch("missing" as never, undefined as never)).toThrow("Unknown command: missing");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("dispatch applies command result to store", () => {
|
test("dispatch applies command result to store", () => {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import type { AppState } from "@editor/state";
|
import type { AppState } from "@editor/state";
|
||||||
import type { CommandContext } from "./command";
|
import type { CommandContext } from "./command";
|
||||||
|
import type { CommandId, CommandPayloads } from "./payloads";
|
||||||
import type { CommandRegistry } from "./registry";
|
import type { CommandRegistry } from "./registry";
|
||||||
|
|
||||||
export type Dispatch = <TPayload>(commandId: string, payload: TPayload) => AppState;
|
export type Dispatch = <TCommandId extends CommandId>(commandId: TCommandId, payload: CommandPayloads[TCommandId]) => AppState;
|
||||||
|
|
||||||
export type CommandDispatcher = {
|
export type CommandDispatcher = {
|
||||||
dispatch: Dispatch;
|
dispatch: Dispatch;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export type { Command, CommandContext } from "./command";
|
export type { Command, CommandContext } from "./command";
|
||||||
export type { CommandDispatcher, Dispatch } from "./dispatcher";
|
export type { CommandDispatcher, Dispatch } from "./dispatcher";
|
||||||
|
export type { CommandId, CommandPayloads } from "./payloads";
|
||||||
export { createCommandDispatcher } from "./dispatcher";
|
export { createCommandDispatcher } from "./dispatcher";
|
||||||
export type { CommandRegistry } from "./registry";
|
export type { CommandRegistry } from "./registry";
|
||||||
export { createCommandRegistry } from "./registry";
|
export { createCommandRegistry } from "./registry";
|
||||||
|
|||||||
16
commands/payloads.ts
Normal file
16
commands/payloads.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import type {
|
||||||
|
ViewportPanPayload,
|
||||||
|
ViewportSetSizePayload,
|
||||||
|
ViewportSetZoomPayload,
|
||||||
|
ViewportZoomAroundPointPayload,
|
||||||
|
} from "./viewport";
|
||||||
|
|
||||||
|
export type CommandPayloads = {
|
||||||
|
"viewport.pan": ViewportPanPayload;
|
||||||
|
"viewport.setZoom": ViewportSetZoomPayload;
|
||||||
|
"viewport.zoomAroundPoint": ViewportZoomAroundPointPayload;
|
||||||
|
"viewport.setSize": ViewportSetSizePayload;
|
||||||
|
"viewport.reset": void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CommandId = keyof CommandPayloads;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Dispatch } from "@commands/dispatcher";
|
import type { Dispatch } from "@commands/dispatcher";
|
||||||
|
import type { CommandId, CommandPayloads } from "@commands/payloads";
|
||||||
|
|
||||||
export type Keybind = string;
|
export type Keybind = string;
|
||||||
|
|
||||||
@@ -13,9 +14,9 @@ export type KeybindEvent = {
|
|||||||
|
|
||||||
export type GlobalKeybindConsumer = (event: KeybindEvent) => boolean;
|
export type GlobalKeybindConsumer = (event: KeybindEvent) => boolean;
|
||||||
|
|
||||||
export type CommandKeybind = {
|
export type CommandKeybind<TCommandId extends CommandId = CommandId> = {
|
||||||
commandId: string;
|
commandId: TCommandId;
|
||||||
payload?: unknown;
|
payload: CommandPayloads[TCommandId];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type KeybindMap = ReadonlyMap<Keybind, CommandKeybind>;
|
export type KeybindMap = ReadonlyMap<Keybind, CommandKeybind>;
|
||||||
|
|||||||
Reference in New Issue
Block a user