At some point you might get limited by the Queue class as a data-structure. You have to add methods for new features, that break the definition of a Queue.
-
Multi threaded
dequeue(): Maybe you want to break the limit of "one request after the other". E.g. run up to max 5 tasks parallel. You can replacethis._pendingPromise(true|false) flag with a counter and check insinsidedequeue().async dequeue() { if (this._pendingCount > 5) return false; .. } -
Break out of the sequence: Image your
queueis filled with "update" tasks for your UI module. When a user opens a full screen overlay, you might want to pause thequeuefor the time the overlay is up (the new overlay needs to be renders with highest priority)..You could inject a new task to the front of your queue for blocking. The task is a Promise that resolves a
DeferredPromisewhich can not be resolved by the queue itself. It will be resolved by the script, on Overlay close- We need a way to insert new tasks at front (start) (1)
- [Deferred Promise][13] implementation (2)
this._blockPromise = false; onOpen() { if (!this._blockPromise ) { this._blockPromise = new DeferredPromise(); // <-- (2) this._queue.enqueue(() => this._blockPromise .then(() => { this._blockPromise = null; }), 'start'); // <-- (1) } } onClose() { if (this._blockPromise) { this._blockPromise.resolve(); } } -
Nested Queues: Since our
AutoQueuehandles functions.. you can enqueue a task toqueueAthat is resolved whenqueueBis empty. So you can fill up a newqueueBwith tasks (you want to disable the defaultauto dequeue()when new tasks get enqueued) and add a "queueB resolver"tasktoqueueA..