refactor: state signal

This commit is contained in:
hypercross 2026-04-02 12:53:49 +08:00
parent 004d49c36f
commit 74f56b9da4
2 changed files with 10 additions and 8 deletions

View File

@ -10,15 +10,17 @@ import {
PromptEvent PromptEvent
} from "../utils/command"; } from "../utils/command";
import {AsyncQueue} from "../utils/async-queue"; 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>>; parts: ReturnType<typeof createEntityCollection<Part>>;
regions: ReturnType<typeof createEntityCollection<Region>>; regions: ReturnType<typeof createEntityCollection<Region>>;
commands: CommandRunnerContextExport<IGameContext<TState>>; commands: CommandRunnerContextExport<IGameContext<TState>>;
prompts: AsyncQueue<PromptEvent>; prompts: AsyncQueue<PromptEvent>;
state: Signal<TState>
} }
export function createGameContext<TState extends {} = {}>( export function createGameContext<TState extends Record<string, unknown> = {} >(
commandRegistry: CommandRegistry<IGameContext<TState>>, commandRegistry: CommandRegistry<IGameContext<TState>>,
initialState?: TState | (() => TState) initialState?: TState | (() => TState)
): IGameContext<TState> { ): IGameContext<TState> {
@ -32,7 +34,7 @@ export function createGameContext<TState extends {} = {}>(
regions, regions,
prompts, prompts,
commands: null!, commands: null!,
state, state: signal(state),
} as IGameContext<TState> } as IGameContext<TState>
ctx.commands = createCommandRunnerContext(commandRegistry, ctx); 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);` * so that we can do `import * as tictactoe from './tic-tac-toe.ts';\n\n createGameContextFromModule(tictactoe);`
* @param module * @param module
*/ */
export function createGameContextFromModule<TState extends {} = {}>( export function createGameContextFromModule<TState extends Record<string, unknown> = {} >(
module: { module: {
registry: CommandRegistry<IGameContext<TState>>, registry: CommandRegistry<IGameContext<TState>>,
createInitialState: () => TState createInitialState: () => TState
@ -54,7 +56,7 @@ export function createGameContextFromModule<TState extends {} = {}>(
return createGameContext(module.registry, module.createInitialState); 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, schema: CommandSchema | string,
run: (this: CommandRunnerContext<IGameContext<TState>>, command: Command) => Promise<TResult> run: (this: CommandRunnerContext<IGameContext<TState>>, command: Command) => Promise<TResult>
): CommandRunner<IGameContext<TState>, TResult> { ): CommandRunner<IGameContext<TState>, TResult> {

View File

@ -1,5 +1,5 @@
import { IGameContext, createGameCommand } from '../core/game'; 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'; import type { Part } from '../core/part';
export type TicTacToeState = { 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)); 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', 'setup',
async function() { async function() {
this.context.regions.add({ 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>', 'turn <player> <turn:number>',
async function(cmd) { async function(cmd) {
const [turnPlayer, turnNumber] = cmd.params as [string, number]; const [turnPlayer, turnNumber] = cmd.params as [string, number];