Skip to content

Commit

Permalink
debounce server restart at 100ms when watching server files
Browse files Browse the repository at this point in the history
  • Loading branch information
EndangeredMassa committed Nov 25, 2014
1 parent e01421e commit 32e46a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
48 changes: 20 additions & 28 deletions lib/tasks/server/express-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var chalk = require('chalk');
var _ = require('lodash-node');
var Promise = require('../../ext/promise');
var Task = require('../../models/task');
var SilentError = require('../../errors/silent');
Expand All @@ -14,7 +15,11 @@ module.exports = Task.extend({
this.emitter = new EventEmitter();
this.express = this.express || require('express');
this.http = this.http || require('http');
this.serverRestartDelayTime = 100;

var serverRestartDelayTime = this.serverRestartDelayTime || 100;
this.scheduleServerRestart = _.debounce(function(){
this.restartHttpServer();
}, serverRestartDelayTime);
},

on: function() {
Expand Down Expand Up @@ -99,44 +104,31 @@ module.exports = Task.extend({
this.scheduleServerRestart();
},

scheduleServerRestart: function () {
// If multiple files change in quick succession, let's only restart the server once
if (!this.serverRestartScheduled) {
this.serverRestartScheduled = true;
var self = this;
setTimeout(function () {
self.serverRestartScheduled = false;
self.restartHttpServer();
}, this.serverRestartDelayTime);
}
},

restartHttpServer: function() {
var self = this;
if (!this.serverRestartPromise) {
this.serverRestartPromise =
this.stopHttpServer()
.then(function () {
self.invalidateCache(self.serverRoot);
return self.startHttpServer();
})
this.invalidateCache(this.serverRoot);
return this.startHttpServer();
}.bind(this))
.then(function () {
self.emitter.emit('restart');
self.ui.writeLine('');
self.ui.writeLine(chalk.green('Server restarted.'));
self.ui.writeLine('');
})
this.emitter.emit('restart');
this.ui.writeLine('');
this.ui.writeLine(chalk.green('Server restarted.'));
this.ui.writeLine('');
}.bind(this))
.catch(function (err) {
self.ui.writeError(err);
})
this.ui.writeError(err);
}.bind(this))
.finally(function () {
self.serverRestartPromise = null;
});
this.serverRestartPromise = null;
}.bind(this));
return this.serverRestartPromise;
} else {
return this.serverRestartPromise.then(function () {
return self.restartHttpServer();
});
return this.restartHttpServer();
}.bind(this));
}
},

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/dummy-project-outdated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"express": "^4.1.1",
"glob": "^3.2.9"
}
}
}
1 change: 1 addition & 0 deletions tests/unit/tasks/server/express-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('express-server', function() {
project: project,
watcher: new MockWatcher(),
serverWatcher: new MockServerWatcher(),
serverRestartDelayTime: 5,
serverRoot: './server',
proxyMiddleware: function() {
return proxy.handler.bind(proxy);
Expand Down

0 comments on commit 32e46a4

Please sign in to comment.