Fifo
Defined in: src/Fifo.ts:38
An asynchronous queue that is also an AsyncIterable.
Producers call send (which waits when the queue is full, giving back
pressure) and finish with end; consumers iterate the fifo with for await.
end(error) finishes the queue with an error that the consumers receive
after the queued items.
Example
const fifo = new Fifo<number>();
const consumed = (async () => {
const items: number[] = [];
for await (const item of fifo) {
items.push(item);
}
return items;
})();
await fifo.send(1);
await fifo.send(2);
fifo.end();
await consumed; // => [1, 2]
Type Parameters
T
T
Implements
AsyncIterable<T>
Constructors
Constructor
new Fifo<
T>(options?):Fifo<T>
Defined in: src/Fifo.ts:43
Parameters
options?
Returns
Fifo<T>
Accessors
stat
Get Signature
get stat():
object
Defined in: src/Fifo.ts:84
Queue statistics: the peak length reached by the queued data, the waiting writers and the waiting readers.
Returns
object
data
data:
QueueStat
readers
readers:
Readonly<QueueStat>
writers
writers:
Readonly<QueueStat>
Methods
[asyncIterator]()
[asyncIterator]():
AsyncGenerator<Awaited<T>,void,any>
Defined in: src/Fifo.ts:88
Returns
AsyncGenerator<Awaited<T>, void, any>
Implementation of
AsyncIterable.[asyncIterator]
end()
end(...
reason):void
Defined in: src/Fifo.ts:69
Ends the queue: no more items can be sent, and a send after this
rejects with chan is closed.
Items already in the queue are still delivered to consumers; the
iteration finishes after them. If an error is passed, every consumer
receives it, thrown from the iteration, after the queued items; even
undefined counts as an error when it is passed explicitly. Ending a
queue that is already ended does nothing, so the first error wins.
Parameters
reason
...[unknown]
the error the queue ends with, if it did not end normally
Returns
void
send()
send(
item):Promise<void>
Defined in: src/Fifo.ts:53
Sends an item to the fifo.
Resolves as soon as the item is actually pushed. If the internal queue is full, waits until a slot is free.
Parameters
item
T
Returns
Promise<void>