GeezerVison is showing github.com — lib,src: make `StatWatcher` a `HandleWrap` · nodejs/node@740d9f1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/watchers.js +40-28Lines changed: 40 additions & 28 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,41 +11,42 @@ const { | 11 | 11 | getStatsFromBinding, | 12 | 12 | validatePath | 13 | 13 | } = require('internal/fs/utils'); | | 14 | +const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); | 14 | 15 | const { toNamespacedPath } = require('path'); | 15 | 16 | const { validateUint32 } = require('internal/validators'); | 16 | 17 | const { getPathFromURL } = require('internal/url'); | 17 | 18 | const util = require('util'); | 18 | 19 | const assert = require('assert'); | 19 | 20 |
| | 21 | +const kOldStatus = Symbol('kOldStatus'); | | 22 | +const kUseBigint = Symbol('kUseBigint'); | | 23 | +const kOwner = Symbol('kOwner'); | | 24 | + | 20 | 25 | function emitStop(self) { | 21 | 26 | self.emit('stop'); | 22 | 27 | } | 23 | 28 |
| 24 | 29 | function StatWatcher(bigint) { | 25 | 30 | EventEmitter.call(this); | 26 | 31 |
| 27 | | - this._handle = new _StatWatcher(bigint); | 28 | | - | 29 | | - // uv_fs_poll is a little more powerful than ev_stat but we curb it for | 30 | | - // the sake of backwards compatibility | 31 | | - let oldStatus = -1; | 32 | | - | 33 | | - this._handle.onchange = (newStatus, stats) => { | 34 | | - if (oldStatus === -1 && | 35 | | - newStatus === -1 && | 36 | | - stats[2/* new nlink */] === stats[16/* old nlink */]) return; | 37 | | - | 38 | | - oldStatus = newStatus; | 39 | | - this.emit('change', getStatsFromBinding(stats), | 40 | | - getStatsFromBinding(stats, kFsStatsFieldsLength)); | 41 | | - }; | 42 | | - | 43 | | - this._handle.onstop = () => { | 44 | | - process.nextTick(emitStop, this); | 45 | | - }; | | 32 | + this._handle = null; | | 33 | + this[kOldStatus] = -1; | | 34 | + this[kUseBigint] = bigint; | 46 | 35 | } | 47 | 36 | util.inherits(StatWatcher, EventEmitter); | 48 | 37 |
| | 38 | +function onchange(newStatus, stats) { | | 39 | + const self = this[kOwner]; | | 40 | + if (self[kOldStatus] === -1 && | | 41 | + newStatus === -1 && | | 42 | + stats[2/* new nlink */] === stats[16/* old nlink */]) { | | 43 | + return; | | 44 | + } | | 45 | + | | 46 | + self[kOldStatus] = newStatus; | | 47 | + self.emit('change', getStatsFromBinding(stats), | | 48 | + getStatsFromBinding(stats, kFsStatsFieldsLength)); | | 49 | +} | 49 | 50 |
| 50 | 51 | // FIXME(joyeecheung): this method is not documented. | 51 | 52 | // At the moment if filename is undefined, we | @@ -54,16 +55,23 @@ util.inherits(StatWatcher, EventEmitter); | 54 | 55 | // on a valid filename and the wrap has been initialized | 55 | 56 | // This method is a noop if the watcher has already been started. | 56 | 57 | StatWatcher.prototype.start = function(filename, persistent, interval) { | 57 | | - assert(this._handle instanceof _StatWatcher, 'handle must be a StatWatcher'); | 58 | | - if (this._handle.isActive) { | | 58 | + if (this._handle !== null) | 59 | 59 | return; | 60 | | - } | | 60 | + | | 61 | + this._handle = new _StatWatcher(this[kUseBigint]); | | 62 | + this._handle[kOwner] = this; | | 63 | + this._handle.onchange = onchange; | | 64 | + if (!persistent) | | 65 | + this._handle.unref(); | | 66 | + | | 67 | + // uv_fs_poll is a little more powerful than ev_stat but we curb it for | | 68 | + // the sake of backwards compatibility | | 69 | + this[kOldStatus] = -1; | 61 | 70 |
| 62 | 71 | filename = getPathFromURL(filename); | 63 | 72 | validatePath(filename, 'filename'); | 64 | 73 | validateUint32(interval, 'interval'); | 65 | | - const err = this._handle.start(toNamespacedPath(filename), | 66 | | - persistent, interval); | | 74 | + const err = this._handle.start(toNamespacedPath(filename), interval); | 67 | 75 | if (err) { | 68 | 76 | const error = errors.uvException({ | 69 | 77 | errno: err, | @@ -80,11 +88,15 @@ StatWatcher.prototype.start = function(filename, persistent, interval) { | 80 | 88 | // FSWatcher is .close() | 81 | 89 | // This method is a noop if the watcher has not been started. | 82 | 90 | StatWatcher.prototype.stop = function() { | 83 | | - assert(this._handle instanceof _StatWatcher, 'handle must be a StatWatcher'); | 84 | | - if (!this._handle.isActive) { | | 91 | + if (this._handle === null) | 85 | 92 | return; | 86 | | - } | 87 | | - this._handle.stop(); | | 93 | + | | 94 | + defaultTriggerAsyncIdScope(this._handle.getAsyncId(), | | 95 | + process.nextTick, | | 96 | + emitStop, | | 97 | + this); | | 98 | + this._handle.close(); | | 99 | + this._handle = null; | 88 | 100 | }; | 89 | 101 |
| 90 | 102 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_stat_watcher.cc +21-87Lines changed: 21 additions & 87 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 | namespace node { | 32 | 32 |
| 33 | 33 | using v8::Context; | 34 | | -using v8::DontDelete; | 35 | | -using v8::DontEnum; | 36 | 34 | using v8::FunctionCallbackInfo; | 37 | 35 | using v8::FunctionTemplate; | 38 | 36 | using v8::HandleScope; | 39 | 37 | using v8::Integer; | 40 | 38 | using v8::Local; | 41 | 39 | using v8::Object; | 42 | | -using v8::PropertyAttribute; | 43 | | -using v8::ReadOnly; | 44 | | -using v8::Signature; | 45 | 40 | using v8::String; | 46 | 41 | using v8::Uint32; | 47 | 42 | using v8::Value; | @@ -58,43 +53,32 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) { | 58 | 53 |
| 59 | 54 | AsyncWrap::AddWrapMethods(env, t); | 60 | 55 | env->SetProtoMethod(t, "start", StatWatcher::Start); | 61 | | - env->SetProtoMethod(t, "stop", StatWatcher::Stop); | 62 | | - | 63 | | - Local<FunctionTemplate> is_active_templ = | 64 | | - FunctionTemplate::New(env->isolate(), | 65 | | - IsActive, | 66 | | - env->as_external(), | 67 | | - Signature::New(env->isolate(), t)); | 68 | | - t->PrototypeTemplate()->SetAccessorProperty( | 69 | | - FIXED_ONE_BYTE_STRING(env->isolate(), "isActive"), | 70 | | - is_active_templ, | 71 | | - Local<FunctionTemplate>(), | 72 | | - static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum)); | | 56 | + env->SetProtoMethod(t, "close", HandleWrap::Close); | | 57 | + env->SetProtoMethod(t, "ref", HandleWrap::Ref); | | 58 | + env->SetProtoMethod(t, "unref", HandleWrap::Unref); | | 59 | + env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef); | 73 | 60 |
| 74 | 61 | target->Set(statWatcherString, t->GetFunction()); | 75 | 62 | } | 76 | 63 |
| 77 | 64 |
| 78 | | -StatWatcher::StatWatcher(Environment* env, Local<Object> wrap, bool use_bigint) | 79 | | - : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER), | 80 | | - watcher_(nullptr), | | 65 | +StatWatcher::StatWatcher(Environment* env, | | 66 | + Local<Object> wrap, | | 67 | + bool use_bigint) | | 68 | + : HandleWrap(env, | | 69 | + wrap, | | 70 | + reinterpret_cast<uv_handle_t*>(&watcher_), | | 71 | + AsyncWrap::PROVIDER_STATWATCHER), | 81 | 72 | use_bigint_(use_bigint) { | 82 | | - MakeWeak(); | 83 | | -} | 84 | | - | 85 | | - | 86 | | -StatWatcher::~StatWatcher() { | 87 | | - if (IsActive()) | 88 | | - Stop(); | | 73 | + CHECK_EQ(0, uv_fs_poll_init(env->event_loop(), &watcher_)); | 89 | 74 | } | 90 | 75 |
| 91 | 76 |
| 92 | 77 | void StatWatcher::Callback(uv_fs_poll_t* handle, | 93 | 78 | int status, | 94 | 79 | const uv_stat_t* prev, | 95 | 80 | const uv_stat_t* curr) { | 96 | | - StatWatcher* wrap = static_cast<StatWatcher*>(handle->data); | 97 | | - CHECK_EQ(wrap->watcher_, handle); | | 81 | + StatWatcher* wrap = ContainerOf(&StatWatcher::watcher_, handle); | 98 | 82 | Environment* env = wrap->env(); | 99 | 83 | HandleScope handle_scope(env->isolate()); | 100 | 84 | Context::Scope context_scope(env->context()); | @@ -118,78 +102,28 @@ void StatWatcher::New(const FunctionCallbackInfo<Value>& args) { | 118 | 102 | new StatWatcher(env, args.This(), args[0]->IsTrue()); | 119 | 103 | } | 120 | 104 |
| 121 | | -bool StatWatcher::IsActive() { | 122 | | - return watcher_ != nullptr; | 123 | | -} | 124 | | - | 125 | | -void StatWatcher::IsActive(const v8::FunctionCallbackInfo<v8::Value>& args) { | 126 | | - StatWatcher* wrap = Unwrap<StatWatcher>(args.This()); | 127 | | - CHECK_NOT_NULL(wrap); | 128 | | - args.GetReturnValue().Set(wrap->IsActive()); | 129 | | -} | 130 | | - | 131 | | -// wrap.start(filename, persistent, interval) | | 105 | +// wrap.start(filename, interval) | 132 | 106 | void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) { | 133 | | - CHECK_EQ(args.Length(), 3); | | 107 | + CHECK_EQ(args.Length(), 2); | 134 | 108 |
| 135 | | - StatWatcher* wrap = Unwrap<StatWatcher>(args.Holder()); | 136 | | - CHECK_NOT_NULL(wrap); | 137 | | - if (wrap->IsActive()) { | 138 | | - return; | 139 | | - } | | 109 | + StatWatcher* wrap; | | 110 | + ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); | | 111 | + CHECK(!uv_is_active(wrap->GetHandle())); | 140 | 112 |
| 141 | 113 | const int argc = args.Length(); | 142 | | - CHECK_GE(argc, 3); | 143 | 114 |
| 144 | 115 | node::Utf8Value path(args.GetIsolate(), args[0]); | 145 | 116 | CHECK_NOT_NULL(*path); | 146 | 117 |
| 147 | | - bool persistent = true; | 148 | | - if (args[1]->IsFalse()) { | 149 | | - persistent = false; | 150 | | - } | 151 | | - | 152 | | - CHECK(args[2]->IsUint32()); | 153 | | - const uint32_t interval = args[2].As<Uint32>()->Value(); | 154 | | - | 155 | | - wrap->watcher_ = new uv_fs_poll_t(); | 156 | | - CHECK_EQ(0, uv_fs_poll_init(wrap->env()->event_loop(), wrap->watcher_)); | 157 | | - wrap->watcher_->data = static_cast<void*>(wrap); | 158 | | - // Safe, uv_ref/uv_unref are idempotent. | 159 | | - if (persistent) | 160 | | - uv_ref(reinterpret_cast<uv_handle_t*>(wrap->watcher_)); | 161 | | - else | 162 | | - uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_)); | | 118 | + CHECK(args[1]->IsUint32()); | | 119 | + const uint32_t interval = args[1].As<Uint32>()->Value(); | 163 | 120 |
| 164 | 121 | // Note that uv_fs_poll_start does not return ENOENT, we are handling | 165 | 122 | // mostly memory errors here. | 166 | | - const int err = uv_fs_poll_start(wrap->watcher_, Callback, *path, interval); | | 123 | + const int err = uv_fs_poll_start(&wrap->watcher_, Callback, *path, interval); | 167 | 124 | if (err != 0) { | 168 | 125 | args.GetReturnValue().Set(err); | 169 | 126 | } | 170 | | - wrap->ClearWeak(); | 171 | 127 | } | 172 | 128 |
| 173 | | - | 174 | | -void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) { | 175 | | - StatWatcher* wrap = Unwrap<StatWatcher>(args.Holder()); | 176 | | - CHECK_NOT_NULL(wrap); | 177 | | - if (!wrap->IsActive()) { | 178 | | - return; | 179 | | - } | 180 | | - | 181 | | - Environment* env = wrap->env(); | 182 | | - Context::Scope context_scope(env->context()); | 183 | | - wrap->MakeCallback(env->onstop_string(), 0, nullptr); | 184 | | - wrap->Stop(); | 185 | | -} | 186 | | - | 187 | | - | 188 | | -void StatWatcher::Stop() { | 189 | | - env()->CloseHandle(watcher_, [](uv_fs_poll_t* handle) { delete handle; }); | 190 | | - watcher_ = nullptr; | 191 | | - MakeWeak(); | 192 | | -} | 193 | | - | 194 | | - | 195 | 129 | } // namespace node |
Collapse file Copy file name to clipboardExpand all lines: src/node_stat_watcher.h +6-10Lines changed: 6 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | 26 | 26 |
| 27 | 27 | #include "node.h" | 28 | | -#include "async_wrap.h" | | 28 | +#include "handle_wrap.h" | 29 | 29 | #include "env.h" | 30 | 30 | #include "uv.h" | 31 | 31 | #include "v8.h" | 32 | 32 |
| 33 | 33 | namespace node { | 34 | 34 |
| 35 | | -class StatWatcher : public AsyncWrap { | | 35 | +class StatWatcher : public HandleWrap { | 36 | 36 | public: | 37 | | - ~StatWatcher() override; | 38 | | - | 39 | 37 | static void Initialize(Environment* env, v8::Local<v8::Object> target); | 40 | 38 |
| 41 | 39 | protected: | 42 | | - StatWatcher(Environment* env, v8::Local<v8::Object> wrap, bool use_bigint); | | 40 | + StatWatcher(Environment* env, | | 41 | + v8::Local<v8::Object> wrap, | | 42 | + bool use_bigint); | 43 | 43 |
| 44 | 44 | static void New(const v8::FunctionCallbackInfo<v8::Value>& args); | 45 | 45 | static void Start(const v8::FunctionCallbackInfo<v8::Value>& args); | 46 | | - static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args); | 47 | | - static void IsActive(const v8::FunctionCallbackInfo<v8::Value>& args); | 48 | 46 |
| 49 | 47 | size_t self_size() const override { return sizeof(*this); } | 50 | 48 |
| @@ -53,10 +51,8 @@ class StatWatcher : public AsyncWrap { | 53 | 51 | int status, | 54 | 52 | const uv_stat_t* prev, | 55 | 53 | const uv_stat_t* curr); | 56 | | - void Stop(); | 57 | | - bool IsActive(); | 58 | 54 |
| 59 | | - uv_fs_poll_t* watcher_; | | 55 | + uv_fs_poll_t watcher_; | 60 | 56 | const bool use_bigint_; | 61 | 57 | }; | 62 | 58 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-fs-watch.js -16Lines changed: 0 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,19 +126,3 @@ tmpdir.refresh(); | 126 | 126 | }); | 127 | 127 | oldhandle.close(); // clean up | 128 | 128 | } | 129 | | - | 130 | | -{ | 131 | | - let oldhandle; | 132 | | - assert.throws(() => { | 133 | | - const w = fs.watchFile(__filename, | 134 | | - { persistent: false }, | 135 | | - common.mustNotCall()); | 136 | | - oldhandle = w._handle; | 137 | | - w._handle = { stop: w._handle.stop }; | 138 | | - w.stop(); | 139 | | - }, { | 140 | | - message: 'handle must be a StatWatcher', | 141 | | - code: 'ERR_ASSERTION' | 142 | | - }); | 143 | | - oldhandle.stop(); // clean up | 144 | | -} |
|