asva-executors
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -212,3 +212,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
"use strict"; | ||
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _Helpers_AsyncHelpers__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../Helpers/AsyncHelpers */ \"./src/modules/Helpers/AsyncHelpers.ts\");\nvar __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nvar __generator = (undefined && undefined.__generator) || function (thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n};\r\n\r\nvar RetrierExecutor = /** @class */ (function () {\r\n function RetrierExecutor(command, timeout, maxTime) {\r\n if (timeout === void 0) { timeout = 100; }\r\n if (maxTime === void 0) { maxTime = 1000; }\r\n this.iterationCount = 0;\r\n this.command = command;\r\n this.timeout = timeout;\r\n this.maxTime = maxTime;\r\n }\r\n Object.defineProperty(RetrierExecutor.prototype, \"timeElapsed\", {\r\n get: function () {\r\n return this.timeout * this.iterationCount;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n RetrierExecutor.prototype.takesTooLong = function () {\r\n return this.timeElapsed > this.maxTime;\r\n };\r\n RetrierExecutor.prototype.run = function () {\r\n return __awaiter(this, void 0, void 0, function () {\r\n var result;\r\n return __generator(this, function (_a) {\r\n switch (_a.label) {\r\n case 0:\r\n this.iterationCount = 0;\r\n result = this.command();\r\n _a.label = 1;\r\n case 1:\r\n if (!!result) return [3 /*break*/, 3];\r\n return [4 /*yield*/, _Helpers_AsyncHelpers__WEBPACK_IMPORTED_MODULE_0__[\"default\"].sleep(this.timeout)];\r\n case 2:\r\n _a.sent();\r\n this.iterationCount++;\r\n if (this.takesTooLong()) {\r\n throw new Error(\"Max time (\" + this.maxTime + \") elapsed.\");\r\n }\r\n result = this.command();\r\n return [3 /*break*/, 1];\r\n case 3: return [2 /*return*/];\r\n }\r\n });\r\n });\r\n };\r\n return RetrierExecutor;\r\n}());\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (RetrierExecutor);\r\n\n\n//# sourceURL=webpack:///./src/modules/Loaders/RetrierExecutor.ts?"); | ||
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _Helpers_AsyncHelpers__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../Helpers/AsyncHelpers */ \"./src/modules/Helpers/AsyncHelpers.ts\");\nvar __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nvar __generator = (undefined && undefined.__generator) || function (thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n};\r\n\r\nvar RetrierExecutor = /** @class */ (function () {\r\n function RetrierExecutor(command, timeout, maxTime) {\r\n if (timeout === void 0) { timeout = 100; }\r\n if (maxTime === void 0) { maxTime = 1000; }\r\n this.iterationCount = 0;\r\n this.command = command;\r\n this.timeout = timeout;\r\n this.maxTime = maxTime;\r\n }\r\n /**\r\n * Shortcut to use executor functionally.\r\n */\r\n RetrierExecutor.waitUntil = function (command, timeout, maxTime) {\r\n return __awaiter(this, void 0, void 0, function () {\r\n var executor;\r\n return __generator(this, function (_a) {\r\n switch (_a.label) {\r\n case 0:\r\n executor = new RetrierExecutor(command, timeout, maxTime);\r\n return [4 /*yield*/, executor.run()];\r\n case 1:\r\n _a.sent();\r\n return [2 /*return*/];\r\n }\r\n });\r\n });\r\n };\r\n Object.defineProperty(RetrierExecutor.prototype, \"timeElapsed\", {\r\n get: function () {\r\n return this.timeout * this.iterationCount;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n RetrierExecutor.prototype.takesTooLong = function () {\r\n return this.timeElapsed > this.maxTime;\r\n };\r\n RetrierExecutor.prototype.run = function () {\r\n return __awaiter(this, void 0, void 0, function () {\r\n var result;\r\n return __generator(this, function (_a) {\r\n switch (_a.label) {\r\n case 0:\r\n this.iterationCount = 0;\r\n result = this.command();\r\n _a.label = 1;\r\n case 1:\r\n if (!!result) return [3 /*break*/, 3];\r\n return [4 /*yield*/, _Helpers_AsyncHelpers__WEBPACK_IMPORTED_MODULE_0__[\"default\"].sleep(this.timeout)];\r\n case 2:\r\n _a.sent();\r\n this.iterationCount++;\r\n if (this.takesTooLong()) {\r\n throw new Error(\"Max time (\" + this.maxTime + \") elapsed.\");\r\n }\r\n result = this.command();\r\n return [3 /*break*/, 1];\r\n case 3: return [2 /*return*/];\r\n }\r\n });\r\n });\r\n };\r\n return RetrierExecutor;\r\n}());\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (RetrierExecutor);\r\n\n\n//# sourceURL=webpack:///./src/modules/Loaders/RetrierExecutor.ts?"); | ||
@@ -215,0 +215,0 @@ /***/ }) |
@@ -0,7 +1,12 @@ | ||
export declare type RetrierExecutorCommand = () => boolean; | ||
export default class RetrierExecutor { | ||
protected command: () => boolean; | ||
protected command: RetrierExecutorCommand; | ||
protected timeout: number; | ||
protected maxTime: number; | ||
iterationCount: number; | ||
constructor(command: () => boolean, timeout?: number, maxTime?: number); | ||
constructor(command: RetrierExecutorCommand, timeout?: number, maxTime?: number); | ||
/** | ||
* Shortcut to use executor functionally. | ||
*/ | ||
static waitUntil(command: RetrierExecutorCommand, timeout?: number, maxTime?: number): Promise<void>; | ||
readonly timeElapsed: number; | ||
@@ -8,0 +13,0 @@ protected takesTooLong(): boolean; |
{ | ||
"name": "asva-executors", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Helper classes for your async flow control", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
import AsyncHelpers from '../Helpers/AsyncHelpers' | ||
export type RetrierExecutorCommand = () => boolean | ||
export default class RetrierExecutor { | ||
protected command: () => boolean | ||
protected command: RetrierExecutorCommand | ||
protected timeout: number | ||
@@ -9,3 +11,3 @@ protected maxTime: number | ||
constructor (command: () => boolean, timeout: number = 100, maxTime: number = 1000) { | ||
constructor (command: RetrierExecutorCommand, timeout: number = 100, maxTime: number = 1000) { | ||
this.command = command | ||
@@ -16,2 +18,10 @@ this.timeout = timeout | ||
/** | ||
* Shortcut to use executor functionally. | ||
*/ | ||
static async waitUntil (command: RetrierExecutorCommand, timeout?: number, maxTime?: number) { | ||
const executor = new RetrierExecutor(command, timeout, maxTime) | ||
await executor.run() | ||
} | ||
get timeElapsed () { | ||
@@ -18,0 +28,0 @@ return this.timeout * this.iterationCount |
75109
1047