Chokidar: Timing file change events

Created on 8 Jan 2016  路  2Comments  路  Source: paulmillr/chokidar

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

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.

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);

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ORESoftware picture ORESoftware  路  3Comments

j-fliegenschmidt picture j-fliegenschmidt  路  6Comments

jbreckmckye picture jbreckmckye  路  7Comments

ahmadnassri picture ahmadnassri  路  6Comments

xsq007 picture xsq007  路  6Comments