Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventEmitter vs facebook's dispatcher

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?

like image 978
Naor Avatar asked Jan 28 '15 16:01

Naor


1 Answers

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 :)

like image 171
James Pearce Avatar answered Oct 19 '22 13:10

James Pearce