Shell update

- changed shell init code to synchronous with guaranteed returns
- Shell.Instance now a normal method not a getter
This commit is contained in:
2026-09-03 11:45:19 +02:00
parent 8a349330b0
commit 2715e5e28e
2 changed files with 20 additions and 24 deletions
+5 -5
View File
@@ -46,14 +46,14 @@ users.set(1000, user);
groups.set(1000, group); groups.set(1000, group);
const state: ShellState = createInitialShellState(fs, users, groups, user); 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));
//console.log(resolveInode(Shell.Instance?.State!, 6, true)); //console.log(resolveInode(Shell.Instance?.State!, 6, true));
//console.log(sortTreeNodes(Shell.Instance?.State!, dnTraverseTree(Shell.Instance?.State.filesystem!, 0))); //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()); const decoded = new TextDecoder().decode(payload.result.stdout.toArray());
process.stdout.write(decoded + "\n"); process.stdout.write(decoded + "\n");
process.stdout.write(resolveInodeToHome(payload.state, getCurrentSession(payload.state).cwd) + "\n> "); process.stdout.write(resolveInodeToHome(payload.state, getCurrentSession(payload.state).cwd) + "\n> ");
@@ -73,13 +73,13 @@ Shell.Instance?.on("result", payload => {
//console.clear(); //console.clear();
process.stdout.write(resolveInodeToHome(state, getCurrentSession(state).cwd) + "\n> "); process.stdout.write(resolveInodeToHome(state, getCurrentSession(state).cwd) + "\n> ");
process.stdin.on("data", async data => { process.stdin.on("data", async data => {
if (Shell.Instance?.BoundPID !== null) { if (Shell.Instance().BoundPID !== null) {
Shell.Instance?.sendInput(data.toString().trim()); Shell.Instance().sendInput(data.toString().trim());
return; return;
} }
const input = data.toString().trim(); const input = data.toString().trim();
await Shell.Instance?.execute(input); await Shell.Instance().execute(input);
}); });
//export { Termos }; //export { Termos };
+13 -17
View File
@@ -10,8 +10,7 @@ import { EventEmitter } from "./event_emitter";
import { ProcessManager } from "./process_manager"; import { ProcessManager } from "./process_manager";
export class Shell { export class Shell {
static #Instance: Shell; static #Instance: Shell | null;
static #Promise: Promise<Shell> | null = null;
private _State: Readonly<ShellState>; private _State: Readonly<ShellState>;
private _ProcessManager: ProcessManager = new ProcessManager(); private _ProcessManager: ProcessManager = new ProcessManager();
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter(); private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
@@ -43,24 +42,19 @@ export class Shell {
}); });
} }
static async init(initialState: ShellState): Promise<Shell> { static destroy(): void {
Shell.#Instance = null;
return;
}
static init(initialState: ShellState): Shell {
if (Shell.#Instance) { if (Shell.#Instance) {
console.warn("Shell already initialized"); 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); Shell.#Instance = new Shell(initialState);
return Shell.#Instance; return Shell.#Instance;
} finally {
Shell.#Promise = null;
}
})();
return Shell.#Promise;
} }
public async execute(input: string): Promise<void> { public async execute(input: string): Promise<void> {
@@ -125,10 +119,12 @@ export class Shell {
return this._State; return this._State;
} }
public static get Instance(): Shell | undefined { public static Instance(initialState?: ShellState): Shell {
if (!Shell.#Instance) { if (!Shell.#Instance) {
console.error("Shell has not been initialised yet. Call `await Shell.init()` first."); if (!initialState) {
return; throw new Error("Shell needs to be initialized first");
}
Shell.#Instance = new Shell(initialState);
} }
return Shell.#Instance; return Shell.#Instance;
} }