diff --git a/src/createStore.js b/src/createStore.js index 3b70865..c77ff36 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -27,7 +27,12 @@ module.exports = function(definition) { throw Error("Store cannot listen to this listenable because of circular loop"); } this.registered.push(listenable); - return listenable.listen(callback, this); + var unsubscribe = listenable.listen(callback, this); + var self = this; + return function () { + unsubscribe(); + self.registered.splice(self.registered.indexOf(listenable), 1); + }; }; Store.prototype.listen = function(callback, bindContext) { var eventHandler = function(args) { diff --git a/test/creatingStores.spec.js b/test/creatingStores.spec.js index 7f1ed44..031966f 100644 --- a/test/creatingStores.spec.js +++ b/test/creatingStores.spec.js @@ -67,6 +67,13 @@ describe('Creating stores', function() { }, 20); }); + it('can listenTo the same action again', function() { + store.listenTo(action, store.actionCalled); + action(1337, 'ninja'); + + return assert.eventually.deepEqual(promise, [1337, 'ninja']); + }); + }); describe('listening to the store', function() {