@oazmi/kitchensink - v0.10.1
    Preparing search index...

    Class AwaitableQueue<T>

    create a queue list that can be asynchronously awaited to receive new items.

    TODO: should I also make the items non-private, so that they're accessible from super classes?

    import { assertEquals } from "jsr:@std/assert"

    const
    queue = new AwaitableQueue<string>(),
    promise1 = queue.shift(),
    promise2 = queue.shift()

    assertEquals(queue.getSize(), -2)

    queue.push("hello")
    assertEquals(await promise1, "hello")
    assertEquals(queue.getSize(), -1)

    queue.push("world")
    assertEquals(await promise2, "world")
    assertEquals(queue.getSize(), 0)

    queue.push("abcd")
    assertEquals(queue.getSize(), 1)
    // notice that when extra items are present,
    // an immediate value is returned rather than a promise.
    assertEquals(queue.shift(), "abcd")

    queue.push("1", "2", "3")
    // dump all immediately available items, clearing off the internal list.
    assertEquals(queue.dump(), ["1", "2", "3"])
    assertEquals(queue.dump(), []) // nothing else to dump.

    const
    promise3 = queue.shift(),
    promise4 = queue.shift(),
    promise5 = queue.shift(),
    // drop all promise resolvers, clearing off the internal list.
    [resolve3, resolve4, resolve5] = queue.drop()

    assertEquals(queue.drop(), []) // no more promise resolvers remain to be dropped.
    queue.push("1", "2", "3", "4", "5")
    assertEquals(queue.shift(), "1")
    assertEquals(queue.shift(), "2")

    // `promise3` to `promise5` remain unresolved right now.
    // but since we don't want forever hagging promises, we will resolve them below:
    resolve3(await queue.shift())
    resolve4(await queue.shift())
    resolve5(await queue.shift())
    assertEquals(await promise3, "3")
    assertEquals(await promise4, "4")
    assertEquals(await promise5, "5")

    Type Parameters

    • T
    Index

    Constructors

    Methods

    Constructors

    Methods

    • dump off all currently available queued items, and receive them as a returned value.

      Note

      the first item in the returned array is the first item in the queue (highest priority/first to be served), while the last item in the array is at the end of the queue (least priority/last to be served).

      Returns T[]

    • drop off the resolvers of all currently unresolved promises in the queue. this means that all existing promises will remain hanging, unless you resolve them yourself with the returned resolver functions.

      Note

      the first item in the returned array is the first resolver function in the queue (highest priority/first to be served), while the last item in the array is the last resolver that should be served in the queue (least priority).

      Returns PromiseResolver<T>[]

    • returns the number of immediately available items, or the number of queued up requests.

      • when the return value is a positive value n, it indicates that |n| number of items are queued up, and you can immediately shift |n| number of times.
      • when the return value is a negative value n, it indicates that |n| number of promises are waiting to be resolved, and that if you shift right now, you will be receiving a promise to the |n| + 1 item in the future (relative to now).

      Returns number