Skip to content

Commit

Permalink
Merge pull request #140 from lmcardle/patch-1
Browse files Browse the repository at this point in the history
cancel scheduled debounce when service destroyed
  • Loading branch information
mike-north authored Aug 23, 2018
2 parents 5d60375 + 5d24525 commit 518c73b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions addon/services/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { classify } from '@ember/string';
import { oneWay, readOnly } from '@ember/object/computed';
import { debounce } from '@ember/runloop';
import { debounce, cancel } from '@ember/runloop';
import EmberObject, { set, getWithDefault } from '@ember/object';

// jscs:disable disallowDirectPropertyAccess
Expand All @@ -15,7 +15,6 @@ export default Base.extend(Evented, {
_oldHeight: window.innerHeight,
_oldWidthDebounced: window.innerWidth,
_oldHeightDebounced: window.innerHeight,

debounceTimeout: oneWay('defaultDebounceTimeout'),
widthSensitive: oneWay('defaultWidthSensitive'),
heightSensitive: oneWay('defaultHeightSensitive'),
Expand All @@ -28,7 +27,8 @@ export default Base.extend(Evented, {
this._setDefaults();
this._onResizeHandler = evt => {
this._fireResizeNotification(evt);
debounce(this, this._fireDebouncedResizeNotification, evt, this.get('debounceTimeout'));
let scheduledDebounce = debounce(this, this._fireDebouncedResizeNotification, evt, this.get('debounceTimeout'));
this._scheduledDebounce = scheduledDebounce;
};
if (typeof FastBoot === 'undefined') {
this._installResizeListener();
Expand All @@ -40,6 +40,7 @@ export default Base.extend(Evented, {
if (typeof FastBoot === 'undefined') {
this._uninstallResizeListener();
}
this._cancelScheduledDebounce();
},

_setDefaults() {
Expand Down Expand Up @@ -76,6 +77,10 @@ export default Base.extend(Evented, {
window.removeEventListener('resize', this._onResizeHandler);
},

_cancelScheduledDebounce() {
cancel(this._scheduledDebounce);
},

_fireResizeNotification(evt) {
const { innerWidth, innerHeight } = window;
if (this._hasWindowSizeChanged(innerWidth, innerHeight)) {
Expand Down

0 comments on commit 518c73b

Please sign in to comment.