From c39bbae091b0ddf004421b31068ea687967c01ed Mon Sep 17 00:00:00 2001 From: hyperion Date: Sun, 16 Aug 2026 15:30:04 +0200 Subject: [PATCH] initial async processes implementation - not finished --- src/core/keybind.ts | 3 +++ src/core/process_context.ts | 7 ++++++ src/core/signal.ts | 8 ++++++ src/shell/event_emitter.ts | 50 +++++++++++++++++++++++++++++++++++++ src/shell/shell.ts | 7 ++++++ 5 files changed, 75 insertions(+) create mode 100644 src/core/keybind.ts create mode 100644 src/core/process_context.ts create mode 100644 src/core/signal.ts create mode 100644 src/shell/event_emitter.ts diff --git a/src/core/keybind.ts b/src/core/keybind.ts new file mode 100644 index 0000000..4018308 --- /dev/null +++ b/src/core/keybind.ts @@ -0,0 +1,3 @@ +export function resolveKeybind(input: string, map: ReadonlyMap): string | undefined { + return map.get(input); +} diff --git a/src/core/process_context.ts b/src/core/process_context.ts new file mode 100644 index 0000000..c09a4b6 --- /dev/null +++ b/src/core/process_context.ts @@ -0,0 +1,7 @@ +import type { Signal } from "./signal"; + +export interface ProcessContext { + writeStdout(data: Uint8Array): void; + writeStderr(data: Uint8Array): void; + getSignal(): Promise; +} diff --git a/src/core/signal.ts b/src/core/signal.ts new file mode 100644 index 0000000..6086df2 --- /dev/null +++ b/src/core/signal.ts @@ -0,0 +1,8 @@ +export type Input = string; + +//Payload optional because command that handles that signal +// is going to know what shape it is, therefore also able to cast it; +export type Signal = { + type: string; + payload?: unknown; +}; diff --git a/src/shell/event_emitter.ts b/src/shell/event_emitter.ts new file mode 100644 index 0000000..bdce7c1 --- /dev/null +++ b/src/shell/event_emitter.ts @@ -0,0 +1,50 @@ +export class EventEmitter { + private handlers: Map> = new Map(); + + public on(type: K, handler: (payload: TEvents[K]) => void): () => void { + const listeners = this.handlers.get(type); + + if (listeners) { + listeners.add(handler); + } else { + const set: Set = new Set(); + set.add(handler); + this.handlers.set(type, set); + } + + return (): void => this.off(type, handler); + } + + public off(type: K, handler: (payload: TEvents[K]) => void): void { + this.handlers.get(type)?.delete(handler); + } + + public once(type: K, handler: (payload: TEvents[K]) => void): () => void { + const listeners = this.handlers.get(type); + + const wrapper = (payload: TEvents[K]) => { + handler(payload); + this.off(type, wrapper); + }; + + if (listeners) { + listeners.add(wrapper); + } else { + const set: Set = new Set(); + set.add(wrapper); + this.handlers.set(type, set); + } + + return (): void => this.off(type, wrapper); + } + + public emit(type: K, payload: TEvents[K]): void { + const listeners = this.handlers.get(type); + + if (listeners) { + for (const listener of [...listeners]) { + listener(payload); + } + } + } +} diff --git a/src/shell/shell.ts b/src/shell/shell.ts index fdcae88..493a606 100644 --- a/src/shell/shell.ts +++ b/src/shell/shell.ts @@ -107,3 +107,10 @@ export class Shell { return Shell.#instance; } } + +export interface ShellEventMap { + process: { pid: number; name: string }; + stdout: { pid: number; data: Uint8Array }; + stderr: { pid: number; data: Uint8Array }; + result: { pid: number; result: Result }; +}