Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/utils/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ define(function (require, exports, module) {
* has finished.
*/
function PromiseQueue() {
this._queue = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should change the initialization in the prototype to null to avoid confusion (since the empty array there would never be used anyway).

this._curPromise = null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary - it should be fine to just assign null to _curPromise in the prototype since it's a primitive value.

}

/**
Expand Down
43 changes: 43 additions & 0 deletions test/spec/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,49 @@ define(function (require, exports, module) {
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});

it("should be able to run two queues simultaneously without clashing", function () {
var queue2 = new PromiseQueue(),
q1FnInfo1 = makeFn("one"),
q1FnInfo2 = makeFn("two"),
q2FnInfo3 = makeFn("three"),
q2FnInfo4 = makeFn("four");

//queue one
queue.add(q1FnInfo1.fn);
queue.add(q1FnInfo2.fn);
expect(calledFns.one).toBe(true);
expect(calledFns.two).toBeUndefined();
//queue one should have one in _queue,
//queue two should have zero in _queue
expect(queue._queue.length).toBe(1);
expect(queue2._queue.length).toBe(0);

//queue two
queue2.add(q2FnInfo3.fn);
queue2.add(q2FnInfo4.fn);
expect(calledFns.three).toBe(true);
expect(calledFns.four).toBeUndefined();
//queue one and two should have one in _queue
expect(queue._queue.length).toBe(1);
expect(queue2._queue.length).toBe(1);

q1FnInfo1.deferred.resolve();
expect(calledFns.two).toBe(true);
expect(queue._queue.length).toBe(0);

q1FnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);

q2FnInfo3.deferred.resolve();
expect(calledFns.three).toBe(true);
expect(queue2._queue.length).toBe(0);

q2FnInfo4.deferred.resolve();
expect(queue2._queue.length).toBe(0);
expect(queue2._curPromise).toBe(null);
});
});
});
});