tamesy
Version:
Tames a set of wild concurrent promises
230 lines (199 loc) • 7.46 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("tamesy", [], factory);
else if(typeof exports === 'object')
exports["tamesy"] = factory();
else
root["tamesy"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* Given an interable, an optional concurrency limit and an iterator resolves Promises from
* the factory (interable) obeying the concurrency limit. The iterator should be used
* to either return Promises as it's invoked with each item or can be left blank
* if the passed each iterm itself is a Promise returning factory function.
*
* @method map
* @param {[any]} iterable [of either Promise returning functions or an list of any]
* @param {Number} [concurrency=Infinity] [maximum limit of concurrently running Promises]
* @param {Function} iterator [if passed a function invoked with each item which should return a Promise]
* @param {Function} log [if passed a function to be used for debugging]
* @param {Function} getPromise [if passed a function providing a Promise (to overwrite native Promises)]
* @return {[any]} [list of any in order of resolved items]
*/
// eslint-disable-next-line max-params
function map(iterable) {
var concurrency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Infinity;
var iterator = arguments[2];
var getPromise = arguments[3];
var log = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : function () {};
var extract = function extract(item, iterator) {
return typeof item === 'function' ? item() : iterator(item);
};
var chain = function chain(fn) {
return typeof getPromise === 'function' ? getPromise(fn) : new Promise(fn);
};
log('map() Starting with iterable of ' + iterable.length + ' in concurrency of ' + concurrency + '.');
return chain(function (resolve, reject) {
var resolutions = [];
var running = 0;
var aborted = false;
/**
* Step steps, maintains running Promises and loops again
*/
function step() {
log('step() Iterator completed with success - continuing queue.');
running--;
loop();
}
/**
* Aborts, marks the whole chain as aborted and rejects the Promise
*/
function abort(err) {
log('abort() iterator had error - rejecting queue.', err);
aborted = true;
reject(err);
}
/**
* Immedidiately invoked function returning an iterator (next, done)
* to manage to iterable as a queue.
*/
var queue = function () {
// To maintain correct indexes on resolved Promises
var idx = -1;
return {
// Each returning an object on next() with value || done
next: function next() {
if (idx < iterable.length - 1) {
return { value: iterable[++idx], idx: idx };
}
return { done: true };
}
};
}();
/**
* The loop. Maintaining a the running Promises vs. concurrency limits
* while walking the queue.
*/
function loop() {
if (aborted) {
return;
}
var _loop = function _loop() {
var next = queue.next();
if (next.done) {
log('loop() Concurrent slice of queue done.');
if (running === 0) {
log('loop() Complete queue is done.');
resolve(resolutions);
return {
v: void 0
};
}
return 'break';
}
running++;
log('loop() Extracting onto iterator with value.');
// Unboxes another value from the queue with either the iterator or directly
extract(next.value, iterator).then(function (resolvation) {
// Store resolved value and step
resolutions[next.idx] = resolvation;
step();
}, abort);
};
_loop2: while (running < concurrency) {
var _ret = _loop();
switch (_ret) {
case 'break':
break _loop2;
default:
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
}
// Initial kicker against the loop
loop();
});
}
exports.default = map;
/***/ })
/******/ ]);
});
//# sourceMappingURL=tamesy.js.map