-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebounce.ts
30 lines (28 loc) · 836 Bytes
/
Debounce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export type CallbackFunction = (...args: any[]) => void;
export type MethodDecorators = (
target: object,
propertyName: string,
propertyDescriptor: PropertyDescriptor
) => PropertyDescriptor;
/**
* 异步函数防抖
* @exports
* @return {MethodDecorators}
*/
export function debounce(): MethodDecorators {
return (target: object, propertyName: string, propertyDescriptor: PropertyDescriptor): PropertyDescriptor => {
const method: (...args: any[]) => Promise<any> = propertyDescriptor.value;
let running: boolean = false;
propertyDescriptor.value = async function (...args: any[]): Promise<any> {
if (!running) {
running = true;
try {
await method.apply(this, args);
} finally {
running = false;
}
}
};
return propertyDescriptor;
};
}