-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib,src: make StatWatcher
a HandleWrap
#21244
Changes from 2 commits
3602ffc
e017a83
66aa5ca
adfb579
c88680b
a78fda2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,41 +11,44 @@ const { | |
getStatsFromBinding, | ||
validatePath | ||
} = require('internal/fs/utils'); | ||
const { | ||
defaultTriggerAsyncIdScope | ||
} = require('internal/async_hooks'); | ||
const { toNamespacedPath } = require('path'); | ||
const { validateUint32 } = require('internal/validators'); | ||
const { getPathFromURL } = require('internal/url'); | ||
const util = require('util'); | ||
const assert = require('assert'); | ||
|
||
const kOldStatus = Symbol('kOldStatus'); | ||
const kUseBigint = Symbol('kUseBigint'); | ||
const kOwner = Symbol('kOwner'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big deal, but don't we usually use a normal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use a symbol in (most/all) new code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I’d like to transition to using a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea (and probably what would have been done if symbols existed back in the day). |
||
|
||
function emitStop(self) { | ||
self.emit('stop'); | ||
} | ||
|
||
function StatWatcher(bigint) { | ||
EventEmitter.call(this); | ||
|
||
this._handle = new _StatWatcher(bigint); | ||
|
||
// uv_fs_poll is a little more powerful than ev_stat but we curb it for | ||
// the sake of backwards compatibility | ||
let oldStatus = -1; | ||
|
||
this._handle.onchange = (newStatus, stats) => { | ||
if (oldStatus === -1 && | ||
newStatus === -1 && | ||
stats[2/* new nlink */] === stats[16/* old nlink */]) return; | ||
|
||
oldStatus = newStatus; | ||
this.emit('change', getStatsFromBinding(stats), | ||
getStatsFromBinding(stats, kFsStatsFieldsLength)); | ||
}; | ||
|
||
this._handle.onstop = () => { | ||
process.nextTick(emitStop, this); | ||
}; | ||
this._handle = null; | ||
this[kOldStatus] = -1; | ||
this[kUseBigint] = bigint; | ||
} | ||
util.inherits(StatWatcher, EventEmitter); | ||
|
||
function onchange(newStatus, stats) { | ||
const self = this[kOwner]; | ||
if (self[kOldStatus] === -1 && | ||
newStatus === -1 && | ||
stats[2/* new nlink */] === stats[16/* old nlink */]) { | ||
return; | ||
} | ||
|
||
self[kOldStatus] = newStatus; | ||
self.emit('change', getStatsFromBinding(stats), | ||
getStatsFromBinding(stats, kFsStatsFieldsLength)); | ||
} | ||
|
||
// FIXME(joyeecheung): this method is not documented. | ||
// At the moment if filename is undefined, we | ||
|
@@ -54,16 +57,23 @@ util.inherits(StatWatcher, EventEmitter); | |
// on a valid filename and the wrap has been initialized | ||
// This method is a noop if the watcher has already been started. | ||
StatWatcher.prototype.start = function(filename, persistent, interval) { | ||
assert(this._handle instanceof _StatWatcher, 'handle must be a StatWatcher'); | ||
if (this._handle.isActive) { | ||
if (this._handle !== null) | ||
return; | ||
} | ||
|
||
this._handle = new _StatWatcher(this[kUseBigint]); | ||
this._handle[kOwner] = this; | ||
this._handle.onchange = onchange; | ||
if (!persistent) | ||
this._handle.unref(); | ||
|
||
// uv_fs_poll is a little more powerful than ev_stat but we curb it for | ||
// the sake of backwards compatibility | ||
this[kOldStatus] = -1; | ||
|
||
filename = getPathFromURL(filename); | ||
validatePath(filename, 'filename'); | ||
validateUint32(interval, 'interval'); | ||
const err = this._handle.start(toNamespacedPath(filename), | ||
persistent, interval); | ||
const err = this._handle.start(toNamespacedPath(filename), interval); | ||
if (err) { | ||
const error = errors.uvException({ | ||
errno: err, | ||
|
@@ -80,11 +90,15 @@ StatWatcher.prototype.start = function(filename, persistent, interval) { | |
// FSWatcher is .close() | ||
// This method is a noop if the watcher has not been started. | ||
StatWatcher.prototype.stop = function() { | ||
assert(this._handle instanceof _StatWatcher, 'handle must be a StatWatcher'); | ||
if (!this._handle.isActive) { | ||
if (this._handle === null) | ||
return; | ||
} | ||
this._handle.stop(); | ||
|
||
defaultTriggerAsyncIdScope(this._handle.getAsyncId(), | ||
process.nextTick, | ||
emitStop, | ||
this); | ||
this._handle.close(); | ||
this._handle = null; | ||
}; | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't all of this fit on one line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!