refactor: entity -> MutableSignal

This commit is contained in:
2026-04-03 14:19:44 +08:00
parent b1b059de8c
commit 86714e7837
8 changed files with 51 additions and 62 deletions
+10 -10
View File
@@ -1,4 +1,4 @@
import {entity, Entity} from "@/utils/entity";
import {MutableSignal, mutableSignal} from "@/utils/mutable-signal";
import {
Command,
CommandRegistry,
@@ -12,16 +12,16 @@ import {
} from "@/utils/command";
export interface IGameContext<TState extends Record<string, unknown> = {} > {
state: Entity<TState>;
commands: CommandRunnerContextExport<Entity<TState>>;
state: MutableSignal<TState>;
commands: CommandRunnerContextExport<MutableSignal<TState>>;
}
export function createGameContext<TState extends Record<string, unknown> = {} >(
commandRegistry: CommandRegistry<Entity<TState>>,
commandRegistry: CommandRegistry<MutableSignal<TState>>,
initialState?: TState | (() => TState)
): IGameContext<TState> {
const stateValue = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState;
const state = entity('state', stateValue);
const state = mutableSignal(stateValue);
const commands = createCommandRunnerContext(commandRegistry, state);
return {
@@ -36,7 +36,7 @@ export function createGameContext<TState extends Record<string, unknown> = {} >(
*/
export function createGameContextFromModule<TState extends Record<string, unknown> = {} >(
module: {
registry: CommandRegistry<Entity<TState>>,
registry: CommandRegistry<MutableSignal<TState>>,
createInitialState: () => TState
},
): IGameContext<TState> {
@@ -44,12 +44,12 @@ export function createGameContextFromModule<TState extends Record<string, unknow
}
export function createGameCommandRegistry<TState extends Record<string, unknown> = {} >() {
const registry = createCommandRegistry<Entity<TState>>();
const registry = createCommandRegistry<MutableSignal<TState>>();
return {
registry,
add<TResult = unknown>(
schema: CommandSchema | string,
run: (this: CommandRunnerContext<Entity<TState>>, command: Command) => Promise<TResult>
run: (this: CommandRunnerContext<MutableSignal<TState>>, command: Command) => Promise<TResult>
){
createGameCommand(registry, schema, run);
return this;
@@ -58,9 +58,9 @@ export function createGameCommandRegistry<TState extends Record<string, unknown>
}
export function createGameCommand<TState extends Record<string, unknown> = {} , TResult = unknown>(
registry: CommandRegistry<Entity<TState>>,
registry: CommandRegistry<MutableSignal<TState>>,
schema: CommandSchema | string,
run: (this: CommandRunnerContext<Entity<TState>>, command: Command) => Promise<TResult>
run: (this: CommandRunnerContext<MutableSignal<TState>>, command: Command) => Promise<TResult>
) {
registerCommand(registry, {
schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema,