Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

debounce-queue

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-queue - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

.history/enqueue_20170111100919.js

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
}
```
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