Skip to content

Commit

Permalink
fix: add debounced.cancel and use it in ResizeManager (#5378)
Browse files Browse the repository at this point in the history
It's possible that ResizeManager will trigger its debounce handler but
then get disposed before the handler runs. This can cause tests to fail,
so, instead, we should cancel the debounce when ResizeManager is
disposed.
  • Loading branch information
gkatsev authored Aug 13, 2018
1 parent c3098ee commit 8e9d92c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/js/resize-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class ResizeManager extends Component {
this.off('load', this.loadListener_);
}

if (this.debouncedHandler_) {
this.debouncedHandler_.cancel();
}

this.ResizeObserver = null;
this.resizeObserver = null;
this.debouncedHandler_ = null;
Expand Down
11 changes: 10 additions & 1 deletion src/js/utils/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ export const throttle = function(fn, wait) {
export const debounce = function(func, wait, immediate, context = window) {
let timeout;

const cancel = () => {
context.clearTimeout(timeout);
timeout = null;
};

/* eslint-disable consistent-this */
return function() {
const debounced = function() {
const self = this;
const args = arguments;

Expand All @@ -119,4 +124,8 @@ export const debounce = function(func, wait, immediate, context = window) {
timeout = context.setTimeout(later, wait);
};
/* eslint-enable consistent-this */

debounced.cancel = cancel;

return debounced;
};

0 comments on commit 8e9d92c

Please sign in to comment.