pathwatcher
Advanced tools
Comparing version 0.1.0 to 0.1.1
var binding = require('../build/Release/pathwatcher.node'); | ||
var events = require("events"); | ||
var events = require('events'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
@@ -18,5 +19,3 @@ | ||
this.path = path; | ||
this.handle = binding.watch(path); | ||
handleWatchers[this.handle] = this; | ||
this.start(); | ||
} | ||
@@ -28,3 +27,19 @@ | ||
if (event == 'rename') { | ||
this.path = path; | ||
var self = this; | ||
// Detect atomic write. | ||
this.close(); | ||
setTimeout(function() { | ||
fs.stat(self.path, function(err) { | ||
if (err) { // original file is gone it's a rename. | ||
self.path = path; | ||
self.start(); | ||
self.trigger('rename', path); | ||
} else { // atomic write. | ||
self.start(); | ||
self.trigger('change', path); | ||
} | ||
}); | ||
}, 100); | ||
return; | ||
} else if (event == 'delete') { | ||
@@ -37,2 +52,7 @@ this.close(); | ||
HandleWatcher.prototype.start = function() { | ||
this.handle = binding.watch(this.path); | ||
handleWatchers[this.handle] = this; | ||
} | ||
HandleWatcher.prototype.closeIfNoListener = function() { | ||
@@ -39,0 +59,0 @@ if (this.listeners('change').length == 0) |
@@ -6,3 +6,3 @@ { | ||
"description": "watch events of a file", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
watcher = require('./lib/main.js'); | ||
var r = watcher.watch('/tmp/', function(event, path) { | ||
var r = watcher.watch('/tmp/test.txt', function(event, path) { | ||
console.log(event, path); | ||
@@ -5,0 +5,0 @@ }); |
8499
84
2