mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
Move watchFile into fs module
This commit is contained in:
parent
810882c1ad
commit
30b700ee22
48
doc/api.txt
48
doc/api.txt
@ -153,30 +153,6 @@ Send a signal to a process. +pid+ is the process id and +signal+ is the
|
|||||||
signal to send; for example, "SIGINT" or "SIGUSR1". See kill(2) for more
|
signal to send; for example, "SIGINT" or "SIGUSR1". See kill(2) for more
|
||||||
information.
|
information.
|
||||||
|
|
||||||
+process.watchFile(filename, [options,] listener)+::
|
|
||||||
Watch for changes on +filename+. The callback +listener+ will be called each
|
|
||||||
time the file changes.
|
|
||||||
+
|
|
||||||
The second argument is optional. The +options+ if provided should be an
|
|
||||||
object containing two members a boolean, +persistent+, and +interval+, a
|
|
||||||
polling value in milliseconds. The default is +{persistent: true, interval:
|
|
||||||
0}+.
|
|
||||||
+
|
|
||||||
The +listener+ gets two arguments the current stat object and the previous
|
|
||||||
stat object:
|
|
||||||
+
|
|
||||||
-------------------------
|
|
||||||
process.watchFile(f, function (curr, prev) {
|
|
||||||
sys.puts("the current mtime is: " + curr.mtime);
|
|
||||||
sys.puts("the previous mtime was: " + prev.mtime);
|
|
||||||
});
|
|
||||||
-------------------------
|
|
||||||
+
|
|
||||||
These stat objects are instances of +fs.Stat+.
|
|
||||||
|
|
||||||
+process.unwatchFile(filename)+::
|
|
||||||
Stop watching for changes on +filename+.
|
|
||||||
|
|
||||||
+process.compile(source, scriptOrigin)+::
|
+process.compile(source, scriptOrigin)+::
|
||||||
Just like +eval()+ except that you can specify a +scriptOrigin+ for better
|
Just like +eval()+ except that you can specify a +scriptOrigin+ for better
|
||||||
error reporting.
|
error reporting.
|
||||||
@ -755,6 +731,30 @@ fs.writeFile("message.txt", "Hello Node", function (err) {
|
|||||||
+fs.writeFileSync(filename, data, encoding="utf8")+::
|
+fs.writeFileSync(filename, data, encoding="utf8")+::
|
||||||
The synchronous version of +fs.writeFile+.
|
The synchronous version of +fs.writeFile+.
|
||||||
|
|
||||||
|
+fs.watchFile(filename, [options,] listener)+::
|
||||||
|
Watch for changes on +filename+. The callback +listener+ will be called each
|
||||||
|
time the file changes.
|
||||||
|
+
|
||||||
|
The second argument is optional. The +options+ if provided should be an
|
||||||
|
object containing two members a boolean, +persistent+, and +interval+, a
|
||||||
|
polling value in milliseconds. The default is +{persistent: true, interval:
|
||||||
|
0}+.
|
||||||
|
+
|
||||||
|
The +listener+ gets two arguments the current stat object and the previous
|
||||||
|
stat object:
|
||||||
|
+
|
||||||
|
-------------------------
|
||||||
|
fs.watchFile(f, function (curr, prev) {
|
||||||
|
sys.puts("the current mtime is: " + curr.mtime);
|
||||||
|
sys.puts("the previous mtime was: " + prev.mtime);
|
||||||
|
});
|
||||||
|
-------------------------
|
||||||
|
+
|
||||||
|
These stat objects are instances of +fs.Stat+.
|
||||||
|
|
||||||
|
+fs.unwatchFile(filename)+::
|
||||||
|
Stop watching for changes on +filename+.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
=== +fs.Stats+
|
=== +fs.Stats+
|
||||||
|
40
lib/fs.js
40
lib/fs.js
@ -250,3 +250,43 @@ exports.catSync = function () {
|
|||||||
throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead.");
|
throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Stat Change Watchers
|
||||||
|
|
||||||
|
var statWatchers = {};
|
||||||
|
|
||||||
|
exports.watchFile = function (filename) {
|
||||||
|
var stat;
|
||||||
|
var options;
|
||||||
|
var listener;
|
||||||
|
|
||||||
|
if ("object" == typeof arguments[1]) {
|
||||||
|
options = arguments[1];
|
||||||
|
listener = arguments[2];
|
||||||
|
} else {
|
||||||
|
options = {};
|
||||||
|
listener = arguments[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.persistent === undefined) options.persistent = true;
|
||||||
|
if (options.interval === undefined) options.interval = 0;
|
||||||
|
|
||||||
|
if (filename in statWatchers) {
|
||||||
|
stat = statWatchers[filename];
|
||||||
|
} else {
|
||||||
|
statWatchers[filename] = new process.Stat();
|
||||||
|
stat = statWatchers[filename];
|
||||||
|
stat.start(filename, options.persistent, options.interval);
|
||||||
|
}
|
||||||
|
stat.addListener("change", listener);
|
||||||
|
return stat;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.unwatchFile = function (filename) {
|
||||||
|
if (filename in statWatchers) {
|
||||||
|
stat = statWatchers[filename];
|
||||||
|
stat.stop();
|
||||||
|
statWatchers[filename] = undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
42
src/node.js
42
src/node.js
@ -19,6 +19,8 @@ GLOBAL.print = removed("print() has moved. Use require('sys') to bring it back."
|
|||||||
GLOBAL.p = removed("p() has moved. Use require('sys') to bring it back.");
|
GLOBAL.p = removed("p() has moved. Use require('sys') to bring it back.");
|
||||||
process.debug = removed("process.debug() has moved. Use require('sys') to bring it back.");
|
process.debug = removed("process.debug() has moved. Use require('sys') to bring it back.");
|
||||||
process.error = removed("process.error() has moved. Use require('sys') to bring it back.");
|
process.error = removed("process.error() has moved. Use require('sys') to bring it back.");
|
||||||
|
process.watchFile = removed("process.watchFile() has moved to fs.watchFile()");
|
||||||
|
process.unwatchFile = removed("process.unwatchFile() has moved to fs.unwatchFile()");
|
||||||
|
|
||||||
GLOBAL.node = {};
|
GLOBAL.node = {};
|
||||||
|
|
||||||
@ -246,46 +248,6 @@ process.addListener("newListener", function (event) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Stat Change Watchers
|
|
||||||
|
|
||||||
var statWatchers = {};
|
|
||||||
|
|
||||||
process.watchFile = function (filename) {
|
|
||||||
var stat;
|
|
||||||
var options;
|
|
||||||
var listener;
|
|
||||||
|
|
||||||
if ("object" == typeof arguments[1]) {
|
|
||||||
options = arguments[1];
|
|
||||||
listener = arguments[2];
|
|
||||||
} else {
|
|
||||||
options = {};
|
|
||||||
listener = arguments[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.persistent === undefined) options.persistent = true;
|
|
||||||
if (options.interval === undefined) options.interval = 0;
|
|
||||||
|
|
||||||
if (filename in statWatchers) {
|
|
||||||
stat = statWatchers[filename];
|
|
||||||
} else {
|
|
||||||
statWatchers[filename] = new process.Stat();
|
|
||||||
stat = statWatchers[filename];
|
|
||||||
stat.start(filename, options.persistent, options.interval);
|
|
||||||
}
|
|
||||||
stat.addListener("change", listener);
|
|
||||||
return stat;
|
|
||||||
};
|
|
||||||
|
|
||||||
process.unwatchFile = function (filename) {
|
|
||||||
if (filename in statWatchers) {
|
|
||||||
stat = statWatchers[filename];
|
|
||||||
stat.stop();
|
|
||||||
statWatchers[filename] = undefined;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// Timers
|
// Timers
|
||||||
function addTimerListener (callback) {
|
function addTimerListener (callback) {
|
||||||
var timer = this;
|
var timer = this;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
process.mixin(require("../common"));
|
process.mixin(require("../common"));
|
||||||
|
|
||||||
|
var fs = require("fs");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
|
|
||||||
var f = path.join(fixturesDir, "x.txt");
|
var f = path.join(fixturesDir, "x.txt");
|
||||||
@ -8,16 +9,14 @@ var f2 = path.join(fixturesDir, "x2.txt");
|
|||||||
puts("watching for changes of " + f);
|
puts("watching for changes of " + f);
|
||||||
|
|
||||||
var changes = 0;
|
var changes = 0;
|
||||||
process.watchFile(f, function (curr, prev) {
|
fs.watchFile(f, function (curr, prev) {
|
||||||
puts(f + " change");
|
puts(f + " change");
|
||||||
changes++;
|
changes++;
|
||||||
assert.ok(curr.mtime != prev.mtime);
|
assert.ok(curr.mtime != prev.mtime);
|
||||||
process.unwatchFile(f);
|
fs.unwatchFile(f);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var fs = require("fs");
|
|
||||||
|
|
||||||
var fd = fs.openSync(f, "w+");
|
var fd = fs.openSync(f, "w+");
|
||||||
fs.writeSync(fd, 'xyz\n');
|
fs.writeSync(fd, 'xyz\n');
|
||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user