-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
abortabledebounce.ts
35 lines (28 loc) · 985 Bytes
/
abortabledebounce.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
31
32
33
34
35
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module utils/abortabledebounce
*/
/**
* Returns a function wrapper that will execute the provided function and abort any previous call that is still in progress.
*
* @param func The function to be called. It will be provided with `AbortSignal` as the first parameter.
*/
export default function abortableDebounce<Args extends Array<any>, Ret>(
func: ( signal: AbortSignal, ...args: Args ) => Ret
): AbortableFunc<Args, Ret> {
let controller = new AbortController();
function abortable( ...args: Args ) {
controller.abort();
controller = new AbortController();
return func( controller.signal, ...args );
}
abortable.abort = () => controller.abort();
return abortable;
}
export interface AbortableFunc<Args extends Array<any>, Ret> {
( ...args: Args ): Ret;
abort(): void;
}