crnt - v0.0.31
    Preparing search index...

    Interface Queue<T>

    An asynchronous queue. This is modelled after:

    • Go's channels.
    • Java's ConcurrentLinkedQueue

    Supported operations:

    • Promise-based enqueue and dequeue operations
    • synchronous maybeEnqueue and maybeDequeue operations
    • AsyncIterable to drain the queue.
    • closing the queue, preventing further enqueue operations but allowing dequeue until exhausted.
    • the queue is unbounded by default but it can be initialized with a finite capacity, including 0. in this case, it behaves like Java's SynchronousQueue / Unbuffered Go Channel.
    import { newQueue } from 'crnt';

    // Create a bounded queue
    const queue = newQueue<string>(2);

    // Producer
    await queue.enqueue('hello');
    await queue.enqueue('world');

    // Consumer using async iteration
    for await (const item of queue) {
    console.log(item); // 'hello', 'world'
    if (item === 'world') break;
    }
    interface Queue<T> {
        capacity: number;
        closed: boolean;
        size: number;
        asArray(): T[];
        close(): void;
        dequeue(options?: Options): Promise<T>;
        enqueue(item: T, options?: Options): Promise<void>;
        maybeDequeue(): [T, true] | [undefined, false];
        maybeEnqueue(item: T): boolean;
    }

    Type Parameters

    • T

    Hierarchy

    Index

    Properties

    capacity: number

    the maximum number of items the queue can hold, infinity if unbounded.

    closed: boolean

    whether the queue is closed

    size: number

    the number of items in the queue

    Methods

    • Returns the copy of the current items in the queue, in the first-in-first-out (FIFO) order. This is mostly useful for debugging.

      Returns T[]

    • close the queue, preventing further enqueue operations but allowing dequeue until exhausted

      Returns void

    • asynchronously dequeue an item, waiting until an item becomes available, or throw if aborted

      Parameters

      Returns Promise<T>

    • asynchronously enqueue an item, waiting until space becomes available, or throw if aborted

      Parameters

      Returns Promise<void>

    • synchronously dequeue an item if there is one, or return false

      Returns [T, true] | [undefined, false]

    • synchronously enqueue an item if there is space, or return false

      Parameters

      • item: T

      Returns boolean