debounce-execution
Advanced tools
Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "debounce-execution", | ||
"version": "1.0.5", | ||
"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.", | ||
"main": "Debounce.js", | ||
"scripts": { | ||
"build": "babel src -d ./" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/dlmma/debounce-execution.git" | ||
}, | ||
"keywords": [ | ||
"debounce", | ||
"javascript", | ||
"timeout", | ||
"collapse", | ||
"function" | ||
], | ||
"author": "Frederik Wessberg <fwe@dlmma.com> (https://dlmma.com)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dlmma/debounce-execution/issues" | ||
}, | ||
"homepage": "https://github.com/dlmma/debounce-execution#readme", | ||
"devDependencies": { | ||
"babel-cli": "^6.6.5", | ||
"babel-plugin-transform-es2015-arrow-functions": "^6.8.0", | ||
"babel-plugin-transform-es2015-block-scoping": "^6.10.1", | ||
"babel-plugin-transform-es2015-classes": "^6.9.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.10.3", | ||
"babel-plugin-transform-es2015-parameters": "^6.9.0", | ||
"babel-plugin-transform-es2015-template-literals": "^6.8.0", | ||
"babel-plugin-transform-object-assign": "^6.8.0", | ||
"install": "^0.5.6", | ||
"npm": "^3.8.0" | ||
} | ||
"name": "debounce-execution", | ||
"version": "1.0.6", | ||
"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.", | ||
"main": "debounce.js", | ||
"scripts": { | ||
"build:es5": "NODE_ENV=production rollup -c rollup.dist.config.js -o debounce.js", | ||
"build:native": "NODE_ENV=production rollup -c rollup.native.config.js -o native.js", | ||
"build:typed": "cp src/Debounce.js typed.js", | ||
"build:all": "npm run build:es5 & npm run build:native & npm run build:typed & wait" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/dlmma/debounce-execution.git" | ||
}, | ||
"keywords": [ | ||
"debounce", | ||
"javascript", | ||
"timeout", | ||
"collapse", | ||
"function" | ||
], | ||
"author": "Frederik Wessberg <fwe@dlmma.com> (https://dlmma.com)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dlmma/debounce-execution/issues" | ||
}, | ||
"homepage": "https://github.com/dlmma/debounce-execution#readme", | ||
"devDependencies": { | ||
"babel-cli": ">=6.11.4", | ||
"babel-core": ">=6.6.5", | ||
"babel-eslint": "^6.1.2", | ||
"babel-loader": ">=6.2.4", | ||
"babel-plugin-async-to-promises": "^1.0.5", | ||
"babel-plugin-syntax-trailing-function-commas": ">=6.13.0", | ||
"babel-plugin-transform-class-constructor-call": ">=6.8.0", | ||
"babel-plugin-transform-class-properties": ">=6.11.5", | ||
"babel-plugin-transform-exponentiation-operator": ">=6.8.0", | ||
"babel-plugin-transform-export-extensions": ">=6.8.0", | ||
"babel-plugin-transform-flow-strip-types": ">=6.8.0", | ||
"babel-plugin-transform-object-rest-spread": ">=6.8.0", | ||
"babel-polyfill": ">=6.6.1", | ||
"babel-preset-es2015": ">=6.13.2", | ||
"babel-register": ">=6.7.2", | ||
"eslint": "^3.0.1", | ||
"eslint-plugin-babel": ">=3.3.0", | ||
"eslint-plugin-flowtype": ">=2.7.1", | ||
"rollup": "^0.34.10", | ||
"rollup-plugin-babel": "^2.6.1", | ||
"rollup-plugin-commonjs": "^3.3.1", | ||
"rollup-plugin-filesize": "^0.4.4", | ||
"rollup-plugin-node-resolve": "^2.0.0", | ||
"rollup-plugin-uglify": "^1.0.1", | ||
"uglify-js": "github:mishoo/UglifyJS2#harmony" | ||
} | ||
} |
@@ -15,2 +15,8 @@ # debounce-execution | ||
## Changelog | ||
**v1.0.6** | ||
- Added optional Flow typings and built with rollup instead. For a typed version, import from `debounce-execution/typed` instead. | ||
- Added an optional version with es-module exports instead of umd. For the native version, import from `debounce-execution/native` instead. | ||
## Examples | ||
@@ -25,3 +31,3 @@ | ||
container.addEventListener("scroll", scrollHandler); | ||
function onScroll () { | ||
@@ -38,3 +44,3 @@ // Any scroll events fired within the last 200ms has been collapsed and only | ||
container.addEventListener("touchstart", touchStartHandler); | ||
function onTouchStart (e) { | ||
@@ -53,6 +59,14 @@ //Only the first touch event within the next 100ms will invocate this function. | ||
```javascript | ||
import Debounce from "debounce-execution"; | ||
import Debounce from "debounce-execution"; // Transpiled to ES5 | ||
// or | ||
import Debounce from "debounce-execution/typed" // With Flow typings and ES-modules | ||
// or | ||
import Debounce from "debounce-execution/native" // With native ES-modules. | ||
``` | ||
or | ||
The standard version works with `amd`, `umd`, `commonjs` and `iife`: | ||
@@ -59,0 +73,0 @@ ```javascript |
@@ -0,110 +1,101 @@ | ||
// @flow | ||
"use strict"; | ||
/** | ||
* Debounce will collapse multiple requests (if any) for a named task | ||
* into one invocation which will execute after the given wait time has passed. | ||
* By default the latest request for a task will be executed after the given wait time, | ||
* but it is possible to execute the first request and block any future requests until | ||
* the given wait time has passed. | ||
* @author Frederik Wessberg <fwe@dlmma.com> | ||
* @license MIT | ||
* @class | ||
*/ | ||
class Debounce { | ||
/** | ||
* @returns {Map<?String, ?function>} | ||
* @private | ||
* @static | ||
*/ | ||
static get _jobNames () { | ||
if (!Debounce.__jobNames) Debounce._jobNames = new Map(); | ||
return Debounce.__jobNames; | ||
} | ||
* Debounce will collapse multiple requests (if any) for a named task | ||
* into one invocation which will execute after the given wait time has passed. | ||
* By default the latest request for a task will be executed after the given wait time, | ||
* but it is possible to execute the first request and block any future requests until | ||
* the given wait time has passed. | ||
* @author Frederik Wessberg <fwe@dlmma.com> | ||
* @license MIT | ||
* @class | ||
*/ | ||
export default class Debounce { | ||
/** | ||
* @param {Map<?String, ?function>} map | ||
* @private | ||
* @static | ||
*/ | ||
static set _jobNames (map) { | ||
Debounce.__jobNames = map; | ||
} | ||
/** | ||
* A Map between job names and methods. | ||
* @type {Map<string, function>} | ||
* @private | ||
*/ | ||
static _jobNames: Map<string, Function> = new Map(); | ||
/** | ||
* @param {String} jobName | ||
* @param {number} [deletionWait = 0] | ||
* @static | ||
*/ | ||
static _run (jobName, deletionWait = 0) { | ||
let func = Debounce._get(jobName); | ||
if (func) func(); | ||
if (deletionWait < 1) Debounce._remove(jobName); | ||
else setTimeout(() => Debounce._remove(jobName), deletionWait); | ||
} | ||
/** | ||
* 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); | ||
} | ||
/** | ||
* | ||
* @param {String} jobName | ||
* @param {function} func | ||
* @param {number} [waitTime = 0] | ||
* @param {boolean} [immediate = false] | ||
* @static | ||
*/ | ||
static debounce (jobName, func, waitTime = 0, immediate = false) { | ||
let isNewJob = !Debounce._contains(jobName); | ||
/** | ||
* 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. | ||
* @param {number} [waitTime=0] - The amount of time to wait before executing the function. | ||
* @param {boolean} [immediate=false] - If true, the function will execute immediatly. | ||
* @returns {void} | ||
*/ | ||
static debounce (jobName: string, func: Function, waitTime: number = 0, immediate: boolean = false): void { | ||
if (immediate && isNewJob) { | ||
Debounce._set(jobName, func); | ||
return Debounce._run(jobName, waitTime); | ||
} | ||
let isNewJob = !Debounce.contains(jobName); | ||
else if (immediate && !isNewJob) return; | ||
else if (!immediate) { | ||
Debounce._set(jobName, func); | ||
setTimeout(() => { | ||
return Debounce._run(jobName); | ||
}, waitTime); | ||
} | ||
} | ||
if (immediate && isNewJob) { | ||
Debounce._set(jobName, func); | ||
return Debounce._run(jobName, waitTime); | ||
} | ||
/** | ||
* @param {String} jobName | ||
* @returns {boolean} | ||
* @private | ||
* @static | ||
*/ | ||
static _contains (jobName) { | ||
return Debounce._jobNames.has(jobName); | ||
} | ||
else if (immediate && !isNewJob) return; | ||
/** | ||
* @param {String} jobName | ||
* @param {function} func | ||
* @private | ||
*/ | ||
static _set (jobName, func) { | ||
Debounce._jobNames.set(jobName, func); | ||
} | ||
else if (!immediate) { | ||
Debounce._set(jobName, func); | ||
setTimeout(() => Debounce._run(jobName), waitTime); | ||
} | ||
} | ||
/** | ||
* @param {String} jobName | ||
* @returns {?Function} | ||
* @private | ||
*/ | ||
static _get (jobName) { | ||
return Debounce._jobNames.get(jobName); | ||
} | ||
/** | ||
* 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); | ||
} | ||
/** | ||
* @param {String} jobName | ||
* @private | ||
*/ | ||
static _remove (jobName) { | ||
Debounce._jobNames.delete(jobName); | ||
} | ||
/** | ||
* 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); | ||
} | ||
} | ||
module.exports = Debounce; |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
16811
11
352
78
25
1