asva-executors
Advanced tools
Comparing version
@@ -238,2 +238,3 @@ import { __extends, __awaiter, __generator } from 'tslib'; | ||
var InfiniteLoader = /** @class */ (function () { | ||
// Is currently running from 0 pointer | ||
function InfiniteLoader(run, perStep) { | ||
@@ -244,4 +245,6 @@ if (perStep === void 0) { perStep = 20; } | ||
this.isFinished = false; | ||
this.isRefreshing = false; | ||
this.perStep = 20; // number of items to load | ||
this.pointer = 0; | ||
// Means no runs were executed | ||
this.isFresh = true; | ||
@@ -272,2 +275,3 @@ this.executor = new LadderExecutor(function (refresh) { | ||
* We tried to load, but list is empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
@@ -286,4 +290,11 @@ get: function () { | ||
* We tried to load, and list is not empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
get: function () { | ||
if (this.isFresh) { | ||
return false; | ||
} | ||
if (this.executor.isRunning) { | ||
return false; | ||
} | ||
return !!this.items.length; | ||
@@ -294,14 +305,2 @@ }, | ||
}); | ||
Object.defineProperty(InfiniteLoader.prototype, "isRefreshing", { | ||
/** | ||
* Is loading anew. This could happen in two cases. | ||
* 1) Executor is running for the first time. Meaning the list is empty. | ||
* 2) Executor is refreshing. Meaning the list is not empty. | ||
*/ | ||
get: function () { | ||
return this.pointer === 0 && this.isRunning; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -311,21 +310,30 @@ * Loads a bunch of items | ||
InfiniteLoader.prototype.next = function () { | ||
if (this.isRunning || this.isFinished) { | ||
return; | ||
} | ||
this.isFresh = false; | ||
this.executor.run(); | ||
}; | ||
/** | ||
* Refresh the list. | ||
*/ | ||
InfiniteLoader.prototype.refresh = function () { | ||
this.executor.run(true); | ||
}; | ||
InfiniteLoader.prototype.runRequest = function (pointerRequest, refresh) { | ||
if (refresh === void 0) { refresh = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var pointer, result, exception_1; | ||
var result, exception_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.log('SP', this.pointer); | ||
if (this.pointer === 0) { | ||
refresh = true; | ||
} | ||
if (refresh) { | ||
this.pointer = 0; | ||
this.isFresh = true; | ||
this.isRefreshing = true; | ||
this.isFinished = false; | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
pointer = refresh ? this.pointer : 0; | ||
return [4 /*yield*/, pointerRequest(pointer, this.perStep)]; | ||
_a.trys.push([1, 3, 4, 5]); | ||
return [4 /*yield*/, pointerRequest(this.pointer, this.perStep)]; | ||
case 2: | ||
@@ -340,12 +348,6 @@ result = _a.sent(); | ||
} | ||
if (refresh) { | ||
this.isFresh = true; | ||
this.isFinished = false; | ||
} | ||
else { | ||
this.pointer = pointer + this.perStep; | ||
} | ||
console.log('EP', this.pointer); | ||
this.applyNew(result); | ||
return [3 /*break*/, 4]; | ||
this.pointer = this.pointer + this.perStep; | ||
this.isFresh = false; | ||
return [3 /*break*/, 5]; | ||
case 3: | ||
@@ -355,3 +357,6 @@ exception_1 = _a.sent(); | ||
throw exception_1; | ||
case 4: return [2 /*return*/]; | ||
case 4: | ||
this.isRefreshing = false; | ||
return [7 /*endfinally*/]; | ||
case 5: return [2 /*return*/]; | ||
} | ||
@@ -361,12 +366,2 @@ }); | ||
}; | ||
/** | ||
* Refresh the list. | ||
* This will replace current items with new ones after loading. Not clear the list immediately. | ||
*/ | ||
InfiniteLoader.prototype.refresh = function () { | ||
this.pointer = 0; | ||
this.isFresh = true; | ||
this.isFinished = false; | ||
this.executor.run(true); | ||
}; | ||
InfiniteLoader.prototype.applyNew = function (items) { | ||
@@ -373,0 +368,0 @@ if (this.isFresh) { |
@@ -176,3 +176,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
"use strict"; | ||
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! tslib */ \"./node_modules/tslib/tslib.es6.js\");\n/* harmony import */ var _Executors_LadderExecutor__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../Executors/LadderExecutor */ \"./src/modules/Executors/LadderExecutor.ts\");\n\r\n\r\nvar InfiniteLoader = /** @class */ (function () {\r\n function InfiniteLoader(run, perStep) {\r\n if (perStep === void 0) { perStep = 20; }\r\n var _this = this;\r\n this.items = [];\r\n this.isFinished = false;\r\n this.perStep = 20; // number of items to load\r\n this.pointer = 0;\r\n this.isFresh = true;\r\n this.executor = new _Executors_LadderExecutor__WEBPACK_IMPORTED_MODULE_1__[\"default\"](function (refresh) {\r\n if (refresh === void 0) { refresh = false; }\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__awaiter\"](_this, void 0, void 0, function () { return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__generator\"](this, function (_a) {\r\n switch (_a.label) {\r\n case 0: return [4 /*yield*/, this.runRequest(run, refresh)];\r\n case 1: return [2 /*return*/, _a.sent()];\r\n }\r\n }); });\r\n });\r\n this.perStep = perStep;\r\n }\r\n Object.defineProperty(InfiniteLoader.prototype, \"isRunning\", {\r\n /**\r\n * Command from this executor is currently running.\r\n */\r\n get: function () {\r\n return this.executor.isRunning;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n Object.defineProperty(InfiniteLoader.prototype, \"isEmpty\", {\r\n /**\r\n * We tried to load, but list is empty\r\n */\r\n get: function () {\r\n if (this.executor.isRunning) {\r\n return false;\r\n }\r\n return this.isFinished && (this.items.length === 0);\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n Object.defineProperty(InfiniteLoader.prototype, \"isFull\", {\r\n /**\r\n * We tried to load, and list is not empty\r\n */\r\n get: function () {\r\n return !!this.items.length;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n Object.defineProperty(InfiniteLoader.prototype, \"isRefreshing\", {\r\n /**\r\n * Is loading anew. This could happen in two cases.\r\n * 1) Executor is running for the first time. Meaning the list is empty.\r\n * 2) Executor is refreshing. Meaning the list is not empty.\r\n */\r\n get: function () {\r\n return this.pointer === 0 && this.isRunning;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n /**\r\n * Loads a bunch of items\r\n */\r\n InfiniteLoader.prototype.next = function () {\r\n if (this.isRunning || this.isFinished) {\r\n return;\r\n }\r\n this.isFresh = false;\r\n this.executor.run();\r\n };\r\n InfiniteLoader.prototype.runRequest = function (pointerRequest, refresh) {\r\n if (refresh === void 0) { refresh = false; }\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__awaiter\"](this, void 0, void 0, function () {\r\n var pointer, result, exception_1;\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__generator\"](this, function (_a) {\r\n switch (_a.label) {\r\n case 0:\r\n console.log('SP', this.pointer);\r\n _a.label = 1;\r\n case 1:\r\n _a.trys.push([1, 3, , 4]);\r\n pointer = refresh ? this.pointer : 0;\r\n return [4 /*yield*/, pointerRequest(pointer, this.perStep)];\r\n case 2:\r\n result = _a.sent();\r\n if (!Array.isArray(result)) {\r\n console.warn('InfiniteLoader function must return array');\r\n return [2 /*return*/];\r\n }\r\n if (result.length < this.perStep) {\r\n this.isFinished = true;\r\n }\r\n if (refresh) {\r\n this.isFresh = true;\r\n this.isFinished = false;\r\n }\r\n else {\r\n this.pointer = pointer + this.perStep;\r\n }\r\n console.log('EP', this.pointer);\r\n this.applyNew(result);\r\n return [3 /*break*/, 4];\r\n case 3:\r\n exception_1 = _a.sent();\r\n this.isFinished = true;\r\n throw exception_1;\r\n case 4: return [2 /*return*/];\r\n }\r\n });\r\n });\r\n };\r\n /**\r\n * Refresh the list.\r\n * This will replace current items with new ones after loading. Not clear the list immediately.\r\n */\r\n InfiniteLoader.prototype.refresh = function () {\r\n this.pointer = 0;\r\n this.isFresh = true;\r\n this.isFinished = false;\r\n this.executor.run(true);\r\n };\r\n InfiniteLoader.prototype.applyNew = function (items) {\r\n if (this.isFresh) {\r\n this.items = items;\r\n return;\r\n }\r\n this.items = this.items.concat(items);\r\n };\r\n return InfiniteLoader;\r\n}());\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (InfiniteLoader);\r\n\n\n//# sourceURL=webpack:///./src/modules/Loaders/InfiniteLoader.ts?"); | ||
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! tslib */ \"./node_modules/tslib/tslib.es6.js\");\n/* harmony import */ var _Executors_LadderExecutor__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../Executors/LadderExecutor */ \"./src/modules/Executors/LadderExecutor.ts\");\n\r\n\r\nvar InfiniteLoader = /** @class */ (function () {\r\n // Is currently running from 0 pointer\r\n function InfiniteLoader(run, perStep) {\r\n if (perStep === void 0) { perStep = 20; }\r\n var _this = this;\r\n this.items = [];\r\n this.isFinished = false;\r\n this.isRefreshing = false;\r\n this.perStep = 20; // number of items to load\r\n this.pointer = 0;\r\n // Means no runs were executed\r\n this.isFresh = true;\r\n this.executor = new _Executors_LadderExecutor__WEBPACK_IMPORTED_MODULE_1__[\"default\"](function (refresh) {\r\n if (refresh === void 0) { refresh = false; }\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__awaiter\"](_this, void 0, void 0, function () { return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__generator\"](this, function (_a) {\r\n switch (_a.label) {\r\n case 0: return [4 /*yield*/, this.runRequest(run, refresh)];\r\n case 1: return [2 /*return*/, _a.sent()];\r\n }\r\n }); });\r\n });\r\n this.perStep = perStep;\r\n }\r\n Object.defineProperty(InfiniteLoader.prototype, \"isRunning\", {\r\n /**\r\n * Command from this executor is currently running.\r\n */\r\n get: function () {\r\n return this.executor.isRunning;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n Object.defineProperty(InfiniteLoader.prototype, \"isEmpty\", {\r\n /**\r\n * We tried to load, but list is empty\r\n * Is false when executor is loading or is fresh.\r\n */\r\n get: function () {\r\n if (this.executor.isRunning) {\r\n return false;\r\n }\r\n return this.isFinished && (this.items.length === 0);\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n Object.defineProperty(InfiniteLoader.prototype, \"isFull\", {\r\n /**\r\n * We tried to load, and list is not empty\r\n * Is false when executor is loading or is fresh.\r\n */\r\n get: function () {\r\n if (this.isFresh) {\r\n return false;\r\n }\r\n if (this.executor.isRunning) {\r\n return false;\r\n }\r\n return !!this.items.length;\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n /**\r\n * Loads a bunch of items\r\n */\r\n InfiniteLoader.prototype.next = function () {\r\n this.executor.run();\r\n };\r\n /**\r\n * Refresh the list.\r\n */\r\n InfiniteLoader.prototype.refresh = function () {\r\n this.executor.run(true);\r\n };\r\n InfiniteLoader.prototype.runRequest = function (pointerRequest, refresh) {\r\n if (refresh === void 0) { refresh = false; }\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__awaiter\"](this, void 0, void 0, function () {\r\n var result, exception_1;\r\n return tslib__WEBPACK_IMPORTED_MODULE_0__[\"__generator\"](this, function (_a) {\r\n switch (_a.label) {\r\n case 0:\r\n if (this.pointer === 0) {\r\n refresh = true;\r\n }\r\n if (refresh) {\r\n this.pointer = 0;\r\n this.isFresh = true;\r\n this.isRefreshing = true;\r\n this.isFinished = false;\r\n }\r\n _a.label = 1;\r\n case 1:\r\n _a.trys.push([1, 3, 4, 5]);\r\n return [4 /*yield*/, pointerRequest(this.pointer, this.perStep)];\r\n case 2:\r\n result = _a.sent();\r\n if (!Array.isArray(result)) {\r\n console.warn('InfiniteLoader function must return array');\r\n return [2 /*return*/];\r\n }\r\n if (result.length < this.perStep) {\r\n this.isFinished = true;\r\n }\r\n this.applyNew(result);\r\n this.pointer = this.pointer + this.perStep;\r\n this.isFresh = false;\r\n return [3 /*break*/, 5];\r\n case 3:\r\n exception_1 = _a.sent();\r\n this.isFinished = true;\r\n throw exception_1;\r\n case 4:\r\n this.isRefreshing = false;\r\n return [7 /*endfinally*/];\r\n case 5: return [2 /*return*/];\r\n }\r\n });\r\n });\r\n };\r\n InfiniteLoader.prototype.applyNew = function (items) {\r\n if (this.isFresh) {\r\n this.items = items;\r\n return;\r\n }\r\n this.items = this.items.concat(items);\r\n };\r\n return InfiniteLoader;\r\n}());\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (InfiniteLoader);\r\n\n\n//# sourceURL=webpack:///./src/modules/Loaders/InfiniteLoader.ts?"); | ||
@@ -179,0 +179,0 @@ /***/ }), |
@@ -6,2 +6,3 @@ import LadderExecutor from '../Executors/LadderExecutor'; | ||
isFinished: boolean; | ||
isRefreshing: boolean; | ||
protected readonly executor: LadderExecutor; | ||
@@ -18,2 +19,3 @@ protected readonly perStep: number; | ||
* We tried to load, but list is empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
@@ -23,21 +25,15 @@ readonly isEmpty: boolean; | ||
* We tried to load, and list is not empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
readonly isFull: boolean; | ||
/** | ||
* Is loading anew. This could happen in two cases. | ||
* 1) Executor is running for the first time. Meaning the list is empty. | ||
* 2) Executor is refreshing. Meaning the list is not empty. | ||
*/ | ||
readonly isRefreshing: boolean; | ||
/** | ||
* Loads a bunch of items | ||
*/ | ||
next(): void; | ||
protected runRequest(pointerRequest: PointerRequest, refresh?: boolean): Promise<void>; | ||
/** | ||
* Refresh the list. | ||
* This will replace current items with new ones after loading. Not clear the list immediately. | ||
*/ | ||
refresh(): void; | ||
protected runRequest(pointerRequest: PointerRequest, refresh?: boolean): Promise<void>; | ||
protected applyNew(items: any[]): void; | ||
} |
{ | ||
"name": "asva-executors", | ||
"version": "0.1.14-beta.3", | ||
"version": "0.1.14-beta.4", | ||
"description": "Helper classes for your async flow control", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
@@ -8,2 +8,3 @@ import LadderExecutor from '../Executors/LadderExecutor' | ||
public isFinished: boolean = false | ||
public isRefreshing: boolean = false | ||
@@ -13,3 +14,5 @@ protected readonly executor: LadderExecutor | ||
protected pointer: number = 0 | ||
// Means no runs were executed | ||
protected isFresh: boolean = true | ||
// Is currently running from 0 pointer | ||
@@ -30,2 +33,3 @@ constructor (run: PointerRequest, perStep: number = 20) { | ||
* We tried to load, but list is empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
@@ -41,4 +45,11 @@ get isEmpty (): boolean { | ||
* We tried to load, and list is not empty | ||
* Is false when executor is loading or is fresh. | ||
*/ | ||
get isFull (): boolean { | ||
if (this.isFresh) { | ||
return false | ||
} | ||
if (this.executor.isRunning) { | ||
return false | ||
} | ||
return !!this.items.length | ||
@@ -48,27 +59,27 @@ } | ||
/** | ||
* Is loading anew. This could happen in two cases. | ||
* 1) Executor is running for the first time. Meaning the list is empty. | ||
* 2) Executor is refreshing. Meaning the list is not empty. | ||
* Loads a bunch of items | ||
*/ | ||
get isRefreshing (): boolean { | ||
return this.pointer === 0 && this.isRunning | ||
public next (): void { | ||
this.executor.run() | ||
} | ||
/** | ||
* Loads a bunch of items | ||
* Refresh the list. | ||
*/ | ||
public next (): void { | ||
if (this.isRunning || this.isFinished) { | ||
return | ||
} | ||
this.isFresh = false | ||
this.executor.run() | ||
public refresh (): void { | ||
this.executor.run(true) | ||
} | ||
protected async runRequest (pointerRequest: PointerRequest, refresh = false): Promise<void> { | ||
console.log('SP', this.pointer) | ||
if (this.pointer === 0) { | ||
refresh = true | ||
} | ||
if (refresh) { | ||
this.pointer = 0 | ||
this.isFresh = true | ||
this.isRefreshing = true | ||
this.isFinished = false | ||
} | ||
try { | ||
const pointer = refresh ? this.pointer : 0 | ||
const result = await pointerRequest(pointer, this.perStep) | ||
const result = await pointerRequest(this.pointer, this.perStep) | ||
@@ -82,27 +93,14 @@ if (!Array.isArray(result)) { | ||
} | ||
if ( refresh ) { | ||
this.isFresh = true | ||
this.isFinished = false | ||
} else { | ||
this.pointer = pointer + this.perStep | ||
} | ||
console.log('EP', this.pointer) | ||
this.applyNew(result) | ||
this.pointer = this.pointer + this.perStep | ||
this.isFresh = false | ||
} catch (exception) { | ||
this.isFinished = true | ||
throw exception | ||
} finally { | ||
this.isRefreshing = false | ||
} | ||
} | ||
/** | ||
* Refresh the list. | ||
* This will replace current items with new ones after loading. Not clear the list immediately. | ||
*/ | ||
public refresh (): void { | ||
this.pointer = 0 | ||
this.isFresh = true | ||
this.isFinished = false | ||
this.executor.run(true) | ||
} | ||
protected applyNew (items: any[]): void { | ||
@@ -109,0 +107,0 @@ if (this.isFresh) { |
100260
-0.81%1564
-0.7%