UNPKG

maximize-iterator

Version:

Maximize the parallel calls of an iterator supporting asyncIterator interface

25 lines (24 loc) 804 B
import nextCallback from 'iterator-next-callback'; import createProcessor from './createProcessor.js'; const DEFAULT_CONCURRENCY = 4096; const DEFAULT_LIMIT = Infinity; export default function worker(iterator, each, options_, callback) { let options = { callbacks: options_.callbacks, concurrency: options_.concurrency || DEFAULT_CONCURRENCY, limit: options_.limit || DEFAULT_LIMIT, // default is exit on error error: options_.error || (()=>true), each, canProcess: options_.canProcess || (()=>true), total: 0, counter: 0, done: false }; let processor = createProcessor(nextCallback(iterator), options, (err)=>{ options = null; processor = null; callback(err); }); processor(); }