Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 336fe54

Browse files
author
Narciso Jaramillo
committed
Merge pull request #7485 from jayther/promisequeuesharefix
Fixed bug with PromiseQueue instances sharing same queue.
2 parents 79df1a0 + f60c89b commit 336fe54

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/utils/Async.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ define(function (require, exports, module) {
424424
* has finished.
425425
*/
426426
function PromiseQueue() {
427+
this._queue = [];
427428
}
428429

429430
/**
@@ -432,7 +433,7 @@ define(function (require, exports, module) {
432433
* The queue of operations to execute sequentially. Note that even if this array is empty, there might
433434
* still be an operation we need to wait on; that operation's promise is stored in _curPromise.
434435
*/
435-
PromiseQueue.prototype._queue = [];
436+
PromiseQueue.prototype._queue = null;
436437

437438
/**
438439
* @private

test/spec/Async-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,49 @@ define(function (require, exports, module) {
499499
expect(queue._queue.length).toBe(0);
500500
expect(queue._curPromise).toBe(null);
501501
});
502+
503+
it("should be able to run two queues simultaneously without clashing", function () {
504+
var queue2 = new PromiseQueue(),
505+
q1FnInfo1 = makeFn("one"),
506+
q1FnInfo2 = makeFn("two"),
507+
q2FnInfo3 = makeFn("three"),
508+
q2FnInfo4 = makeFn("four");
509+
510+
//queue one
511+
queue.add(q1FnInfo1.fn);
512+
queue.add(q1FnInfo2.fn);
513+
expect(calledFns.one).toBe(true);
514+
expect(calledFns.two).toBeUndefined();
515+
//queue one should have one in _queue,
516+
//queue two should have zero in _queue
517+
expect(queue._queue.length).toBe(1);
518+
expect(queue2._queue.length).toBe(0);
519+
520+
//queue two
521+
queue2.add(q2FnInfo3.fn);
522+
queue2.add(q2FnInfo4.fn);
523+
expect(calledFns.three).toBe(true);
524+
expect(calledFns.four).toBeUndefined();
525+
//queue one and two should have one in _queue
526+
expect(queue._queue.length).toBe(1);
527+
expect(queue2._queue.length).toBe(1);
528+
529+
q1FnInfo1.deferred.resolve();
530+
expect(calledFns.two).toBe(true);
531+
expect(queue._queue.length).toBe(0);
532+
533+
q1FnInfo2.deferred.resolve();
534+
expect(queue._queue.length).toBe(0);
535+
expect(queue._curPromise).toBe(null);
536+
537+
q2FnInfo3.deferred.resolve();
538+
expect(calledFns.three).toBe(true);
539+
expect(queue2._queue.length).toBe(0);
540+
541+
q2FnInfo4.deferred.resolve();
542+
expect(queue2._queue.length).toBe(0);
543+
expect(queue2._curPromise).toBe(null);
544+
});
502545
});
503546
});
504547
});

0 commit comments

Comments
 (0)