Bugfix: watchFile, unwatch, watch causes error
authorJohan Dahlberg <dahlberg.johan@gmail.com>
Tue, 30 Mar 2010 13:27:23 +0000 (15:27 +0200)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 31 Mar 2010 17:38:25 +0000 (10:38 -0700)
Fixed bug that caused application to cast a "TypeError: Cannot call method
'addListener' of undefined" when first watching a file, unwatching and then
watching same file again.

lib/fs.js
test/pummel/test-watch-file.js

index f9fc33999b67436802920625b8700958279ce474..22dba6dc60137014819505c06f97f03b684014de 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -319,7 +319,7 @@ exports.watchFile = function (filename) {
   if (options.persistent === undefined) options.persistent = true;
   if (options.interval === undefined) options.interval = 0;
 
-  if (filename in statWatchers) {
+  if (statWatchers[filename]) {
     stat = statWatchers[filename];
   } else {
     statWatchers[filename] = new fs.StatWatcher();
@@ -331,7 +331,7 @@ exports.watchFile = function (filename) {
 };
 
 exports.unwatchFile = function (filename) {
-  if (filename in statWatchers) {
+  if (statWatchers[filename]) {
     stat = statWatchers[filename];
     stat.stop();
     statWatchers[filename] = undefined;
index aaef2da7f491845eb8a5375ea53017c4db6972be..3f50714149c4b313f326aab3eb4b5516e2da119d 100644 (file)
@@ -9,12 +9,18 @@ var f2 = path.join(fixturesDir, "x2.txt");
 puts("watching for changes of " + f);
 
 var changes = 0;
-fs.watchFile(f, function (curr, prev) {
-  puts(f + " change");
-  changes++;
-  assert.ok(curr.mtime != prev.mtime);
-  fs.unwatchFile(f);
-});
+function watchFile () {
+  fs.watchFile(f, function (curr, prev) {
+    puts(f + " change");
+    changes++;
+    assert.ok(curr.mtime != prev.mtime);
+    fs.unwatchFile(f);
+    watchFile();
+    fs.unwatchFile(f);
+  });
+}
+
+watchFile();
 
 
 var fd = fs.openSync(f, "w+");