refactor: state signal
This commit is contained in:
+7
-5
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user