From 2715e5e28ec6451c7ca5260fa12fd5cfa745b568 Mon Sep 17 00:00:00 2001 From: hyperion Date: Thu, 3 Sep 2026 11:45:19 +0200 Subject: [PATCH] Shell update - changed shell init code to synchronous with guaranteed returns - Shell.Instance now a normal method not a getter --- src/index.ts | 10 +++++----- src/shell/shell.ts | 34 +++++++++++++++------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6cf6b36..0b2b47c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,14 +46,14 @@ users.set(1000, user); groups.set(1000, group); const state: ShellState = createInitialShellState(fs, users, groups, user); -await Shell.init(state); +Shell.init(state); //console.log(resolveInode(Shell.Instance?.State!, 6)); //console.log(resolveInode(Shell.Instance?.State!, 6, true)); //console.log(sortTreeNodes(Shell.Instance?.State!, dnTraverseTree(Shell.Instance?.State.filesystem!, 0))); -Shell.Instance?.on("result", payload => { +Shell.Instance().on("result", payload => { const decoded = new TextDecoder().decode(payload.result.stdout.toArray()); process.stdout.write(decoded + "\n"); process.stdout.write(resolveInodeToHome(payload.state, getCurrentSession(payload.state).cwd) + "\n> "); @@ -73,13 +73,13 @@ Shell.Instance?.on("result", payload => { //console.clear(); process.stdout.write(resolveInodeToHome(state, getCurrentSession(state).cwd) + "\n> "); process.stdin.on("data", async data => { - if (Shell.Instance?.BoundPID !== null) { - Shell.Instance?.sendInput(data.toString().trim()); + if (Shell.Instance().BoundPID !== null) { + Shell.Instance().sendInput(data.toString().trim()); return; } const input = data.toString().trim(); - await Shell.Instance?.execute(input); + await Shell.Instance().execute(input); }); //export { Termos }; diff --git a/src/shell/shell.ts b/src/shell/shell.ts index 34c9421..9d5c532 100644 --- a/src/shell/shell.ts +++ b/src/shell/shell.ts @@ -10,8 +10,7 @@ import { EventEmitter } from "./event_emitter"; import { ProcessManager } from "./process_manager"; export class Shell { - static #Instance: Shell; - static #Promise: Promise | null = null; + static #Instance: Shell | null; private _State: Readonly; private _ProcessManager: ProcessManager = new ProcessManager(); private _EventEmitter: EventEmitter = new EventEmitter(); @@ -43,24 +42,19 @@ export class Shell { }); } - static async init(initialState: ShellState): Promise { + static destroy(): void { + Shell.#Instance = null; + return; + } + + static init(initialState: ShellState): Shell { if (Shell.#Instance) { console.warn("Shell already initialized"); - return this.#Instance; + return Shell.#Instance; } - if (Shell.#Promise) return Shell.#Promise; - - Shell.#Promise = (async () => { - try { - Shell.#Instance = new Shell(initialState); - return Shell.#Instance; - } finally { - Shell.#Promise = null; - } - })(); - - return Shell.#Promise; + Shell.#Instance = new Shell(initialState); + return Shell.#Instance; } public async execute(input: string): Promise { @@ -125,10 +119,12 @@ export class Shell { return this._State; } - public static get Instance(): Shell | undefined { + public static Instance(initialState?: ShellState): Shell { if (!Shell.#Instance) { - console.error("Shell has not been initialised yet. Call `await Shell.init()` first."); - return; + if (!initialState) { + throw new Error("Shell needs to be initialized first"); + } + Shell.#Instance = new Shell(initialState); } return Shell.#Instance; }