import { Observable , Subject } from 'rxjs' ; import { filter , map } from 'rxjs/operators' ; import { Injectable } from '@angular/core' ; interface BroadCastObject { key : any ; value : any ; } @ Injectable ( { providedIn : 'root' } ) export class BroadcasterService { private _eventQueue : Subject < BroadCastObject > ; constructor ( ) { this . _eventQueue = new Subject < BroadCastObject > ( ) ; } broadcast ( key , value ) { this . _eventQueue . next ( { key , value } ) ; } unsubscribe ( ) { this . _eventQueue . complete ( ) ; } listen < T > ( key ) { return this . _eventQueue . asObservable ( ) . pipe ( filter ( ( e ) => e . key === key ) , map ( ( e ) => < T > e . value ) ) ; } }