MCPcopy Index your code
hub / github.com/nodejs/node / watchFile

Function watchFile

lib/fs.js:3106–3145  ·  view source on GitHub ↗

* Watches for changes on `filename`. * @param {string | Buffer | URL} filename * @param {{ * bigint?: boolean; * persistent?: boolean; * interval?: number; * }} [options] * @param {( * current?: Stats, * previous?: Stats * ) => any} listener * @returns {watchers.StatWatcher}

(filename, options, listener)

Source from the content-addressed store, hash-verified

3104 * @returns {watchers.StatWatcher}
3105 */
3106function watchFile(filename, options, listener) {
3107 const h = vfsState.handlers;
3108 if (h !== null) {
3109 const result = h.watchFile(filename, options, listener);
3110 if (result !== undefined) return result;
3111 }
3112 filename = getValidatedPath(filename);
3113 filename = pathModule.resolve(filename);
3114 let stat;
3115
3116 if (options === null || typeof options !== 'object') {
3117 listener = options;
3118 options = null;
3119 }
3120
3121 options = {
3122 // Poll interval in milliseconds. 5007 is what libev used to use. It's
3123 // a little on the slow side but let's stick with it for now to keep
3124 // behavioral changes to a minimum.
3125 interval: 5007,
3126 persistent: true,
3127 ...options,
3128 };
3129
3130 validateFunction(listener, 'listener');
3131
3132 stat = statWatchers.get(filename);
3133 const watchers = require('internal/fs/watchers');
3134 if (stat === undefined) {
3135 stat = new watchers.StatWatcher(options.bigint);
3136 stat[watchers.kFSStatWatcherStart](filename,
3137 options.persistent, options.interval);
3138 statWatchers.set(filename, stat);
3139 } else {
3140 stat[watchers.kFSStatWatcherAddOrCleanRef]('add');
3141 }
3142
3143 stat.addListener('change', listener);
3144 return stat;
3145}
3146
3147/**
3148 * Stops watching for changes on `filename`.

Callers

nothing calls this directly

Calls 6

getMethod · 0.65
requireFunction · 0.50
watchFileMethod · 0.45
resolveMethod · 0.45
setMethod · 0.45
addListenerMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…