0

I have multiple Flux stores. Now clearly, all of them are extending the same Event emitter singleton. This has led to events across stores clashing with each other (even the most common, emitChange). There seems to be no difference between doing Store1.getID() and Store2.getID(), because stores seem to be one large object extended from every other store. What am I doing wrong?

I have been having this issue for a while now, and its driving me nuts. I am sure this has a simple answer that I am missing. It's one of the reasons I am waiting for relay and GraphQL.

EDIT: What all my stores look like in code.

var Events = require('events'), extend = require('deep_extend'),
    EventEmitter = Events.EventEmitter,
    CHANGE_EVENT = 'change';

var SomeStore = extend(EventEmitter.prototype, {
    someGetter: function(){
        return _someVar;
    },
    dispatchToken: AppDispatcher.register(function(action) {
        switch(action.type) {
            case 'SOME_ACTION':
                _someVar = 'someValue'
                break;

            default:
                return true;
        }

        SomeStore.emitChange();
        return true;
    })
});

return SomeStore;
nilgun
  • 10,460
  • 4
  • 46
  • 57
Varun Arora
  • 332
  • 4
  • 8

1 Answers1

3

stores seem to be one large object extended from every other store.

There must be some problem with how you extend from EventEmitter otherwise your code should be working fine.

Now that there are a few ways to do the same thing, here is how facebook implemented it in their official examples:

var assign = require('object-assign');
var EventEmitter = require('events').EventEmitter;
var TodoStore = assign({}, EventEmitter.prototype, {
    ...

UPDATE

Now looking at your code

extend(EventEmitter.prototype, {

is actually writing on the prototype itself, hence the errors you got. Instead you should be extending an empty object:

extend({}, EventEmitter.prototype, {
nilgun
  • 10,460
  • 4
  • 46
  • 57
  • OH! So I guess I have majorly gotten confused about what `assign` does. Is that the case here? (posted store for you) – Varun Arora Feb 04 '15 at 22:30
  • You are probably writing on EventEmitter prototype. Can you try `extend({},EventEmitter.prototype, {` .. – nilgun Feb 04 '15 at 22:32
  • deep_extend doesn't take in more than two objects. I'll just use `assign` I guess. thanks so much! You are right - I am writing on the prototype of EE which is a bad idea – Varun Arora Feb 04 '15 at 22:37
  • If I am looking at the right code, `deep_extend` can take more than 2 arguments : https://github.com/unclechu/node-deep-extend/blob/master/index.js – nilgun Feb 04 '15 at 22:39
  • You are right again, sir @nilgun. When in doubt, one should go beyond the docs. Works! – Varun Arora Feb 04 '15 at 22:43