Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created April 14, 2020 12:29
Show Gist options
  • Select an option

  • Save FFKL/99ca0cb76d12f127ac071d84c8a1ff25 to your computer and use it in GitHub Desktop.

Select an option

Save FFKL/99ca0cb76d12f127ac071d84c8a1ff25 to your computer and use it in GitHub Desktop.
Angular pubSub service
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