import { Subject, Subscription, TeardownLogic } from "rxjs"; export interface EventBusType { type: string; payload: PayloadEventBus; } export interface PayloadEventBus { type: "resolve" | "reject"; data: any; } export default class EventBus { private static instance: EventBus; private eventSubject = new Subject(); private subscription = new Subscription(); static getInstance(): EventBus { if (!EventBus.instance) { EventBus.instance = new EventBus(); } return EventBus.instance; } get events() { return this.eventSubject.asObservable(); } post(event: EventBusType) { this.eventSubject.next(event); } postEvent = (event: string, payload: PayloadEventBus) => { this.post({ type: event, payload, }); }; unsubcribe() { this.subscription.unsubscribe(); } add(teardown: TeardownLogic): Subscription { return this.subscription.add(teardown); } }