Skip to content

Instantly share code, notes, and snippets.

@alexburykin
Created October 4, 2017 10:00
Show Gist options
  • Save alexburykin/105d3700a451e161da462ba9e22faad3 to your computer and use it in GitHub Desktop.
Save alexburykin/105d3700a451e161da462ba9e22faad3 to your computer and use it in GitHub Desktop.
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
interface BroadcastEvent {
key: any;
data?: any;
}
export class Broadcaster {
private _eventBus: Subject<BroadcastEvent>;
constructor() {
this._eventBus = new Subject<BroadcastEvent>();
}
broadcast(key: any, data?: any) {
this._eventBus.next({key, data});
}
on<T>(key: any): Observable<T> {
return this._eventBus.asObservable()
.filter(event => event.key === key)
.map(event => <T>event.data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment