Skip to content

Commit

Permalink
watch: unwatch before onChange
Browse files Browse the repository at this point in the history
Some IDEs like WebStorm change multiple files at once; this caused
and issue where we  ended up starting multiple MAGE workers upon
restart.
  • Loading branch information
stelcheck committed Aug 29, 2017
1 parent 2c12c82 commit efb79d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ var APP_LIB_PATH = path.join(process.cwd(), 'lib');

var debug = require('./debug');

function watchFiles(onChange) {
watch(APP_LIB_PATH, {
function onceSomeFilesChanged(onChange) {
var called = false;
var watcher = watch(APP_LIB_PATH, {
recursive: true
}, function (event, name) {
if (name.split(path.sep).pop()[0] === '.') {
return;
}

if (called) {
return;
}

called = true;
watcher.close();
onChange(event, name);
});
}
Expand Down Expand Up @@ -198,7 +205,7 @@ function connect() {
});

// Watch the lib folder for changes
watchFiles(function (event, name) {
onceSomeFilesChanged(function (event, name) {
logger.debug('File ' + name + ' was ' + event + 'd, reloading');
saveHistory(repl.history);
process.send('reload');
Expand Down Expand Up @@ -231,7 +238,7 @@ cluster.on('exit', function (worker) {
// spawn a new worker to replace the dead one

processManager.emit('workerOffline', worker.id);
watchFiles(function (event, name) {
onceSomeFilesChanged(function (event, name) {
logger.debug('File ' + name + ' was ' + event + 'd, reloading');
processManager.createWorker();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage-console",
"version": "0.3.0",
"version": "0.3.1",
"description": "Alternative MAGE development console, with REPL interface",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit efb79d6

Please sign in to comment.