Skip to content

Commit 2228f67

Browse files
committed
Sync with fixes on master branch.
1 parent d06334e commit 2228f67

File tree

2 files changed

+85
-41
lines changed

2 files changed

+85
-41
lines changed

lib/utils/debounce.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
this._callback = callback;
4040
this._timer = this._asyncModule.run(() => {
4141
this._timer = null;
42-
this._callback();
4342
debouncerQueue.delete(this);
43+
this._callback();
4444
});
4545
}
4646
/**
@@ -51,7 +51,6 @@
5151
cancel() {
5252
if (this.isActive()) {
5353
this._cancelAsync();
54-
this._timer = null;
5554
// Canceling a debouncer removes its spot from the flush queue,
5655
// so if a debouncer is manually canceled and re-debounced, it
5756
// will reset its flush order (this is a very minor difference from 1.x)
@@ -65,7 +64,10 @@
6564
* @return {void}
6665
*/
6766
_cancelAsync() {
68-
this._asyncModule.cancel(/** @type {number} */(this._timer));
67+
if (this.isActive()) {
68+
this._asyncModule.cancel(/** @type {number} */(this._timer));
69+
this._timer = null;
70+
}
6971
}
7072
/**
7173
* Flushes an active debouncer and returns a reference to itself.
@@ -158,7 +160,6 @@
158160
});
159161
}
160162
});
161-
debouncerQueue = new Set();
162163
return didFlush;
163164
};
164165

test/unit/debounce.html

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,86 @@
3434
Polymer({is: 'x-basic'});
3535
});
3636

37+
suite('enqueueDebouncer & flush', function() {
38+
39+
// NOTE: This is a regression test; the bug it fixed only occurred if the
40+
// debouncer was flushed before any microtasks run, hence it should be
41+
// first in this file
42+
test('re-enqueue canceled debouncer', function() {
43+
const cb = sinon.spy();
44+
let db;
45+
db = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb);
46+
Polymer.enqueueDebouncer(db);
47+
db.cancel();
48+
assert.equal(db.isActive(), false);
49+
assert.equal(cb.callCount, 0);
50+
db = Polymer.Debouncer.debounce(db, Polymer.Async.microTask, cb);
51+
Polymer.enqueueDebouncer(db);
52+
Polymer.flush();
53+
assert.isTrue(cb.calledOnce);
54+
});
55+
56+
test('flushDebouncers from enqueued debouncer', function(done) {
57+
const cb = sinon.spy(() => Polymer.flush());
58+
let db = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb);
59+
Polymer.enqueueDebouncer(db);
60+
setTimeout(() => {
61+
assert.isTrue(cb.calledOnce);
62+
done();
63+
});
64+
});
65+
66+
const testEnqueue = (shouldFlush, done) => {
67+
const actualOrder = [];
68+
const enqueue = (type, {db, cb} = {}) => {
69+
cb = cb || (() => actualOrder.push(cb));
70+
db = Polymer.Debouncer.debounce(db, type, cb);
71+
Polymer.enqueueDebouncer(db);
72+
return {db, cb};
73+
};
74+
const db1 = enqueue(Polymer.Async.microTask);
75+
const db2 = enqueue(Polymer.Async.microTask);
76+
const db3 = enqueue(Polymer.Async.timeOut);
77+
const db4 = enqueue(Polymer.Async.microTask);
78+
enqueue(Polymer.Async.microTask, db2);
79+
enqueue(Polymer.Async.microTask, db1);
80+
if (shouldFlush) {
81+
Polymer.flush();
82+
assert.deepEqual(actualOrder, [db1.cb, db2.cb, db3.cb, db4.cb]);
83+
done();
84+
} else {
85+
Polymer.Async.timeOut.run(() => {
86+
assert.deepEqual(actualOrder, [db4.cb, db2.cb, db1.cb, db3.cb]);
87+
done();
88+
});
89+
}
90+
};
91+
92+
test('non-flushed', function(done) {
93+
testEnqueue(false, done);
94+
});
95+
96+
test('flushed', function(done) {
97+
testEnqueue(true, done);
98+
});
99+
100+
test('reentrant flush', function() {
101+
const cb2 = sinon.spy();
102+
let db2;
103+
const cb1 = sinon.spy(() => {
104+
Polymer.flush();
105+
db2 = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb2);
106+
Polymer.enqueueDebouncer(db2);
107+
});
108+
const db1 = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb1);
109+
Polymer.enqueueDebouncer(db1);
110+
Polymer.flush();
111+
assert.isTrue(cb1.calledOnce);
112+
assert.isTrue(cb2.calledOnce);
113+
});
114+
115+
});
116+
37117
suite('debounce', function() {
38118
var element;
39119

@@ -208,43 +288,6 @@
208288
});
209289

210290
});
211-
});
212-
213-
suite('enqueueDebouncer & flush', function() {
214-
215-
const testEnqueue = (shouldFlush, done) => {
216-
const actualOrder = [];
217-
const enqueue = (type, {db, cb} = {}) => {
218-
cb = cb || (() => actualOrder.push(cb));
219-
db = Polymer.Debouncer.debounce(db, type, cb);
220-
Polymer.enqueueDebouncer(db);
221-
return {db, cb};
222-
};
223-
const db1 = enqueue(Polymer.Async.microTask);
224-
const db2 = enqueue(Polymer.Async.microTask);
225-
const db3 = enqueue(Polymer.Async.timeOut);
226-
const db4 = enqueue(Polymer.Async.microTask);
227-
enqueue(Polymer.Async.microTask, db2);
228-
enqueue(Polymer.Async.microTask, db1);
229-
if (shouldFlush) {
230-
Polymer.flush();
231-
assert.deepEqual(actualOrder, [db1.cb, db2.cb, db3.cb, db4.cb]);
232-
done();
233-
} else {
234-
Polymer.Async.timeOut.run(() => {
235-
assert.deepEqual(actualOrder, [db4.cb, db2.cb, db1.cb, db3.cb]);
236-
done();
237-
});
238-
}
239-
};
240-
241-
test('non-flushed', function(done) {
242-
testEnqueue(false, done);
243-
});
244-
245-
test('flushed', function(done) {
246-
testEnqueue(true, done);
247-
});
248291

249292
});
250293
</script>

0 commit comments

Comments
 (0)