named-promise-task
Advanced tools
Comparing version 1.0.1 to 1.1.0
'use strict'; | ||
/** | ||
* A Named Promise Task | ||
* Insipre from https://stackoverflow.com/questions/53540348/js-async-await-tasks-queue | ||
*/ | ||
@@ -13,67 +9,126 @@ var _regeneratorRuntime = require("@babel/runtime/regenerator"); | ||
var _createClass = require("@babel/runtime/helpers/createClass"); | ||
var _assertThisInitialized = require("@babel/runtime/helpers/assertThisInitialized"); | ||
var _inherits = require("@babel/runtime/helpers/inherits"); | ||
var _possibleConstructorReturn = require("@babel/runtime/helpers/possibleConstructorReturn"); | ||
var _getPrototypeOf = require("@babel/runtime/helpers/getPrototypeOf"); | ||
var _defineProperty = require("@babel/runtime/helpers/defineProperty"); | ||
var PromiseTask = function PromiseTask(context, namedWorkers) { | ||
var _this = this; | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
_classCallCheck(this, PromiseTask); | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
_defineProperty(this, "addTask", function () { | ||
var pending = Promise.resolve(); | ||
var events = require('events'); | ||
/** | ||
* A Named Promise Task | ||
* Insipre from https://stackoverflow.com/questions/53540348/js-async-await-tasks-queue | ||
*/ | ||
var run = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(name) { | ||
var _this$_namedWorkers$n, | ||
_len, | ||
values, | ||
_key, | ||
_args = arguments; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return pending; | ||
var PromiseTask = /*#__PURE__*/function (_events$EventEmitter) { | ||
_inherits(PromiseTask, _events$EventEmitter); | ||
case 3: | ||
_context.prev = 3; | ||
var _super = _createSuper(PromiseTask); | ||
for (_len = _args.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
values[_key - 1] = _args[_key]; | ||
} | ||
function PromiseTask(context, namedWorkers) { | ||
var _this; | ||
return _context.abrupt("return", (_this$_namedWorkers$n = _this._namedWorkers[name]).call.apply(_this$_namedWorkers$n, [_this._context].concat(values))); | ||
_classCallCheck(this, PromiseTask); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
_this = _super.call(this); | ||
_defineProperty(_assertThisInitialized(_this), "addTask", function () { | ||
var pending = Promise.resolve(); | ||
var run = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(name) { | ||
var _this$_namedWorkers$n, | ||
_len, | ||
values, | ||
_key, | ||
_args = arguments; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return pending; | ||
case 3: | ||
_context.prev = 3; | ||
for (_len = _args.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
values[_key - 1] = _args[_key]; | ||
} | ||
return _context.abrupt("return", (_this$_namedWorkers$n = _this._namedWorkers[name]).call.apply(_this$_namedWorkers$n, [_this._context].concat(values))["finally"](function () { | ||
_this._size--; | ||
if (_this._size === 0) { | ||
_this.emit('stop'); | ||
} | ||
})); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
} | ||
}, _callee, null, [[0,, 3, 7]]); | ||
})); | ||
}, _callee, null, [[0,, 3, 7]]); | ||
})); | ||
return function run(_x) { | ||
return _ref.apply(this, arguments); | ||
return function run(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); // update pending promise so that next task could await for it | ||
return function (name) { | ||
_this._size++; | ||
if (_this._size === 1) { | ||
_this.emit('start'); | ||
} | ||
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
return pending = run.apply(void 0, [name].concat(values)); | ||
}; | ||
}(); // update pending promise so that next task could await for it | ||
}()); | ||
_this._context = context; | ||
_this._namedWorkers = namedWorkers; | ||
_this._pending = Promise.resolve(); | ||
_this._size = 0; | ||
return _this; | ||
} // task queue size | ||
return function (name) { | ||
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
return pending = run.apply(void 0, [name].concat(values)); | ||
}; | ||
}()); | ||
_createClass(PromiseTask, [{ | ||
key: "size", | ||
get: function get() { | ||
return this._size; | ||
} // checking is running | ||
this._context = context; | ||
this._namedWorkers = namedWorkers; | ||
this._pending = Promise.resolve(); | ||
} // task executor | ||
; | ||
}, { | ||
key: "isRunning", | ||
get: function get() { | ||
return this._size !== 0; | ||
} // task executor | ||
}]); | ||
return PromiseTask; | ||
}(events.EventEmitter); | ||
module.exports = PromiseTask; | ||
//# sourceMappingURL=index.cjs.js.map |
import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/createClass"; | ||
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; | ||
import _inherits from "@babel/runtime/helpers/inherits"; | ||
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; | ||
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } | ||
import { EventEmitter } from 'events'; | ||
/** | ||
@@ -10,65 +20,104 @@ * A Named Promise Task | ||
*/ | ||
var PromiseTask = function PromiseTask(context, namedWorkers) { | ||
var _this = this; | ||
_classCallCheck(this, PromiseTask); | ||
var PromiseTask = /*#__PURE__*/function (_EventEmitter) { | ||
_inherits(PromiseTask, _EventEmitter); | ||
_defineProperty(this, "addTask", function () { | ||
var pending = Promise.resolve(); | ||
var _super = _createSuper(PromiseTask); | ||
var run = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(name) { | ||
var _this$_namedWorkers$n, | ||
_len, | ||
values, | ||
_key, | ||
_args = arguments; | ||
function PromiseTask(context, namedWorkers) { | ||
var _this; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return pending; | ||
_classCallCheck(this, PromiseTask); | ||
case 3: | ||
_context.prev = 3; | ||
_this = _super.call(this); | ||
for (_len = _args.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
values[_key - 1] = _args[_key]; | ||
} | ||
_defineProperty(_assertThisInitialized(_this), "addTask", function () { | ||
var pending = Promise.resolve(); | ||
return _context.abrupt("return", (_this$_namedWorkers$n = _this._namedWorkers[name]).call.apply(_this$_namedWorkers$n, [_this._context].concat(values))); | ||
var run = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(name) { | ||
var _this$_namedWorkers$n, | ||
_len, | ||
values, | ||
_key, | ||
_args = arguments; | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return pending; | ||
case 3: | ||
_context.prev = 3; | ||
for (_len = _args.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
values[_key - 1] = _args[_key]; | ||
} | ||
return _context.abrupt("return", (_this$_namedWorkers$n = _this._namedWorkers[name]).call.apply(_this$_namedWorkers$n, [_this._context].concat(values))["finally"](function () { | ||
_this._size--; | ||
if (_this._size === 0) { | ||
_this.emit('stop'); | ||
} | ||
})); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
} | ||
}, _callee, null, [[0,, 3, 7]]); | ||
})); | ||
}, _callee, null, [[0,, 3, 7]]); | ||
})); | ||
return function run(_x) { | ||
return _ref.apply(this, arguments); | ||
return function run(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); // update pending promise so that next task could await for it | ||
return function (name) { | ||
_this._size++; | ||
if (_this._size === 1) { | ||
_this.emit('start'); | ||
} | ||
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
return pending = run.apply(void 0, [name].concat(values)); | ||
}; | ||
}(); // update pending promise so that next task could await for it | ||
}()); | ||
_this._context = context; | ||
_this._namedWorkers = namedWorkers; | ||
_this._pending = Promise.resolve(); | ||
_this._size = 0; | ||
return _this; | ||
} // task queue size | ||
return function (name) { | ||
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
return pending = run.apply(void 0, [name].concat(values)); | ||
}; | ||
}()); | ||
_createClass(PromiseTask, [{ | ||
key: "size", | ||
get: function get() { | ||
return this._size; | ||
} // checking is running | ||
this._context = context; | ||
this._namedWorkers = namedWorkers; | ||
this._pending = Promise.resolve(); | ||
} // task executor | ||
; | ||
}, { | ||
key: "isRunning", | ||
get: function get() { | ||
return this._size !== 0; | ||
} // task executor | ||
}]); | ||
return PromiseTask; | ||
}(EventEmitter); | ||
export default PromiseTask; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "named-promise-task", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "No Queue, Just a simple promise mechanics to help you run serial tasks.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -19,2 +19,36 @@ # A Named Promise Task | ||
## Usage | ||
### Method | ||
``` | ||
addTask: Add a named task with parameters | ||
``` | ||
### Properties | ||
``` | ||
size: the running task numbers | ||
``` | ||
``` | ||
isRunning: true/false of having running task | ||
``` | ||
### Events | ||
``` | ||
start: one or more tasks is running | ||
``` | ||
``` | ||
stop: all task completed | ||
``` | ||
### Promise methods | ||
``` | ||
then: the tasks's result | ||
``` | ||
``` | ||
catch: a exception from task | ||
``` | ||
### Examples | ||
``` Javascript | ||
@@ -61,2 +95,9 @@ | ||
manager.on('start', () =>{ | ||
console.log('start') | ||
}) | ||
manager.on('stop', () =>{ | ||
console.log('stop') | ||
}) | ||
manager.addTask( 'fetch', 1 ).then( console.log ) | ||
@@ -63,0 +104,0 @@ manager.addTask( 'fetch2', 2, "str" ) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
18290
191
112