Skip to content

Instantly share code, notes, and snippets.

@iwehrman
Created October 16, 2013 20:04
Show Gist options
  • Save iwehrman/7013888 to your computer and use it in GitHub Desktop.
Save iwehrman/7013888 to your computer and use it in GitHub Desktop.
A small node fs.watch test case. Calls to FSWatcher.close() hang on Mac if the number of watch calls is too high---sometimes as low as 4 or 5. Reproduces on 0.10.18 and 0.11.7 but not 0.8.20.
/*jslint node:true, vars: true, plusplus: true */
/*global require */
var fs = require("fs");
(function () {
"use strict";
function getDirectoryNames(prefix, depth) {
var dirname = "",
dirnames = [],
i;
for (i = 0; i < depth; i++) {
dirname += prefix + i + "/";
dirnames.push(dirname);
}
return dirnames;
}
function createDirectories(dirs) {
dirs.forEach(function (dir) {
try {
fs.mkdirSync(dir);
} catch (err) { }
});
}
function watchDirectories(dirs) {
console.log("Watching directories...");
var watchers = {};
dirs.forEach(function (dir) {
console.log("Watching: ", dir);
watchers[dir] = fs.watch(dir, function (e) {
console.log("Change: ", dir);
});
});
console.log("Done.");
return watchers;
}
function unwatchDirectories(watchers) {
var dir;
console.log("Unwatching directories...");
Object.keys(watchers)
.sort(function (a, b) {
return a.length - b.length;
})
.forEach(function (dir) {
var watcher = watchers[dir];
console.log("Unwatching: ", dir);
watcher.close();
});
console.log("Done.");
}
var NUM_DIRS = 5;
var dirnames1 = getDirectoryNames("t1-", NUM_DIRS),
dirnames2 = getDirectoryNames("t2-", NUM_DIRS);
createDirectories(dirnames1);
createDirectories(dirnames2);
var watchers1 = watchDirectories(dirnames1);
unwatchDirectories(watchers1);
var watchers2 = watchDirectories(dirnames2);
unwatchDirectories(watchers2);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment