UNPKG

2.83 kBJavaScriptView Raw
1"use strict";
2/*! *****************************************************************************
3Copyright (c) Microsoft Corporation.
4Licensed under the Apache License, Version 2.0.
5
6See LICENSE file in the project root for details.
7***************************************************************************** */
8Object.defineProperty(exports, "__esModule", { value: true });
9const utils_1 = require("./utils");
10/**
11 * An asynchronous queue.
12 */
13class AsyncQueue {
14 /**
15 * Initializes a new instance of the AsyncQueue class.
16 *
17 * @param iterable An optional iterable of values or promises.
18 */
19 constructor(iterable) {
20 this._available = undefined;
21 this._pending = undefined;
22 if (!utils_1.isIterable(iterable, /*optional*/ true))
23 throw new TypeError("Object not iterable: iterable.");
24 if (!utils_1.isMissing(iterable)) {
25 this._available = [];
26 for (const value of iterable) {
27 this._available.push(Promise.resolve(value));
28 }
29 }
30 }
31 /**
32 * Gets the number of entries in the queue.
33 * When positive, indicates the number of entries available to get.
34 * When negative, indicates the number of requests waiting to be fulfilled.
35 */
36 get size() {
37 if (this._available && this._available.length > 0) {
38 return this._available.length;
39 }
40 if (this._pending && this._pending.length > 0) {
41 return -this._pending.length;
42 }
43 return 0;
44 }
45 /**
46 * Adds a value to the end of the queue. If the queue is empty but has a pending
47 * dequeue request, the value will be dequeued and the request fulfilled.
48 *
49 * @param value A value or promise to add to the queue.
50 */
51 put(value) {
52 if (this._pending !== undefined) {
53 const resolve = this._pending.shift();
54 if (resolve !== undefined) {
55 resolve(value);
56 return;
57 }
58 }
59 if (this._available === undefined) {
60 this._available = [];
61 }
62 this._available.push(Promise.resolve(value));
63 }
64 /**
65 * Removes and returns a Promise for the first value in the queue. If the queue is empty,
66 * returns a Promise for the next value to be added to the queue.
67 */
68 get() {
69 if (this._available !== undefined) {
70 const promise = this._available.shift();
71 if (promise !== undefined) {
72 return promise;
73 }
74 }
75 if (this._pending === undefined) {
76 this._pending = [];
77 }
78 return new Promise(resolve => { this._pending.push(resolve); });
79 }
80}
81exports.AsyncQueue = AsyncQueue;