refactor: update produce signature to return undefined

Change the return type of `produce` and `produceAsync` from `void` to
`undefined` in `MutableSignal` and `IGameContext` to more accurately
reflect that the mutation functions should not return a value.
This commit is contained in:
hypercross 2026-04-22 01:13:39 +08:00
parent 270b9b744e
commit 6cb0626f05
3 changed files with 15 additions and 12 deletions

View File

@ -15,8 +15,8 @@ import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
export interface IGameContext<TState extends Record<string, unknown> = {}> { export interface IGameContext<TState extends Record<string, unknown> = {}> {
get value(): TState; get value(): TState;
get rng(): ReadonlyRNG; get rng(): ReadonlyRNG;
produce(fn: (draft: TState) => void): void; produce(fn: (draft: TState) => undefined): void;
produceAsync(fn: (draft: TState) => void): Promise<void>; produceAsync(fn: (draft: TState) => undefined): Promise<void>;
run<T>(input: string): Promise<CommandResult<T>>; run<T>(input: string): Promise<CommandResult<T>>;
runParsed<T>(command: Command): Promise<CommandResult<T>>; runParsed<T>(command: Command): Promise<CommandResult<T>>;
prompt: <TResult, TArgs extends any[] = any[]>( prompt: <TResult, TArgs extends any[] = any[]>(
@ -51,10 +51,10 @@ export function createGameContext<TState extends Record<string, unknown> = {}>(
get rng() { get rng() {
return this._rng; return this._rng;
}, },
produce(fn) { produce(fn: (draft: TState) => undefined) {
return state.produce(fn); return state.produce(fn);
}, },
produceAsync(fn) { produceAsync(fn: (draft: TState) => undefined) {
return state.produceAsync(fn); return state.produceAsync(fn);
}, },
run<T>(input: string) { run<T>(input: string) {

View File

@ -65,9 +65,9 @@ export function createTriggers(run: IRunContext) {
for (const cardId of Object.values(regions.hand.childIds)) { for (const cardId of Object.values(regions.hand.childIds)) {
await triggers.onCardDiscarded.execute(ctx.game, { cardId }); await triggers.onCardDiscarded.execute(ctx.game, { cardId });
} }
await ctx.game.produceAsync( await ctx.game.produceAsync((draft) => {
(draft) => (draft.player.energy = draft.player.maxEnergy), draft.player.energy = draft.player.maxEnergy;
); });
await triggers.onDraw.execute(ctx.game, { count: 5 }); await triggers.onDraw.execute(ctx.game, { count: 5 });
}), }),
onShuffle: createTrigger("onShuffle", async (ctx) => { onShuffle: createTrigger("onShuffle", async (ctx) => {

View File

@ -1,5 +1,5 @@
import {Signal, SignalOptions} from '@preact/signals-core'; import { Signal, SignalOptions } from "@preact/signals-core";
import {create} from 'mutative'; import { create } from "mutative";
export class MutableSignal<T> extends Signal<T> { export class MutableSignal<T> extends Signal<T> {
private _interruptions: Promise<void>[] = []; private _interruptions: Promise<void>[] = [];
@ -7,7 +7,7 @@ export class MutableSignal<T> extends Signal<T> {
public constructor(t?: T, options?: SignalOptions<T>) { public constructor(t?: T, options?: SignalOptions<T>) {
super(t, options); super(t, options);
} }
produce(fn: (draft: T) => void) { produce(fn: (draft: T) => undefined) {
this.value = create(this.value, fn); this.value = create(this.value, fn);
} }
@ -30,13 +30,16 @@ export class MutableSignal<T> extends Signal<T> {
* produce `addInterruption` Promise * produce `addInterruption` Promise
* *
*/ */
async produceAsync(fn: (draft: T) => void): Promise<void> { async produceAsync(fn: (draft: T) => undefined): Promise<void> {
await Promise.allSettled(this._interruptions); await Promise.allSettled(this._interruptions);
this._interruptions = []; this._interruptions = [];
this.produce(fn); this.produce(fn);
} }
} }
export function mutableSignal<T>(initial?: T, options?: SignalOptions<T>): MutableSignal<T> { export function mutableSignal<T>(
initial?: T,
options?: SignalOptions<T>,
): MutableSignal<T> {
return new MutableSignal<T>(initial, options); return new MutableSignal<T>(initial, options);
} }