Files
node/lib/internal/util/wrapped_function.js
James M Snell 67798df884 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: https://github.com/nodejs/node/pull/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>
2026-11-17 18:37:00 +00:00

70 lines
1.6 KiB
JavaScript

'use strict';
// Helpers shared by util.debounce() and util.throttle().
const {
ObjectDefineProperties,
} = primordials;
const {
AbortError,
} = require('internal/errors');
/**
* Creates the AbortError used to reject calls that were cancelled or aborted.
* @param {any} reason
* @returns {AbortError}
*/
function createAbortError(reason) {
return reason === undefined ?
new AbortError() :
new AbortError(undefined, { __proto__: null, cause: reason });
}
/**
* Defines the control methods, state getters, `length`, and `name` on a
* function returned by util.debounce() or util.throttle().
* @param {Function} wrapper The function returned to the user.
* @param {Function} fn The wrapped user function.
* @param {{ [name: string]: Function }} methods Enumerable, writable methods.
* @param {{ [name: string]: Function }} getters Non-enumerable getters.
* @returns {Function} `wrapper`.
*/
function defineWrapperProperties(wrapper, fn, methods, getters) {
const descriptors = {
__proto__: null,
length: {
__proto__: null,
configurable: true,
value: fn.length,
},
name: {
__proto__: null,
configurable: true,
value: fn.name,
},
};
for (const name in methods) {
descriptors[name] = {
__proto__: null,
configurable: true,
enumerable: true,
writable: true,
value: methods[name],
};
}
for (const name in getters) {
descriptors[name] = {
__proto__: null,
enumerable: false,
get: getters[name],
};
}
return ObjectDefineProperties(wrapper, descriptors);
}
module.exports = {
createAbortError,
defineWrapperProperties,
};