refactor: remove host options
This commit is contained in:
+2
-13
@@ -1,14 +1,10 @@
|
|||||||
import { ReadonlySignal, signal, Signal } from '@preact/signals-core';
|
import { ReadonlySignal, Signal } from '@preact/signals-core';
|
||||||
import type { CommandSchema, CommandRegistry, CommandResult, PromptEvent } from '@/utils/command';
|
import type { CommandSchema, CommandRegistry, PromptEvent } from '@/utils/command';
|
||||||
import type { MutableSignal } from '@/utils/mutable-signal';
|
import type { MutableSignal } from '@/utils/mutable-signal';
|
||||||
import { createGameContext } from './game';
|
import { createGameContext } from './game';
|
||||||
|
|
||||||
export type GameHostStatus = 'created' | 'running' | 'disposed';
|
export type GameHostStatus = 'created' | 'running' | 'disposed';
|
||||||
|
|
||||||
export interface GameHostOptions {
|
|
||||||
autoStart?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GameModule<TState extends Record<string, unknown>> {
|
export interface GameModule<TState extends Record<string, unknown>> {
|
||||||
registry: CommandRegistry<MutableSignal<TState>>;
|
registry: CommandRegistry<MutableSignal<TState>>;
|
||||||
createInitialState: () => TState;
|
createInitialState: () => TState;
|
||||||
@@ -32,7 +28,6 @@ export class GameHost<TState extends Record<string, unknown>> {
|
|||||||
constructor(
|
constructor(
|
||||||
registry: CommandRegistry<MutableSignal<TState>>,
|
registry: CommandRegistry<MutableSignal<TState>>,
|
||||||
createInitialState: () => TState,
|
createInitialState: () => TState,
|
||||||
options?: GameHostOptions
|
|
||||||
) {
|
) {
|
||||||
this._createInitialState = createInitialState;
|
this._createInitialState = createInitialState;
|
||||||
this._eventListeners = new Map();
|
this._eventListeners = new Map();
|
||||||
@@ -55,10 +50,6 @@ export class GameHost<TState extends Record<string, unknown>> {
|
|||||||
this.state = this._state;
|
this.state = this._state;
|
||||||
|
|
||||||
this._setupPromptTracking();
|
this._setupPromptTracking();
|
||||||
|
|
||||||
if (options?.autoStart !== false) {
|
|
||||||
this._status.value = 'running';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _setupPromptTracking() {
|
private _setupPromptTracking() {
|
||||||
@@ -145,11 +136,9 @@ export class GameHost<TState extends Record<string, unknown>> {
|
|||||||
|
|
||||||
export function createGameHost<TState extends Record<string, unknown>>(
|
export function createGameHost<TState extends Record<string, unknown>>(
|
||||||
module: GameModule<TState>,
|
module: GameModule<TState>,
|
||||||
options?: GameHostOptions
|
|
||||||
): GameHost<TState> {
|
): GameHost<TState> {
|
||||||
return new GameHost(
|
return new GameHost(
|
||||||
module.registry,
|
module.registry,
|
||||||
module.createInitialState,
|
module.createInitialState,
|
||||||
options
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -70,7 +70,7 @@ export function createGameCommand<TState extends Record<string, unknown> = {} ,
|
|||||||
}
|
}
|
||||||
|
|
||||||
export { GameHost, createGameHost } from './game-host';
|
export { GameHost, createGameHost } from './game-host';
|
||||||
export type { GameHostStatus, GameHostOptions, GameModule } from './game-host';
|
export type { GameHostStatus, GameModule } from './game-host';
|
||||||
|
|
||||||
export function createGameModule<TState extends Record<string, unknown>>(
|
export function createGameModule<TState extends Record<string, unknown>>(
|
||||||
module: GameModule<TState>
|
module: GameModule<TState>
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@
|
|||||||
export type { IGameContext } from './core/game';
|
export type { IGameContext } from './core/game';
|
||||||
export { createGameContext, createGameCommandRegistry } from './core/game';
|
export { createGameContext, createGameCommandRegistry } from './core/game';
|
||||||
|
|
||||||
export type { GameHost, GameHostStatus, GameHostOptions, GameModule } from './core/game';
|
export type { GameHost, GameHostStatus, GameModule } from './core/game';
|
||||||
export { createGameHost, createGameModule } from './core/game';
|
export { createGameHost, createGameModule } from './core/game';
|
||||||
|
|
||||||
export type { Part } from './core/part';
|
export type { Part } from './core/part';
|
||||||
|
|||||||
@@ -34,10 +34,10 @@ describe('GameHost', () => {
|
|||||||
expect(Object.keys(host.state.value.parts).length).toBe(0);
|
expect(Object.keys(host.state.value.parts).length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have status "running" by default', () => {
|
it('should have status "created" by default', () => {
|
||||||
const { host } = createTestHost();
|
const { host } = createTestHost();
|
||||||
|
|
||||||
expect(host.status.value).toBe('running');
|
expect(host.status.value).toBe('created');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have null activePromptSchema initially', () => {
|
it('should have null activePromptSchema initially', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user