Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Added BaseStore#unregisterAll().
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Feb 6, 2015
1 parent 1106300 commit b6655f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
this.actionHandlers[action] = [store];
}
},
unregister: function(action, store) {
console.log("unregister", action, store);
if (this.registeredFor(action).indexOf(store) === -1) return;
if (!this.actionHandlers.hasOwnProperty(action)) return;
this.actionHandlers[action] = this.actionHandlers[action].filter(function(registeredStore) {
return store !== registeredStore;
});
},
dispatch: function(action) {
var actionArgs = slice.call(arguments, 1);
(this.actionHandlers[action] || []).forEach(function(store) {
Expand Down Expand Up @@ -119,6 +127,20 @@
this.__listeners = this.__listeners.filter(function(registered) {
return registered !== listener;
});
},
registerAll: function() {
this.actions.forEach(function(Actions) {
Actions._registered.forEach(function(action) {
this._dispatcher.register(action, this);
}, this);
}, this);
},
unregisterAll: function() {
this.actions.forEach(function(Actions) {
Actions._registered.forEach(function(action) {
this._dispatcher.unregister(action, this);
}, this);
}, this);
}
};

Expand All @@ -139,13 +161,8 @@
if (!Array.isArray(this.actions) || this.actions.length === 0) {
throw new Error("Stores must define a non-empty actions array");
}
this.actions.forEach(function(Actions) {
// XXX check for valid Actions object
var dispatcher = Actions._dispatcher;
Actions._registered.forEach(function(action) {
dispatcher.register(action, this);
}, this);
}, this);
this._dispatcher = this.actions[0]._dispatcher;
this.registerAll();
}
BaseStore.prototype = merge({
get state() {
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,23 @@ describe("DocBrown.createStore()", function() {
sinon.assert.calledOnce(listener);
});
});

describe("#unregisterAll()", function() {
it("should unregister as action handler from the dispatcher", function() {
var Dispatcher = DocBrown.createDispatcher();
var Actions = DocBrown.createActions(Dispatcher, ["a", "b"]);
var Store = DocBrown.createStore({actions: [Actions]});
var store = new Store();

expect(Dispatcher.registeredFor("a")).to.include(store);
expect(Dispatcher.registeredFor("b")).to.include(store);

store.unregisterAll();

expect(Dispatcher.registeredFor("a")).to.not.include(store);
expect(Dispatcher.registeredFor("b")).to.not.include(store);
});
});
});

describe("DocBrown.storeMixin()", function() {
Expand Down

0 comments on commit b6655f7

Please sign in to comment.