I am using react with Flux architecture.
I've read on the web that in order to define Store, I have to do something like that:
var AppDispatcher = require('../dispatcher/dispatcher'), //facebook's dispatcher
EventEmitter = require('events').EventEmitter,
assign = require('object-assign');
var MyStore = assign({}, EventEmitter.prototype, {
.....
As far as I understand, EventEmitter and facebook's dispatcher has a lot in common. For example, the both can emit (or dispatch) an event.
My question is why do I need both EventEmitter and dispatcher? Isn't it redundant?
Isn't it better to create a dispatcher that includes also the EventEmitter needed behavior?
The Dispatcher has functionality not provided nor expected in EventEmitter, the most notable being waitFor, which allows a store to ensure that another store has been updated in response to an action before it proceeds.
Pattern-wise, the Dispatcher is also a singleton, whereas EventEmitter is an API that you might object-assign onto multiple stores.
Of course you could create your own hybrid class to serve both purposes. The Facebook Flux dispatcher is a reference implementation :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With