Skip to content

Commit

Permalink
refactor: update decorators types
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed May 11, 2024
1 parent 294eef4 commit 8731609
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { done } from './util';
export function memoize<Return>(
target: any,
key: string,
descriptor: PropertyDescriptor
descriptor: TypedPropertyDescriptor<Return>
) {
const memoizeKey = `$memoize$${key}`;
const fn = descriptor.get!;
Expand Down Expand Up @@ -73,7 +73,7 @@ export function memoize<Return>(

// return trigger;
// }
export function throttle(
export function throttle<Args extends any[], T>(
target: any,
key: string,
descriptor: PropertyDescriptor
Expand All @@ -83,9 +83,9 @@ export function throttle(
const fn = descriptor.value!;

descriptor.value = function (
this: Record<string, Promise<any> | undefined>,
...args: any
): Promise<any> {
this: Record<string, Promise<T> | undefined>,
...args: Args
): Promise<T> {
if (this[nextKey]) {
return this[nextKey]!;
}
Expand All @@ -109,7 +109,7 @@ export function throttle(
}

// Make sure asynchronous functions are called one after another.
// type ThisPromise = Record<string, Promise<any>>;
type ThisPromise = Record<string, Promise<any>>;

// export function sequentialize<Args extends any[]>(
// target: (this: ThisPromise, ...args: Args) => Promise<any>,
Expand All @@ -126,15 +126,20 @@ export function throttle(
// };
// }

export function sequentialize(
export function sequentialize<Args extends any[]>(
target: any,
key: string,
descriptor: PropertyDescriptor
descriptor: TypedPropertyDescriptor<
(this: ThisPromise, ...args: Args) => Promise<any>
>
) {
const currentKey = `$s11e$${key}`; // sequentialize
const fn = descriptor.value!;

descriptor.value = function (this: any, ...args: any): Promise<any> {
descriptor.value = function (
this: ThisPromise,
...args: Args
): Promise<any> {
const currentPromise =
(this[currentKey] as Promise<any>) || Promise.resolve(null);
const run = async () => await fn.apply(this, args);
Expand All @@ -143,7 +148,7 @@ export function sequentialize(
};
}

// type ThisTimer = Record<string, ReturnType<typeof setTimeout>>;
type ThisTimer = Record<string, ReturnType<typeof setTimeout>>;

// export function debounce(delay: number) {
// return function <Args extends any[]>(
Expand All @@ -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);
};
Expand Down

0 comments on commit 8731609

Please sign in to comment.