From 49109963bcafb1c502e77d8b531ac5e8a8f90bc9 Mon Sep 17 00:00:00 2001 From: hypercross Date: Mon, 6 Apr 2026 11:59:39 +0800 Subject: [PATCH] refactor: hide api --- src/core/game-host.ts | 33 ++++++++++++++------------------- tests/core/game-host.test.ts | 9 ++++++++- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/core/game-host.ts b/src/core/game-host.ts index 213ee80..6aa0968 100644 --- a/src/core/game-host.ts +++ b/src/core/game-host.ts @@ -1,23 +1,20 @@ import { ReadonlySignal, Signal } from '@preact/signals-core'; -import type { +import { CommandSchema, CommandRegistry, PromptEvent, - CommandRunnerContextExport, } from '@/utils/command'; -import type { MutableSignal } from '@/utils/mutable-signal'; import {createGameCommandRegistry, createGameContext, IGameContext} from './game'; export type GameHostStatus = 'created' | 'running' | 'disposed'; export class GameHost, TResult=unknown> { - readonly context: IGameContext; + readonly state: ReadonlySignal; readonly status: ReadonlySignal; readonly activePromptSchema: ReadonlySignal; readonly activePromptPlayer: ReadonlySignal; - private _state: MutableSignal; - private _commands: CommandRunnerContextExport>; + private _context: IGameContext; private _start: (ctx: IGameContext) => Promise; private _status: Signal; private _activePromptSchema: Signal; @@ -35,8 +32,9 @@ export class GameHost, TResult=unknown> { this._eventListeners = new Map(); const initialState = createInitialState(); - this.context = createGameContext(registry, initialState); + this._context = createGameContext(registry, initialState); this._start = start; + this.state = this._context._state; this._status = new Signal('created'); this.status = this._status; @@ -47,22 +45,19 @@ export class GameHost, TResult=unknown> { this._activePromptPlayer = new Signal(null); this.activePromptPlayer = this._activePromptPlayer; - this._state = this.context._state; - this._commands = this.context._commands; - this._setupPromptTracking(); } private _setupPromptTracking() { let currentPromptEvent: PromptEvent | null = null; - this._commands.on('prompt', (e) => { + this._context._commands.on('prompt', (e) => { currentPromptEvent = e as PromptEvent; this._activePromptSchema.value = currentPromptEvent.schema; this._activePromptPlayer.value = currentPromptEvent.currentPlayer; }); - this._commands.on('promptEnd', () => { + this._context._commands.on('promptEnd', () => { currentPromptEvent = null; this._activePromptSchema.value = null; this._activePromptPlayer.value = null; @@ -77,7 +72,7 @@ export class GameHost, TResult=unknown> { if (this._isDisposed) { return 'GameHost is disposed'; } - return this._commands._tryCommit(input); + return this._context._commands._tryCommit(input); } /** @@ -85,7 +80,7 @@ export class GameHost, TResult=unknown> { * @see MutableSignal.addInterruption */ addInterruption(promise: Promise): void { - this._state.addInterruption(promise); + this._context._state.addInterruption(promise); } /** @@ -93,7 +88,7 @@ export class GameHost, TResult=unknown> { * @see MutableSignal.clearInterruptions */ clearInterruptions(): void { - this._state.clearInterruptions(); + this._context._state.clearInterruptions(); } start(): Promise { @@ -101,12 +96,12 @@ export class GameHost, TResult=unknown> { throw new Error('GameHost is disposed'); } - this._commands._cancel(); + this._context._commands._cancel(); const initialState = this._createInitialState(); - this._state.value = initialState as any; + this._context._state.value = initialState as any; - const promise = this._start(this.context); + const promise = this._start(this._context); this._status.value = 'running'; this._emitEvent('start'); @@ -120,7 +115,7 @@ export class GameHost, TResult=unknown> { } this._isDisposed = true; - this._commands._cancel(); + this._context._commands._cancel(); this._status.value = 'disposed'; // Emit dispose event BEFORE clearing listeners diff --git a/tests/core/game-host.test.ts b/tests/core/game-host.test.ts index 1ac3241..e1f6116 100644 --- a/tests/core/game-host.test.ts +++ b/tests/core/game-host.test.ts @@ -10,11 +10,18 @@ import { import { createGameHost, GameHost } from '@/core/game-host'; import type { PromptEvent } from '@/utils/command'; import { MutableSignal } from '@/utils/mutable-signal'; +import {IGameContext} from "../../src"; + +type TestGameHost = GameHost & { + _context: IGameContext; + context: IGameContext; +} function createTestHost() { - const host = createGameHost( + const host: TestGameHost = createGameHost( { registry, createInitialState, start } ); + host.context = host._context; return { host }; }