Permalink
Browse files

Live Reload: Handle "rename" event and re-watch file

- Symptom: When using an editor (e.g. like Vim) that renames a file
    when saving changes, socketstream Live Reload will stop
    working after the first change reload.

- Change: Check for the "rename" event, close old watch
    and open a new watch for the file.

- Test: Besides testing functionality, run in bash:
    ls -l /proc/<pid>/fd | grep inotify | wc -l
    and ensure line count stays the same across multiple
    watch file changes.
    <pid> is the process id of the running socketstream node app.

- Based on Trevor Burnhams answer to:
    http://stackoverflow.com/questions/8280915/coffeescript-1-1-3-watch-only-works-once

- OS: Ubuntu 10.04
  • Loading branch information...
madscoaducom committed Apr 24, 2012
1 parent f907cdf commit 87858053f3a27808164c3b52d1338b9c4d135516
Showing with 15 additions and 4 deletions.
  1. +10 −3 lib/client/live_reload.js
  2. +5 −1 src/client/live_reload.coffee
View
@@ -47,11 +47,18 @@ module.exports = function(root, options, ss) {
return fs.watch(dir, detectNewFiles);
});
return paths.files.forEach(function(file) {
- var changeAction, extension;
+ var changeAction, extension, watcher;
extension = file.split('.')[file.split('.').length - 1];
changeAction = cssExtensions.indexOf(extension) >= 0 && 'updateCSS' || 'reload';
- return fs.watch(file, function(event, filename) {
- return handleFileChange(changeAction);
+ return watcher = fs.watch(file, function(event, filename) {
+ handleFileChange(changeAction);
+ if (event === "rename") {
+ watcher.close();
+ return watch({
+ files: [file],
+ dirs: []
+ });
+ }
});
});
};
@@ -40,7 +40,11 @@ module.exports = (root, options, ss) ->
paths.files.forEach (file) ->
extension = file.split('.')[file.split('.').length-1]
changeAction = cssExtensions.indexOf(extension) >= 0 && 'updateCSS' || 'reload'
- fs.watch file, (event, filename) -> handleFileChange(changeAction)
+ watcher = fs.watch file, (event, filename) ->
+ handleFileChange(changeAction)
+ if event == "rename"
+ watcher.close()
+ watch({files: [file], dirs: []})
detectNewFiles = ->
pathsNow = assetsToWatch()

0 comments on commit 8785805

Please sign in to comment.