Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Clean up IOWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jan 28, 2010
1 parent bf803f4 commit e82893d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 28 additions & 22 deletions src/node_io_watcher.cc
Expand Up @@ -13,6 +13,7 @@ using namespace v8;
Persistent<FunctionTemplate> IOWatcher::constructor_template;
Persistent<String> callback_symbol;


void IOWatcher::Initialize(Handle<Object> target) {
HandleScope scope;

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/node_io_watcher.h
Expand Up @@ -20,7 +20,7 @@ class IOWatcher : ObjectWrap {
}

~IOWatcher() {
Stop();
ev_io_stop(EV_DEFAULT_UC_ &watcher_);
assert(!ev_is_active(&watcher_));
assert(!ev_is_pending(&watcher_));
}
Expand Down

0 comments on commit e82893d

Please sign in to comment.