refactor: make game host return the setup promise
This commit is contained in:
parent
dd73deabb0
commit
cc7f302677
|
|
@ -1,5 +1,11 @@
|
||||||
import { ReadonlySignal, Signal } from '@preact/signals-core';
|
import { ReadonlySignal, Signal } from '@preact/signals-core';
|
||||||
import type {CommandSchema, CommandRegistry, PromptEvent, CommandRunnerContextExport} from '@/utils/command';
|
import type {
|
||||||
|
CommandSchema,
|
||||||
|
CommandRegistry,
|
||||||
|
PromptEvent,
|
||||||
|
CommandRunnerContextExport,
|
||||||
|
CommandResult
|
||||||
|
} from '@/utils/command';
|
||||||
import type { MutableSignal } from '@/utils/mutable-signal';
|
import type { MutableSignal } from '@/utils/mutable-signal';
|
||||||
import {createGameContext, IGameContext} from './game';
|
import {createGameContext, IGameContext} from './game';
|
||||||
|
|
||||||
|
|
@ -93,7 +99,7 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||||
this._state.clearInterruptions();
|
this._state.clearInterruptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
async setup(setupCommand: string): Promise<void> {
|
setup(setupCommand: string): Promise<CommandResult<unknown>> {
|
||||||
if (this._isDisposed) {
|
if (this._isDisposed) {
|
||||||
throw new Error('GameHost is disposed');
|
throw new Error('GameHost is disposed');
|
||||||
}
|
}
|
||||||
|
|
@ -103,14 +109,12 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||||
const initialState = this._createInitialState();
|
const initialState = this._createInitialState();
|
||||||
this._state.value = initialState as any;
|
this._state.value = initialState as any;
|
||||||
|
|
||||||
// Start the setup command but don't wait for it to complete
|
const promise = this._commands.run(setupCommand);
|
||||||
// The command will run in the background and prompt for input
|
|
||||||
this._commands.run(setupCommand).catch(() => {
|
|
||||||
// Command may be cancelled or fail, which is expected
|
|
||||||
});
|
|
||||||
|
|
||||||
this._status.value = 'running';
|
this._status.value = 'running';
|
||||||
this._emitEvent('setup');
|
this._emitEvent('setup');
|
||||||
|
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,7 @@ describe('GameHost', () => {
|
||||||
const newPromptPromise = waitForPromptEvent(host);
|
const newPromptPromise = waitForPromptEvent(host);
|
||||||
|
|
||||||
// Reset - should reset state and start new game
|
// Reset - should reset state and start new game
|
||||||
await host.setup('setup');
|
host.setup('setup');
|
||||||
|
|
||||||
// State should be back to initial
|
// State should be back to initial
|
||||||
expect(host.context._state.value.currentPlayer).toBe('X');
|
expect(host.context._state.value.currentPlayer).toBe('X');
|
||||||
|
|
@ -173,7 +173,7 @@ describe('GameHost', () => {
|
||||||
await promptPromise;
|
await promptPromise;
|
||||||
|
|
||||||
// Setup should cancel the active prompt and reset state
|
// Setup should cancel the active prompt and reset state
|
||||||
await host.setup('setup');
|
host.setup('setup');
|
||||||
|
|
||||||
// The original runPromise should be rejected due to cancellation
|
// The original runPromise should be rejected due to cancellation
|
||||||
try {
|
try {
|
||||||
|
|
@ -188,11 +188,11 @@ describe('GameHost', () => {
|
||||||
expect(host.context._state.value.turn).toBe(0);
|
expect(host.context._state.value.turn).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw error when disposed', async () => {
|
it('should throw error when disposed', () => {
|
||||||
const { host } = createTestHost();
|
const { host } = createTestHost();
|
||||||
host.dispose();
|
host.dispose();
|
||||||
|
|
||||||
await expect(host.setup('setup')).rejects.toThrow('GameHost is disposed');
|
expect(() => host.setup('setup')).toThrow('GameHost is disposed');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -245,7 +245,7 @@ describe('GameHost', () => {
|
||||||
const promptPromise = waitForPromptEvent(host);
|
const promptPromise = waitForPromptEvent(host);
|
||||||
|
|
||||||
// Initial setup via reset
|
// Initial setup via reset
|
||||||
await host.setup('setup');
|
host.setup('setup');
|
||||||
expect(setupCount).toBe(1);
|
expect(setupCount).toBe(1);
|
||||||
|
|
||||||
// State should be running
|
// State should be running
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue