New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

debounce-execution

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

debounce-execution - npm Package Compare versions

Comparing version 1.0.6 to 1.0.7

2

debounce.js

@@ -1,1 +0,1 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define("debounce",n):e.debounce=n()}(this,function(){"use strict";function e(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}var n=function(){function n(){e(this,n)}return n._run=function(e){var t=arguments.length<=1||void 0===arguments[1]?0:arguments[1],o=n._get(e);o&&o(),t<1?n._remove(e):setTimeout(function(){return n._remove(e)},t)},n.debounce=function(e,t){var o=arguments.length<=2||void 0===arguments[2]?0:arguments[2],u=!(arguments.length<=3||void 0===arguments[3])&&arguments[3],i=!n.contains(e);return u&&i?(n._set(e,t),n._run(e,o)):void(u&&!i||u||(n._set(e,t),setTimeout(function(){return n._run(e)},o)))},n.contains=function(e){return n._jobNames.has(e)},n._set=function(e,t){n._jobNames.set(e,t)},n._get=function(e){return n._jobNames.get(e)},n._remove=function(e){n._jobNames.delete(e)},n}();return n._jobNames=new Map,n});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("debounce",["exports"],t):t(e.debounce=e.debounce||{})}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(){t(this,e)}return e.debounce=function(t){var o=arguments.length<=1||void 0===arguments[1]?0:arguments[1],n=!(arguments.length<=2||void 0===arguments[2])&&arguments[2];if(null==t)throw new ReferenceError("You must provide a function to execute as the first argument!");if("number"!=typeof o)throw new TypeError("You must provide a wait time (in ms) as a number as the second argument!");if("boolean"!=typeof n)throw new TypeError("'immediate' must be of type: boolean!");var u=""+t,i=e._timeouts.get(u);null!=i?(clearTimeout(i),e._timeouts.delete(u)):n&&t();var r=setTimeout(function(){n||t(),e._timeouts.delete(u)},o);e._timeouts.set(u,r)},e}();o._timeouts=new Map;var n=o.debounce,u=o.debounce;e.debounce=n,e.default=u,Object.defineProperty(e,"__esModule",{value:!0})});

@@ -1,1 +0,1 @@

function _classCallCheck(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}var Debounce=function(){function n(){_classCallCheck(this,n)}return n._run=function(e){var t=arguments.length<=1||void 0===arguments[1]?0:arguments[1],o=n._get(e);o&&o(),t<1?n._remove(e):setTimeout(function(){return n._remove(e)},t)},n.debounce=function(e,t){var o=arguments.length<=2||void 0===arguments[2]?0:arguments[2],u=!(arguments.length<=3||void 0===arguments[3])&&arguments[3],c=!n.contains(e);return u&&c?(n._set(e,t),n._run(e,o)):void(u&&!c||u||(n._set(e,t),setTimeout(function(){return n._run(e)},o)))},n.contains=function(e){return n._jobNames.has(e)},n._set=function(e,t){n._jobNames.set(e,t)},n._get=function(e){return n._jobNames.get(e)},n._remove=function(e){n._jobNames.delete(e)},n}();Debounce._jobNames=new Map;export default Debounce;
function _classCallCheck(e,o){if(!(e instanceof o))throw new TypeError("Cannot call a class as a function")}var Debounce=function(){function e(){_classCallCheck(this,e)}return e.debounce=function(o){var t=arguments.length<=1||void 0===arguments[1]?0:arguments[1],n=!(arguments.length<=2||void 0===arguments[2])&&arguments[2];if(null==o)throw new ReferenceError("You must provide a function to execute as the first argument!");if("number"!=typeof t)throw new TypeError("You must provide a wait time (in ms) as a number as the second argument!");if("boolean"!=typeof n)throw new TypeError("'immediate' must be of type: boolean!");var u=""+o,r=e._timeouts.get(u);null!=r?(clearTimeout(r),e._timeouts.delete(u)):n&&o();var a=setTimeout(function(){n||o(),e._timeouts.delete(u)},t);e._timeouts.set(u,a)},e}();Debounce._timeouts=new Map;var debounce=Debounce.debounce,Debounce$1=Debounce.debounce;export{debounce};export default Debounce$1;
{
"name": "debounce-execution",
"version": "1.0.6",
"version": "1.0.7",
"description": "Debounce will collapse multiple requests (if any) for a named task into one invocation which will execute after the given wait time has passed.",

@@ -5,0 +5,0 @@ "main": "debounce.js",

@@ -17,2 +17,5 @@ # debounce-execution

**v1.0.7**
- Fixed a bug where the same function could be called multiple times.
**v1.0.6**

@@ -19,0 +22,0 @@ - Added optional Flow typings and built with rollup instead. For a typed version, import from `debounce-execution/typed` instead.

@@ -15,27 +15,13 @@ // @flow

*/
export default class Debounce {
class Debounce {
/**
* A Map between job names and methods.
* @type {Map<string, function>}
* @private
*/
static _jobNames: Map<string, Function> = new Map();
* A map between the timeouts returned by 'setTimeout' and the string representations of the functions to execute.
* @type {Map<string, function>}
* @private
*/
static _timeouts: Map<string, number> = new Map();
/**
* Runs a job and removes it from the collection of jobs.
* @param {string} jobName - The name of the job to execute.
* @param {number} [deletionWait=0] - The amount of time to wait before removing the job from the collection of jobs.
* @returns {void}
*/
static _run (jobName: string, deletionWait: number = 0): void {
let func = Debounce._get(jobName);
if (func) func();
if (deletionWait < 1) Debounce._remove(jobName);
else setTimeout(() => Debounce._remove(jobName), deletionWait);
}
/**
* Adds a new job to the collection of debounced functions.
* @param {String} jobName - The name of the job.
* @param {function} func - The function to execute.

@@ -45,59 +31,31 @@ * @param {number} [waitTime=0] - The amount of time to wait before executing the function.

* @returns {void}
* @throws {ReferenceError} If no function is given.
* @throws {TypeError} If no waitTime is given as a number.
* @throws {TypeError} If no 'immediate' value is given as a boolean.
*/
static debounce (jobName: string, func: Function, waitTime: number = 0, immediate: boolean = false): void {
static debounce (func: Function, waitTime: number = 0, immediate: boolean = false): void {
if (func == null) throw new ReferenceError(`You must provide a function to execute as the first argument!`);
if (typeof waitTime !== "number") throw new TypeError(`You must provide a wait time (in ms) as a number as the second argument!`);
if (typeof immediate !== "boolean") throw new TypeError(`'immediate' must be of type: boolean!`);
let isNewJob = !Debounce.contains(jobName);
const stringified = func.toString();
if (immediate && isNewJob) {
Debounce._set(jobName, func);
return Debounce._run(jobName, waitTime);
}
const existingTimeout = Debounce._timeouts.get(stringified);
else if (immediate && !isNewJob) return;
if (existingTimeout != null) {
// Clear the existing timeout first.
clearTimeout(existingTimeout);
Debounce._timeouts.delete(stringified);
} else if (immediate) func(); // That will be true on the first call.
else if (!immediate) {
Debounce._set(jobName, func);
setTimeout(() => Debounce._run(jobName), waitTime);
}
}
const timeout = setTimeout(() => {
if (!immediate) func(); // If immediate, the function would already have been called previously.
Debounce._timeouts.delete(stringified);
}, waitTime);
/**
* Returns true if the collection of jobs contains the given job name.
* @param {string} jobName - The name of the job to check for.
* @returns {boolean} Whether or not the job exists.
*/
static contains (jobName: string): boolean {
return Debounce._jobNames.has(jobName);
// Debounce the timeout and call the provided function after the given wait time.
Debounce._timeouts.set(stringified, timeout);
}
/**
* Maps the given function to the given job name.
* @param {string} jobName - The name of the job to attach the function to.
* @param {function} func - The function to attach to the job.
* @returns {void}
* @private
*/
static _set (jobName: string, func: Function): void {
Debounce._jobNames.set(jobName, func);
}
/**
* Gets the function that is attached to the given job name.
* @param {String} jobName - The name of the job to get.
* @returns {?function} The attached function, if any.
* @private
*/
static _get (jobName: string): ?Function {
return Debounce._jobNames.get(jobName);
}
/**
* Removes a job from the collection of jobs.
* @param {String} jobName - the job to remove.
* @returns {void}
* @private
*/
static _remove (jobName: string): void {
Debounce._jobNames.delete(jobName);
}
}
export const debounce = Debounce.debounce;
export default Debounce.debounce;

@@ -15,27 +15,13 @@ // @flow

*/
export default class Debounce {
class Debounce {
/**
* A Map between job names and methods.
* @type {Map<string, function>}
* @private
*/
static _jobNames: Map<string, Function> = new Map();
* A map between the timeouts returned by 'setTimeout' and the string representations of the functions to execute.
* @type {Map<string, function>}
* @private
*/
static _timeouts: Map<string, number> = new Map();
/**
* Runs a job and removes it from the collection of jobs.
* @param {string} jobName - The name of the job to execute.
* @param {number} [deletionWait=0] - The amount of time to wait before removing the job from the collection of jobs.
* @returns {void}
*/
static _run (jobName: string, deletionWait: number = 0): void {
let func = Debounce._get(jobName);
if (func) func();
if (deletionWait < 1) Debounce._remove(jobName);
else setTimeout(() => Debounce._remove(jobName), deletionWait);
}
/**
* Adds a new job to the collection of debounced functions.
* @param {String} jobName - The name of the job.
* @param {function} func - The function to execute.

@@ -45,59 +31,31 @@ * @param {number} [waitTime=0] - The amount of time to wait before executing the function.

* @returns {void}
* @throws {ReferenceError} If no function is given.
* @throws {TypeError} If no waitTime is given as a number.
* @throws {TypeError} If no 'immediate' value is given as a boolean.
*/
static debounce (jobName: string, func: Function, waitTime: number = 0, immediate: boolean = false): void {
static debounce (func: Function, waitTime: number = 0, immediate: boolean = false): void {
if (func == null) throw new ReferenceError(`You must provide a function to execute as the first argument!`);
if (typeof waitTime !== "number") throw new TypeError(`You must provide a wait time (in ms) as a number as the second argument!`);
if (typeof immediate !== "boolean") throw new TypeError(`'immediate' must be of type: boolean!`);
let isNewJob = !Debounce.contains(jobName);
const stringified = func.toString();
if (immediate && isNewJob) {
Debounce._set(jobName, func);
return Debounce._run(jobName, waitTime);
}
const existingTimeout = Debounce._timeouts.get(stringified);
else if (immediate && !isNewJob) return;
if (existingTimeout != null) {
// Clear the existing timeout first.
clearTimeout(existingTimeout);
Debounce._timeouts.delete(stringified);
} else if (immediate) func(); // That will be true on the first call.
else if (!immediate) {
Debounce._set(jobName, func);
setTimeout(() => Debounce._run(jobName), waitTime);
}
}
const timeout = setTimeout(() => {
if (!immediate) func(); // If immediate, the function would already have been called previously.
Debounce._timeouts.delete(stringified);
}, waitTime);
/**
* Returns true if the collection of jobs contains the given job name.
* @param {string} jobName - The name of the job to check for.
* @returns {boolean} Whether or not the job exists.
*/
static contains (jobName: string): boolean {
return Debounce._jobNames.has(jobName);
// Debounce the timeout and call the provided function after the given wait time.
Debounce._timeouts.set(stringified, timeout);
}
/**
* Maps the given function to the given job name.
* @param {string} jobName - The name of the job to attach the function to.
* @param {function} func - The function to attach to the job.
* @returns {void}
* @private
*/
static _set (jobName: string, func: Function): void {
Debounce._jobNames.set(jobName, func);
}
/**
* Gets the function that is attached to the given job name.
* @param {String} jobName - The name of the job to get.
* @returns {?function} The attached function, if any.
* @private
*/
static _get (jobName: string): ?Function {
return Debounce._jobNames.get(jobName);
}
/**
* Removes a job from the collection of jobs.
* @param {String} jobName - the job to remove.
* @returns {void}
* @private
*/
static _remove (jobName: string): void {
Debounce._jobNames.delete(jobName);
}
}
export const debounce = Debounce.debounce;
export default Debounce.debounce;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc