Options
All
  • Public
  • Public/Protected
  • All
Menu

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

{@link AsyncLimitedQueue#queue} operations are delayed (in unspecified order) until space becomes available through dequeue operations.

Type parameters

  • T

Hierarchy

  • AsyncLimitedQueue

Implements

Index

Constructors

constructor

  • Initialize the queue.

    throws

    An exception in case the limit is not an integer or is <= 0.

    Type parameters

    • T

    Parameters

    • limit: number

      A integer >= 1 specifying the number of elements after which queue() effectively blocks (i.e. the promise returned by it does not get "immediately" fulfilled for some informal value of immediately).

    • storageQueue: IAsyncQueue<T> = ...

      An asynchronous (non-limiting) queue backing the data. It defaults to a AsyncQueue.

    Returns AsyncLimitedQueue<T>

Properties

Private limitSem

limitSem: ISemaphore

Methods

[Symbol.asyncIterator]

  • [Symbol.asyncIterator](): AsyncIterableIterator<T>

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>

    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>

    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

queue

  • queue(data: T): Promise<void>

queueAll

  • queueAll(iterable: Iterable<T>): Promise<void>

queueAllAsync

  • queueAllAsync(iterable: AsyncIterable<T>): 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