Skip to main content

Built and signed on GitHub Actions

# Typescript utils for async, cancellation, etc This is currently a work in progress.

This package works with Bun, Cloudflare Workers, Node.js, Deno, Browsers
JSR Score
70%
Published
3 months ago (0.2.21)
interface AsyncQueue
extends Disposable, AsyncIterable<T>

An async queue that allows for enqueuing and dequeuing items concurrently

Type Parameters

Properties

readonly
state: "rw" | "r" | "-rw"

Indicates the current state of the queue with respect to its read/write capabilities.

"rw": The queue is in a read-write state,items can be enqueued/dequeued. "r": The queue is in a read-only state, items can only be dequeued. "-rw": The queue is in a state neither enqueuing/dequeuing is allowed.

readonly
size: number

The current number of items in the queue

readonly
isEmpty: boolean

Returns whether the queue is empty (true) or not (false)

readonly
isClosed: boolean

Returns whether the queue has been closed (true) or is still open (false)

Methods

enqueue(item: T): void

Enqueues an item of type T to the queue.

Errors will be thrown in one of the three following conditions:

  1. The queue is read-only
  2. The queue is closed.
  3. The max buffer size has been reached and the buffer strategy chosen is "fixed".

Asynchronously dequeues an item from the queue. If the queue is empty, it will return a promise that resolves when an item is added to the queue.

tryDequeue(): MaybeResult<T>

Synchronously dequeues an item from the queue if the queue us not empty, otherwise it will return an object with the value undefined and ok set to false.

setReadOnly(): void

Sets the queue to a read-only state, preventing any further enqueuing of items. Items can still be dequeued until the queue is empty. NOTE: When the queue is empty, it will be automatically closed.

close(): void

Closes the queue.

onClose(): Promise<void>

Returns a promise that resolves when the queue is closed.

Add Package

deno add @rdtlabs/ts-utils

Import symbol

import { AsyncQueue } from "@rdtlabs/ts-utils/async";

Add Package

npx jsr add @rdtlabs/ts-utils

Import symbol

import { AsyncQueue } from "@rdtlabs/ts-utils/async";

Add Package

yarn dlx jsr add @rdtlabs/ts-utils

Import symbol

import { AsyncQueue } from "@rdtlabs/ts-utils/async";

Add Package

pnpm dlx jsr add @rdtlabs/ts-utils

Import symbol

import { AsyncQueue } from "@rdtlabs/ts-utils/async";

Add Package

bunx jsr add @rdtlabs/ts-utils

Import symbol

import { AsyncQueue } from "@rdtlabs/ts-utils/async";