From 3b164761a791e371981ffa47c1df4ea477f1d4ba Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Tue, 12 Mar 2019 17:32:29 -0700 Subject: [PATCH 1/4] Don't clear set at end for flush reentrancy safety; canceling removes from set --- lib/utils/debounce.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils/debounce.js b/lib/utils/debounce.js index 6fb246052b..bd6f6dd7d2 100644 --- a/lib/utils/debounce.js +++ b/lib/utils/debounce.js @@ -161,6 +161,5 @@ export const flushDebouncers = function() { }); } }); - debouncerQueue = new Set(); return didFlush; }; \ No newline at end of file From a23ac645619a9ced1dc26ae3ae0a7d742c82f07e Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Tue, 12 Mar 2019 18:28:44 -0700 Subject: [PATCH 2/4] Ensure debouncer is removed from queue before running callback. --- lib/utils/debounce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/debounce.js b/lib/utils/debounce.js index bd6f6dd7d2..cf4311abbc 100644 --- a/lib/utils/debounce.js +++ b/lib/utils/debounce.js @@ -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(); }); } /** From 5886be5da947179c72d9280ae1aa74acbef7a73d Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Tue, 12 Mar 2019 18:28:55 -0700 Subject: [PATCH 3/4] Add tests. --- test/unit/debounce.html | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/unit/debounce.html b/test/unit/debounce.html index 5d20439147..2d770bb611 100644 --- a/test/unit/debounce.html +++ b/test/unit/debounce.html @@ -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() { @@ -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} = {}) => { @@ -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() { From 4cd703335fb0901c4d92da0d6200fd857fae3916 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Tue, 12 Mar 2019 23:20:31 -0700 Subject: [PATCH 4/4] Fix lint --- test/unit/debounce.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/debounce.html b/test/unit/debounce.html index 2d770bb611..a5a97d599c 100644 --- a/test/unit/debounce.html +++ b/test/unit/debounce.html @@ -62,7 +62,7 @@ setTimeout(() => { assert.isTrue(cb.calledOnce); done(); - }) + }); }); const testEnqueue = (shouldFlush, done) => {