debounce-queue
Advanced tools
Comparing version 0.0.1 to 0.1.0
52
index.js
@@ -1,25 +0,28 @@ | ||
module.exports = function debounceQueue( fn, delay, opts ) { | ||
if ( typeof fn !== 'function' ) { | ||
throw new Error( 'Required: the function to debounce' ); | ||
const defaultEnqueue = require('./enqueue'); | ||
module.exports = function debounceQueue(callback, delay, opts) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('Required: the function to debounce'); | ||
} | ||
if (typeof delay !== 'number' && !opts) { | ||
opts = delay | ||
delay = opts.delay | ||
} | ||
opts = opts || {}; | ||
delay = delay || 100; | ||
let queue = []; | ||
const enqueue = opts.enqueue || function( data, queue ) { | ||
if ( queue.indexOf( data ) === -1 ) { | ||
queue.push( data ) | ||
} | ||
return queue; | ||
} | ||
const enqueue = opts.enqueue || defaultEnqueue; | ||
delay = delay || 100; | ||
const maxWait = opts.maxWait || ( delay + 2000 ); | ||
const maxWait = opts.maxWait || (delay + 2000); | ||
let time = new Date(); | ||
function debounced( data ) { | ||
const ret = enqueue( data, queue.slice() ); | ||
if ( !( ret instanceof Array ) ) { | ||
throw new Error( 'opts.enqueue must return a modified queue array' ); | ||
function debounced(data) { | ||
const queueCopy = queue.slice() | ||
const ret = enqueue(data, queueCopy, (_data = data, queue = queueCopy) => defaultEnqueue(_data, queue)); | ||
if (!(ret instanceof Array)) { | ||
throw new Error('opts.enqueue must return a modified queue array'); | ||
} else { | ||
@@ -31,3 +34,3 @@ // if (ret.length < queue.length) { | ||
} | ||
if ( new Date( time + maxWait ) < new Date() ) { | ||
if (new Date(time + maxWait) < new Date()) { | ||
setNextTimer(); | ||
@@ -43,11 +46,13 @@ time = new Date(); | ||
let ret; | ||
if ( flush.length ) { | ||
ret = fn( flush ); | ||
if (flush.length) { | ||
ret = callback(flush); | ||
} | ||
if ( ret && ret.then ) { | ||
ret.then( setNextTimer ); | ||
if (ret && ret.then) { | ||
ret.then(setNextTimer); | ||
} else { | ||
setNextTimer(); | ||
} | ||
return ret | ||
} | ||
@@ -57,11 +62,14 @@ | ||
debounced.clearTimeout = () => { | ||
if ( !timer ) return; | ||
if (!timer) return; | ||
clearTimeout(timer); | ||
timer = null; | ||
}; | ||
function setNextTimer() { | ||
debounced.clearTimeout(); | ||
timer = setTimeout( timeoutFn, delay ); | ||
timer = setTimeout(timeoutFn, delay); | ||
} | ||
// start timer at the beginning? | ||
// or let the first invocation start it? | ||
// setNextTimer(); | ||
@@ -68,0 +76,0 @@ |
{ | ||
"name": "debounce-queue", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Like lodash.debounce but you get an array of all previous (unique) events", | ||
"main": "index.js", | ||
"repository": "laggingreflex/debounce-queue", | ||
"author": "laggingreflex@gmail.com", | ||
"license": "ISC" | ||
"repository": "laggingreflex/debounce-queue" | ||
} |
@@ -26,14 +26,12 @@ # debounce-queue | ||
You can customize which items get enqueued (this is the default): | ||
You can customize which items get enqueued: | ||
```js | ||
function enqueue( data, queue ) { | ||
if ( queue.indexOf( data ) === -1 ) { | ||
queue.push( data ) | ||
const debounced = debounce(onChange, 1000, { | ||
enqueue( data, queue, defaultEnqueue ) { | ||
if ( queue.indexOf( data ) === -1 ) { | ||
queue.push( data ) | ||
} | ||
return queue; | ||
} | ||
return queue; | ||
} | ||
const debounced = debounce(onChange, 1000, { | ||
enqueue: enqueue | ||
}); | ||
@@ -52,4 +50,4 @@ ``` | ||
}); | ||
// Next onChange will fire after (delay+(n(files)*1000))ms | ||
// Next onChange will fire after `delay + files.length * 1000` ms | ||
} | ||
``` |
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
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 contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
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
26520
21
858
1
2
51
1