Skip to content

Commit

Permalink
Don't watch all files. #36, #90, #118, #140, #192
Browse files Browse the repository at this point in the history
Replaced the watchr module with a normal fs.watch. However, fs.watch isn't prefect on all platforms (on Windows changes in subdirectories doesn't get picked up) so there's also an "re-activity" hack to detect when a user is interested in ungit (which triggers a refresh)
  • Loading branch information
FredrikNoren committed Sep 9, 2013
1 parent 21a7832 commit 4c48f93
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"superagent": "0.14.7",
"underscore": "1.4.4",
"temp": "0.5.0",
"watchr": "2.4.3",
"socket.io": "0.9.16",
"moment": "2.0.0",
"async": "0.2.9",
Expand Down
4 changes: 2 additions & 2 deletions public/source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Api.prototype._decChangeEventBlock = function() {
this.repositoryChanged.dispatch({ repository: null });
}
}
Api.prototype._dispatchChangeEvent = function(data) {
Api.prototype.dispatchChangeEvent = function(data) {
if (this._changeDispatchBlockers > 0) {
this._triedToDispatchWhileBlocked = true;
return;
Expand All @@ -51,7 +51,7 @@ Api.prototype._initSocket = function() {
self.socketId = data.socketId;
});
this.socket.on('changed', function (data) {
self._dispatchChangeEvent(data);
self.dispatchChangeEvent(data);
});
this.socket.on('request-credentials', function (data) {
self.getCredentialsHandler(function(credentials) {
Expand Down
15 changes: 15 additions & 0 deletions public/source/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ ko.bindingHandlers.autocomplete = {
};


// Used to catch when a user was tabbed away and re-visits the page.
// If fs.watch worked better on Windows (i.e. on subdirectories) we wouldn't need this
(function detectReActivity() {
var lastMoved = Date.now();
document.addEventListener('mousemove', function() {
// If the user didn't move for 3 sec and then moved again, it's likely it's a tab-back
if (Date.now() - lastMoved > 3000) {
console.log('Fire change event due to re-activity');
api.dispatchChangeEvent();
}
lastMoved = Date.now();
});
})();


var prevTimestamp = 0;
var updateAnimationFrame = function(timestamp) {
var delta = timestamp - prevTimestamp;
Expand Down
38 changes: 14 additions & 24 deletions source/git-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var gerrit = require('./gerrit');
var gitParser = require('./git-parser');
var winston = require('winston');
var socketIO;
var watchr;


exports.pathPrefix = '';
Expand All @@ -30,7 +29,6 @@ exports.registerApi = function(app, server, ensureAuthenticated, config) {
// To speed up loading times, we start this the next tick since it doesn't have to be instantly started with the server
process.nextTick(function() {
if (!socketIO) socketIO = require('socket.io');
if (!watchr) watchr = require('watchr');
io = socketIO.listen(server, {
logger: {
debug: winston.debug.bind(winston),
Expand All @@ -43,33 +41,25 @@ exports.registerApi = function(app, server, ensureAuthenticated, config) {
sockets.push(socket);
socket.emit('connected', { socketId: sockets.length - 1 });
socket.on('disconnect', function () {
if (socket.watchr) {
socket.watchr.close();
socket.watchr = null;
winston.info('Stop watching ' + socket.watchrPath);
if (socket.watcher) {
socket.watcher.close();
socket.watcher = null;
winston.info('Stop watching ' + socket.watcherPath);
}
});
socket.on('watch', function (data, callback) {
if (socket.watchr) {
socket.leave(socket.watchrPath);
socket.watchr.close(); // only one watcher per socket
winston.info('Stop watching ' + socket.watchrPath);
if (socket.watcher) {
socket.leave(socket.watcherPath);
socket.watcher.close(); // only one watcher per socket
winston.info('Stop watching ' + socket.watcherPath);
}
socket.join(path.normalize(data.path)); // join room for this path
var watchOptions = {
path: data.path,
ignoreCommonPatterns: true,
listener: function() {
socket.emit('changed', { repository: data.path });
},
next: function(err, watchers) {
callback();
}
};
if (data.safeMode) watchOptions.preferredMethods = ['watchFile', 'watch'];
socket.watchrPath = data.path;
socket.watchr = watchr.watch(watchOptions);
winston.info('Start watching ' + socket.watchrPath);
socket.watcherPath = data.path;
socket.watcher = fs.watch(data.path, function() {
socket.emit('changed', { repository: data.path });
});
winston.info('Start watching ' + socket.watcherPath);
callback();
});
});
});
Expand Down

0 comments on commit 4c48f93

Please sign in to comment.