Created
October 8, 2016 02:49
-
-
Save dschenkelman/5721f696975ec7d9aa7afb155714f1a7 to your computer and use it in GitHub Desktop.
JS event aggregator
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 * 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