Skip to content

Commit

Permalink
Properly reset queue after flush completes
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeyls committed Jul 24, 2019
1 parent e527b10 commit ed72580
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ function queue() {
},

flush() {
if (!completed) {
if (completed === null) {
completed = new Promise(function (resolve) {
window.setTimeout(function () {
flush();
completed = null;
resolve();
}, 0);
});
Expand Down
15 changes: 15 additions & 0 deletions test/queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ test('flush will run fns added after initial call', () => {
expect(fn2.mock.calls.length).toEqual(1);
});
});

test('flush will run again after delayed completion', (done) => {
let q = queue();
let fn = jest.fn();
let fn2 = jest.fn();
q.add(fn);

q.flush().then(() => {
q.add(fn2);
q.flush().then(() => {
expect(fn2.mock.calls.length).toEqual(1);
done();
});
});
});

0 comments on commit ed72580

Please sign in to comment.