-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f9ab48
commit fb9f0eb
Showing
4 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
export const executeCommand = (command: string) => { | ||
console.log(`Executing command: ${command}`); | ||
import { command as saveAsCurrentWorkspace } from "./saveAsCurrentWorkspace"; | ||
import { command as addWorkspaceLayer } from "./addWorkspaceLayer"; | ||
import { CommandContext } from "./context"; | ||
const commands = [saveAsCurrentWorkspace, addWorkspaceLayer] as const; | ||
const commandByName = new Map( | ||
commands.map((command) => [command.name, command]) | ||
); | ||
|
||
export type Command = (typeof commands)[number]; | ||
export type CommandId = Command["name"]; | ||
|
||
type MapToExecuteCommand<U> = U extends Command | ||
? Parameters<U["execute"]>[1] extends undefined | ||
? [name: U["name"]] | ||
: [name: U["name"], params: Parameters<U["execute"]>[1]] | ||
: never; | ||
|
||
const createContext = (): CommandContext => ({}); | ||
|
||
export const executeCommand = async ( | ||
...[name, params]: MapToExecuteCommand<Command> | ||
) => { | ||
const context = createContext(); | ||
const command = commandByName.get(name); | ||
|
||
if (!command) { | ||
throw new Error(`Command not found: ${name}`); | ||
} | ||
|
||
return command.execute(context, params); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { CommandContext } from "./context"; | ||
|
||
export const command = { | ||
name: "saveAsCurrentWorkspace", | ||
description: "Save the current workspace as a new file", | ||
execute: async ( | ||
context: CommandContext, | ||
payload: { | ||
workspaceId: string; | ||
layerName: string; | ||
} | ||
) => { | ||
console.log("Saving sheet", context, payload); | ||
}, | ||
} as const; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters