EventEmitter vs facebook's dispatcher
Asked Answered
G

1

10

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?

Gerger answered 28/1, 2015 at 16:25 Comment(2)
Exactly what I thought as well!Kolyma
@PineappleUndertheSea: did you came to a conclusion? :)Gerger
M
5

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

Minimum answered 13/2, 2015 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.