debounce-queue
Like lodash.debounce
but you get an array of all previous (unique) events instead of just the last/first one.
Example
import {watch} from 'chokidar';
import debounce from 'debounce-queue';
function onChange(files) {
}
const debounced = debounce(onChange, 1000);
watch('.')
.on('change', debounced)
You can customize which items get enqueued:
const debounced = debounce(onChange, 1000, {
enqueue( data, queue, defaultEnqueue ) {
if ( queue.indexOf( data ) === -1 ) {
queue.push( data )
}
return queue;
}
});
Options
sleep
(boolean) (default: false)
Sleeps while callback is being awaited. Helps prevent feedback loop when a process modifies files in the same path it's watching for changes.
Be sure to return a promise in your callback.
Promise support
Your debounced function can return a promise
function onChange(files) {
return Promise.map(files, () => {
return Promise.delay(1000);
});
}