batch-perform-promise
Advanced tools
Comparing version 2.1.0 to 3.0.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('core-js-pure/stable/instance/slice'), require('core-js-pure/stable/instance/concat'), require('core-js-pure/stable/instance/index-of'), require('core-js-pure/stable/instance/every'), require('regenerator-runtime'), require('core-js-pure/stable/promise'), require('core-js-pure/features/promise'), require('core-js-pure/features/array/is-array'), require('core-js-pure/features/array/from'), require('core-js-pure/features/is-iterable'), require('core-js-pure/features/symbol'), require('core-js-pure/features/instance/slice'), require('core-js-pure/stable/instance/map'), require('core-js-pure/features/object/define-property')) : | ||
typeof define === 'function' && define.amd ? define(['core-js-pure/stable/instance/slice', 'core-js-pure/stable/instance/concat', 'core-js-pure/stable/instance/index-of', 'core-js-pure/stable/instance/every', 'regenerator-runtime', 'core-js-pure/stable/promise', 'core-js-pure/features/promise', 'core-js-pure/features/array/is-array', 'core-js-pure/features/array/from', 'core-js-pure/features/is-iterable', 'core-js-pure/features/symbol', 'core-js-pure/features/instance/slice', 'core-js-pure/stable/instance/map', 'core-js-pure/features/object/define-property'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.batchPerformPromise = factory(global.slice$2, global.concat$1, global.indexOf$1, global.every$1, global.regeneratorRuntime, global.promise$2, global.promise$3, global.isArray$1, global.from, global.isIterable$1, global.symbol$1, global.slice$3, global.map$1, global.defineProperty$1)); | ||
}(this, (function (slice$2, concat$1, indexOf$1, every$1, regeneratorRuntime, promise$2, promise$3, isArray$1, from, isIterable$1, symbol$1, slice$3, map$1, defineProperty$1) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.batchPerformPromise = factory()); | ||
})(this, (function () { 'use strict'; | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
/** | ||
* 批量执行Promise任务 | ||
*/ | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
// 当前获得的任务 | ||
// 当前未执行的任务 | ||
// promise任务链最大并发数 | ||
// 当前执行的任务回调 | ||
// 当前batcher执行完任务 | ||
// 当前执行的任务 | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
this.tasks = void 0; | ||
this.undoTasks = void 0; | ||
this.concurrentSize = void 0; | ||
this.callback = void 0; | ||
this.resolve = void 0; | ||
this.performWorks = void 0; | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
}; | ||
}); | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = [].concat(this.tasks); | ||
this.callback = callback; | ||
this.performWorks = []; | ||
var slice__default = /*#__PURE__*/_interopDefaultLegacy(slice$2); | ||
var concat__default = /*#__PURE__*/_interopDefaultLegacy(concat$1); | ||
var indexOf__default = /*#__PURE__*/_interopDefaultLegacy(indexOf$1); | ||
var every__default = /*#__PURE__*/_interopDefaultLegacy(every$1); | ||
var regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(regeneratorRuntime); | ||
var promise__default = /*#__PURE__*/_interopDefaultLegacy(promise$2); | ||
var promise__default$1 = /*#__PURE__*/_interopDefaultLegacy(promise$3); | ||
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray$1); | ||
var from__default = /*#__PURE__*/_interopDefaultLegacy(from); | ||
var isIterable__default = /*#__PURE__*/_interopDefaultLegacy(isIterable$1); | ||
var symbol__default = /*#__PURE__*/_interopDefaultLegacy(symbol$1); | ||
var slice__default$1 = /*#__PURE__*/_interopDefaultLegacy(slice$3); | ||
var map__default = /*#__PURE__*/_interopDefaultLegacy(map$1); | ||
var defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(defineProperty$1); | ||
this.resolve = function () { | ||
return undefined; | ||
}; | ||
} // 当前是否可以继续放任务 | ||
var slice = slice__default['default']; | ||
var concat = concat__default['default']; | ||
var _proto = BatchPerfromer.prototype; | ||
var indexOf = indexOf__default['default']; | ||
_proto.shouldPerform = function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
; | ||
var every = every__default['default']; | ||
_proto.exec = async function exec() { | ||
var _this = this; | ||
var regenerator = regeneratorRuntime__default['default']; | ||
return new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
var promise = promise__default['default']; | ||
_this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
; | ||
var promise$1 = promise__default$1['default']; | ||
_proto.beginWork = async function beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
promise$1.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
var nextTask = this.undoTasks.shift(); | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new promise$1(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
var asyncToGenerator = _asyncToGenerator; | ||
var isArray = isArray__default['default']; | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) { | ||
arr2[i] = arr[i]; | ||
if (nextTask) { | ||
this.performWork(nextTask); | ||
} | ||
} | ||
} // 开始执行任务 | ||
; | ||
return arr2; | ||
} | ||
_proto.performWork = async function performWork(task) { | ||
// 当前任务压入队列 | ||
var performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
var arrayLikeToArray = _arrayLikeToArray; | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
function _arrayWithoutHoles(arr) { | ||
if (isArray(arr)) return arrayLikeToArray(arr); | ||
} | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
var arrayWithoutHoles = _arrayWithoutHoles; | ||
var currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(this.performWorks.slice(0, currentPromiseIdx), this.performWorks.slice(currentPromiseIdx + 1)); // 标志此任务完成 | ||
var from_1 = from__default['default']; | ||
var isIterable = isIterable__default['default']; | ||
var symbol = symbol__default['default']; | ||
function _iterableToArray(iter) { | ||
if (typeof symbol !== "undefined" && isIterable(Object(iter))) return from_1(iter); | ||
} | ||
var iterableToArray = _iterableToArray; | ||
var slice$1 = slice__default$1['default']; | ||
function _unsupportedIterableToArray(o, minLen) { | ||
var _context; | ||
if (!o) return; | ||
if (typeof o === "string") return arrayLikeToArray(o, minLen); | ||
var n = slice$1(_context = Object.prototype.toString.call(o)).call(_context, 8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return from_1(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return arrayLikeToArray(o, minLen); | ||
} | ||
var unsupportedIterableToArray = _unsupportedIterableToArray; | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var nonIterableSpread = _nonIterableSpread; | ||
function _toConsumableArray(arr) { | ||
return arrayWithoutHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableSpread(); | ||
} | ||
var toConsumableArray = _toConsumableArray; | ||
var map = map__default['default']; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
this.beginWork(); | ||
} | ||
} | ||
}; | ||
var classCallCheck = _classCallCheck; | ||
return BatchPerfromer; | ||
}(); | ||
/** | ||
* 批量执行Promise的方法 | ||
* @param tasks 需要被Promise执行的任务队列 | ||
* @param callback 执行Promise的方法 | ||
* @param concurrentSize 并发执行Promise数量.default=2 | ||
*/ | ||
var defineProperty = defineProperty__default['default']; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
defineProperty(target, descriptor.key, descriptor); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize) { | ||
if (concurrentSize === void 0) { | ||
concurrentSize = 2; | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
// 初始化 | ||
var batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
var createClass = _createClass; | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
/** | ||
* 批量执行Promise任务 | ||
*/ | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
classCallCheck(this, BatchPerfromer); | ||
return batchPerfromer.tasks; | ||
} | ||
this.tasks = map(tasks).call(tasks, function (t) { | ||
return { | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
}; | ||
}); | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = toConsumableArray(this.tasks); | ||
this.callback = callback; | ||
this.performWorks = []; | ||
} // 当前是否可以继续放任务 | ||
return batchPerformPromise; | ||
createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() { | ||
var _this = this; | ||
return regenerator.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new promise(function (resolve) { | ||
_this.resolve = resolve; | ||
_this.beginWork(); | ||
})); | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
return exec; | ||
}() // 开始批量操作 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() { | ||
var _context2; | ||
var nextTask; | ||
return regenerator.wrap(function _callee2$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && every(_context2 = this.tasks).call(_context2, function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
} | ||
return beginWork; | ||
}() // 开始执行任务 | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3(task) { | ||
var performPromise, _context4, _context5, _context6, _context7, currentPromiseIdx; | ||
return regenerator.wrap(function _callee3$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context8.prev = 2; | ||
_context8.next = 5; | ||
return performPromise; | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context8.next = 13; | ||
break; | ||
case 9: | ||
_context8.prev = 9; | ||
_context8.t0 = _context8["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context8.t0; | ||
case 13: | ||
_context8.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = indexOf(_context4 = this.performWorks).call(_context4, performPromise); | ||
this.performWorks = concat(_context5 = []).call(_context5, toConsumableArray(slice(_context6 = this.performWorks).call(_context6, 0, currentPromiseIdx)), toConsumableArray(slice(_context7 = this.performWorks).call(_context7, currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context8.finish(13); | ||
case 19: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
}(); | ||
/** | ||
* 批量执行Promise的方法 | ||
* @param tasks 需要被Promise执行的任务队列 | ||
* @param callback 执行Promise的方法 | ||
* @param concurrentSize 并发执行Promise数量.default=2 | ||
*/ | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regenerator.wrap(function _callee4$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
_context9.next = 4; | ||
return batchPerfromer.exec(); | ||
case 4: | ||
return _context9.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
return batchPerformPromise; | ||
}))); | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("core-js-pure/stable/instance/slice"),require("core-js-pure/stable/instance/concat"),require("core-js-pure/stable/instance/index-of"),require("core-js-pure/stable/instance/every"),require("regenerator-runtime"),require("core-js-pure/stable/promise"),require("core-js-pure/features/promise"),require("core-js-pure/features/array/is-array"),require("core-js-pure/features/array/from"),require("core-js-pure/features/is-iterable"),require("core-js-pure/features/symbol"),require("core-js-pure/features/instance/slice"),require("core-js-pure/stable/instance/map"),require("core-js-pure/features/object/define-property")):"function"==typeof define&&define.amd?define(["core-js-pure/stable/instance/slice","core-js-pure/stable/instance/concat","core-js-pure/stable/instance/index-of","core-js-pure/stable/instance/every","regenerator-runtime","core-js-pure/stable/promise","core-js-pure/features/promise","core-js-pure/features/array/is-array","core-js-pure/features/array/from","core-js-pure/features/is-iterable","core-js-pure/features/symbol","core-js-pure/features/instance/slice","core-js-pure/stable/instance/map","core-js-pure/features/object/define-property"],r):(e="undefined"!=typeof globalThis?globalThis:e||self).batchPerformPromise=r(e.slice$2,e.concat$1,e.indexOf$1,e.every$1,e.regeneratorRuntime,e.promise$2,e.promise$3,e.isArray$1,e.from,e.isIterable$1,e.symbol$1,e.slice$3,e.map$1,e.defineProperty$1)}(this,(function(e,r,t,n,s,a,o,u,i,c,f,l,p,h){"use strict";function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var v=d(e),m=d(r),b=d(t),y=d(n),j=d(s),k=d(a),g=d(o),w=d(u),x=d(i),q=d(c),$=d(f),W=d(l),T=d(p),P=d(h),S=v.default,A=m.default,I=b.default,O=y.default,z=j.default,C=k.default,E=g.default;function M(e,r,t,n,s,a,o){try{var u=e[a](o),i=u.value}catch(e){return void t(e)}u.done?r(i):E.resolve(i).then(n,s)}var R=function(e){return function(){var r=this,t=arguments;return new E((function(n,s){var a=e.apply(r,t);function o(e){M(a,n,s,o,u,"next",e)}function u(e){M(a,n,s,o,u,"throw",e)}o(void 0)}))}},U=w.default;var B=function(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n};var D=function(e){if(U(e))return B(e)},F=x.default,G=q.default,H=$.default;var J=function(e){if(void 0!==H&&G(Object(e)))return F(e)},K=W.default;var L=function(e,r){var t;if(e){if("string"==typeof e)return B(e,r);var n=K(t=Object.prototype.toString.call(e)).call(t,8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?F(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?B(e,r):void 0}};var N=function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")};var Q=function(e){return D(e)||J(e)||L(e)||N()},V=T.default;var X=function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")},Y=P.default;function Z(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Y(e,n.key,n)}}var _=function(e,r,t){return r&&Z(e.prototype,r),t&&Z(e,t),e},ee=function(){function e(r,t,n){X(this,e),this.tasks=V(r).call(r,(function(e){return{done:!1,success:!0,error:null,payload:e}})),this.concurrentSize=n,this.undoTasks=Q(this.tasks),this.callback=t,this.performWorks=[]}var r,t,n;return _(e,[{key:"shouldPerform",value:function(){return this.performWorks.length<this.concurrentSize}},{key:"exec",value:(n=R(z.mark((function e(){var r=this;return z.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new C((function(e){r.resolve=e,r.beginWork()})));case 1:case"end":return e.stop()}}),e)}))),function(){return n.apply(this,arguments)})},{key:"beginWork",value:(t=R(z.mark((function e(){var r,t;return z.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(this.undoTasks.length<=0&&O(r=this.tasks).call(r,(function(e){return!0===e.done}))&&this.resolve();!0===this.shouldPerform()&&this.undoTasks.length>0;)t=this.undoTasks.shift(),this.performWork(t);case 2:case"end":return e.stop()}}),e,this)}))),function(){return t.apply(this,arguments)})},{key:"performWork",value:(r=R(z.mark((function e(r){var t,n,s,a,o,u;return z.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=this.callback(r.payload),this.performWorks.push(t),e.prev=2,e.next=5,t;case 5:r.success=!0,r.error=null,e.next=13;break;case 9:e.prev=9,e.t0=e.catch(2),r.success=!1,r.error=e.t0;case 13:return e.prev=13,r.done=!0,u=I(n=this.performWorks).call(n,t),this.performWorks=A(s=[]).call(s,Q(S(a=this.performWorks).call(a,0,u)),Q(S(o=this.performWorks).call(o,u+1))),this.beginWork(),e.finish(13);case 19:case"end":return e.stop()}}),e,this,[[2,9,13,19]])}))),function(e){return r.apply(this,arguments)})}]),e}();function re(){return(re=R(z.mark((function e(r,t){var n,s,a=arguments;return z.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=a.length>2&&void 0!==a[2]?a[2]:2,s=new ee(r,t,n),e.next=4,s.exec();case 4:return e.abrupt("return",s.tasks);case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}return function(e,r){return re.apply(this,arguments)}})); | ||
!function(s,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(s="undefined"!=typeof globalThis?globalThis:s||self).batchPerformPromise=r()}(this,(function(){"use strict";var s=function(){function s(s,r,o){this.tasks=void 0,this.undoTasks=void 0,this.concurrentSize=void 0,this.callback=void 0,this.resolve=void 0,this.performWorks=void 0,this.tasks=s.map((function(s){return{done:!1,success:!0,error:null,payload:s}})),this.concurrentSize=o,this.undoTasks=[].concat(this.tasks),this.callback=r,this.performWorks=[],this.resolve=function(){}}var r=s.prototype;return r.shouldPerform=function(){return this.concurrentSize>this.performWorks.length},r.exec=async function(){var s=this;return new Promise((function(r){s.resolve=r,s.beginWork()}))},r.beginWork=async function(){for(0>=this.undoTasks.length&&this.tasks.every((function(s){return!0===s.done}))&&this.resolve();!0===this.shouldPerform()&&this.undoTasks.length>0;){var s=this.undoTasks.shift();s&&this.performWork(s)}},r.performWork=async function(s){var r=this.callback(s.payload);this.performWorks.push(r);try{await r,s.success=!0,s.error=null}catch(r){s.success=!1,s.error=r}finally{s.done=!0;var o=this.performWorks.indexOf(r);this.performWorks=[].concat(this.performWorks.slice(0,o),this.performWorks.slice(o+1)),this.beginWork()}},s}();return async function(r,o,e){void 0===e&&(e=2);var t=new s(r,o,e);return await t.exec(),t.tasks}})); |
384
es/index.js
@@ -1,160 +0,1 @@ | ||
import slice$2 from 'core-js-pure/stable/instance/slice'; | ||
import concat$1 from 'core-js-pure/stable/instance/concat'; | ||
import indexOf$1 from 'core-js-pure/stable/instance/index-of'; | ||
import every$1 from 'core-js-pure/stable/instance/every'; | ||
import regeneratorRuntime from 'regenerator-runtime'; | ||
import promise$2 from 'core-js-pure/stable/promise'; | ||
import promise$3 from 'core-js-pure/features/promise'; | ||
import isArray$1 from 'core-js-pure/features/array/is-array'; | ||
import from from 'core-js-pure/features/array/from'; | ||
import isIterable$1 from 'core-js-pure/features/is-iterable'; | ||
import symbol$1 from 'core-js-pure/features/symbol'; | ||
import slice$3 from 'core-js-pure/features/instance/slice'; | ||
import map$1 from 'core-js-pure/stable/instance/map'; | ||
import defineProperty$1 from 'core-js-pure/features/object/define-property'; | ||
var slice = slice$2; | ||
var concat = concat$1; | ||
var indexOf = indexOf$1; | ||
var every = every$1; | ||
var regenerator = regeneratorRuntime; | ||
var promise = promise$2; | ||
var promise$1 = promise$3; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
promise$1.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new promise$1(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
var asyncToGenerator = _asyncToGenerator; | ||
var isArray = isArray$1; | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) { | ||
arr2[i] = arr[i]; | ||
} | ||
return arr2; | ||
} | ||
var arrayLikeToArray = _arrayLikeToArray; | ||
function _arrayWithoutHoles(arr) { | ||
if (isArray(arr)) return arrayLikeToArray(arr); | ||
} | ||
var arrayWithoutHoles = _arrayWithoutHoles; | ||
var from_1 = from; | ||
var isIterable = isIterable$1; | ||
var symbol = symbol$1; | ||
function _iterableToArray(iter) { | ||
if (typeof symbol !== "undefined" && isIterable(Object(iter))) return from_1(iter); | ||
} | ||
var iterableToArray = _iterableToArray; | ||
var slice$1 = slice$3; | ||
function _unsupportedIterableToArray(o, minLen) { | ||
var _context; | ||
if (!o) return; | ||
if (typeof o === "string") return arrayLikeToArray(o, minLen); | ||
var n = slice$1(_context = Object.prototype.toString.call(o)).call(_context, 8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return from_1(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return arrayLikeToArray(o, minLen); | ||
} | ||
var unsupportedIterableToArray = _unsupportedIterableToArray; | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var nonIterableSpread = _nonIterableSpread; | ||
function _toConsumableArray(arr) { | ||
return arrayWithoutHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableSpread(); | ||
} | ||
var toConsumableArray = _toConsumableArray; | ||
var map = map$1; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
var classCallCheck = _classCallCheck; | ||
var defineProperty = defineProperty$1; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
var createClass = _createClass; | ||
/** | ||
@@ -164,6 +5,16 @@ * 批量执行Promise任务 | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
// 当前获得的任务 | ||
// 当前未执行的任务 | ||
// promise任务链最大并发数 | ||
// 当前执行的任务回调 | ||
// 当前batcher执行完任务 | ||
// 当前执行的任务 | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
classCallCheck(this, BatchPerfromer); | ||
this.tasks = map(tasks).call(tasks, function (t) { | ||
this.tasks = void 0; | ||
this.undoTasks = void 0; | ||
this.concurrentSize = void 0; | ||
this.callback = void 0; | ||
this.resolve = void 0; | ||
this.performWorks = void 0; | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
@@ -177,141 +28,74 @@ done: false, | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = toConsumableArray(this.tasks); | ||
this.undoTasks = [].concat(this.tasks); | ||
this.callback = callback; | ||
this.performWorks = []; | ||
this.resolve = function () { | ||
return undefined; | ||
}; | ||
} // 当前是否可以继续放任务 | ||
createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
var _proto = BatchPerfromer.prototype; | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() { | ||
var _this = this; | ||
_proto.shouldPerform = function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
; | ||
return regenerator.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new promise(function (resolve) { | ||
_this.resolve = resolve; | ||
_proto.exec = async function exec() { | ||
var _this = this; | ||
_this.beginWork(); | ||
})); | ||
return new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
_this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
; | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
_proto.beginWork = async function beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
return exec; | ||
}() // 开始批量操作 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() { | ||
var _context2; | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
var nextTask = this.undoTasks.shift(); | ||
var nextTask; | ||
return regenerator.wrap(function _callee2$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && every(_context2 = this.tasks).call(_context2, function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
if (nextTask) { | ||
this.performWork(nextTask); | ||
} | ||
} | ||
} // 开始执行任务 | ||
; | ||
return beginWork; | ||
}() // 开始执行任务 | ||
_proto.performWork = async function performWork(task) { | ||
// 当前任务压入队列 | ||
var performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3(task) { | ||
var performPromise, _context4, _context5, _context6, _context7, currentPromiseIdx; | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
return regenerator.wrap(function _callee3$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context8.prev = 2; | ||
_context8.next = 5; | ||
return performPromise; | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context8.next = 13; | ||
break; | ||
var currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(this.performWorks.slice(0, currentPromiseIdx), this.performWorks.slice(currentPromiseIdx + 1)); // 标志此任务完成 | ||
case 9: | ||
_context8.prev = 9; | ||
_context8.t0 = _context8["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context8.t0; | ||
this.beginWork(); | ||
} | ||
}; | ||
case 13: | ||
_context8.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = indexOf(_context4 = this.performWorks).call(_context4, performPromise); | ||
this.performWorks = concat(_context5 = []).call(_context5, toConsumableArray(slice(_context6 = this.performWorks).call(_context6, 0, currentPromiseIdx)), toConsumableArray(slice(_context7 = this.performWorks).call(_context7, currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context8.finish(13); | ||
case 19: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
@@ -327,35 +111,15 @@ }(); | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize) { | ||
if (concurrentSize === void 0) { | ||
concurrentSize = 2; | ||
} | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regenerator.wrap(function _callee4$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
// 初始化 | ||
var batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
_context9.next = 4; | ||
return batchPerfromer.exec(); | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
case 4: | ||
return _context9.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
return batchPerfromer.tasks; | ||
} | ||
export default batchPerformPromise; | ||
export { batchPerformPromise as default }; |
399
lib/index.js
'use strict'; | ||
var slice$2 = require('core-js-pure/stable/instance/slice'); | ||
var concat$1 = require('core-js-pure/stable/instance/concat'); | ||
var indexOf$1 = require('core-js-pure/stable/instance/index-of'); | ||
var every$1 = require('core-js-pure/stable/instance/every'); | ||
var regeneratorRuntime = require('regenerator-runtime'); | ||
var promise$2 = require('core-js-pure/stable/promise'); | ||
var promise$3 = require('core-js-pure/features/promise'); | ||
var isArray$1 = require('core-js-pure/features/array/is-array'); | ||
var from = require('core-js-pure/features/array/from'); | ||
var isIterable$1 = require('core-js-pure/features/is-iterable'); | ||
var symbol$1 = require('core-js-pure/features/symbol'); | ||
var slice$3 = require('core-js-pure/features/instance/slice'); | ||
var map$1 = require('core-js-pure/stable/instance/map'); | ||
var defineProperty$1 = require('core-js-pure/features/object/define-property'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var slice__default = /*#__PURE__*/_interopDefaultLegacy(slice$2); | ||
var concat__default = /*#__PURE__*/_interopDefaultLegacy(concat$1); | ||
var indexOf__default = /*#__PURE__*/_interopDefaultLegacy(indexOf$1); | ||
var every__default = /*#__PURE__*/_interopDefaultLegacy(every$1); | ||
var regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(regeneratorRuntime); | ||
var promise__default = /*#__PURE__*/_interopDefaultLegacy(promise$2); | ||
var promise__default$1 = /*#__PURE__*/_interopDefaultLegacy(promise$3); | ||
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray$1); | ||
var from__default = /*#__PURE__*/_interopDefaultLegacy(from); | ||
var isIterable__default = /*#__PURE__*/_interopDefaultLegacy(isIterable$1); | ||
var symbol__default = /*#__PURE__*/_interopDefaultLegacy(symbol$1); | ||
var slice__default$1 = /*#__PURE__*/_interopDefaultLegacy(slice$3); | ||
var map__default = /*#__PURE__*/_interopDefaultLegacy(map$1); | ||
var defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(defineProperty$1); | ||
var slice = slice__default['default']; | ||
var concat = concat__default['default']; | ||
var indexOf = indexOf__default['default']; | ||
var every = every__default['default']; | ||
var regenerator = regeneratorRuntime__default['default']; | ||
var promise = promise__default['default']; | ||
var promise$1 = promise__default$1['default']; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
promise$1.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new promise$1(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
var asyncToGenerator = _asyncToGenerator; | ||
var isArray = isArray__default['default']; | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) { | ||
arr2[i] = arr[i]; | ||
} | ||
return arr2; | ||
} | ||
var arrayLikeToArray = _arrayLikeToArray; | ||
function _arrayWithoutHoles(arr) { | ||
if (isArray(arr)) return arrayLikeToArray(arr); | ||
} | ||
var arrayWithoutHoles = _arrayWithoutHoles; | ||
var from_1 = from__default['default']; | ||
var isIterable = isIterable__default['default']; | ||
var symbol = symbol__default['default']; | ||
function _iterableToArray(iter) { | ||
if (typeof symbol !== "undefined" && isIterable(Object(iter))) return from_1(iter); | ||
} | ||
var iterableToArray = _iterableToArray; | ||
var slice$1 = slice__default$1['default']; | ||
function _unsupportedIterableToArray(o, minLen) { | ||
var _context; | ||
if (!o) return; | ||
if (typeof o === "string") return arrayLikeToArray(o, minLen); | ||
var n = slice$1(_context = Object.prototype.toString.call(o)).call(_context, 8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return from_1(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return arrayLikeToArray(o, minLen); | ||
} | ||
var unsupportedIterableToArray = _unsupportedIterableToArray; | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var nonIterableSpread = _nonIterableSpread; | ||
function _toConsumableArray(arr) { | ||
return arrayWithoutHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableSpread(); | ||
} | ||
var toConsumableArray = _toConsumableArray; | ||
var map = map__default['default']; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
var classCallCheck = _classCallCheck; | ||
var defineProperty = defineProperty__default['default']; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
var createClass = _createClass; | ||
/** | ||
@@ -183,6 +7,16 @@ * 批量执行Promise任务 | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
// 当前获得的任务 | ||
// 当前未执行的任务 | ||
// promise任务链最大并发数 | ||
// 当前执行的任务回调 | ||
// 当前batcher执行完任务 | ||
// 当前执行的任务 | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
classCallCheck(this, BatchPerfromer); | ||
this.tasks = map(tasks).call(tasks, function (t) { | ||
this.tasks = void 0; | ||
this.undoTasks = void 0; | ||
this.concurrentSize = void 0; | ||
this.callback = void 0; | ||
this.resolve = void 0; | ||
this.performWorks = void 0; | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
@@ -196,141 +30,74 @@ done: false, | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = toConsumableArray(this.tasks); | ||
this.undoTasks = [].concat(this.tasks); | ||
this.callback = callback; | ||
this.performWorks = []; | ||
this.resolve = function () { | ||
return undefined; | ||
}; | ||
} // 当前是否可以继续放任务 | ||
createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
var _proto = BatchPerfromer.prototype; | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() { | ||
var _this = this; | ||
_proto.shouldPerform = function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
; | ||
return regenerator.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new promise(function (resolve) { | ||
_this.resolve = resolve; | ||
_proto.exec = async function exec() { | ||
var _this = this; | ||
_this.beginWork(); | ||
})); | ||
return new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
_this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
; | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
_proto.beginWork = async function beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
return exec; | ||
}() // 开始批量操作 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() { | ||
var _context2; | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
var nextTask = this.undoTasks.shift(); | ||
var nextTask; | ||
return regenerator.wrap(function _callee2$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && every(_context2 = this.tasks).call(_context2, function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
if (nextTask) { | ||
this.performWork(nextTask); | ||
} | ||
} | ||
} // 开始执行任务 | ||
; | ||
return beginWork; | ||
}() // 开始执行任务 | ||
_proto.performWork = async function performWork(task) { | ||
// 当前任务压入队列 | ||
var performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3(task) { | ||
var performPromise, _context4, _context5, _context6, _context7, currentPromiseIdx; | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
return regenerator.wrap(function _callee3$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context8.prev = 2; | ||
_context8.next = 5; | ||
return performPromise; | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context8.next = 13; | ||
break; | ||
var currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(this.performWorks.slice(0, currentPromiseIdx), this.performWorks.slice(currentPromiseIdx + 1)); // 标志此任务完成 | ||
case 9: | ||
_context8.prev = 9; | ||
_context8.t0 = _context8["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context8.t0; | ||
this.beginWork(); | ||
} | ||
}; | ||
case 13: | ||
_context8.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = indexOf(_context4 = this.performWorks).call(_context4, performPromise); | ||
this.performWorks = concat(_context5 = []).call(_context5, toConsumableArray(slice(_context6 = this.performWorks).call(_context6, 0, currentPromiseIdx)), toConsumableArray(slice(_context7 = this.performWorks).call(_context7, currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context8.finish(13); | ||
case 19: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
@@ -346,35 +113,15 @@ }(); | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize) { | ||
if (concurrentSize === void 0) { | ||
concurrentSize = 2; | ||
} | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regenerator.wrap(function _callee4$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
// 初始化 | ||
var batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
_context9.next = 4; | ||
return batchPerfromer.exec(); | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
case 4: | ||
return _context9.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
return batchPerfromer.tasks; | ||
} | ||
module.exports = batchPerformPromise; |
{ | ||
"name": "batch-perform-promise", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "batch perform promise", | ||
"scripts": { | ||
"test": "jest --config jest.config.json", | ||
"dev": "rollup -w -c --environment FORMAT:esm", | ||
"build:cjs": "rollup -c --environment FORMAT:cjs", | ||
"build:esm": "rollup -c --environment FORMAT:esm", | ||
"build:umd": "rollup -c --environment FORMAT:umd", | ||
"build:min": "rollup -c --environment FORMAT:min", | ||
"build": "rimraf lib/* es/* dist/* && run-p build:cjs build:esm build:umd build:min" | ||
}, | ||
"license": "MIT", | ||
"repository": "https://github.com/Z-HNAN/batch-perform-promise.git", | ||
"keywords": [], | ||
"author": "zhaohaonan", | ||
"main": "lib/index.js", | ||
@@ -27,47 +22,56 @@ "unpkg": "dist/index.js", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Z-HNAN/batch-perform-promise" | ||
"scripts": { | ||
"clean": "rimraf ./lib ./dist ./es ./coverage ./types", | ||
"lint": "eslint --ext js,ts src tests", | ||
"check-types": "tsc --noEmit", | ||
"dev": "rollup -w -c", | ||
"test": "jest --config jest.config.json", | ||
"test:watch": "jest --watch --config jest.config.json", | ||
"test:coverage": "jest --coverage --config jest.config.json", | ||
"build": "rollup -c", | ||
"pretest": "npm run build", | ||
"prepublishOnly": "npm run clean && npm run check-types && npm run lint && npm test" | ||
}, | ||
"keywords": [ | ||
"promise" | ||
], | ||
"author": "z-hnan", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@babel/runtime-corejs3": "^7.11.2" | ||
"@babel/runtime": "^7.9.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.11.4", | ||
"@babel/plugin-transform-runtime": "^7.11.5", | ||
"@babel/preset-env": "^7.11.5", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@rollup/plugin-node-resolve": "^9.0.0", | ||
"@types/jest": "^26.0.10", | ||
"@types/node": "^14.6.2", | ||
"@typescript-eslint/eslint-plugin": "^3.10.1", | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"eslint": "^7.7.0", | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.9.0", | ||
"@babel/node": "^7.8.7", | ||
"@babel/plugin-external-helpers": "^7.8.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.9.5", | ||
"@babel/plugin-transform-runtime": "^7.9.0", | ||
"@babel/preset-env": "^7.9.5", | ||
"@babel/preset-typescript": "^7.9.0", | ||
"@babel/register": "^7.9.0", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-node-resolve": "^13.0.4", | ||
"@rollup/plugin-replace": "^3.0.0", | ||
"@types/jest": "^26.0.0", | ||
"@types/node": "^16.4.10", | ||
"@typescript-eslint/eslint-plugin": "^4.23.0", | ||
"@typescript-eslint/parser": "^4.23.0", | ||
"babel-eslint": "^10.1.0", | ||
"babel-jest": "^27.0.0", | ||
"cross-env": "^7.0.2", | ||
"eslint": "^7.26.0", | ||
"eslint-import-resolver-typescript": "^2.0.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-config-airbnb-typescript": "^9.0.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-jest": "^23.20.0", | ||
"jest": "^26.4.2", | ||
"lodash.merge": "^4.6.2", | ||
"npm-run-all": "^4.1.5", | ||
"glob": "^7.1.6", | ||
"jest": "^27.0.0", | ||
"netlify-plugin-cache": "^1.0.3", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.26.8", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-terser": "^7.0.1", | ||
"rollup-plugin-typescript2": "^0.27.2", | ||
"ts-jest": "^26.3.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
"browserslist": [ | ||
"last 1 version", | ||
"> 1%", | ||
"maintained node versions", | ||
"not dead" | ||
] | ||
"rollup": "^2.7.2", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"rxjs": "^7.3.0", | ||
"ts-jest": "^27.0.4", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
# batch-perform-promise | ||
> 3.0版本使用新的打包配置 | ||
批量执行promise的方法,能够自定义“并行”发出的promise的个数 | ||
@@ -4,0 +6,0 @@ |
@@ -31,3 +31,3 @@ /** | ||
constructor(tasks: T[], callback: (value: T) => Promise<any>, concurrentSize) { | ||
constructor(tasks: T[], callback: (value: T) => Promise<any>, concurrentSize: number) { | ||
this.tasks = tasks.map((t) => ({ done: false, success: true, error: null, payload: t })); | ||
@@ -38,2 +38,3 @@ this.concurrentSize = concurrentSize; | ||
this.performWorks = []; | ||
this.resolve = () => undefined; | ||
} | ||
@@ -64,3 +65,5 @@ | ||
const nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
if (nextTask) { | ||
this.performWork(nextTask); | ||
} | ||
} | ||
@@ -84,3 +87,3 @@ } | ||
task.success = false; | ||
task.error = error; | ||
task.error = error as Error; | ||
} finally { | ||
@@ -87,0 +90,0 @@ task.done = true; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
10
1
67
21095
37
434
1
+ Added@babel/runtime@^7.9.2
+ Added@babel/runtime@7.26.0(transitive)
- Removed@babel/runtime-corejs3@^7.11.2
- Removed@babel/runtime-corejs3@7.26.0(transitive)
- Removedcore-js-pure@3.39.0(transitive)