|
| 1 | +Async Iterable Buffer |
| 2 | +===================== |
| 3 | + |
| 4 | +[](https://www.npmjs.com/package/async-iterable-buffer) |
| 5 | +[](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/audit.yml) |
| 6 | +[](https://github.com/snatalenko/async-iterable-buffer/actions/workflows/tests.yml) |
| 7 | +[](https://coveralls.io/github/snatalenko/async-iterable-buffer?branch=main) |
| 8 | +[](https://www.npmjs.com/package/async-iterable-buffer) |
| 9 | + |
| 10 | + |
| 11 | +AsyncIterableBuffer is a lightweight, stateful asynchronous buffer that allows you to push values and consume them using async iteration (e.g. `for await...of`). It’s ideal for bridging event-driven or callback-based code with modern async/await patterns. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- **Asynchronous Iteration:** Implements the `AsyncIterableIterator` interface. |
| 16 | +- **Push and Consume:** Dynamically push values into the buffer and consume them asynchronously. |
| 17 | +- **Graceful Termination:** Signal completion with an `end()` method and handle early termination via `return()`. |
| 18 | +- **TypeScript Support:** Fully typed for TypeScript projects. |
| 19 | +- **Lightweight:** No dependencies. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install async-iterable-buffer |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Basic Example |
| 30 | + |
| 31 | +```ts |
| 32 | +import { AsyncIterableBuffer } from 'async-iterable-buffer'; |
| 33 | + |
| 34 | +const buffer = new AsyncIterableBuffer<number>(); |
| 35 | + |
| 36 | +// Push some values into the buffer. |
| 37 | +buffer.push(1); |
| 38 | +buffer.push(2); |
| 39 | +buffer.push(3); |
| 40 | + |
| 41 | +// Signal that no more values will be pushed. |
| 42 | +buffer.end(); |
| 43 | + |
| 44 | +// Consume the buffer with async iteration. |
| 45 | +(async () => { |
| 46 | + for await (const num of buffer) { |
| 47 | + console.log(num); // Outputs: 1, 2, 3 |
| 48 | + } |
| 49 | +})(); |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +### Advanced Usage with Early Termination |
| 54 | + |
| 55 | +```ts |
| 56 | +import { AsyncIterableBuffer } from 'async-iterable-buffer'; |
| 57 | + |
| 58 | +const buffer = new AsyncIterableBuffer<string>(); |
| 59 | + |
| 60 | +// Start an async iterator. |
| 61 | +(async () => { |
| 62 | + for await (const value of buffer) { |
| 63 | + console.log(value); |
| 64 | + if (value === 'stop') { |
| 65 | + // Terminate iteration early. |
| 66 | + await buffer.return(); |
| 67 | + } |
| 68 | + } |
| 69 | +})(); |
| 70 | + |
| 71 | +// Push values asynchronously. |
| 72 | +buffer.push('first'); |
| 73 | +buffer.push('stop'); |
| 74 | +buffer.push('ignored'); // This value won't be processed since the buffer is closed after "stop". |
| 75 | +``` |
| 76 | + |
| 77 | + |
| 78 | +### API |
| 79 | + |
| 80 | +API |
| 81 | + |
| 82 | +new AsyncIterableBuffer<T>() |
| 83 | + |
| 84 | +Creates a new instance of the asynchronous buffer. |
| 85 | + |
| 86 | +Methods |
| 87 | +- **push(value: T): void** |
| 88 | + Pushes a new value into the buffer. Throws an error if the buffer has been ended. |
| 89 | +- **end(): void** |
| 90 | + Marks the buffer as completed and resolves any pending requests with { done: true }. |
| 91 | +- **next(): Promise<IteratorResult<T>>** |
| 92 | + Returns a promise that resolves to the next buffered value. If the buffer is empty but not ended, it waits until a value is pushed. |
| 93 | +- **return(): Promise<IteratorResult<T>>** |
| 94 | + Ends the buffer and returns a result indicating that the iterator is done. This is useful for early termination of iteration. |
| 95 | +- **[Symbol.asyncIterator](): AsyncIterableIterator<T>** |
| 96 | + Returns the async iterator itself, enabling for await...of loops. |
0 commit comments