node-watch
Advanced tools
Comparing version 0.5.4 to 0.5.5
# Changelog | ||
## 0.5.5 | ||
* Remove duplicate events from a composed watcher. | ||
## 0.5.4 | ||
@@ -4,0 +7,0 @@ * Accept Buffer filename. |
@@ -45,2 +45,17 @@ var fs = require('fs'); | ||
function assertEncoding(encoding) { | ||
if (encoding && encoding != 'buffer' && !Buffer.isEncoding(encoding)) { | ||
throw new Error('Unknown encoding: ' + encoding); | ||
} | ||
} | ||
function toStringBuffer(input) { | ||
try { | ||
return Buffer.from(input, 'utf8'); | ||
} catch (e) { | ||
return new Buffer(input, 'utf8'); | ||
} | ||
} | ||
function guard(fn) { | ||
@@ -87,6 +102,8 @@ return function(arg, action) { | ||
var info = fn.info; | ||
var encoding = info.options.encoding; | ||
function handle() { | ||
getMessages(cache).forEach(function(msg) { | ||
if (info.options.encoding == 'buffer') { | ||
msg[1] = new Buffer(msg[1]); | ||
msg[1] = toStringBuffer(msg[1]); | ||
if (encoding != 'buffer') { | ||
msg[1] = msg[1].toString(encoding); | ||
} | ||
@@ -99,5 +116,2 @@ fn.apply(null, msg); | ||
return function(evt, name) { | ||
if (is.buffer(name)) { | ||
name = name.toString() | ||
} | ||
if (is.nil(name)) { | ||
@@ -115,2 +129,17 @@ name = ''; | ||
function createDupsFilter() { | ||
var memo = {}; | ||
return function(fn) { | ||
return function(evt, name) { | ||
memo[evt + name] = [evt, name]; | ||
setTimeout(function() { | ||
Object.keys(memo).forEach(function(n) { | ||
fn.apply(null, memo[n]); | ||
}); | ||
memo = {}; | ||
}); | ||
} | ||
} | ||
} | ||
function getSubDirectories(dir, fn) { | ||
@@ -191,3 +220,3 @@ if (is.directory(dir)) { | ||
var self = this; | ||
info = info || {}; | ||
info = info || { fpath: '' }; | ||
var fullPath = path.resolve(info.fpath); | ||
@@ -240,3 +269,3 @@ this.watchers[fullPath] = watcher; | ||
} else { | ||
self.emit('error', err); | ||
self.emit('error', err); | ||
} | ||
@@ -251,4 +280,7 @@ }); | ||
var opts = assign({}, options, { | ||
// no need to watch recursively | ||
recursive: false, | ||
filter: null | ||
// no filter for single file | ||
filter: null, | ||
encoding: 'utf8' | ||
}); | ||
@@ -260,3 +292,5 @@ | ||
fpath: parent, | ||
options: opts, | ||
options: assign({}, opts, { | ||
encoding: options.encoding | ||
}), | ||
compareName: function(n) { | ||
@@ -276,7 +310,12 @@ return is.sameFile(n, file); | ||
hasNativeRecursive(function(has) { | ||
// always specify recursive | ||
options.recursive = !!options.recursive; | ||
var opts = assign({}, options); | ||
// using utf8 internally | ||
var opts = assign({}, options, { | ||
encoding: 'utf8' | ||
}); | ||
if (!has) { | ||
opts = assign(opts, { recursive: false }); | ||
assign(opts, { recursive: false }); | ||
} | ||
var watcher = fs.watch(dir, opts); | ||
@@ -308,6 +347,7 @@ | ||
var watcher = new Watcher(); | ||
var filterDups = createDupsFilter(); | ||
watchers.forEach(function(w) { | ||
w.on('change', function(evt, name) { | ||
w.on('change', filterDups(function(evt, name) { | ||
watcher.emit('change', evt, name); | ||
}); | ||
})); | ||
w.on('error', function(err) { | ||
@@ -333,4 +373,7 @@ watcher.emit('error', err); | ||
if (is.array(fpath)) { | ||
var filterDups = createDupsFilter(); | ||
return composeWatcher(unique(fpath).map(function(f) { | ||
return watch(f, options, fn); | ||
var w = watch(f, options); | ||
if (fn) w.on('change', filterDups(fn)); | ||
return w; | ||
})); | ||
@@ -360,2 +403,8 @@ }; | ||
if (options.encoding) { | ||
assertEncoding(options.encoding); | ||
} else { | ||
options.encoding = 'utf8'; | ||
} | ||
if (is.file(fpath)) { | ||
@@ -362,0 +411,0 @@ watcher.watchFile(fpath, options, fn); |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"bugs": { | ||
@@ -17,0 +17,0 @@ "url": "https://github.com/yuanchuan/node-watch/issues" |
@@ -114,2 +114,3 @@ # node-watch [![Status](https://travis-ci.org/yuanchuan/node-watch.svg?branch=master)](https://travis-ci.org/yuanchuan/node-watch "See test builds") | ||
* `.getMaxListeners` | ||
##### Extra methods | ||
@@ -116,0 +117,0 @@ * `.isClosed` detect if the watcher is closed |
103
test/test.js
@@ -44,3 +44,3 @@ var assert = require('assert'); | ||
]; | ||
watcher = watch(new Buffer(fpath), function(evt, name) { | ||
watcher = watch(fpath, function(evt, name) { | ||
stack.splice(stack.indexOf(name), 1); | ||
@@ -54,13 +54,2 @@ if (!stack.length) done(); | ||
it('should watch recursively with `recursive: true` option', function(done) { | ||
var dir = tree.getPath('home'); | ||
var file = tree.getPath('home/bb/file1'); | ||
watcher = watch(dir, { recursive: true }, function(evt, name) { | ||
assert.equal(file, name); | ||
done(); | ||
}); | ||
tree.modify('home/bb/file1', 200); | ||
}); | ||
it('should ignore duplicate changes', function(done) { | ||
@@ -106,4 +95,3 @@ var file = 'home/a/file2'; | ||
}); | ||
tree.newFile('home/new/file1'); | ||
tree.newFile('home/new/file1', 100); | ||
tree.modify('home/new/file1', 500); | ||
@@ -113,3 +101,2 @@ }); | ||
describe('events', function() { | ||
@@ -147,3 +134,24 @@ it('should identify `remove` event', function(done) { | ||
describe('options', function() { | ||
describe('recursive', function() { | ||
it('should watch recursively with `recursive: true` option', function(done) { | ||
var dir = tree.getPath('home'); | ||
var file = tree.getPath('home/bb/file1'); | ||
watcher = watch(dir, { recursive: true }, function(evt, name) { | ||
assert.equal(file, name); | ||
done(); | ||
}); | ||
tree.modify('home/bb/file1', 200); | ||
}); | ||
}); | ||
describe('encoding', function() { | ||
it('should throw on invalid encoding', function(done) { | ||
var dir = tree.getPath('home/a'); | ||
try { | ||
watcher = watch(dir, 'unknown'); | ||
} catch (e) { | ||
done(); | ||
} | ||
}); | ||
it('should accept options as an encoding string', function(done) { | ||
@@ -171,2 +179,30 @@ var dir = tree.getPath('home/a'); | ||
}); | ||
it('should support base64 encoding', function(done) { | ||
var dir = tree.getPath('home/a'); | ||
var file = 'home/a/file1'; | ||
var fpath = tree.getPath(file); | ||
watcher = watch(dir, 'base64', function(evt, name) { | ||
assert( | ||
name === (Buffer.from ? Buffer.from(fpath) : new Buffer(fpath)).toString('base64'), | ||
'wrong base64 encoding' | ||
); | ||
done(); | ||
}); | ||
tree.modify(file, 200); | ||
}); | ||
it('should support hex encoding', function(done) { | ||
var dir = tree.getPath('home/a'); | ||
var file = 'home/a/file1'; | ||
var fpath = tree.getPath(file); | ||
watcher = watch(dir, 'hex', function(evt, name) { | ||
assert( | ||
name === (Buffer.from ? Buffer.from(fpath) : new Buffer(fpath)).toString('hex'), | ||
'wrong hex encoding' | ||
); | ||
done(); | ||
}); | ||
tree.modify(file, 200); | ||
}); | ||
}); | ||
@@ -265,2 +301,11 @@ | ||
it('should accept filename as Buffer', function(done) { | ||
var fpath = tree.getPath('home/a/file1'); | ||
watcher = watch(new Buffer(fpath), function(evt, name) { | ||
assert(name == fpath); | ||
done(); | ||
}); | ||
tree.modify('home/a/file1', 100); | ||
}); | ||
it('should compose array of files or directories', function(done) { | ||
@@ -284,2 +329,30 @@ var file1 = 'home/a/file1'; | ||
it('should filter duplicate events for composed watcher', function(done) { | ||
var file1 = 'home'; | ||
var file2 = 'home/a'; | ||
var file3 = 'home/a/file2'; | ||
var newFile = 'home/a/newfile'; | ||
var fpaths = [ | ||
tree.getPath(file1), | ||
tree.getPath(file2), | ||
tree.getPath(file3) | ||
]; | ||
var changed = []; | ||
watcher = watch(fpaths, { recursive: true }, function(evt, name) { | ||
changed.push(name); | ||
}); | ||
tree.modify(file3, 100); | ||
tree.newFile(newFile, 200); | ||
setTimeout(function() { | ||
assert(changed.length == 2, 'should log extactly 2 events'); | ||
assert(~changed.indexOf(tree.getPath(file3)), 'should include ' + file3); | ||
assert(~changed.indexOf(tree.getPath(newFile)), 'should include ' + newFile); | ||
done(); | ||
}, 600); | ||
}); | ||
}); | ||
@@ -286,0 +359,0 @@ |
@@ -86,4 +86,7 @@ var fs = require('fs-extra'); | ||
}, | ||
newFile: function(fpath) { | ||
fs.ensureFileSync(this.getPath(fpath)); | ||
newFile: function(fpath, delay) { | ||
var filePath = this.getPath(fpath); | ||
setTimeout(function() { | ||
fs.ensureFileSync(filePath); | ||
}, delay || 0) | ||
}, | ||
@@ -96,4 +99,7 @@ newSymLink: function(src, dist) { | ||
}, | ||
newDir: function(fpath) { | ||
fs.ensureDirSync(this.getPath(fpath)); | ||
newDir: function(fpath, delay) { | ||
var filePath = this.getPath(fpath); | ||
setTimeout(function() { | ||
fs.ensureDirSync(filePath); | ||
}, delay || 0); | ||
}, | ||
@@ -100,0 +106,0 @@ cleanup: function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31405
924
156