diff --git a/src/decorators.ts b/src/decorators.ts index 747c1c4..595d1a1 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -21,7 +21,7 @@ import { done } from './util'; export function memoize( target: any, key: string, - descriptor: PropertyDescriptor + descriptor: TypedPropertyDescriptor ) { const memoizeKey = `$memoize$${key}`; const fn = descriptor.get!; @@ -73,7 +73,7 @@ export function memoize( // return trigger; // } -export function throttle( +export function throttle( target: any, key: string, descriptor: PropertyDescriptor @@ -83,9 +83,9 @@ export function throttle( const fn = descriptor.value!; descriptor.value = function ( - this: Record | undefined>, - ...args: any - ): Promise { + this: Record | undefined>, + ...args: Args + ): Promise { if (this[nextKey]) { return this[nextKey]!; } @@ -109,7 +109,7 @@ export function throttle( } // Make sure asynchronous functions are called one after another. -// type ThisPromise = Record>; +type ThisPromise = Record>; // export function sequentialize( // target: (this: ThisPromise, ...args: Args) => Promise, @@ -126,15 +126,20 @@ export function throttle( // }; // } -export function sequentialize( +export function sequentialize( target: any, key: string, - descriptor: PropertyDescriptor + descriptor: TypedPropertyDescriptor< + (this: ThisPromise, ...args: Args) => Promise + > ) { const currentKey = `$s11e$${key}`; // sequentialize const fn = descriptor.value!; - descriptor.value = function (this: any, ...args: any): Promise { + descriptor.value = function ( + this: ThisPromise, + ...args: Args + ): Promise { const currentPromise = (this[currentKey] as Promise) || Promise.resolve(null); const run = async () => await fn.apply(this, args); @@ -143,7 +148,7 @@ export function sequentialize( }; } -// type ThisTimer = Record>; +type ThisTimer = Record>; // export function debounce(delay: number) { // return function ( @@ -159,11 +164,17 @@ export function sequentialize( // }; // } export function debounce(delay: number) { - return function (target: any, key: string, descriptor: PropertyDescriptor) { + return function ( + target: any, + key: string, + descriptor: TypedPropertyDescriptor< + (this: ThisTimer, ...args: []) => void + > + ) { const timerKey = `$d6e$${key}`; // debounce const fn = descriptor.value!; - descriptor.value = function (this: any, ...args: any): void { + descriptor.value = function (this: ThisTimer, ...args: []): void { clearTimeout(this[timerKey]); this[timerKey] = setTimeout(() => fn.apply(this, args), delay); };