es-toolkit
Advanced tools
Comparing version 1.2.2-dev.69 to 1.2.2-dev.70
@@ -0,1 +1,4 @@ | ||
interface DebounceOptions { | ||
signal?: AbortSignal; | ||
} | ||
/** | ||
@@ -8,2 +11,4 @@ * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds | ||
* @param {number} debounceMs - The number of milliseconds to delay. | ||
* @param {DebounceOptions} options - The options object. | ||
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function. | ||
* @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method. | ||
@@ -21,4 +26,16 @@ * | ||
* debouncedFunction.cancel(); | ||
* | ||
* // With AbortSignal | ||
* const controller = new AbortController(); | ||
* const signal = controller.signal; | ||
* const debouncedWithSignal = debounce(() => { | ||
* console.log('Function executed'); | ||
* }, 1000, { signal }); | ||
* | ||
* debouncedWithSignal(); | ||
* | ||
* // Will cancel the debounced function call | ||
* controller.abort(); | ||
*/ | ||
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & { | ||
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): F & { | ||
cancel: () => void; | ||
@@ -25,0 +42,0 @@ }; |
@@ -26,17 +26,27 @@ "use strict"; | ||
module.exports = __toCommonJS(debounce_exports); | ||
function debounce(func, debounceMs) { | ||
function debounce(func, debounceMs, { signal } = {}) { | ||
let timeoutId = null; | ||
const debounced = function(...args) { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
} | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return; | ||
} | ||
timeoutId = setTimeout(() => { | ||
func(...args); | ||
timeoutId = null; | ||
}, debounceMs); | ||
}; | ||
const onAbort = function() { | ||
debounced.cancel(); | ||
}; | ||
debounced.cancel = function() { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
return debounced; | ||
@@ -43,0 +53,0 @@ } |
@@ -31,17 +31,27 @@ "use strict"; | ||
// src/function/debounce.ts | ||
function debounce(func, debounceMs) { | ||
function debounce(func, debounceMs, { signal } = {}) { | ||
let timeoutId = null; | ||
const debounced = function(...args) { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
} | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return; | ||
} | ||
timeoutId = setTimeout(() => { | ||
func(...args); | ||
timeoutId = null; | ||
}, debounceMs); | ||
}; | ||
const onAbort = function() { | ||
debounced.cancel(); | ||
}; | ||
debounced.cancel = function() { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
return debounced; | ||
@@ -48,0 +58,0 @@ } |
@@ -338,17 +338,27 @@ "use strict"; | ||
// src/function/debounce.ts | ||
function debounce(func, debounceMs) { | ||
function debounce(func, debounceMs, { signal } = {}) { | ||
let timeoutId = null; | ||
const debounced = function(...args) { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
} | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return; | ||
} | ||
timeoutId = setTimeout(() => { | ||
func(...args); | ||
timeoutId = null; | ||
}, debounceMs); | ||
}; | ||
const onAbort = function() { | ||
debounced.cancel(); | ||
}; | ||
debounced.cancel = function() { | ||
if (timeoutId != null) { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
return debounced; | ||
@@ -355,0 +365,0 @@ } |
{ | ||
"name": "es-toolkit", | ||
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.", | ||
"version": "1.2.2-dev.69+fde86f7f", | ||
"version": "1.2.2-dev.70+a707c06f", | ||
"workspaces": [ | ||
@@ -6,0 +6,0 @@ "docs" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
488174
5185