Skip to content

Instantly share code, notes, and snippets.

@dschenkelman
Created October 8, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschenkelman/5721f696975ec7d9aa7afb155714f1a7 to your computer and use it in GitHub Desktop.
Save dschenkelman/5721f696975ec7d9aa7afb155714f1a7 to your computer and use it in GitHub Desktop.
JS event aggregator
import * as Rx from 'rx';
const kSubject = Symbol('subject');
export default class EventAggregator {
constructor(){
this[kSubject] = new Rx.Subject();
}
publish(event, payload){
this[kSubject].onNext({ payload, event });
}
suscribe(event, handler){
this[kSubject]
.filter(p => p.event === event)
.map(p => p.payload)
.forEach(handler);
}
dispose(){
this[kSubject].dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment