batch-perform-promise
Advanced tools
Comparing version 1.0.0 to 2.0.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global['package-seed-lib'] = factory()); | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.batchPerformPromise = factory()); | ||
}(this, (function () { 'use strict'; | ||
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.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(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); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
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; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
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; | ||
} | ||
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."); | ||
} | ||
/** | ||
* 批量执行Promise任务 | ||
*/ | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
_classCallCheck(this, BatchPerfromer); | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
}; | ||
}); | ||
class BatchPerfromer { | ||
constructor(tasks, callback, concurrentSize) { | ||
this.tasks = tasks.map(t => ({ | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
})); | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = _toConsumableArray(this.tasks); | ||
this.undoTasks = [...this.tasks]; | ||
this.callback = callback; | ||
@@ -120,134 +25,55 @@ this.performWorks = []; | ||
_createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var _this = this; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
async exec() { | ||
return new Promise(resolve => { | ||
this.resolve = resolve; | ||
this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
_this.beginWork(); | ||
})); | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
async beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) { | ||
this.resolve(); | ||
} // 放入任务 | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
return exec; | ||
}() // 开始批量操作 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
const nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
} // 开始执行任务 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var nextTask; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
async performWork(task) { | ||
// 当前任务压入队列 | ||
const performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
} | ||
const currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成 | ||
return beginWork; | ||
}() // 开始执行任务 | ||
this.beginWork(); | ||
} | ||
} | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(task) { | ||
var performPromise, currentPromiseIdx; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context3.prev = 2; | ||
_context3.next = 5; | ||
return performPromise; | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context3.next = 13; | ||
break; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context3.t0; | ||
case 13: | ||
_context3.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(_toConsumableArray(this.performWorks.slice(0, currentPromiseIdx)), _toConsumableArray(this.performWorks.slice(currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context3.finish(13); | ||
case 19: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
}(); | ||
} | ||
/** | ||
@@ -261,33 +87,9 @@ * 批量执行Promise的方法 | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize = 2) { | ||
// 初始化 | ||
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
_context4.next = 4; | ||
return batchPerfromer.exec(); | ||
case 4: | ||
return _context4.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
return batchPerfromer.tasks; | ||
} | ||
@@ -294,0 +96,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self)["package-seed-lib"]=r()}(this,(function(){"use strict";function e(e,r,t,n,o,i,s){try{var a=e[i](s),u=a.value}catch(e){return void t(e)}a.done?r(u):Promise.resolve(u).then(n,o)}function r(r){return function(){var t=this,n=arguments;return new Promise((function(o,i){var s=r.apply(t,n);function a(r){e(s,o,i,a,u,"next",r)}function u(r){e(s,o,i,a,u,"throw",r)}a(void 0)}))}}function t(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),Object.defineProperty(e,n.key,n)}}function n(e){return function(e){if(Array.isArray(e))return o(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,r){if(!e)return;if("string"==typeof e)return o(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return o(e,r)}(e)||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.")}()}function o(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 i=function(){function e(r,t,o){!function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}(this,e),this.tasks=r.map((function(e){return{done:!1,success:!0,error:null,payload:e}})),this.concurrentSize=o,this.undoTasks=n(this.tasks),this.callback=t,this.performWorks=[]}var o,i,s,a,u,c;return o=e,(i=[{key:"shouldPerform",value:function(){return this.performWorks.length<this.concurrentSize}},{key:"exec",value:(c=r(regeneratorRuntime.mark((function e(){var r=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e){r.resolve=e,r.beginWork()})));case 1:case"end":return e.stop()}}),e)}))),function(){return c.apply(this,arguments)})},{key:"beginWork",value:(u=r(regeneratorRuntime.mark((function e(){var r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(this.undoTasks.length<=0&&this.tasks.every((function(e){return!0===e.done}))&&this.resolve();!0===this.shouldPerform()&&this.undoTasks.length>0;)r=this.undoTasks.shift(),this.performWork(r);case 2:case"end":return e.stop()}}),e,this)}))),function(){return u.apply(this,arguments)})},{key:"performWork",value:(a=r(regeneratorRuntime.mark((function e(r){var t,o;return regeneratorRuntime.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,o=this.performWorks.indexOf(t),this.performWorks=[].concat(n(this.performWorks.slice(0,o)),n(this.performWorks.slice(o+1))),this.beginWork(),e.finish(13);case 19:case"end":return e.stop()}}),e,this,[[2,9,13,19]])}))),function(e){return a.apply(this,arguments)})}])&&t(o.prototype,i),s&&t(o,s),e}();function s(){return(s=r(regeneratorRuntime.mark((function e(r,t){var n,o,s=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=s.length>2&&void 0!==s[2]?s[2]:2,o=new i(r,t,n),e.next=4,o.exec();case 4:return e.abrupt("return",o.tasks);case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}return function(e,r){return s.apply(this,arguments)}})); | ||
!function(s,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(s="undefined"!=typeof globalThis?globalThis:s||self).batchPerformPromise=e()}(this,(function(){"use strict";class s{constructor(s,e,r){this.tasks=s.map(s=>({done:!1,success:!0,error:null,payload:s})),this.concurrentSize=r,this.undoTasks=[...this.tasks],this.callback=e,this.performWorks=[]}shouldPerform(){return this.performWorks.length<this.concurrentSize}async exec(){return new Promise(s=>{this.resolve=s,this.beginWork()})}async beginWork(){for(this.undoTasks.length<=0&&this.tasks.every(s=>!0===s.done)&&this.resolve();!0===this.shouldPerform()&&this.undoTasks.length>0;){const s=this.undoTasks.shift();this.performWork(s)}}async performWork(s){const e=this.callback(s.payload);this.performWorks.push(e);try{await e,s.success=!0,s.error=null}catch(e){s.success=!1,s.error=e}finally{s.done=!0;const r=this.performWorks.indexOf(e);this.performWorks=[...this.performWorks.slice(0,r),...this.performWorks.slice(r+1)],this.beginWork()}}}return async function(e,r,o=2){const t=new s(e,r,o);return await t.exec(),t.tasks}})); |
306
es/index.js
@@ -1,109 +0,14 @@ | ||
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.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(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); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
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; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
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; | ||
} | ||
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."); | ||
} | ||
/** | ||
* 批量执行Promise任务 | ||
*/ | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
_classCallCheck(this, BatchPerfromer); | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
}; | ||
}); | ||
class BatchPerfromer { | ||
constructor(tasks, callback, concurrentSize) { | ||
this.tasks = tasks.map(t => ({ | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
})); | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = _toConsumableArray(this.tasks); | ||
this.undoTasks = [...this.tasks]; | ||
this.callback = callback; | ||
@@ -114,134 +19,55 @@ this.performWorks = []; | ||
_createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var _this = this; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
async exec() { | ||
return new Promise(resolve => { | ||
this.resolve = resolve; | ||
this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
_this.beginWork(); | ||
})); | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
async beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) { | ||
this.resolve(); | ||
} // 放入任务 | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
return exec; | ||
}() // 开始批量操作 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
const nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
} // 开始执行任务 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var nextTask; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
async performWork(task) { | ||
// 当前任务压入队列 | ||
const performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
} | ||
const currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成 | ||
return beginWork; | ||
}() // 开始执行任务 | ||
this.beginWork(); | ||
} | ||
} | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(task) { | ||
var performPromise, currentPromiseIdx; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context3.prev = 2; | ||
_context3.next = 5; | ||
return performPromise; | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context3.next = 13; | ||
break; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context3.t0; | ||
case 13: | ||
_context3.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(_toConsumableArray(this.performWorks.slice(0, currentPromiseIdx)), _toConsumableArray(this.performWorks.slice(currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context3.finish(13); | ||
case 19: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
}(); | ||
} | ||
/** | ||
@@ -255,35 +81,11 @@ * 批量执行Promise的方法 | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize = 2) { | ||
// 初始化 | ||
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
_context4.next = 4; | ||
return batchPerfromer.exec(); | ||
case 4: | ||
return _context4.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
return batchPerfromer.tasks; | ||
} | ||
export default batchPerformPromise; |
306
lib/index.js
'use strict'; | ||
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.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(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); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
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; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
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; | ||
} | ||
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."); | ||
} | ||
/** | ||
* 批量执行Promise任务 | ||
*/ | ||
var BatchPerfromer = /*#__PURE__*/function () { | ||
function BatchPerfromer(tasks, callback, concurrentSize) { | ||
_classCallCheck(this, BatchPerfromer); | ||
this.tasks = tasks.map(function (t) { | ||
return { | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
}; | ||
}); | ||
class BatchPerfromer { | ||
constructor(tasks, callback, concurrentSize) { | ||
this.tasks = tasks.map(t => ({ | ||
done: false, | ||
success: true, | ||
error: null, | ||
payload: t | ||
})); | ||
this.concurrentSize = concurrentSize; | ||
this.undoTasks = _toConsumableArray(this.tasks); | ||
this.undoTasks = [...this.tasks]; | ||
this.callback = callback; | ||
@@ -116,134 +21,55 @@ this.performWorks = []; | ||
_createClass(BatchPerfromer, [{ | ||
key: "shouldPerform", | ||
value: function shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
shouldPerform() { | ||
return this.performWorks.length < this.concurrentSize; | ||
} // 启动任务 | ||
}, { | ||
key: "exec", | ||
value: function () { | ||
var _exec = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var _this = this; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt("return", new Promise(function (resolve) { | ||
_this.resolve = resolve; | ||
async exec() { | ||
return new Promise(resolve => { | ||
this.resolve = resolve; | ||
this.beginWork(); | ||
}); | ||
} // 开始批量操作 | ||
_this.beginWork(); | ||
})); | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
async beginWork() { | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) { | ||
this.resolve(); | ||
} // 放入任务 | ||
function exec() { | ||
return _exec.apply(this, arguments); | ||
} | ||
return exec; | ||
}() // 开始批量操作 | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
const nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
} // 开始执行任务 | ||
}, { | ||
key: "beginWork", | ||
value: function () { | ||
var _beginWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var nextTask; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
// 出口条件,没有剩余的任务,并且所有任务已经done | ||
if (this.undoTasks.length <= 0 && this.tasks.every(function (t) { | ||
return t.done === true; | ||
})) { | ||
this.resolve(); | ||
} // 放入任务 | ||
async performWork(task) { | ||
// 当前任务压入队列 | ||
const performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
while (this.shouldPerform() === true && this.undoTasks.length > 0) { | ||
nextTask = this.undoTasks.shift(); | ||
this.performWork(nextTask); | ||
} | ||
try { | ||
// 执行任务 | ||
await performPromise; // success | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
task.success = true; | ||
task.error = null; | ||
} catch (error) { | ||
// error | ||
task.success = false; | ||
task.error = error; | ||
} finally { | ||
task.done = true; // 去除该元素 | ||
function beginWork() { | ||
return _beginWork.apply(this, arguments); | ||
} | ||
const currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成 | ||
return beginWork; | ||
}() // 开始执行任务 | ||
this.beginWork(); | ||
} | ||
} | ||
}, { | ||
key: "performWork", | ||
value: function () { | ||
var _performWork = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(task) { | ||
var performPromise, currentPromiseIdx; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
// 当前任务压入队列 | ||
performPromise = this.callback(task.payload); | ||
this.performWorks.push(performPromise); | ||
_context3.prev = 2; | ||
_context3.next = 5; | ||
return performPromise; | ||
case 5: | ||
// success | ||
task.success = true; | ||
task.error = null; | ||
_context3.next = 13; | ||
break; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3["catch"](2); | ||
// error | ||
task.success = false; | ||
task.error = _context3.t0; | ||
case 13: | ||
_context3.prev = 13; | ||
task.done = true; // 去除该元素 | ||
currentPromiseIdx = this.performWorks.indexOf(performPromise); | ||
this.performWorks = [].concat(_toConsumableArray(this.performWorks.slice(0, currentPromiseIdx)), _toConsumableArray(this.performWorks.slice(currentPromiseIdx + 1))); // 标志此任务完成 | ||
this.beginWork(); | ||
return _context3.finish(13); | ||
case 19: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 9, 13, 19]]); | ||
})); | ||
function performWork(_x) { | ||
return _performWork.apply(this, arguments); | ||
} | ||
return performWork; | ||
}() | ||
}]); | ||
return BatchPerfromer; | ||
}(); | ||
} | ||
/** | ||
@@ -257,35 +83,11 @@ * 批量执行Promise的方法 | ||
function batchPerformPromise(_x2, _x3) { | ||
return _batchPerformPromise.apply(this, arguments); | ||
} | ||
async function batchPerformPromise(tasks, callback, concurrentSize = 2) { | ||
// 初始化 | ||
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
function _batchPerformPromise() { | ||
_batchPerformPromise = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(tasks, callback) { | ||
var concurrentSize, | ||
batchPerfromer, | ||
_args4 = arguments; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
concurrentSize = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 2; | ||
// 初始化 | ||
batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作 | ||
await batchPerfromer.exec(); // 返回操作结果 | ||
_context4.next = 4; | ||
return batchPerfromer.exec(); | ||
case 4: | ||
return _context4.abrupt("return", batchPerfromer.tasks); | ||
case 5: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4); | ||
})); | ||
return _batchPerformPromise.apply(this, arguments); | ||
return batchPerfromer.tasks; | ||
} | ||
module.exports = batchPerformPromise; |
{ | ||
"name": "batch-perform-promise", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "batch perform promise", | ||
@@ -31,3 +31,5 @@ "scripts": { | ||
}, | ||
"keywords": ["promise"], | ||
"keywords": [ | ||
"promise" | ||
], | ||
"author": "z-hnan", | ||
@@ -38,3 +40,2 @@ "license": "MIT", | ||
"@babel/core": "^7.11.4", | ||
"@babel/preset-env": "^7.11.0", | ||
"@babel/preset-typescript": "^7.10.4", | ||
@@ -57,2 +58,3 @@ "@rollup/plugin-node-resolve": "^9.0.0", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-terser": "^7.0.1", | ||
@@ -62,3 +64,9 @@ "rollup-plugin-typescript2": "^0.27.2", | ||
"typescript": "^4.0.2" | ||
} | ||
}, | ||
"browserslist": [ | ||
"last 1 version", | ||
"> 1%", | ||
"maintained node versions", | ||
"not dead" | ||
] | ||
} |
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
16525
335
1