Created
April 14, 2020 12:29
-
-
Save FFKL/99ca0cb76d12f127ac071d84c8a1ff25 to your computer and use it in GitHub Desktop.
Angular pubSub service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { Observable, ReplaySubject } from 'rxjs'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class EventBusService { | |
| private readonly events: { [eventName: string]: ReplaySubject<any> } = {}; | |
| public sub(event: string): Observable<any> { | |
| if (!this.events[event]) { | |
| this.events[event] = new ReplaySubject<any>(); | |
| } | |
| return this.events[event].asObservable(); | |
| } | |
| public pub(event: string, eventObject?: any): void { | |
| if (!this.events[event]) return; | |
| this.events[event].next(eventObject); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment