SHARE
TWEET

Untitled

a guest Jan 24th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Observable, Subject } from 'rxjs';
  3. import { map, filter } from 'rxjs/operators';
  4.  
  5. interface IMessage {
  6.   channel: string;
  7.   data: any;
  8. }
  9.  
  10. @Injectable()
  11. export class EventBusService {
  12.   private message$: Subject<IMessage>;
  13.  
  14.   constructor() {
  15.     this.message$ = new Subject<IMessage>();
  16.   }
  17.  
  18.   public publish<T>(message: T): void {
  19.     const channel = (<any>message.constructor).name;
  20.     this.message$.next({ channel: channel, data: message });
  21.   }
  22.  
  23.   public of<T>(messageType: { new(...args: any[]): T }): Observable<T> {
  24.     const channel = (<any>messageType).name;
  25.     return this.message$.pipe(
  26.       filter<IMessage>(m => m.channel === channel),
  27.       map<IMessage, T>(x => x.data)
  28.     );
  29.   }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top