Commands and selectors
Commands are actions that plugins provide, and selectors are how plugins share their data. You can call any command or selector, and decorate it to change how it behaves.
getCommands
Section titled “getCommands”Returns every available command.
getCommands(): CommandsExample
const { buyItem } = getCommands();const { didBuy } = await buyItem({ itemId: "brass-key", price: 5 });getSelectors
Section titled “getSelectors”Returns every available selector.
getSelectors(): SelectorsSelectors work well with onChange.
Example
const { getInventory } = getSelectors();onChange(getInventory, ({ inventory }) => console.log(`Now carrying ${inventory.length} items`),);decorateCommand
Section titled “decorateCommand”Wraps a command with your own function.
decorateCommand<TName extends keyof Commands>( commandName: TName, fn: (context: Parameters<Commands[TName]>[0], next: Commands[TName]) => ReturnType<Commands[TName]>, priority?: number): void| Parameter | Description |
|---|---|
commandName | The command to wrap. |
fn | Receives the command’s arguments, and next, which calls the implementation beneath yours. |
priority | Decorators with higher numbers run first. Defaults to 0. Decorators with the same priority run in the order their plugins were loaded. |
| Priority | Runs |
|---|---|
Above 0 | Before the default behavior |
Below 0 | After the default behavior |
Number.MAX_VALUE | First |
-Number.MAX_VALUE | Last |
Always call next, unless you mean to replace the command’s behavior completely.
Example
decorateCommand("init", (context, next) => { console.log("Before init"); const result = next(context); console.log("After init"); return result;});decorateSelector
Section titled “decorateSelector”Wraps a selector with your own function. Works the same way as decorateCommand.
decorateSelector<TName extends keyof Selectors>( name: TName, fn: (context: Parameters<Selectors[TName]>[0], next: Selectors[TName]) => ReturnType<Selectors[TName]>, priority?: number): voidExample
decorateSelector("getInventory", (context, next) => { const { inventory } = next(context); return { inventory: [...inventory].sort() };});