Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tjenkinson/forkable-iterator
Make a JS Iterator forkable.
https://github.com/tjenkinson/forkable-iterator
clone fork generator iterable iterator tee
Last synced: 3 months ago
JSON representation
Make a JS Iterator forkable.
- Host: GitHub
- URL: https://github.com/tjenkinson/forkable-iterator
- Owner: tjenkinson
- License: mit
- Created: 2022-08-19T09:05:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-12T23:02:12.000Z (10 months ago)
- Last Synced: 2024-04-14T10:01:01.296Z (9 months ago)
- Topics: clone, fork, generator, iterable, iterator, tee
- Language: TypeScript
- Homepage:
- Size: 709 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# forkable-iterator
Make a JS [`Iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#iterators) forkable.
Be aware that if you have a fork that is not consuming values as it gets further and further behind more memory will be used. Make sure you `null` out references to forks that you no longer need to allow garbage collection to occur.
## Installation
```sh
npm install --save forkable-iterator
```Also available on JSDelivr at "https://cdn.jsdelivr.net/npm/forkable-iterator@1".
## Usage
```ts
import { buildForkableIterator, fork } from 'forkable-iterator';function* Source() {
yield 1;
yield 2;
return 'return';
}const forkableIterator = buildForkableIterator(source());
console.log(forkableIterator.next()); // { value: 1, done: false }
const child1 = fork(forkableIterator);
// { value: 2, done: false }
console.log(child1.next());
// { value: 2, done: false }
console.log(forkableIterator.next());// { value: 'return', done: true }
console.log(child1.next());
// { value: 'return', done: true }
console.log(forkableIterator.next());
```## API
### `buildForkableIterator(source)`
Returns a `ForkableIterator` from the provided `source` iterator, which has the same API as `Iterator`, but can be forked.
To create a fork use the exported [`fork(forkableIterator)`](#forkforkableiterator) function.
The source iterator must not be read from directly as any forks will miss the values.
The returned `ForkableIterator` will not implement `return()` or `throw()` functions.
### `fork(forkableIterator)`
Create a fork of the provided `ForkableIterator` at the current point.