From 7d44b181f23ff19dfa9e8af12c7f0e6e27b39100 Mon Sep 17 00:00:00 2001 From: Gareth Oates Date: Fri, 16 Feb 2018 10:58:18 +0100 Subject: [PATCH 1/3] this._changeHandlers was undefined when calling cancel() --- store.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/store.js b/store.js index 63a99cca050b..69234551135b 100644 --- a/store.js +++ b/store.js @@ -115,7 +115,7 @@ assign(Store.prototype, { cancel: function() { var index = this._changeHandlers.indexOf(callback); if (~index) this._changeHandlers.splice(index, 1); - } + }.bind(this) }; }, @@ -163,4 +163,4 @@ assign(Store.prototype, { } }); -export { Store }; \ No newline at end of file +export { Store }; From 389b5c9ff98d23df161564a91ed842b09e23db1e Mon Sep 17 00:00:00 2001 From: Gareth Oates Date: Sun, 18 Feb 2018 14:19:46 +0100 Subject: [PATCH 2/3] There was no test to determine if onchange cancel threw an error --- test/store/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/store/index.js b/test/store/index.js index e3c024e4d504..fd84322d3528 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -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({ From 957179fc7fc719d29b6203b2caff66b8c26e229f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2018 08:47:49 -0500 Subject: [PATCH 3/3] use local `this` variable instead of .bind --- store.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/store.js b/store.js index 69234551135b..9f0661811b34 100644 --- a/store.js +++ b/store.js @@ -111,11 +111,14 @@ 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); - }.bind(this) + var index = store._changeHandlers.indexOf(callback); + if (~index) store._changeHandlers.splice(index, 1); + } }; },