import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs'; const CHANNELS: BomChannels[] = [ 'ch:loader', 'ch:filters', 'ch:cart', 'ch:sbo', 'ch:notification' ]; export class BomMessage { name?: string; payload: any; } export type BomChannels = | 'ch:loader' | 'ch:filters' | 'ch:cart' | 'ch:sbo' | 'ch:notification'; @Injectable({ providedIn: 'root' }) export class MessageBusService { private _channelMap: Map> = new Map< string, Subject >(); constructor() { for (const channel of CHANNELS) { this._initChannel(channel); } } private _initChannel(channel: BomChannels): void { this._channelMap.set(channel, new Subject()); } from(channel: BomChannels): Observable { return this._channelMap.get(channel).asObservable(); } to(channel: BomChannels, message: BomMessage): MessageBusService { this._channelMap.get(channel).next(message); return this; } }