asyncreiterable
Version:
An AsyncReiterable is an append-only collection that allows multiple asynchronous iterations.
92 lines (63 loc) • 2.75 kB
Markdown
[](https://travis-ci.org/rubensworks/asyncreiterable.js)
[](https://coveralls.io/github/rubensworks/asyncreiterable.js?branch=master)
[](https://www.npmjs.com/package/asyncreiterable)
An `AsyncReiterable` is an append-only collection that allows _multiple asynchronous iterations_.
Each time the `iterator()` method of this `AsyncReiterable` is called,
a new [AsyncIterator](https://www.npmjs.com/package/asynciterator) is produced.
This package can be used in cases where you need an [AsyncIterator](https://www.npmjs.com/package/asynciterator),
but you need to be able to iterate over them _multiple times_.
```
$ npm install asyncreiterable
```
At the time of writing, this package provides `AsyncReiterableArray`,
an array-backed implementation of the `AsyncReiterable` interface.
It can be constructed in different ways through the following static methods:
```javascript
import {AsyncReiterableArray} from "asyncreiterable";
AsyncReiterableArray.fromFixedData([1, 2, 3])
AsyncReiterableArray.fromInitialData([1, 2, 3])
AsyncReiterableArray.fromInitialEmpty()
```
Data elements can only be _pushed_,
no data elements can be removed.
The iterable becomes _ended_ when `null` is pushed.
```javascript
const iterable = AsyncReiterableArray.fromInitialData([1, 2, 3])
iterable.push(4);
iterable.push(5);
iterable.push(6);
iterable.push(null);
```
`AsyncReiterable` that are either ended or not ended yet can both be iterated.
The `iterator()` method returns an [AsyncIterator](https://www.npmjs.com/package/asynciterator)
that will contains all current and future data elements in this `AsyncReiterable`.
It will be ended once the `AsyncReiterable` is ended.
```javascript
const iterable = AsyncReiterableArray.fromInitialData([1, 2])
const it1 = iterable.iterator();
const it2 = iterable.iterator();
it1.on('data', console.log);
it2.on('data', console.log);
iterable.push(3);
iterable.push(4);
iterable.push(null);
```
This software is written by [Ruben Taelman](http://rubensworks.net/).
This code is released under the [MIT license](http://opensource.org/licenses/MIT).