Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damianc/hot-generator
Allows making the hot generators
https://github.com/damianc/hot-generator
es6 generator generator-functions hot-generators javascript sharing
Last synced: about 1 month ago
JSON representation
Allows making the hot generators
- Host: GitHub
- URL: https://github.com/damianc/hot-generator
- Owner: damianc
- Created: 2019-07-14T22:11:50.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-05T09:17:36.000Z (over 4 years ago)
- Last Synced: 2024-11-10T16:21:54.332Z (about 2 months ago)
- Topics: es6, generator, generator-functions, hot-generators, javascript, sharing
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hot Generator
Wraps a generator so that it shares the current value among all the instances.
## Usage
```
var hotNums = new HotGenerator(function* () {
yield* [1, 2, 3, 4];
});var genA = hotNums();
var genB = hotNums();genA.next();
// {value: 1, done: false}genB.next();
// {value: 2, done: false}genA.next();
// {value: 3, done: false}genB.next();
// {value: 4, done: false}genA.next();
// {value: undefined, done: true}
```### `last()` method
The `last()` method of `HotGenerator` receives the last known value returned from any instance of a generator.
```
var hotNums = new HotGenerator(function* () {
yield* [1, 2, 3, 4];
});var genA = hotNums();
var genB = hotNums();genA.next();
// {value: 1, done: false}genB.next();
// {value: 2, done: false}hotNums.last();
// {value: 2, done: false}
```### `lastFrom()` method
Similar to the `last()` method, but here we can explicitly tell what instance of a generator we are interested in.
```
var hotNums = new HotGenerator(function* () {
yield* [1, 2, 3, 4];
});var genA = hotNums();
var genB = hotNums();genA.next();
// {value: 1, done: false}genB.next();
// {value: 2, done: false}genA.next();
// {value: 3, done: false}hotNums.last();
// {value: 3, done: false}hotNums.lastFrom(genA);
// {value: 3, done: false}hotNums.lastFrom(genB);
// {value: 2, done: false}
```### Caveats
Currently it is impossible to pass arguments to any of the generators. The problem is that each newest instance of a generator would override behavior of all previous, so far defined generators.