diff --git a/apps/solver/src/features/algorithm/index.ts b/apps/solver/src/features/algorithm/index.ts index 693a93c..b765685 100644 --- a/apps/solver/src/features/algorithm/index.ts +++ b/apps/solver/src/features/algorithm/index.ts @@ -21,5 +21,5 @@ export class AlgorithmFeature extends FeatureBase { init() {} - async onReady() {} + async start() {} } diff --git a/apps/solver/src/features/base.ts b/apps/solver/src/features/base.ts index eb6b265..41dc80b 100644 --- a/apps/solver/src/features/base.ts +++ b/apps/solver/src/features/base.ts @@ -11,7 +11,7 @@ export abstract class FeatureBase { abstract init(): void; - abstract onReady(): Promise; + abstract start(): Promise; onDispose() {} } diff --git a/apps/solver/src/features/server-comm/index.ts b/apps/solver/src/features/server-comm/index.ts index d85b331..2a06383 100644 --- a/apps/solver/src/features/server-comm/index.ts +++ b/apps/solver/src/features/server-comm/index.ts @@ -7,5 +7,5 @@ export class ServerCommunicatorFeature extends FeatureBase { init() {} - async onReady() {} + async start() {} } diff --git a/apps/solver/src/features/ui-comm/index.ts b/apps/solver/src/features/ui-comm/index.ts index 43e86a8..ce891ef 100644 --- a/apps/solver/src/features/ui-comm/index.ts +++ b/apps/solver/src/features/ui-comm/index.ts @@ -52,7 +52,7 @@ export class UICommunicatorFeature extends FeatureBase { } } - async onReady() { + async start() { console.info(`UI Communicator is ready at http://${HOST}:${PORT}`); } } diff --git a/apps/solver/src/index.ts b/apps/solver/src/index.ts index 94262d9..7e114b9 100644 --- a/apps/solver/src/index.ts +++ b/apps/solver/src/index.ts @@ -3,6 +3,8 @@ import { AlgorithmFeature } from "./features/algorithm"; import type { FeatureBase } from "./features/base"; import { ServerCommunicatorFeature } from "./features/server-comm"; import { UICommunicatorFeature } from "./features/ui-comm"; +import { IdleState } from "./state/idle.ts"; +import { StateManager } from "./state/manager.ts"; import { spmc } from "./util/channel"; import type { Falsy } from "./util/types"; @@ -22,6 +24,8 @@ const features = createFeatures(); console.time("All features ready"); +StateManager.init(spmc().tx); + console.group( "Registered features:\n", ...features.map((feature) => `- ${feature.getName()}\n`), @@ -40,10 +44,12 @@ for (const feature of features) { console.timeEnd(`Initialized feature: ${name}`); } -const allFeaturesPromise = Promise.all(features.map((feature) => feature.onReady())); +const allFeaturesPromise = Promise.all(features.map((feature) => feature.start())); console.timeEnd("All features ready"); +StateManager.instance.setState(IdleState.instance); + await allFeaturesPromise; export type { SolverApp } from "./features/ui-comm"; diff --git a/apps/solver/src/state/manager.ts b/apps/solver/src/state/manager.ts index c56f00d..1769a3b 100644 --- a/apps/solver/src/state/manager.ts +++ b/apps/solver/src/state/manager.ts @@ -4,18 +4,30 @@ import type { UIMessageEvent } from "../events/base.ts"; import type { StateBase } from "./base.ts"; import { InitState } from "./init.ts"; +const INITIAL_STATE = InitState.instance; + export class StateManager { static #instance: StateManager; readonly #state$: Store; #oldState: StateBase; #tx: ChannelTx; - constructor(tx: ChannelTx) { - this.#state$ = makeStore(InitState.instance); - this.#oldState = InitState.instance; + private constructor(tx: ChannelTx) { + this.#state$ = makeStore(INITIAL_STATE); + this.#oldState = INITIAL_STATE; this.#tx = tx; } + static init(tx: ChannelTx) { + if (StateManager.#instance) { + throw new Error("State manager already initialized"); + } + + StateManager.#instance = new StateManager(tx); + + console.info("State manager initialized with:", INITIAL_STATE.getName()); + } + static get instance() { if (!StateManager.#instance) { throw new Error("State manager not initialized"); @@ -29,6 +41,8 @@ export class StateManager { return; } + console.info("[State change]", this.#oldState.getName(), "-->", state.getName()); + this.#oldState = this.#state$.content(); this.#state$.set(state); }