Skip to content

Commit a204908

Browse files
jasnelladuh95
authored andcommitted
util: share helpers between debounce() and throttle()
util.debounce() and util.throttle() duplicated the construction of the AbortError used for cancel()/abort, and the property definitions for the returned function's control methods, state getters, length, and name. Move both into internal/util/wrapped_function. Assisted-by: Claude Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #67495 Reviewed-By: Guilherme Araújo <arauujogui@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent d605804 commit a204908

3 files changed

Lines changed: 104 additions & 95 deletions

File tree

‎lib/internal/util/debounce.js‎

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayPrototypePush,
5-
ObjectDefineProperties,
65
PromiseReject,
76
PromiseWithResolvers,
87
ReflectApply,
@@ -20,6 +19,10 @@ const {
2019
validateObject,
2120
} = require('internal/validators');
2221
const { addAbortListener } = require('internal/events/abort_listener');
22+
const {
23+
createAbortError,
24+
defineWrapperProperties,
25+
} = require('internal/util/wrapped_function');
2326
const { kEmptyObject } = require('internal/util');
2427
const { TIMEOUT_MAX } = require('internal/timers');
2528
const { clearTimeout, setTimeout } = require('timers');
@@ -85,17 +88,12 @@ function debounce(fn, wait, options = kEmptyObject) {
8588

8689
function cancel(reason) {
8790
if (timeout === undefined) return;
88-
const error = reason === undefined ?
89-
new AbortError() :
90-
new AbortError(undefined, { __proto__: null, cause: reason });
91-
cancelWithError(error);
91+
cancelWithError(createAbortError(reason));
9292
}
9393

9494
function abort() {
9595
if (abortError === undefined) {
96-
abortError = signal.reason === undefined ?
97-
new AbortError() :
98-
new AbortError(undefined, { __proto__: null, cause: signal.reason });
96+
abortError = createAbortError(signal.reason);
9997
}
10098
cancelWithError(abortError);
10199
}
@@ -206,44 +204,18 @@ function debounce(fn, wait, options = kEmptyObject) {
206204
return call.promise;
207205
}
208206

209-
function createFn(value) {
210-
return {
211-
__proto__: null,
212-
configurable: true,
213-
enumerable: true,
214-
writable: true,
215-
value,
216-
};
217-
}
218-
219-
ObjectDefineProperties(debounced, {
207+
defineWrapperProperties(debounced, fn, {
220208
__proto__: null,
221-
cancel: createFn(cancel),
222-
flush: createFn(flush),
223-
ref: createFn(ref),
224-
unref: createFn(unref),
225-
pending: {
226-
__proto__: null,
227-
enumerable: false,
228-
get() {
229-
return pendingCalls.length === 0 ? null : pendingCalls[pendingCalls.length - 1].promise;
230-
},
231-
},
232-
pendingCount: {
233-
__proto__: null,
234-
enumerable: false,
235-
get() { return pendingCalls.length; },
236-
},
237-
length: {
238-
__proto__: null,
239-
configurable: true,
240-
value: fn.length,
241-
},
242-
name: {
243-
__proto__: null,
244-
configurable: true,
245-
value: fn.name,
209+
cancel,
210+
flush,
211+
ref,
212+
unref,
213+
}, {
214+
__proto__: null,
215+
pending() {
216+
return pendingCalls.length === 0 ? null : pendingCalls[pendingCalls.length - 1].promise;
246217
},
218+
pendingCount() { return pendingCalls.length; },
247219
});
248220

249221
return debounced;

‎lib/internal/util/throttle.js‎

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const {
44
ArrayPrototypePush,
55
ArrayPrototypeSlice,
6-
ObjectDefineProperties,
76
PromisePrototypeThen,
87
PromiseReject,
98
PromiseResolve,
@@ -27,6 +26,10 @@ const {
2726
validateOneOf,
2827
} = require('internal/validators');
2928
const { addAbortListener } = require('internal/events/abort_listener');
29+
const {
30+
createAbortError,
31+
defineWrapperProperties,
32+
} = require('internal/util/wrapped_function');
3033
const { kEmptyObject } = require('internal/util');
3134
const { TIMEOUT_MAX } = require('internal/timers');
3235
const { clearTimeout, setTimeout } = require('timers');
@@ -139,17 +142,12 @@ function throttle(fn, limit, interval, options = kEmptyObject) {
139142
}
140143

141144
function cancel(reason) {
142-
const error = reason === undefined ?
143-
new AbortError() :
144-
new AbortError(undefined, { __proto__: null, cause: reason });
145-
cancelWithError(error);
145+
cancelWithError(createAbortError(reason));
146146
}
147147

148148
function abort() {
149149
if (abortError === undefined) {
150-
abortError = signal.reason === undefined ?
151-
new AbortError() :
152-
new AbortError(undefined, { __proto__: null, cause: signal.reason });
150+
abortError = createAbortError(signal.reason);
153151
}
154152
cancelWithError(abortError);
155153
}
@@ -330,51 +328,21 @@ function throttle(fn, limit, interval, options = kEmptyObject) {
330328
return call.promise;
331329
}
332330

333-
function createFn(value) {
334-
return {
335-
__proto__: null,
336-
configurable: true,
337-
enumerable: true,
338-
writable: true,
339-
value,
340-
};
341-
}
342-
343-
ObjectDefineProperties(throttled, {
331+
defineWrapperProperties(throttled, fn, {
344332
__proto__: null,
345-
cancel: createFn(cancel),
346-
hasImmediateCapacity: createFn(hasImmediateCapacity),
347-
ref: createFn(ref),
348-
unref: createFn(unref),
349-
pending: {
350-
__proto__: null,
351-
enumerable: false,
352-
get() {
353-
return pendingCount() === 0 ? null :
354-
pendingCalls[pendingCalls.length - 1].promise;
355-
},
356-
},
357-
pendingCount: {
358-
__proto__: null,
359-
enumerable: false,
360-
get: pendingCount,
361-
},
362-
activeCount: {
363-
__proto__: null,
364-
enumerable: false,
365-
get() {
366-
return activeCount;
367-
},
368-
},
369-
length: {
370-
__proto__: null,
371-
configurable: true,
372-
value: fn.length,
333+
cancel,
334+
hasImmediateCapacity,
335+
ref,
336+
unref,
337+
}, {
338+
__proto__: null,
339+
pending() {
340+
return pendingCount() === 0 ? null :
341+
pendingCalls[pendingCalls.length - 1].promise;
373342
},
374-
name: {
375-
__proto__: null,
376-
configurable: true,
377-
value: fn.name,
343+
pendingCount,
344+
activeCount() {
345+
return activeCount;
378346
},
379347
});
380348

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
// Helpers shared by util.debounce() and util.throttle().
4+
5+
const {
6+
ObjectDefineProperties,
7+
} = primordials;
8+
9+
const {
10+
AbortError,
11+
} = require('internal/errors');
12+
13+
/**
14+
* Creates the AbortError used to reject calls that were cancelled or aborted.
15+
* @param {any} reason
16+
* @returns {AbortError}
17+
*/
18+
function createAbortError(reason) {
19+
return reason === undefined ?
20+
new AbortError() :
21+
new AbortError(undefined, { __proto__: null, cause: reason });
22+
}
23+
24+
/**
25+
* Defines the control methods, state getters, `length`, and `name` on a
26+
* function returned by util.debounce() or util.throttle().
27+
* @param {Function} wrapper The function returned to the user.
28+
* @param {Function} fn The wrapped user function.
29+
* @param {{ [name: string]: Function }} methods Enumerable, writable methods.
30+
* @param {{ [name: string]: Function }} getters Non-enumerable getters.
31+
* @returns {Function} `wrapper`.
32+
*/
33+
function defineWrapperProperties(wrapper, fn, methods, getters) {
34+
const descriptors = {
35+
__proto__: null,
36+
length: {
37+
__proto__: null,
38+
configurable: true,
39+
value: fn.length,
40+
},
41+
name: {
42+
__proto__: null,
43+
configurable: true,
44+
value: fn.name,
45+
},
46+
};
47+
for (const name in methods) {
48+
descriptors[name] = {
49+
__proto__: null,
50+
configurable: true,
51+
enumerable: true,
52+
writable: true,
53+
value: methods[name],
54+
};
55+
}
56+
for (const name in getters) {
57+
descriptors[name] = {
58+
__proto__: null,
59+
enumerable: false,
60+
get: getters[name],
61+
};
62+
}
63+
return ObjectDefineProperties(wrapper, descriptors);
64+
}
65+
66+
module.exports = {
67+
createAbortError,
68+
defineWrapperProperties,
69+
};

0 commit comments

Comments
 (0)