just-debounce-it
Advanced tools
Comparing version 1.4.0 to 1.5.0
// Definitions by: Aziz Khambati <https://github.com/azizhk> | ||
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never; | ||
type CancelType = () => void; | ||
type MethodTypes = { | ||
cancel: () => void; | ||
flush: () => void; | ||
} | ||
export = debounce; | ||
declare function debounce<T extends Function>( | ||
@@ -12,3 +15,3 @@ fn: T, | ||
callFirst?: boolean | ||
): T & {cancel: CancelType}; | ||
): T & MethodTypes; | ||
@@ -19,3 +22,3 @@ declare function debounce<T extends Function>( | ||
callFirst: true | ||
): T & {cancel: CancelType}; | ||
): T & MethodTypes; | ||
@@ -26,2 +29,2 @@ declare function debounce<T extends Function>( | ||
callFirst?: false | ||
): ((...args: ArgumentTypes<T>) => void) & {cancel: CancelType}; | ||
): ((...args: ArgumentTypes<T>) => void) & MethodTypes; |
25
index.js
@@ -5,2 +5,3 @@ module.exports = debounce; | ||
var timeout = null; | ||
var debouncedFn = null; | ||
@@ -10,2 +11,4 @@ var clear = function() { | ||
clearTimeout(timeout); | ||
debouncedFn = null; | ||
timeout = null; | ||
@@ -15,2 +18,11 @@ } | ||
var flush = function() { | ||
var call = debouncedFn; | ||
clear(); | ||
if (call) { | ||
call(); | ||
} | ||
}; | ||
var debounceWrapper = function() { | ||
@@ -24,5 +36,8 @@ if (!wait) { | ||
var callNow = callFirst && !timeout; | ||
clear(); | ||
debouncedFn = function() { | ||
fn.apply(context, args); | ||
}; | ||
timeout = setTimeout(function() { | ||
@@ -32,3 +47,6 @@ timeout = null; | ||
if (!callNow) { | ||
return fn.apply(context, args); | ||
var call = debouncedFn; | ||
debouncedFn = null; | ||
return call(); | ||
} | ||
@@ -38,3 +56,3 @@ }, wait); | ||
if (callNow) { | ||
return fn.apply(this, arguments); | ||
return debouncedFn(); | ||
} | ||
@@ -44,4 +62,5 @@ }; | ||
debounceWrapper.cancel = clear; | ||
debounceWrapper.flush = flush; | ||
return debounceWrapper; | ||
} |
@@ -11,2 +11,5 @@ import debounce = require("./index"); | ||
debounce(() => {}, 1000, false).cancel(); | ||
debounce(() => {}, 50).flush(); | ||
debounce(() => {}, 200, true).flush(); | ||
debounce(() => {}, 1000, false).flush(); | ||
@@ -19,2 +22,6 @@ // not OK | ||
// @ts-expect-error | ||
debounce().flush(); | ||
// @ts-expect-error | ||
debounce({}, 50).flush(); | ||
// @ts-expect-error | ||
debounce({}, 50).cancel(); | ||
@@ -21,0 +28,0 @@ // @ts-expect-error |
{ | ||
"name": "just-debounce-it", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "return a debounced function", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
## just-debounce-it | ||
Part of a [library](../../../../) of zero-dependency npm modules that do just do one thing. | ||
Part of a [library](../../../../) of zero-dependency npm modules that do just do one thing. | ||
Guilt-free utilities for every occasion. | ||
@@ -24,7 +24,14 @@ | ||
const fn3 = debounce(() => console.log("Hello"), 500); | ||
fn1(); | ||
fn1(); | ||
fn1(); | ||
fn3(); | ||
fn3(); | ||
fn3(); | ||
fn3.cancel(); | ||
// function cancelled before 'hello' is logged | ||
const fn4 = debounce(() => console.log("Hello"), 500); | ||
fn4(); | ||
fn4(); | ||
fn4(); | ||
fn4.flush(); | ||
// immediately invoke the debounced function | ||
``` |
4848
104
37