Code coverage report for lib\node\NodeWatchFileSystem.js

Statements: 89.53% (154 / 172)      Branches: 71.79% (56 / 78)      Functions: 94.44% (34 / 36)      Lines: 94.19% (146 / 155)      Ignored: none     

All files » lib\node\ » NodeWatchFileSystem.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252        1 1 1   1 492   1                     1 8   8 8 8 8 8 8   8   8   8 8 6         8 6 6     6     8 10 10   8 2 2   8 10 10       8 8 6     1 90 84 1 84 19 84 10 10   84   84 84 398 398 398 180 90 85   5     398 398 398 5 5   398     84 84 398 94 304   84       6 6 6 6 6 4 4   6         8 82 82 82 8 8 8   82 78 78 78 78 324 324 324 324 72       72 72       4 1 82 82 82     1 90 90       90       8 8   4 4 4 4 4         4   4 4 4 4 4                 1 4 4 4           1 8 8 8       8 8 8 8 8       8   8     1 8 8 82 82       8         1 180 180   180 90 90 90 85 85   175    
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
var fs = require("fs");
var path = require("path");
var async = require("async");
 
function NodeWatchFileSystem(inputFileSystem) {
	this.inputFileSystem = inputFileSystem;
}
module.exports = NodeWatchFileSystem;
 
/**
 *
 * @param files {String[]} a sorted array of paths to files
 * @param dirs {String[]} a sorted array of paths to directories
 * @param startTime {number} the virtual start time
 * @param delay {number} in ms, the time to wait to signal after the first change
 * @param callback {function(err, filesModified: String[], dirsModified: String[], fileTimestamps: Object, dirTimestamps: Object)] called once after change plus delay
 * @param callbackUndelayed {function()} called once after first change
 */
NodeWatchFileSystem.prototype.watch = function(files, dirs, startTime, delay, callback, callbackUndelayed) {
	var inputFileSystem = this.inputFileSystem;
 
	Eif(!callbackUndelayed) callbackUndelayed = function() {}
	var closed = false;
	var fileTimestamps = {};
	var dirTimestamps = {};
	var filesModified = {};
	var dirsModified = {};
 
	var lastChangeTime;
 
	startTime = Math.floor(startTime / 1000) * 1000; // only 1 second accuracy
 
	var directories = {};
	dirs.forEach(function(dir) {
		directories[dir] = {
			context: dir,
			files: []
		};
	});
	files.forEach(function(file) {
		var dir = path.dirname(file);
		if(!directories[dir]) directories[dir] = {
			files: []
		};
		directories[dir].files.push(file);
	});
 
	var items = Object.keys(directories).map(function(dir) {
		directories[dir].path = dir;
		return directories[dir];
	});
	items.sort(function(a,b) {
		Iif(a.path === b.path) return 0;
		return a.path < b.path ? -1 : 1;
	});
	items.forEach(function(item) {
		Eif(item.files) {
			item.files.sort();
		}
	});
 
	var initialChange = false;
	var change = function(path) {
		initialChange = true;
	}
 
	function readStat(item, callback) {
		if(item.context) {
			fs.readdir(item.path, function(err, files) {
				function onTimestamp(ts) {
					if(!dirTimestamps[item.context] || dirTimestamps[item.context] < ts)
						dirTimestamps[item.context] = ts;
					if(ts >= startTime) {
						dirsModified[item.context] = true;
						change(item.path);
					}
					return callback();
				}
				Iif(err) return onTimestamp(Infinity);
				async.map(files, function(file, callback) {
					file = path.join(item.path, file);
					var isFile = false;
					if(item.files) {
						if(binarySearch(item.files, function(path) {
							if(path === file) return 0;
							return path < file ? -1 : 1;
						}) >= 0) {
							isFile = true;
						}
					}
					fs.stat(file, function(err, stat) {
						var ts = err ? Infinity : stat.mtime.getTime();
						if(isFile) {
							fileTimestamps[file] = ts;
							if(ts >= startTime) filesModified[file] = true;
						}
						return callback(null, ts);
					});
				}, function(err, timestamps) {
					Iif(err) return onTimestamp(Infinity);
					var ts = timestamps.reduce(function(max, ts) {
						if(ts > max)
							return ts;
						return max;
					}, 0);
					onTimestamp(ts);
				});
			});
		} else {
			async.forEach(item.files, function(file, callback) {
				fs.stat(file, function(err, stat) {
					var ts = err ? Infinity : stat.mtime.getTime();
					fileTimestamps[file] = ts;
					if(ts >= startTime) {
						filesModified[file] = true;
						change(file);
					}
					return callback(null, ts);
				});
			}, callback);
		}
	}
	async.forEach(items, function processItem(item, callback) {
		var isRunning = false;
		var isScheduled = false;
		item.watcher = fs.watch(item.path, function() {
			Iif(isRunning) return isScheduled = true;
			isRunning = true;
			readStat(item, done);
		});
		if(item.context) {
			item.children = [];
			fs.readdir(item.path, function(err, files) {
				Iif(err) return change(file), onWatcherApplied();
				async.forEach(files, function(file, callback) {
					file = path.join(item.path, file);
					fs.stat(file, function(err, stat) {
						Iif(err) return change(file), callback();
						if(!stat.isDirectory()) return callback();
						var subitem = {
							path: file,
							context: item.context
						};
						item.children.push(subitem);
						processItem(subitem, callback);
					});
				}, onWatcherApplied);
			});
		} else onWatcherApplied();
		function onWatcherApplied() {
			readStat(item, function() {
				callback();
				done();
			});
		}
		function done() {
			Iif(closed) return;
			Iif(isScheduled) {
				isScheduled = false;
				readStat(item, done);
			} else {
				isRunning = false;
			}
		}
	}, function() {
		var timeout;
		if(initialChange) {
 
			callbackUndelayed();
			Eif(delay) {
				lastChangeTime = Date.now();
				change = restartDelay;
				timeout = setTimeout(onTimeout, delay);
			} else onTimeout();
 
		} else {
 
			change = function(path) {
 
				callbackUndelayed();
				Eif(delay) {
					lastChangeTime = Date.now();
					change = restartDelay;
					timeout = setTimeout(onTimeout, delay);
				} else {
					change = function() {};
					onTimeout();
				}
			};
 
		}
 
		function restartDelay() {
			lastChangeTime = Date.now();
			clearTimeout(timeout);
			timeout = setTimeout(onTimeout, delay);
		}
 
	});
 
	// 7.
	function onTimeout() {
		var nextSecond = Math.ceil(lastChangeTime / 1000) * 1000;
		var timeToNextSecond = nextSecond - Date.now();
		Iif(timeToNextSecond > 0) {
			setTimeout(onTimeout, timeToNextSecond);
			return;
		}
		change = function() {};
		Iif(closed) return;
		var outdatedFiles = Object.keys(filesModified).sort();
		var outdatedDirs = Object.keys(dirsModified).sort();
		Iif(inputFileSystem && inputFileSystem.purge) {
			inputFileSystem.purge(outdatedFiles);
			inputFileSystem.purge(outdatedDirs);
		}
		callback(null, outdatedFiles, outdatedDirs, fileTimestamps, dirTimestamps);
 
		close();
	}
 
	function close() {
		closed = true;
		items.forEach(function closeItem(item) {
			item.watcher.close();
			if(item.children) item.children.forEach(closeItem);
		});
	}
 
	return {
		close: close
	}
};
 
function binarySearch(array, comparator) {
	var left = 0;
	var right = array.length - 1;
 
	while(left <= right) {
		var middle = ((left + right)/2)|0;
		var comp = comparator(array[middle]);
		if(comp === 0) return middle;
		Eif(comp > 0) right = middle-1;
		Iif(comp < 0) left = middle+1;
	}
	return -1;
}