From 18de108e4c0853ad846e8f1970a97974a33aad59 Mon Sep 17 00:00:00 2001 From: Johan Dahlberg Date: Tue, 30 Mar 2010 15:27:23 +0200 Subject: [PATCH] Bugfix: watchFile, unwatch, watch causes error 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 | 4 ++-- test/pummel/test-watch-file.js | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index f9fc33999b..22dba6dc60 100644 --- 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; diff --git a/test/pummel/test-watch-file.js b/test/pummel/test-watch-file.js index aaef2da7f4..3f50714149 100644 --- a/test/pummel/test-watch-file.js +++ b/test/pummel/test-watch-file.js @@ -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+");