@taraflex/debounce
Advanced tools
+2
-2
| export interface IDebounce<R> extends Function { | ||
| (...args: any[]): R; | ||
| (...args: any[]): void; | ||
| timeout: number; | ||
| stop(): any; | ||
| run(): any; | ||
| run(...args: any[]): R; | ||
| } | ||
@@ -7,0 +7,0 @@ export declare function debounce<R>(f: (...args: any[]) => R, timeout: number, runFirst?: boolean): IDebounce<R>; |
+44
-43
@@ -66,3 +66,3 @@ 'use strict'; | ||
| try { | ||
| return f.apply(this, arguments); | ||
| f.apply(this, arguments); | ||
| } | ||
@@ -107,2 +107,41 @@ finally { | ||
| exports.delay = delay; | ||
| function trottleHelper(self, onReject) { | ||
| return __awaiter(this, void 0, void 0, function () { | ||
| var lastCallArgs, e_1, _a; | ||
| return __generator(this, function (_b) { | ||
| switch (_b.label) { | ||
| case 0: | ||
| if (!self.lastCallArgs) return [3, 8]; | ||
| self.started = true; | ||
| lastCallArgs = self.lastCallArgs; | ||
| self.lastCallArgs = null; | ||
| _b.label = 1; | ||
| case 1: | ||
| _b.trys.push([1, 3, , 6]); | ||
| return [4, self.f.apply(this, lastCallArgs)]; | ||
| case 2: | ||
| _b.sent(); | ||
| return [3, 6]; | ||
| case 3: | ||
| e_1 = _b.sent(); | ||
| _a = onReject; | ||
| if (!_a) return [3, 5]; | ||
| return [4, onReject(e_1)]; | ||
| case 4: | ||
| _a = (_b.sent()); | ||
| _b.label = 5; | ||
| case 5: | ||
| _a; | ||
| return [3, 6]; | ||
| case 6: return [4, delay(self.timeout)]; | ||
| case 7: | ||
| _b.sent(); | ||
| return [3, 0]; | ||
| case 8: | ||
| self.started = false; | ||
| return [2]; | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| var AsyncTrottle = (function (_super) { | ||
@@ -116,44 +155,6 @@ __extends(AsyncTrottle, _super); | ||
| } | ||
| return __awaiter(this, void 0, void 0, function () { | ||
| var lastCallArgs, e_1, _a; | ||
| return __generator(this, function (_b) { | ||
| switch (_b.label) { | ||
| case 0: | ||
| self.lastCallArgs = rest; | ||
| if (!!self.hasStarted) return [3, 10]; | ||
| _b.label = 1; | ||
| case 1: | ||
| if (!self.lastCallArgs) return [3, 9]; | ||
| self.hasStarted = true; | ||
| lastCallArgs = self.lastCallArgs; | ||
| self.lastCallArgs = null; | ||
| _b.label = 2; | ||
| case 2: | ||
| _b.trys.push([2, 4, , 7]); | ||
| return [4, f.apply(this, lastCallArgs)]; | ||
| case 3: | ||
| _b.sent(); | ||
| return [3, 7]; | ||
| case 4: | ||
| e_1 = _b.sent(); | ||
| _a = onReject; | ||
| if (!_a) return [3, 6]; | ||
| return [4, onReject(e_1)]; | ||
| case 5: | ||
| _a = (_b.sent()); | ||
| _b.label = 6; | ||
| case 6: | ||
| _a; | ||
| return [3, 7]; | ||
| case 7: return [4, delay(self.timeout)]; | ||
| case 8: | ||
| _b.sent(); | ||
| return [3, 1]; | ||
| case 9: | ||
| self.hasStarted = false; | ||
| _b.label = 10; | ||
| case 10: return [2]; | ||
| } | ||
| }); | ||
| }); | ||
| self.lastCallArgs = rest; | ||
| if (!self.started) { | ||
| trottleHelper(self, onReject); | ||
| } | ||
| }) || this; | ||
@@ -160,0 +161,0 @@ var self = _this; |
+20
-16
@@ -53,3 +53,3 @@ 'use strict'; | ||
| export interface IDebounce<R> extends Function { | ||
| (...args); | ||
| (...args): void; | ||
| timeout: number; | ||
@@ -68,2 +68,17 @@ stop(); | ||
| async function trottleHelper(self: AsyncTrottle, onReject?: Function) { | ||
| while (self.lastCallArgs) { | ||
| self.started = true; | ||
| const { lastCallArgs } = self; | ||
| self.lastCallArgs = null; | ||
| try { | ||
| await self.f.apply(this, lastCallArgs); | ||
| } catch (e) { | ||
| onReject && await onReject(e); | ||
| } | ||
| await delay(self.timeout); | ||
| } | ||
| self.started = false; | ||
| } | ||
| // @ts-ignore | ||
@@ -74,19 +89,8 @@ class AsyncTrottle extends ExtensibleFunction { | ||
| lastCallArgs: any; | ||
| hasStarted: boolean; | ||
| started: boolean; | ||
| constructor(f: Function, timeout: number, onReject?: Function) { | ||
| super(async function (...rest) { | ||
| super(function (...rest) { | ||
| self.lastCallArgs = rest; | ||
| if (!self.hasStarted) { | ||
| while (self.lastCallArgs) { | ||
| self.hasStarted = true; | ||
| const { lastCallArgs } = self; | ||
| self.lastCallArgs = null; | ||
| try { | ||
| await f.apply(this, lastCallArgs); | ||
| } catch (e) { | ||
| onReject && await onReject(e); | ||
| } | ||
| await delay(self.timeout); | ||
| } | ||
| self.hasStarted = false; | ||
| if (!self.started) { | ||
| trottleHelper(self, onReject); | ||
| } | ||
@@ -93,0 +97,0 @@ }); |
+5
-5
| { | ||
| "name": "@taraflex/debounce", | ||
| "version": "0.1.2", | ||
| "version": "0.1.3", | ||
| "description": "", | ||
| "license": "ISC", | ||
| "author": "taraflex", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "build": "tsc -p tsconfig.json", | ||
| "publish-library": "npm publish --registry=https://registry.npmjs.org/ --access=public" | ||
| "publish-library": "tsc -p tsconfig.json && npm publish --registry=https://registry.npmjs.org/ --access=public", | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "taraflex", | ||
| "license": "ISC", | ||
| "devDependencies": {} | ||
| } |
298
1.36%11317
-3.06%