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:
parent
270b9b744e
commit
6cb0626f05
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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) => {
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue