Scenario: I'm watching 1000s of files. For each add, change or delete an emit gets triggered and the client (browser) will sometimes get slammed with 100's of requests in a short amount of time. And some will come in bursts. Many heavy duty visual aspects of the client can change. I see the CPU get very high during these spurts. I want to be able to control the speed and number that come down at any given time so browser is not overloaded.
My question is - does anyone have a recommendation for queuing the events that chokiar (correctly) fires off and store them. Then release events out the clients in a controlled, timed manner, say every 30 seconds. Has anyone used or seen such an implementation
Thanks.
Rob
You could collect the events to an array yourself and/or use async.queue (https://github.com/caolan/async#queue) to process them in a controlled manner.
var async = require('async')
, chokidar = require('chokidar')
function parse(path, cb){
// do your stuff. Make sure to call the cb() at some point.
}
// the second argument will allow you to control how many files will be processed at the same time
var parsequeue = async.queue(parse, 50);
function onAdd(path){
parsequeue.push(path, null);
}
var watcher = chokidar('c:\test', {});
watcher.on('add', onAdd);
You might be looking for a debounce or throttle sort of implementation, both of which are provided by lodash and elsewhere.
This is out of scope for chokidar to provide itself, so closing the issue, but feel free to continue to discuss.
Most helpful comment
You could collect the events to an array yourself and/or use async.queue (https://github.com/caolan/async#queue) to process them in a controlled manner.