Skip to content

Commit

Permalink
make state manager working
Browse files Browse the repository at this point in the history
  • Loading branch information
instructr13 committed Oct 5, 2024
1 parent 473d512 commit 7629520
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/solver/src/features/algorithm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export class AlgorithmFeature extends FeatureBase {

init() {}

async onReady() {}
async start() {}
}
2 changes: 1 addition & 1 deletion apps/solver/src/features/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class FeatureBase {

abstract init(): void;

abstract onReady(): Promise<void>;
abstract start(): Promise<void>;

onDispose() {}
}
2 changes: 1 addition & 1 deletion apps/solver/src/features/server-comm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export class ServerCommunicatorFeature extends FeatureBase {

init() {}

async onReady() {}
async start() {}
}
2 changes: 1 addition & 1 deletion apps/solver/src/features/ui-comm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class UICommunicatorFeature extends FeatureBase {
}
}

async onReady() {
async start() {
console.info(`UI Communicator is ready at http://${HOST}:${PORT}`);
}
}
Expand Down
8 changes: 7 additions & 1 deletion apps/solver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -22,6 +24,8 @@ const features = createFeatures();

console.time("All features ready");

StateManager.init(spmc<UIMessageEvent>().tx);

console.group(
"Registered features:\n",
...features.map((feature) => `- ${feature.getName()}\n`),
Expand All @@ -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";
20 changes: 17 additions & 3 deletions apps/solver/src/state/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateBase>;
#oldState: StateBase;
#tx: ChannelTx<UIMessageEvent>;

constructor(tx: ChannelTx<UIMessageEvent>) {
this.#state$ = makeStore(InitState.instance);
this.#oldState = InitState.instance;
private constructor(tx: ChannelTx<UIMessageEvent>) {
this.#state$ = makeStore(INITIAL_STATE);
this.#oldState = INITIAL_STATE;
this.#tx = tx;
}

static init(tx: ChannelTx<UIMessageEvent>) {
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");
Expand All @@ -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);
}
Expand Down

0 comments on commit 7629520

Please sign in to comment.