forked from Ninjalama/streamyfin_mirror
Co-authored-by: lostb1t <coding-mosses0z@icloud.com> Co-authored-by: Fredrik Burmester <fredrik.burmester@gmail.com> Co-authored-by: Gauvain <68083474+Gauvino@users.noreply.github.com> Co-authored-by: Gauvino <uruknarb20@gmail.com> Co-authored-by: storm1er <le.storm1er@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Chris <182387676+whoopsi-daisy@users.noreply.github.com> Co-authored-by: arch-fan <55891793+arch-fan@users.noreply.github.com> Co-authored-by: Alex Kim <alexkim@Alexs-MacBook-Pro.local>
29 lines
737 B
TypeScript
29 lines
737 B
TypeScript
type Listener<T = void> = (data?: T) => void;
|
|
|
|
class EventBus {
|
|
private listeners: Record<string, Listener<any>[]> = {};
|
|
|
|
on<T = void>(event: string, callback: Listener<T>): () => void {
|
|
if (!this.listeners[event]) {
|
|
this.listeners[event] = [];
|
|
}
|
|
this.listeners[event].push(callback);
|
|
return () => this.off(event, callback);
|
|
}
|
|
|
|
off<T = void>(event: string, callback: Listener<T>): void {
|
|
if (!this.listeners[event]) return;
|
|
this.listeners[event] = this.listeners[event].filter(
|
|
(fn) => fn !== callback,
|
|
);
|
|
}
|
|
|
|
emit<T = void>(event: string, data?: T): void {
|
|
this.listeners[event]?.forEach((callback) => {
|
|
callback(data);
|
|
});
|
|
}
|
|
}
|
|
|
|
export const eventBus = new EventBus();
|