Comparing version 0.1.0 to 0.1.1
@@ -28,2 +28,5 @@ /* | ||
// Node v0.6 compat | ||
fs.existsSync = fs.existsSync || path.existsSync; | ||
// CoffeeScript's __extends utility | ||
@@ -62,5 +65,8 @@ var __extends = function(child, parent) { | ||
this.options = _.defaults(opts || {}, { | ||
// Tell glob to mark directories | ||
mark: true, | ||
persistent: true, | ||
interval: 100 | ||
// Interval to pass to fs.watchFile | ||
interval: 100, | ||
// Delay for events called in succession for the same file/event | ||
debounceDelay: 500 | ||
}); | ||
@@ -80,2 +86,5 @@ | ||
// Cached events for debouncing | ||
this._cached = Object.create(null); | ||
// Set maxListeners | ||
@@ -97,11 +106,38 @@ if (this.options.maxListeners) { | ||
// Override the emit function to emit `all` events | ||
Gaze.prototype.emit = _.debounce(function() { | ||
var args, event; | ||
event = arguments[0], args = 2 <= arguments.length ? [].slice.call(arguments, 1) : []; | ||
Gaze.__super__.emit.apply(this, arguments); | ||
if (event === 'added' || event === 'changed' || event === 'deleted') { | ||
return Gaze.__super__.emit.apply(this, ['all', event].concat([].slice.call(args))); | ||
// and debounce on duplicate events per file | ||
Gaze.prototype.emit = function() { | ||
var _this = this; | ||
var args = arguments; | ||
var e = args[0]; | ||
var filepath = args[1]; | ||
var timeoutId; | ||
// Actually emit the event and `all` event | ||
var emit = function() { | ||
Gaze.__super__.emit.apply(_this, args); | ||
if (e === 'added' || e === 'changed' || e === 'deleted') { | ||
Gaze.__super__.emit.apply(_this, ['all', e].concat([].slice.call(args, 1))); | ||
} | ||
return _this; | ||
}; | ||
// If no filepath just emit the event | ||
if (typeof filepath !== 'string') { return emit(); } | ||
// If cached doesnt exist, create a delay before running the next | ||
// then emit the event | ||
var cache = this._cached[filepath] || []; | ||
if (_.indexOf(cache, e) === -1) { | ||
this._objectPush(_this._cached, filepath, e); | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(function() { | ||
delete _this._cached[filepath]; | ||
}, this.options.debounceDelay); | ||
return emit(); | ||
} | ||
}, delay, true); | ||
return this; | ||
}; | ||
// Close watchers | ||
@@ -257,3 +293,3 @@ Gaze.prototype.close = function() { | ||
_this._watchFile(dir, function(event, dirpath) { | ||
var relDir = path.relative(process.cwd(), dir); | ||
var relDir = process.cwd() === dir ? '.' : path.relative(process.cwd(), dir); | ||
return fs.readdir(dirpath, function(err, current) { | ||
@@ -295,3 +331,7 @@ if (err) { return _this.emit('error', err); } | ||
// TODO: If event rename, update the watched index | ||
_this.emit('changed', filepath); | ||
// Only emit changed if the file still exists | ||
// Prevents changed/deleted duplicate events | ||
if (fs.existsSync(filepath)) { | ||
_this.emit('changed', filepath); | ||
} | ||
}); | ||
@@ -298,0 +338,0 @@ }); |
{ | ||
"name": "gaze", | ||
"description": "A globbing fs.watch wrapper built from the best parts of other fine watch libs.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/shama/gaze", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -141,2 +141,3 @@ # gaze [![Build Status](https://secure.travis-ci.org/shama/gaze.png?branch=master)](http://travis-ci.org/shama/gaze) | ||
## Release History | ||
* 0.1.1 - Minor fixes | ||
* 0.1.0 - Initial release | ||
@@ -143,0 +144,0 @@ |
@@ -51,5 +51,3 @@ 'use strict'; | ||
this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); | ||
setTimeout(function() { | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); | ||
}, 100); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); | ||
}); | ||
@@ -68,5 +66,3 @@ }, | ||
this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); | ||
setTimeout(function() { | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;'); | ||
}, 100); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;'); | ||
}); | ||
@@ -85,6 +81,4 @@ }, | ||
}); | ||
setTimeout(function() { | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!'); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!'); | ||
}, 100); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!'); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!'); | ||
}); | ||
@@ -116,7 +110,5 @@ }, | ||
}); | ||
setTimeout(function() { | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); | ||
}, 100); | ||
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); | ||
}); | ||
} | ||
}; |
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
24161
576
147