iter-tools-es
Version:
The iterable toolbox
57 lines (42 loc) • 1.21 kB
JavaScript
const {
asyncEnsureIterable
} = require('../../internal/async-iterable.js');
const {
AsyncExchange
} = require('../../internal/async-fork.js');
const {
AsyncPeekerator
} = require('../../internal/async-peekerator.js');
const _ = Symbol.for('_');
class AsyncForkerator extends AsyncPeekerator {
static async from(source) {
const exchange = new AsyncExchange(asyncEnsureIterable(source)[Symbol.asyncIterator]());
return await super.from(exchange.fork(), exchange);
}
constructor(iterator, first, exchange) {
super(iterator, first);
this[_].exchange = exchange;
}
async advance(n = 1) {
for (let i = 0; i < n; i++) {
await super.advance();
this[_].exchange.advance();
}
return this;
}
fork() {
return this[_].exchange.fork();
}
[Symbol.asyncIterator]() {
return this[_].exchange.fork();
}
}
function __asyncForkerate(source) {
return AsyncForkerator.from(source);
}
exports.__asyncForkerate = __asyncForkerate;
function asyncWrapWithEnsureIterable(fn) {
return source => fn(asyncEnsureIterable(source));
}
const asyncForkerate = asyncWrapWithEnsureIterable(__asyncForkerate);
exports.asyncForkerate = asyncForkerate;