UNPKG

batched-promise-all

Version:

:flight_departure: zero deps utility for keeping your memory footprint manageable with varying datasets

201 lines (185 loc) 5.13 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); // A type of promise-like that resolves synchronously and supports only one observer const _Pact = /*#__PURE__*/(function() { function _Pact() {} _Pact.prototype.then = function(onFulfilled, onRejected) { const result = new _Pact(); const state = this.s; if (state) { const callback = state & 1 ? onFulfilled : onRejected; if (callback) { try { _settle(result, 1, callback(this.v)); } catch (e) { _settle(result, 2, e); } return result; } else { return this; } } this.o = function(_this) { try { const value = _this.v; if (_this.s & 1) { _settle(result, 1, onFulfilled ? onFulfilled(value) : value); } else if (onRejected) { _settle(result, 1, onRejected(value)); } else { _settle(result, 2, value); } } catch (e) { _settle(result, 2, e); } }; return result; }; return _Pact; })(); // Settles a pact synchronously function _settle(pact, state, value) { if (!pact.s) { if (value instanceof _Pact) { if (value.s) { if (state & 1) { state = value.s; } value = value.v; } else { value.o = _settle.bind(null, pact, state); return; } } if (value && value.then) { value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2)); return; } pact.s = state; pact.v = value; const observer = pact.o; if (observer) { observer(pact); } } } function _isSettledPact(thenable) { return thenable instanceof _Pact && thenable.s & 1; } // Asynchronously iterate through an object that has a length property, passing the index as the first argument to the callback (even as the length property changes) function _forTo(array, body, check) { var i = -1, pact, reject; function _cycle(result) { try { while (++i < array.length && (!check || !check())) { result = body(i); if (result && result.then) { if (_isSettledPact(result)) { result = result.v; } else { result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2))); return; } } } if (pact) { _settle(pact, 1, result); } else { pact = result; } } catch (e) { _settle(pact || (pact = new _Pact()), 2, e); } } _cycle(); return pact; } const _iteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator"))) : "@@iterator"; // Asynchronously iterate through an object's values // Uses for...of if the runtime supports it, otherwise iterates until length on a copy function _forOf(target, body, check) { if (typeof target[_iteratorSymbol] === "function") { var iterator = target[_iteratorSymbol](), step, pact, reject; function _cycle(result) { try { while (!(step = iterator.next()).done && (!check || !check())) { result = body(step.value); if (result && result.then) { if (_isSettledPact(result)) { result = result.v; } else { result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2))); return; } } } if (pact) { _settle(pact, 1, result); } else { pact = result; } } catch (e) { _settle(pact || (pact = new _Pact()), 2, e); } } _cycle(); if (iterator.return) { var _fixup = function(value) { try { if (!step.done) { iterator.return(); } } catch(e) { } return value; }; if (pact && pact.then) { return pact.then(_fixup, function(e) { throw _fixup(e); }); } _fixup(); } return pact; } // No support for Symbol.iterator if (!("length" in target)) { throw new TypeError("Object is not iterable"); } // Handle live collections properly var values = []; for (var i = 0; i < target.length; i++) { values.push(target[i]); } return _forTo(values, function(i) { return body(values[i]); }, check); } const _asyncIteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.asyncIterator || (Symbol.asyncIterator = Symbol("Symbol.asyncIterator"))) : "@@asyncIterator"; function chunkArray(array, chunkSize) { const resultArray = []; for (let i = 0, len = array.length; i < len; i += chunkSize) { resultArray.push(array.slice(i, i + chunkSize)); } return resultArray; } const batchedPromiseAll = function (promiseArr, iterationCb, batchSize) { try { const chunkedPromiseArr = chunkArray(promiseArr, batchSize); let result = []; let currentIndex = -1; const _temp = _forOf(chunkedPromiseArr, function (chunk) { return Promise.resolve(Promise.all(chunk.map(item => { currentIndex++; return iterationCb(item, currentIndex); }))).then(function (chunkResult) { result = result.concat(chunkResult); }); }); return Promise.resolve(_temp && _temp.then ? _temp.then(function () { return result; }) : result); } catch (e) { return Promise.reject(e); } }; exports.batchedPromiseAll = batchedPromiseAll; //# sourceMappingURL=batched-promise-all.cjs.development.js.map