Skip to content

Commit 327bda1

Browse files
committed
Code style/formatting tweaks on awaitWriteFinish
1 parent 2fdc7cc commit 327bda1

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

index.js

+34-26
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ function FSWatcher(_opts) {
7474
if (undef('followSymlinks')) opts.followSymlinks = true;
7575

7676
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;
8282

8383
this._pendingWrites = Object.create(null);
8484
}
@@ -122,7 +122,8 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
122122
else if (val2 !== undefined) args.push(val1, val2);
123123
else if (val1 !== undefined) args.push(val1);
124124

125-
if ((this.options.awaitWriteFinish && this._pendingWrites[path])) return this;
125+
var awf = this.options.awaitWriteFinish;
126+
if (awf && this._pendingWrites[path]) return this;
126127

127128
if (this.options.atomic) {
128129
if (event === 'unlink') {
@@ -141,7 +142,6 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
141142
}
142143
}
143144

144-
145145
if (event === 'change') {
146146
if (!this._throttle('change', path, 50)) return this;
147147
}
@@ -151,16 +151,16 @@ FSWatcher.prototype._emit = function(event, path, val1, val2, val3) {
151151
if (event !== 'error') this.emit.apply(this, ['all'].concat(args));
152152
}.bind(this);
153153

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) {
157157
event = args[0] = 'error';
158158
args[1] = err;
159159
emitEvent();
160160
} else if(stats) {
161161
// if stats doesn't exist the file must have been deleted
162162
args.push(stats);
163-
emitEvent();
163+
emitEvent();
164164
}
165165
});
166166
} else if (
@@ -223,25 +223,26 @@ FSWatcher.prototype._throttle = function(action, path, timeout) {
223223
// Private method: Awaits write operation to finish
224224
//
225225
// * 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.
230231
FSWatcher.prototype._awaitWriteFinish = function(path, threshold, callback) {
231232
var timeoutHandler;
232233

233-
var awaitWriteFinish = function(prevStat) {
234+
(function awaitWriteFinish (prevStat) {
234235
fs.stat(path, function(err, curStat) {
235236
if(err) {
236237
// if the file have been erased, the file entry in _pendingWrites will
237238
// be deleted in the unlink event.
238239
if(err.code == 'ENOENT') return;
239240

240241
return callback(err);
241-
}
242-
242+
}
243+
243244
var now = new Date();
244-
if(this._pendingWrites[path] === undefined) {
245+
if (this._pendingWrites[path] === undefined) {
245246
this._pendingWrites[path] = {
246247
creationTime: now,
247248
cancelWait: function() {
@@ -250,19 +251,26 @@ FSWatcher.prototype._awaitWriteFinish = function(path, threshold, callback) {
250251
return callback();
251252
}.bind(this)
252253
}
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+
);
254258
}
255259

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+
) {
257264
delete this._pendingWrites[path];
258265
callback(null, curStat);
259266
} 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+
);
261271
}
262272
}.bind(this));
263-
}.bind(this);
264-
265-
awaitWriteFinish();
273+
}.bind(this))();
266274
}
267275

268276
// Private method: Determines whether user has asked to ignore this path
@@ -531,7 +539,7 @@ FSWatcher.prototype.unwatch = function(paths) {
531539
} else {
532540
//convert to absolute path
533541
path = sysPath.resolve(path);
534-
542+
535543
this._ignoredPaths[path] = true;
536544
if (path in this._watched) {
537545
this._ignoredPaths[path + '/**/*'] = true;

0 commit comments

Comments
 (0)