diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-01-27 16:07:15 -0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-01-27 16:07:15 -0800 |
commit | e82893d3f26e2ecb21a4108ada57f4170d382156 (patch) | |
tree | 850267a8bee2df3645dd4cf4961d991ba7928c87 /src/node_io_watcher.cc | |
parent | bf803f478bfdee522adf42b5c1207deb75cdb7dc (diff) | |
download | node-e82893d3f26e2ecb21a4108ada57f4170d382156.tar.gz |
Clean up IOWatcher
Diffstat (limited to 'src/node_io_watcher.cc')
-rw-r--r-- | src/node_io_watcher.cc | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/src/node_io_watcher.cc b/src/node_io_watcher.cc index 4a2d9378d..9b5c3b3ff 100644 --- a/src/node_io_watcher.cc +++ b/src/node_io_watcher.cc @@ -13,6 +13,7 @@ using namespace v8; Persistent<FunctionTemplate> IOWatcher::constructor_template; Persistent<String> callback_symbol; + void IOWatcher::Initialize(Handle<Object> target) { HandleScope scope; @@ -50,7 +51,9 @@ void IOWatcher::Callback(EV_P_ ev_io *w, int revents) { argv[0] = Local<Value>::New(revents & EV_READ ? True() : False()); argv[1] = Local<Value>::New(revents & EV_WRITE ? True() : False()); + io->Ref(); callback->Call(io->handle_, 2, argv); + io->Unref(); if (try_catch.HasCaught()) { FatalException(try_catch); @@ -59,34 +62,51 @@ void IOWatcher::Callback(EV_P_ ev_io *w, int revents) { // -// var io = new process.IOWatcher(function (readable, writable) { -// -// }); +// var io = new process.IOWatcher(); +// process.callback = function (readable, writable) { ... }; // io.set(fd, true, false); // io.start(); // Handle<Value> IOWatcher::New(const Arguments& args) { HandleScope scope; - IOWatcher *s = new IOWatcher(); s->Wrap(args.This()); - return args.This(); } Handle<Value> IOWatcher::Start(const Arguments& args) { HandleScope scope; + IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder()); + io->Start(); + return Undefined(); +} + +Handle<Value> IOWatcher::Stop(const Arguments& args) { + HandleScope scope; IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder()); + io->Stop(); + return Undefined(); +} - ev_io_start(EV_DEFAULT_UC_ &io->watcher_); - io->Ref(); +void IOWatcher::Start() { + if (!ev_is_active(&watcher_)) { + ev_io_start(EV_DEFAULT_UC_ &watcher_); + Ref(); + } +} - return Undefined(); + +void IOWatcher::Stop() { + if (ev_is_active(&watcher_)) { + ev_io_stop(EV_DEFAULT_UC_ &watcher_); + Unref(); + } } + Handle<Value> IOWatcher::Set(const Arguments& args) { HandleScope scope; @@ -120,20 +140,6 @@ Handle<Value> IOWatcher::Set(const Arguments& args) { return Undefined(); } -Handle<Value> IOWatcher::Stop(const Arguments& args) { - HandleScope scope; - IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder()); - io->Stop(); - return Undefined(); -} - - -void IOWatcher::Stop () { - if (watcher_.active) { - ev_io_stop(EV_DEFAULT_UC_ &watcher_); - Unref(); - } -} } // namespace node |