Options
All
  • Public
  • Public/Protected
  • All
Menu

Asynchronous entrance-limited FIFO queue with a Promise-driven dequeue operation.

Contrary to IAsyncQueue, the queue operation is Promise-driven as well, e.g. implementations might delay entrance into the queue, e.g. to enforce a limit on the number of elements stored in the queue at the same time, cf. AsyncLimitedQueue. Other types of entrance limitations are conceivable as well, such as a restriction on the sum of contained elements in case of a number queue.

All element values are allowed, especially falsy ones, e.g. false, 0, undefined, null, [], {} are all valid elements which can be queued and dequeued.

IAsyncLimitedQueue#queue operations are possibly delayed and executed in implementation-dependenent order.

example

Issueing multiple queue operations without awaiting the previous ones may result in implementation-defined insertion order.

queue.queue(1);
queue.queue(2);

await queue.dequeue(); // can be 1 or 2
await queue.dequeue(); // can be 1 or 2 as well (the remaining number)
example

If you would like to retain the order, await the queue operations, use IAsyncLimitedQueue#queueAll or IAsyncLimitedQueue#queueAllAsync.

await queue.queue(1);
await queue.queue(2);
queue.queueAll([1, 2]);

The {@link AsyncIterable} interface iterates the queue's (future) contents ad infinitum. Users are advised to signal the end by manual insertion of a special value (a so-called poison pill), see IAsyncQueue.

Type parameters

  • T

Hierarchy

  • AsyncIterable<T>
    • IAsyncLimitedQueue

Implemented by

Index

Methods

[Symbol.asyncIterator]

  • [Symbol.asyncIterator](): AsyncIterator<T, any, undefined>
  • Returns AsyncIterator<T, any, undefined>

dequeue

  • dequeue(): Promise<T>
  • Dequeue an element, waiting for data to be available if necessary.

    Returns Promise<T>

    A promise which is fulfilled when an element (as queued by queue()) becomes available. If multiple dequeus() are issued sequentially, it is implementation-defined whether they are fulfilled in the same order or not. However, the data is still retrieved in FIFO fashion, meaning the first fulfilled promise gets the first element, the second fulfilled the second one and so forth.

offer

  • offer(data: T): boolean
  • Offer an element, only queueing it if entrance is available at the time of the call.

    Parameters

    • data: T

    Returns boolean

    True if the element could be inserted right away. False otherwise.

offerAll

  • offerAll(iterable: Iterable<T>): number
  • Offer all elements of an iterable for in-order insertion.

    Parameters

    • iterable: Iterable<T>

      An iterable whose first (limit - queue.size()) elements will be inserted. Iterables which iterate an infinite number of elements can also be passed and will not result in an endless loop.

    Returns number

    The number of elements, which could be inserted right away. Possibly 0 when the queue was full at the time of the call.

offerAllAsync

  • offerAllAsync(iterable: AsyncIterable<T>): Promise<number>
  • Offer all elements of an asynchronous iterable for in-order insertion.

    Parameters

    • iterable: AsyncIterable<T>

      An iterable whose elements will be offered in-order for this queue. The method will stop querying and offering further elements upon the first offer call, which returns false.
      Contrary to offerAll, iterables iterating an infinite number of elements might prevent the Promise, which offerAllAsync returns, from ever resolving.
      This depends on dequeue operations which could get scheduled by the JS VM while elements from the passed asynchronous iterator are accessed.

    Returns Promise<number>

    A promise resolving to the number of elements, which could be inserted (offered successfully) consecutively without waiting. Possibly 0 when the queue was full at the time of the call. Fulfillment of this promise is not guaranteed in case of infinite iterables.

poll

  • poll(): T
  • Dequeue an element if available or throw an exception otherwise.

    throws

    A NoElementError exception if the queue is empty at the time of the call.

    Returns T

    The first element of the queue.

queue

  • queue(data: T): Promise<void>
  • Queue an element, waiting for entrance if necessary.

    example
    queue.queue(42).then(() => {
      // 42 is now stored within the queue
    });
    

    Parameters

    • data: T

    Returns Promise<void>

queueAll

  • queueAll(iterable: Iterable<T>): Promise<void>
  • Queue all elements of an iterable, e.g. an array or a generator function.

    see

    IAsyncQueue#queueAll

    Parameters

    • iterable: Iterable<T>

    Returns Promise<void>

queueAllAsync

  • queueAllAsync(iterable: AsyncIterable<T>): Promise<void>
  • Queue all elements of an asynchronous iterable, e.g. an asynchronous generator functions.

    see

    IAsyncQueue#queueAllAsync

    Parameters

    • iterable: AsyncIterable<T>

    Returns Promise<void>

size

  • size(): number
  • Return the current size at the moment of the call.

    Even though code like

    if (queue.size() >= 1) {
      const element = queue.poll();
    }
    

    is technically not wrong (due to JS' execution model), users are advised to avoid this pattern. Instead, users are encouraged to

    • in cases where waiting for a promise is impossible, to use poll and catch the exception,
    • or to use dequeue with JS' await or queue.dequeue().then(...).

    Returns number

Generated using TypeDoc