Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store.onchange.cancel() function not scoped correctly. #1177

Closed
GarethOates opened this issue Feb 16, 2018 · 1 comment
Closed

store.onchange.cancel() function not scoped correctly. #1177

GarethOates opened this issue Feb 16, 2018 · 1 comment

Comments

@GarethOates
Copy link

When attempting to cancel the onchange event listener on my svelte store.

let handler = store.onchange(storeChanged);
handler.cancel();

I kept receiving the following error:

TypeError: this._changeHandlers is undefined

This is due to the scope of the returned object which contains the cancel() function not having a reference to the changeHandlers array.

The workaround in code that I did for this was the following, which I suspect is not what you want the user to have to do.

handler.cancel.bind(store)()

I've written a pull request to fix this, which binds the cancel() function to this, in order to maintain the reference to this._changeHandlers.

return {
    cancel: function() {
		var index = this._changeHandlers.indexOf(callback);
		if (~index) this._changeHandlers.splice(index, 1);
    }.bind(this)
};
@Rich-Harris
Copy link
Member

Released 1.55.1 with the fix, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants