blowater/event-bus.ts
2023-09-07 13:11:59 +00:00

20 lines
508 B
TypeScript

import { chan, multi } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
export class EventBus<T> implements EventEmitter<T> {
private readonly c = chan<T>();
private readonly caster = multi<T>(this.c);
emit = async (event: T) => {
await this.c.put(event);
};
onChange() {
return this.caster.copy();
}
}
export type EventEmitter<T> = {
emit: (event: T) => void;
};
export type emitFunc<T extends { type: string }> = (event: T) => void;