Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

batch-perform-promise

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batch-perform-promise - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

387

dist/index.js
(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.batchPerformPromise = factory());
}(this, (function () { 'use strict';
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';
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;
/**
* 批量执行Promise任务
*/
class BatchPerfromer {
constructor(tasks, callback, concurrentSize) {
this.tasks = tasks.map(t => ({
done: false,
success: true,
error: null,
payload: t
}));
var BatchPerfromer = /*#__PURE__*/function () {
function BatchPerfromer(tasks, callback, concurrentSize) {
classCallCheck(this, BatchPerfromer);
this.tasks = map(tasks).call(tasks, function (t) {
return {
done: false,
success: true,
error: null,
payload: t
};
});
this.concurrentSize = concurrentSize;
this.undoTasks = [...this.tasks];
this.undoTasks = toConsumableArray(this.tasks);
this.callback = callback;

@@ -25,55 +190,137 @@ this.performWorks = [];

shouldPerform() {
return this.performWorks.length < this.concurrentSize;
} // 启动任务
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;
async exec() {
return new Promise(resolve => {
this.resolve = resolve;
this.beginWork();
});
} // 开始批量操作
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();
}));
async beginWork() {
// 出口条件,没有剩余的任务,并且所有任务已经done
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) {
this.resolve();
} // 放入任务
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function exec() {
return _exec.apply(this, arguments);
}
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
const nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
} // 开始执行任务
return exec;
}() // 开始批量操作
}, {
key: "beginWork",
value: function () {
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
var _context2;
async performWork(task) {
// 当前任务压入队列
const performPromise = this.callback(task.payload);
this.performWorks.push(performPromise);
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();
} // 放入任务
try {
// 执行任务
await performPromise; // success
task.success = true;
task.error = null;
} catch (error) {
// error
task.success = false;
task.error = error;
} finally {
task.done = true; // 去除该元素
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
const currentPromiseIdx = this.performWorks.indexOf(performPromise);
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成
case 2:
case "end":
return _context3.stop();
}
}
}, _callee2, this);
}));
this.beginWork();
}
}
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;
}();
/**

@@ -87,9 +334,33 @@ * 批量执行Promise的方法

async function batchPerformPromise(tasks, callback, concurrentSize = 2) {
// 初始化
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作
function batchPerformPromise(_x2, _x3) {
return _batchPerformPromise.apply(this, arguments);
}
await batchPerfromer.exec(); // 返回操作结果
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); // 执行批量操作
return batchPerfromer.tasks;
_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);
}

@@ -96,0 +367,0 @@

2

dist/index.min.js

@@ -1,1 +0,1 @@

!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}}));
!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)}}));

@@ -0,14 +1,177 @@

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;
/**
* 批量执行Promise任务
*/
class BatchPerfromer {
constructor(tasks, callback, concurrentSize) {
this.tasks = tasks.map(t => ({
done: false,
success: true,
error: null,
payload: t
}));
var BatchPerfromer = /*#__PURE__*/function () {
function BatchPerfromer(tasks, callback, concurrentSize) {
classCallCheck(this, BatchPerfromer);
this.tasks = map(tasks).call(tasks, function (t) {
return {
done: false,
success: true,
error: null,
payload: t
};
});
this.concurrentSize = concurrentSize;
this.undoTasks = [...this.tasks];
this.undoTasks = toConsumableArray(this.tasks);
this.callback = callback;

@@ -19,55 +182,137 @@ this.performWorks = [];

shouldPerform() {
return this.performWorks.length < this.concurrentSize;
} // 启动任务
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;
async exec() {
return new Promise(resolve => {
this.resolve = resolve;
this.beginWork();
});
} // 开始批量操作
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();
}));
async beginWork() {
// 出口条件,没有剩余的任务,并且所有任务已经done
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) {
this.resolve();
} // 放入任务
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function exec() {
return _exec.apply(this, arguments);
}
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
const nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
} // 开始执行任务
return exec;
}() // 开始批量操作
}, {
key: "beginWork",
value: function () {
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
var _context2;
async performWork(task) {
// 当前任务压入队列
const performPromise = this.callback(task.payload);
this.performWorks.push(performPromise);
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();
} // 放入任务
try {
// 执行任务
await performPromise; // success
task.success = true;
task.error = null;
} catch (error) {
// error
task.success = false;
task.error = error;
} finally {
task.done = true; // 去除该元素
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
const currentPromiseIdx = this.performWorks.indexOf(performPromise);
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成
case 2:
case "end":
return _context3.stop();
}
}
}, _callee2, this);
}));
this.beginWork();
}
}
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;
}();
/**

@@ -81,11 +326,35 @@ * 批量执行Promise的方法

async function batchPerformPromise(tasks, callback, concurrentSize = 2) {
// 初始化
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作
function batchPerformPromise(_x2, _x3) {
return _batchPerformPromise.apply(this, arguments);
}
await batchPerfromer.exec(); // 返回操作结果
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); // 执行批量操作
return batchPerfromer.tasks;
_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);
}
export default batchPerformPromise;
'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;
/**
* 批量执行Promise任务
*/
class BatchPerfromer {
constructor(tasks, callback, concurrentSize) {
this.tasks = tasks.map(t => ({
done: false,
success: true,
error: null,
payload: t
}));
var BatchPerfromer = /*#__PURE__*/function () {
function BatchPerfromer(tasks, callback, concurrentSize) {
classCallCheck(this, BatchPerfromer);
this.tasks = map(tasks).call(tasks, function (t) {
return {
done: false,
success: true,
error: null,
payload: t
};
});
this.concurrentSize = concurrentSize;
this.undoTasks = [...this.tasks];
this.undoTasks = toConsumableArray(this.tasks);
this.callback = callback;

@@ -21,55 +201,137 @@ this.performWorks = [];

shouldPerform() {
return this.performWorks.length < this.concurrentSize;
} // 启动任务
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;
async exec() {
return new Promise(resolve => {
this.resolve = resolve;
this.beginWork();
});
} // 开始批量操作
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();
}));
async beginWork() {
// 出口条件,没有剩余的任务,并且所有任务已经done
if (this.undoTasks.length <= 0 && this.tasks.every(t => t.done === true)) {
this.resolve();
} // 放入任务
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function exec() {
return _exec.apply(this, arguments);
}
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
const nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
} // 开始执行任务
return exec;
}() // 开始批量操作
}, {
key: "beginWork",
value: function () {
var _beginWork = asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
var _context2;
async performWork(task) {
// 当前任务压入队列
const performPromise = this.callback(task.payload);
this.performWorks.push(performPromise);
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();
} // 放入任务
try {
// 执行任务
await performPromise; // success
task.success = true;
task.error = null;
} catch (error) {
// error
task.success = false;
task.error = error;
} finally {
task.done = true; // 去除该元素
while (this.shouldPerform() === true && this.undoTasks.length > 0) {
nextTask = this.undoTasks.shift();
this.performWork(nextTask);
}
const currentPromiseIdx = this.performWorks.indexOf(performPromise);
this.performWorks = [...this.performWorks.slice(0, currentPromiseIdx), ...this.performWorks.slice(currentPromiseIdx + 1)]; // 标志此任务完成
case 2:
case "end":
return _context3.stop();
}
}
}, _callee2, this);
}));
this.beginWork();
}
}
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;
}();
/**

@@ -83,11 +345,35 @@ * 批量执行Promise的方法

async function batchPerformPromise(tasks, callback, concurrentSize = 2) {
// 初始化
const batchPerfromer = new BatchPerfromer(tasks, callback, concurrentSize); // 执行批量操作
function batchPerformPromise(_x2, _x3) {
return _batchPerformPromise.apply(this, arguments);
}
await batchPerfromer.exec(); // 返回操作结果
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); // 执行批量操作
return batchPerfromer.tasks;
_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);
}
module.exports = batchPerformPromise;
{
"name": "batch-perform-promise",
"version": "2.0.0",
"version": "2.1.0",
"description": "batch perform promise",

@@ -36,5 +36,9 @@ "scripts": {

"license": "MIT",
"dependencies": {},
"dependencies": {
"@babel/runtime-corejs3": "^7.11.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",

@@ -41,0 +45,0 @@ "@rollup/plugin-node-resolve": "^9.0.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc