Skip to content

Commit

Permalink
Merge pull request #5509 from Polymer/5250-dequeue-before-run
Browse files Browse the repository at this point in the history
Additional debouncer fixes
  • Loading branch information
kevinpschaaf authored Mar 14, 2019
2 parents ef82ddf + 4cd7033 commit 3079cae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/utils/debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class Debouncer {
this._callback = callback;
this._timer = this._asyncModule.run(() => {
this._timer = null;
this._callback();
debouncerQueue.delete(this);
this._callback();
});
}
/**
Expand Down Expand Up @@ -161,6 +161,5 @@ export const flushDebouncers = function() {
});
}
});
debouncerQueue = new Set();
return didFlush;
};
27 changes: 26 additions & 1 deletion test/unit/debounce.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

suite('enqueueDebouncer & flush', function() {

// NOTE: This is a regression test; the bug it fixed only occured if the
// NOTE: This is a regression test; the bug it fixed only occurred if the
// debouncer was flushed before any microtasks run, hence it should be
// first in this file
test('re-enqueue canceled debouncer', function() {
Expand All @@ -55,6 +55,16 @@
assert.isTrue(cb.calledOnce);
});

test('flushDebouncers from enqueued debouncer', function(done) {
const cb = sinon.spy(() => flush());
let db = Debouncer.debounce(null, microTask, cb);
enqueueDebouncer(db);
setTimeout(() => {
assert.isTrue(cb.calledOnce);
done();
});
});

const testEnqueue = (shouldFlush, done) => {
const actualOrder = [];
const enqueue = (type, {db, cb} = {}) => {
Expand Down Expand Up @@ -89,6 +99,21 @@
testEnqueue(true, done);
});

test('reentrant flush', function() {
const cb2 = sinon.spy();
let db2;
const cb1 = sinon.spy(() => {
flush();
db2 = Debouncer.debounce(null, microTask, cb2);
enqueueDebouncer(db2);
});
const db1 = Debouncer.debounce(null, microTask, cb1);
enqueueDebouncer(db1);
flush();
assert.isTrue(cb1.calledOnce);
assert.isTrue(cb2.calledOnce);
});

});

suite('debounce', function() {
Expand Down

0 comments on commit 3079cae

Please sign in to comment.