ProzillaOS/packages/apps/terminal/core/stream.ts
2024-06-16 21:46:44 +02:00

38 lines
No EOL
643 B
TypeScript

import { EventEmitter } from "../../_utils/event.utils";
const StreamEvents = {
new: "new",
start: "start",
stop: "stop",
} as const;
export class Stream extends EventEmitter<typeof StreamEvents> {
static EVENT_NAMES = StreamEvents;
enabled: boolean = false;
start(callback?: Function): Stream {
if (this.enabled)
return this;
callback?.(this);
this.enabled = true;
this.emit("start");
return this;
}
stop(): Stream {
if (!this.enabled)
return this;
this.enabled = false;
this.emit("stop");
return this;
}
send(text: string): Stream {
if (this.enabled)
this.emit("new", text);
return this;
}
}