import { Injectable } from '@angular/core' ; import { Subject , Subscription } from 'rxjs' ; author Shashank Agrawal
  @ Injectable ( { providedIn : 'root' } ) export class Events { private channels : { [ key ] : Subject < any > ; } = { } ; param topic param observer returns subscribe ( topic , observer ) { if ( ! this . channels [ topic ] ) { this . channels [ topic ] = new Subject < any > ( ) ; } return this . channels [ topic ] . subscribe ( observer ) ; } param topic param data publish ( topic , data ) { const subject = this . channels [ topic ] ; if ( ! subject ) { return ; } subject . next ( data ) ; } param topic destroy ( topic ) { const subject = this . channels [ topic ] ; if ( ! subject ) { return ; } subject . complete ( ) ; delete this . channels [ topic ] ; } }