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

    Class AwaitableStack<T>

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

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

    const
    stack = new AwaitableStack<string>(),
    promise1 = stack.pop(),
    promise2 = stack.pop()

    assertEquals(stack.getSize(), -2)

    // since this is a stack, the last promise is the first one to be served.
    stack.push("hello")
    assertEquals(await promise2, "hello")
    assertEquals(stack.getSize(), -1)

    stack.push("world")
    assertEquals(await promise1, "world")
    assertEquals(stack.getSize(), 0)

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

    stack.push("1", "2", "3")
    // dump all immediately available items, clearing off the internal list.
    // the last item in the array (`"3"`) is at the top of the stack.
    assertEquals(stack.dump(), ["1", "2", "3"])
    assertEquals(stack.dump(), []) // nothing else to dump.

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

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

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

    Type Parameters

    • T
    Index

    Constructors

    Methods

    Constructors

    Methods

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

      Note

      the last item in the returned array is at the top of the stack, while the first item in the array is the bottom of the stack.

      Returns T[]

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

      Note

      the last item in the returned array is at the top of the stack, meaning that it should be served/resolved first, while the first item in the array is the last resolver that should be served (bottom of the stack).

      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