debounce
Advanced tools
Comparing version 1.2.1 to 2.0.0
143
index.js
@@ -1,70 +0,93 @@ | ||
/** | ||
* Returns a function, that, as long as it continues to be invoked, will not | ||
* be triggered. The function will be called after it stops being called for | ||
* N milliseconds. If `immediate` is passed, trigger the function on the | ||
* leading edge, instead of the trailing. The function also has a property 'clear' | ||
* that is a function which will clear the timer to prevent previously scheduled executions. | ||
* | ||
* @source underscore.js | ||
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ | ||
* @param {Function} function to wrap | ||
* @param {Number} timeout in ms (`100`) | ||
* @param {Boolean} whether to execute at the beginning (`false`) | ||
* @api public | ||
*/ | ||
function debounce(func, wait, immediate){ | ||
var timeout, args, context, timestamp, result; | ||
if (null == wait) wait = 100; | ||
function debounce(function_, wait = 100, options = {}) { | ||
if (typeof function_ !== 'function') { | ||
throw new TypeError(`Expected the first parameter to be a function, got \`${typeof function_}\`.`); | ||
} | ||
function later() { | ||
var last = Date.now() - timestamp; | ||
if (wait < 0) { | ||
throw new RangeError('`wait` must not be negative.'); | ||
} | ||
if (last < wait && last >= 0) { | ||
timeout = setTimeout(later, wait - last); | ||
} else { | ||
timeout = null; | ||
if (!immediate) { | ||
result = func.apply(context, args); | ||
context = args = null; | ||
} | ||
} | ||
}; | ||
// TODO: Deprecate the boolean parameter at some point. | ||
const {immediate} = typeof options === 'boolean' ? {immediate: options} : options; | ||
var debounced = function(){ | ||
context = this; | ||
args = arguments; | ||
timestamp = Date.now(); | ||
var callNow = immediate && !timeout; | ||
if (!timeout) timeout = setTimeout(later, wait); | ||
if (callNow) { | ||
result = func.apply(context, args); | ||
context = args = null; | ||
} | ||
let storedContext; | ||
let storedArguments; | ||
let timeoutId; | ||
let timestamp; | ||
let result; | ||
return result; | ||
}; | ||
function later() { | ||
const last = Date.now() - timestamp; | ||
debounced.clear = function() { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
}; | ||
debounced.flush = function() { | ||
if (timeout) { | ||
result = func.apply(context, args); | ||
context = args = null; | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
}; | ||
if (last < wait && last >= 0) { | ||
timeoutId = setTimeout(later, wait - last); | ||
} else { | ||
timeoutId = undefined; | ||
return debounced; | ||
}; | ||
if (!immediate) { | ||
const callContext = storedContext; | ||
const callArguments = storedArguments; | ||
storedContext = undefined; | ||
storedArguments = undefined; | ||
result = function_.apply(callContext, callArguments); | ||
} | ||
} | ||
} | ||
const debounced = function (...arguments_) { | ||
if (storedContext && this !== storedContext) { | ||
throw new Error('Debounced method called with different contexts.'); | ||
} | ||
storedContext = this; // eslint-disable-line unicorn/no-this-assignment | ||
storedArguments = arguments_; | ||
timestamp = Date.now(); | ||
const callNow = immediate && !timeoutId; | ||
if (!timeoutId) { | ||
timeoutId = setTimeout(later, wait); | ||
} | ||
if (callNow) { | ||
const callContext = storedContext; | ||
const callArguments = storedArguments; | ||
storedContext = undefined; | ||
storedArguments = undefined; | ||
result = function_.apply(callContext, callArguments); | ||
} | ||
return result; | ||
}; | ||
debounced.clear = () => { | ||
if (!timeoutId) { | ||
return; | ||
} | ||
clearTimeout(timeoutId); | ||
timeoutId = undefined; | ||
}; | ||
debounced.flush = () => { | ||
if (!timeoutId) { | ||
return; | ||
} | ||
const callContext = storedContext; | ||
const callArguments = storedArguments; | ||
storedContext = undefined; | ||
storedArguments = undefined; | ||
result = function_.apply(callContext, callArguments); | ||
clearTimeout(timeoutId); | ||
timeoutId = undefined; | ||
}; | ||
return debounced; | ||
} | ||
// Adds compatibility for ES modules | ||
debounce.debounce = debounce; | ||
module.exports.debounce = debounce; | ||
module.exports = debounce; |
{ | ||
"name": "debounce", | ||
"description": "Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked", | ||
"version": "1.2.1", | ||
"repository": "git://github.com/component/debounce", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "minijasminenode test.js" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"function", | ||
"throttle", | ||
"invoke" | ||
], | ||
"devDependencies": { | ||
"minijasminenode": "^1.1.1", | ||
"sinon": "^1.17.7", | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
"component": { | ||
"scripts": { | ||
"debounce/index.js": "index.js" | ||
} | ||
} | ||
"name": "debounce", | ||
"version": "2.0.0", | ||
"description": "Delay function calls until a set time elapses after the last invocation", | ||
"license": "MIT", | ||
"repository": "sindresorhus/debounce", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "xo && node --test" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"debounce", | ||
"debouncing", | ||
"function", | ||
"throttle", | ||
"invoke", | ||
"limit", | ||
"limited", | ||
"interval", | ||
"rate", | ||
"batch", | ||
"ratelimit" | ||
], | ||
"devDependencies": { | ||
"sinon": "^17.0.1", | ||
"xo": "^0.56.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"unicorn/prefer-module": "off" | ||
} | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
2
6415
5
92
51
2