JSPM

dynamic-async-iterable

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q40321F
  • License ISC

A simple asynchronous queue with an async iterator interface, supporting dynamic enqueueing.

Package Exports

  • dynamic-async-iterable

Readme

Dynamic Async Iterable

A simple asynchronous queue with an async iterator interface, supporting dynamic enqueueing.

Features

  • Async iterable interface (for await...of)
  • Enqueue items dynamically
  • Suitable for producer-consumer scenarios
  • Written in TypeScript, with type declarations
  • Lightweight and zero dependencies

TL;DR

Install:

npm install dynamic-async-iterable

Use:

import DynamicAsyncIterable from "dynamic-async-iterable"

const iterable = new DynamicAsyncIterable<string>()
iterable.enqueue("hello")
iterable.enqueue("world")

for await (const item of iterable) {
  console.log(item)
}

Installation

Install via npm:

npm install dynamic-async-iterable

or yarn:

yarn add dynamic-async-iterable

Usage

Create an instance of DynamicAsyncIterable:

import DynamicAsyncIterable from "dynamic-async-iterable"

const iterable = new DynamicAsyncIterable<string>()

Produce

Produce items asynchronously by calling the enqueue method:

iterable.enqueue("hello")
iterable.enqueue("world")

Consume

Consume items with the async iterator interface, using for await...of:

for await (const item of iterable) {
  console.log(item)
}

Example

setTimeout producer

import DynamicAsyncIterable from "dynamic-async-iterable"

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const produce = async (iterable: DynamicAsyncIterable<string>) => {
  await sleep(1000)
  iterable.enqueue("hello")
  await sleep(1000)
  iterable.enqueue("world")

  // Send the end signal
  iterable.end()
}

const iterable = new DynamicAsyncIterable<string>()

// Produce items asynchronously
void produce(iterable)

// Consume items
for await (const item of iterable) {
  console.log(item)
}

// Output:
// hello
// world

EventEmitter producer

import DynamicAsyncIterable from "dynamic-async-iterable"
import EventEmitter from "node:events"

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const produce = async (emitter: EventEmitter) => {
  await sleep(1000)
  emitter.emit("data", "hello")
  await sleep(1000)
  emitter.emit("data", "world")

  emitter.emit("end")
}

const iterable = new DynamicAsyncIterable<string>()
const emitter = new EventEmitter()

emitter.on("data", (data: string) => iterable.enqueue(data))
emitter.on("end", () => iterable.end())

// Produce items asynchronously
void produce(emitter)

for await (const item of iterable) {
  console.log(item)
}

Development

Clone the repo and install dependencies:

git clone https://github.com/allemonta/dynamic-async-iterable.git
cd dynamic-async-iterable
npm install

Build

npm run build

Run tests

Using Node.js built-in test runner and tsx for TypeScript support:

npm test