refactor: state signal
This commit is contained in:
parent
004d49c36f
commit
74f56b9da4
|
|
@ -10,15 +10,17 @@ import {
|
|||
PromptEvent
|
||||
} from "../utils/command";
|
||||
import {AsyncQueue} from "../utils/async-queue";
|
||||
import {signal, Signal} from "@preact/signals-core";
|
||||
|
||||
export interface IGameContext<TState extends {} = {}> {
|
||||
export interface IGameContext<TState extends Record<string, unknown> = {} > {
|
||||
parts: ReturnType<typeof createEntityCollection<Part>>;
|
||||
regions: ReturnType<typeof createEntityCollection<Region>>;
|
||||
commands: CommandRunnerContextExport<IGameContext<TState>>;
|
||||
prompts: AsyncQueue<PromptEvent>;
|
||||
state: Signal<TState>
|
||||
}
|
||||
|
||||
export function createGameContext<TState extends {} = {}>(
|
||||
export function createGameContext<TState extends Record<string, unknown> = {} >(
|
||||
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
||||
initialState?: TState | (() => TState)
|
||||
): IGameContext<TState> {
|
||||
|
|
@ -32,7 +34,7 @@ export function createGameContext<TState extends {} = {}>(
|
|||
regions,
|
||||
prompts,
|
||||
commands: null!,
|
||||
state,
|
||||
state: signal(state),
|
||||
} as IGameContext<TState>
|
||||
|
||||
ctx.commands = createCommandRunnerContext(commandRegistry, ctx);
|
||||
|
|
@ -45,7 +47,7 @@ export function createGameContext<TState extends {} = {}>(
|
|||
* so that we can do `import * as tictactoe from './tic-tac-toe.ts';\n\n createGameContextFromModule(tictactoe);`
|
||||
* @param module
|
||||
*/
|
||||
export function createGameContextFromModule<TState extends {} = {}>(
|
||||
export function createGameContextFromModule<TState extends Record<string, unknown> = {} >(
|
||||
module: {
|
||||
registry: CommandRegistry<IGameContext<TState>>,
|
||||
createInitialState: () => TState
|
||||
|
|
@ -54,7 +56,7 @@ export function createGameContextFromModule<TState extends {} = {}>(
|
|||
return createGameContext(module.registry, module.createInitialState);
|
||||
}
|
||||
|
||||
export function createGameCommand<TState extends {} = {}, TResult = unknown>(
|
||||
export function createGameCommand<TState extends Record<string, unknown> = {} , TResult = unknown>(
|
||||
schema: CommandSchema | string,
|
||||
run: (this: CommandRunnerContext<IGameContext<TState>>, command: Command) => Promise<TResult>
|
||||
): CommandRunner<IGameContext<TState>, TResult> {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { IGameContext, createGameCommand } from '../core/game';
|
||||
import { createCommandRegistry, type CommandRegistry, registerCommand } from '../utils/command';
|
||||
import { createCommandRegistry, registerCommand } from '../utils/command';
|
||||
import type { Part } from '../core/part';
|
||||
|
||||
export type TicTacToeState = {
|
||||
|
|
@ -77,7 +77,7 @@ export function placePiece(host: TicTacToeContext, row: number, col: number, mov
|
|||
board.value.children.push(host.parts.get(piece.id));
|
||||
}
|
||||
|
||||
const setup = createGameCommand<TicTacToeContext, { winner: 'X' | 'O' | 'draw' | null }>(
|
||||
const setup = createGameCommand<TicTacToeState, { winner: 'X' | 'O' | 'draw' | null }>(
|
||||
'setup',
|
||||
async function() {
|
||||
this.context.regions.add({
|
||||
|
|
@ -106,7 +106,7 @@ const setup = createGameCommand<TicTacToeContext, { winner: 'X' | 'O' | 'draw' |
|
|||
}
|
||||
)
|
||||
|
||||
const turn = createGameCommand<TicTacToeContext, TurnResult>(
|
||||
const turn = createGameCommand<TicTacToeState, TurnResult>(
|
||||
'turn <player> <turn:number>',
|
||||
async function(cmd) {
|
||||
const [turnPlayer, turnNumber] = cmd.params as [string, number];
|
||||
|
|
|
|||
Loading…
Reference in New Issue