-
Notifications
You must be signed in to change notification settings - Fork 7
/
debounce.ts
218 lines (200 loc) · 6.27 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Debounces function calls for frequent access.
* @module
*/
import { AsyncTask, asyncTask } from "./async.ts";
type Debounce = {
for: any;
tasks: AsyncTask<any>[];
data: any;
timer: NodeJS.Timeout | number | undefined;
};
const registry = new Map<any, Debounce>();
/**
* Options for the {@link debounce} function.
*/
export interface DebounceOptions {
/**
* The delay duration (in milliseconds) to wait before invoking the
* handler function.
*/
delay: number,
/**
* Use the debounce strategy `for` the given key, this will keep the
* debounce context in a global registry, binding new `handler` function
* for the same key will override the previous settings. This mechanism
* guarantees that both creating the debounced function in function
* scopes and overwriting the handler are possible.
*/
for?: any;
/**
* Cancel the pending task if the signal is aborted.
*/
signal?: AbortSignal;
}
/**
* Creates a debounced function that delays invoking `handler` until after
* `delay` duration (in milliseconds) have elapsed since the last time the
* debounced function was invoked.
*
* If a subsequent call happens within the `delay` duration (in milliseconds),
* the previous call will be canceled and it will result in the same return
* value as the new call's.
*
* Optionally, we can provide a `reducer` function to merge data before
* processing so multiple calls can be merged into one.
*
* @example
* ```ts
* import debounce from "@ayonli/jsext/debounce";
* import { sleep } from "@ayonli/jsext/async";
*
* let count = 0;
*
* const fn = debounce((obj: { foo?: string; bar?: string }) => {
* count++;
* return obj;
* }, 1_000);
*
* const [res1, res2] = await Promise.all([
* fn({ foo: "hello", bar: "world" }),
* sleep(100).then(() => fn({ foo: "hi" })),
* ]);
*
* console.log(res1); // { foo: "hi" }
* console.log(res2); // { foo: "hi" }
* console.log(count); // 1
* ```
*
* @example
* ```ts
* // with reducer
* import debounce from "@ayonli/jsext/debounce";
*
* const fn = debounce((obj: { foo?: string; bar?: string }) => {
* return obj;
* }, 1_000, (prev, curr) => {
* return { ...prev, ...curr };
* });
*
* const [res1, res2] = await Promise.all([
* fn({ foo: "hello", bar: "world" }),
* fn({ foo: "hi" }),
* ]);
*
* console.log(res1); // { foo: "hi", bar: "world" }
* console.assert(res2 === res1);
* ```
*/
export default function debounce<I, T, R>(
handler: (this: I, data: T) => R | Promise<R>,
delay: number,
reducer?: (prev: T, current: T) => T
): (this: I, data: T) => Promise<R>;
/**
* @example
* ```ts
* import debounce from "@ayonli/jsext/debounce";
*
* const key = "unique_key"
* let count = 0;
*
* const [res1, res2] = await Promise.all([
* debounce(async (obj: { foo?: string; bar?: string }) => {
* count += 1;
* return await Promise.resolve(obj);
* }, { delay: 5, for: "foo" }, (prev, data) => {
* return { ...prev, ...data };
* })({ foo: "hello", bar: "world" }),
*
* debounce(async (obj: { foo?: string; bar?: string }) => {
* count += 2;
* return await Promise.resolve(obj);
* }, { delay: 5, for: "foo" }, (prev, data) => {
* return { ...prev, ...data };
* })({ foo: "hi" }),
* ]);
*
* console.log(res1); // { foo: "hi", bar: "world" }
* console.assert(res1 === res2);
* console.assert(count === 2);
* ```
*/
export default function debounce<I, T, R>(
handler: (this: I, data: T) => R | Promise<R>,
options: DebounceOptions,
reducer?: (prev: T, current: T) => T
): (this: I, data: T) => Promise<R>;
export default function debounce<I, T = any, R = any>(
handler: (this: I, data: T) => R | Promise<R>,
options: number | DebounceOptions,
reducer: ((prev: T, current: T) => T) | undefined = undefined,
): (this: I, data: T) => Promise<R> {
const signal = typeof options === "number" ? undefined : options.signal;
const delay = typeof options === "number" ? options : options.delay;
const key = typeof options === "number" ? null : options.for;
const hasKey = key !== null && key !== undefined && key !== "";
let _cache = hasKey ? registry.get(key) : undefined;
if (!_cache) {
_cache = {
for: key,
tasks: [],
data: undefined,
timer: undefined,
};
if (hasKey) {
registry.set(key, _cache);
}
}
const cache = _cache;
const getResolvers = (cache: Debounce) => {
// Move tasks and cached data to new variables, so during the middle
// of handler running, new calls won't interfere the running process.
const _tasks = cache.tasks;
const data = cache.data as T;
cache.tasks = [];
cache.data = undefined;
if (hasKey) {
registry.delete(key);
}
const resolve = (result: R) => {
_tasks.forEach(({ resolve }) => resolve(result));
};
const reject = (err: unknown) => {
_tasks.forEach(({ reject }) => reject(err));
};
return { resolve, reject, data };
};
signal?.addEventListener("abort", () => {
cache.timer && clearTimeout(cache.timer);
const { reject } = getResolvers(cache);
reject(signal.reason);
});
return async function (this: I, data: T) {
if (signal?.aborted) {
throw signal.reason;
}
if (typeof reducer === "function" && cache.data !== undefined) {
cache.data = reducer(cache.data, data);
} else {
cache.data = data;
}
cache.timer && clearTimeout(cache.timer);
cache.timer = setTimeout((cache) => {
const { resolve, reject, data } = getResolvers(cache);
try {
const res = handler.call(this, data) as any;
if (typeof res?.then === "function") {
Promise.resolve(res).then(resolve, reject);
} else {
resolve(res);
}
} catch (err) {
reject(err);
}
}, delay, cache);
const task = asyncTask<R>();
cache.tasks.push(task);
return await task;
};
}