refactor: entity -> MutableSignal
This commit is contained in:
+10
-10
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user