Skip to content

Commit

Permalink
Merge pull request #1178 from GarethOates/storeCancelScope
Browse files Browse the repository at this point in the history
this._changeHandlers was undefined when calling cancel()
  • Loading branch information
Rich-Harris authored Feb 23, 2018
2 parents 72a65c4 + 957179f commit fba8a94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ assign(Store.prototype, {

onchange: function(callback) {
this._changeHandlers.push(callback);

var store = this;

return {
cancel: function() {
var index = this._changeHandlers.indexOf(callback);
if (~index) this._changeHandlers.splice(index, 1);
var index = store._changeHandlers.indexOf(callback);
if (~index) store._changeHandlers.splice(index, 1);
}
};
},
Expand Down Expand Up @@ -163,4 +166,4 @@ assign(Store.prototype, {
}
});

export { Store };
export { Store };
9 changes: 9 additions & 0 deletions test/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe('store', () => {
});
});

it('allows user to cancel state change callback', () => {
const store = new Store();
const handler = store.onchange(() => {});

assert.doesNotThrow(() => {
handler.cancel();
}, TypeError, 'this._changeHandlers is undefined');
});

describe('computed', () => {
it('computes a property based on data', () => {
const store = new Store({
Expand Down

0 comments on commit fba8a94

Please sign in to comment.