initial async processes implementation - not finished
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
export function resolveKeybind(input: string, map: ReadonlyMap<string, string>): string | undefined {
|
||||||
|
return map.get(input);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import type { Signal } from "./signal";
|
||||||
|
|
||||||
|
export interface ProcessContext {
|
||||||
|
writeStdout(data: Uint8Array): void;
|
||||||
|
writeStderr(data: Uint8Array): void;
|
||||||
|
getSignal(): Promise<Signal>;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
export class EventEmitter<TEvents> {
|
||||||
|
private handlers: Map<keyof TEvents, Set<Function>> = new Map();
|
||||||
|
|
||||||
|
public on<K extends keyof TEvents>(type: K, handler: (payload: TEvents[K]) => void): () => void {
|
||||||
|
const listeners = this.handlers.get(type);
|
||||||
|
|
||||||
|
if (listeners) {
|
||||||
|
listeners.add(handler);
|
||||||
|
} else {
|
||||||
|
const set: Set<Function> = new Set();
|
||||||
|
set.add(handler);
|
||||||
|
this.handlers.set(type, set);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (): void => this.off(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public off<K extends keyof TEvents>(type: K, handler: (payload: TEvents[K]) => void): void {
|
||||||
|
this.handlers.get(type)?.delete(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once<K extends keyof TEvents>(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<Function> = new Set();
|
||||||
|
set.add(wrapper);
|
||||||
|
this.handlers.set(type, set);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (): void => this.off(type, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public emit<K extends keyof TEvents>(type: K, payload: TEvents[K]): void {
|
||||||
|
const listeners = this.handlers.get(type);
|
||||||
|
|
||||||
|
if (listeners) {
|
||||||
|
for (const listener of [...listeners]) {
|
||||||
|
listener(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -107,3 +107,10 @@ export class Shell {
|
|||||||
return Shell.#instance;
|
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<ShellError> };
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user