Chokidar: weird issue whereby watcher.getWatched() is empty object

Created on 29 May 2016  路  3Comments  路  Source: paulmillr/chokidar

Just starting use with this lib, this is a weird one, I have the following code, which initializes the watcher, given a socketio message:

    socket.on('watch', function (msg) {

            msg = JSON.parse(msg);

            console.log('watch has been received by server', msg);
            console.log('paths received by server', msg.paths);

            if (watcher) {
                console.log('Watched paths before:', watcher.getWatched());
                watcher.add(msg.paths);
            }
            else {
                console.log('Watcher initialized.');
                watcher = chokidar.watch(msg.paths);
                console.log('Newly watched paths:', watcher.getWatched());
            }

        });

this is what getslogged, not sure what to say

watch has been received by server { paths:
[ '/Users/Olegzandr/WebstormProjects/suman/test/integration-tests/test0.js',
'/Users/Olegzandr/WebstormProjects/suman/test/integration-tests' ] }
paths received by server [ '/Users/Olegzandr/WebstormProjects/suman/test/integration-tests/test0.js',
'/Users/Olegzandr/WebstormProjects/suman/test/integration-tests' ]
Watcher initialized.
Newly watched paths: {} <<<< ???

why might the watched paths object be empty?

Most helpful comment

You should be waiting for the ready event.

watcher = chokidar.watch(msg.paths).on('ready', function() {
    console.log('Newly watched paths:', watcher.getWatched());
}

All 3 comments

my guess is that we have to wait for the 'add' event to fire, to actually get the list of actively watched paths. I can live with that, and if you don't think this should change, please close :)

perhaps a list of "getRegistered" along with "getWatched", because there is a period whereby a path is registered by the user but getWatched() won't return it yet.

Let me give you a real life example of where a getRegistered call might be useful.

User, registers a path x, but x doesn't exist yet. The user may want to know it is registered, before an add event gets fired. Then the user creates the path x, and chokidar picks it up.

You should be waiting for the ready event.

watcher = chokidar.watch(msg.paths).on('ready', function() {
    console.log('Newly watched paths:', watcher.getWatched());
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ORESoftware picture ORESoftware  路  6Comments

ORESoftware picture ORESoftware  路  8Comments

RedMickey picture RedMickey  路  4Comments

Pomax picture Pomax  路  3Comments

rcarmich picture rcarmich  路  4Comments