@@ -74,11 +74,11 @@ function FSWatcher(_opts) {
74
74
if ( undef ( 'followSymlinks' ) ) opts . followSymlinks = true ;
75
75
76
76
if ( undef ( 'awaitWriteFinish' ) ) opts . awaitWriteFinish = false ;
77
-
78
- if ( opts . awaitWriteFinish === true ) opts . awaitWriteFinish = { }
79
- if ( opts . awaitWriteFinish ) {
80
- if ( opts . awaitWriteFinish . stabilityThreshold === undefined ) opts . awaitWriteFinish . stabilityThreshold = 2000 ;
81
- if ( opts . awaitWriteFinish . pollInterval === undefined ) opts . awaitWriteFinish . pollInterval = 100 ;
77
+ if ( opts . awaitWriteFinish === true ) opts . awaitWriteFinish = { } ;
78
+ var awf = opts . awaitWriteFinish ;
79
+ if ( awf ) {
80
+ if ( ! awf . stabilityThreshold ) awf . stabilityThreshold = 2000 ;
81
+ if ( ! awf . pollInterval ) awf . pollInterval = 100 ;
82
82
83
83
this . _pendingWrites = Object . create ( null ) ;
84
84
}
@@ -122,7 +122,8 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
122
122
else if ( val2 !== undefined ) args . push ( val1 , val2 ) ;
123
123
else if ( val1 !== undefined ) args . push ( val1 ) ;
124
124
125
- if ( ( this . options . awaitWriteFinish && this . _pendingWrites [ path ] ) ) return this ;
125
+ var awf = this . options . awaitWriteFinish ;
126
+ if ( awf && this . _pendingWrites [ path ] ) return this ;
126
127
127
128
if ( this . options . atomic ) {
128
129
if ( event === 'unlink' ) {
@@ -141,7 +142,6 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
141
142
}
142
143
}
143
144
144
-
145
145
if ( event === 'change' ) {
146
146
if ( ! this . _throttle ( 'change' , path , 50 ) ) return this ;
147
147
}
@@ -151,16 +151,16 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
151
151
if ( event !== 'error' ) this . emit . apply ( this , [ 'all' ] . concat ( args ) ) ;
152
152
} . bind ( this ) ;
153
153
154
- if ( this . options . awaitWriteFinish && event === 'add' ) {
155
- this . _awaitWriteFinish ( path , this . options . awaitWriteFinish . stabilityThreshold , function ( err , stats ) {
156
- if ( err ) {
154
+ if ( awf && event === 'add' ) {
155
+ this . _awaitWriteFinish ( path , awf . stabilityThreshold , function ( err , stats ) {
156
+ if ( err ) {
157
157
event = args [ 0 ] = 'error' ;
158
158
args [ 1 ] = err ;
159
159
emitEvent ( ) ;
160
160
} else if ( stats ) {
161
161
// if stats doesn't exist the file must have been deleted
162
162
args . push ( stats ) ;
163
- emitEvent ( ) ;
163
+ emitEvent ( ) ;
164
164
}
165
165
} ) ;
166
166
} else if (
@@ -223,25 +223,26 @@ FSWatcher.prototype._throttle = function(action, path, timeout) {
223
223
// Private method: Awaits write operation to finish
224
224
//
225
225
// * path - string, path being acted upon
226
- // * threshold - int, time in milliseconds a file size must be fixed before acknowledgeing write operation is finished
227
- // * callback - function, callback to call when write operation is finished
228
- // Polls a newly created file for size variations. When files size does not change for 'threshold'
229
- // milliseconds calls callback.
226
+ // * threshold - int, time in milliseconds a file size must be fixed before
227
+ // acknowledgeing write operation is finished
228
+ // * callback - function, callback to call when write operation is finished
229
+ // Polls a newly created file for size variations. When files size does not
230
+ // change for 'threshold' milliseconds calls callback.
230
231
FSWatcher . prototype . _awaitWriteFinish = function ( path , threshold , callback ) {
231
232
var timeoutHandler ;
232
233
233
- var awaitWriteFinish = function ( prevStat ) {
234
+ ( function awaitWriteFinish ( prevStat ) {
234
235
fs . stat ( path , function ( err , curStat ) {
235
236
if ( err ) {
236
237
// if the file have been erased, the file entry in _pendingWrites will
237
238
// be deleted in the unlink event.
238
239
if ( err . code == 'ENOENT' ) return ;
239
240
240
241
return callback ( err ) ;
241
- }
242
-
242
+ }
243
+
243
244
var now = new Date ( ) ;
244
- if ( this . _pendingWrites [ path ] === undefined ) {
245
+ if ( this . _pendingWrites [ path ] === undefined ) {
245
246
this . _pendingWrites [ path ] = {
246
247
creationTime : now ,
247
248
cancelWait : function ( ) {
@@ -250,19 +251,26 @@ FSWatcher.prototype._awaitWriteFinish = function(path, threshold, callback) {
250
251
return callback ( ) ;
251
252
} . bind ( this )
252
253
}
253
- return timeoutHandler = setTimeout ( awaitWriteFinish . bind ( this , curStat ) , this . options . awaitWriteFinish . pollInterval ) ;
254
+ return timeoutHandler = setTimeout (
255
+ awaitWriteFinish . bind ( this , curStat ) ,
256
+ this . options . awaitWriteFinish . pollInterval
257
+ ) ;
254
258
}
255
259
256
- if ( curStat . size == prevStat . size && now - this . _pendingWrites [ path ] . creationTime > threshold ) {
260
+ if (
261
+ curStat . size == prevStat . size &&
262
+ now - this . _pendingWrites [ path ] . creationTime > threshold
263
+ ) {
257
264
delete this . _pendingWrites [ path ] ;
258
265
callback ( null , curStat ) ;
259
266
} else {
260
- return timeoutHandler = setTimeout ( awaitWriteFinish . bind ( this , curStat ) , this . options . awaitWriteFinish . pollInterval ) ;
267
+ return timeoutHandler = setTimeout (
268
+ awaitWriteFinish . bind ( this , curStat ) ,
269
+ this . options . awaitWriteFinish . pollInterval
270
+ ) ;
261
271
}
262
272
} . bind ( this ) ) ;
263
- } . bind ( this ) ;
264
-
265
- awaitWriteFinish ( ) ;
273
+ } . bind ( this ) ) ( ) ;
266
274
}
267
275
268
276
// Private method: Determines whether user has asked to ignore this path
@@ -531,7 +539,7 @@ FSWatcher.prototype.unwatch = function(paths) {
531
539
} else {
532
540
//convert to absolute path
533
541
path = sysPath . resolve ( path ) ;
534
-
542
+
535
543
this . _ignoredPaths [ path ] = true ;
536
544
if ( path in this . _watched ) {
537
545
this . _ignoredPaths [ path + '/**/*' ] = true ;
0 commit comments