Skip to content

Commit

Permalink
fix: Properly remove listener, look for destroyed state before settin…
Browse files Browse the repository at this point in the history
…g properties (#87)
  • Loading branch information
lolmaus authored and knownasilya committed Sep 9, 2017
1 parent 398f8d6 commit a9ee22b
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions addon/components/x-toggle-switch/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default Component.extend(RecognizerMixin, {
this._disableLabelUntilMouseUp();
},

willDestroyElement() {
this._removeListener()
},

/*
When you pan with a mouse and release the mouse button over the <label>
element, a click event happens and returns the toggle to its initial
Expand All @@ -44,10 +48,31 @@ export default Component.extend(RecognizerMixin, {
return;
}

this.set('labelDisabled', true);
const _listener = () => {
next(() => {
if (this.get('isDestroying') || this.get('isDestroyed')) {
return;
}

document.addEventListener('mouseup', () => {
next(() => this.set('labelDisabled', false));
this._removeListener()
this.set('labelDisabled', false);
});
};

this.setProperties({
labelDisabled: true,
_listener
});

document.addEventListener('mouseup', _listener);
},

_removeListener() {
const _listener = this.get('_listener')

if (_listener) {
document.removeEventListener('mouseup', _listener)
this.set('_listener', null)
}
}
});

0 comments on commit a9ee22b

Please sign in to comment.