mirror of https://github.com/lukechilds/node.git
Ryan Dahl
15 years ago
6 changed files with 181 additions and 0 deletions
@ -0,0 +1,118 @@ |
|||||
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||||
|
#include <node_idle_watcher.h> |
||||
|
|
||||
|
#include <node.h> |
||||
|
#include <v8.h> |
||||
|
|
||||
|
#include <assert.h> |
||||
|
|
||||
|
namespace node { |
||||
|
|
||||
|
using namespace v8; |
||||
|
|
||||
|
Persistent<FunctionTemplate> IdleWatcher::constructor_template; |
||||
|
Persistent<String> callback_symbol; |
||||
|
|
||||
|
void IdleWatcher::Initialize(Handle<Object> target) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
Local<FunctionTemplate> t = FunctionTemplate::New(IdleWatcher::New); |
||||
|
constructor_template = Persistent<FunctionTemplate>::New(t); |
||||
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
||||
|
constructor_template->SetClassName(String::NewSymbol("IdleWatcher")); |
||||
|
|
||||
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", IdleWatcher::Start); |
||||
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", IdleWatcher::Stop); |
||||
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setPriority", |
||||
|
IdleWatcher::SetPriority); |
||||
|
|
||||
|
target->Set(String::NewSymbol("IdleWatcher"), constructor_template->GetFunction()); |
||||
|
|
||||
|
callback_symbol = NODE_PSYMBOL("callback"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Handle<Value> IdleWatcher::SetPriority(const Arguments& args) { |
||||
|
IdleWatcher *idle = ObjectWrap::Unwrap<IdleWatcher>(args.Holder()); |
||||
|
|
||||
|
HandleScope scope; |
||||
|
|
||||
|
int priority = args[0]->Int32Value(); |
||||
|
|
||||
|
ev_set_priority(&idle->watcher_, priority); |
||||
|
|
||||
|
return Undefined(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void IdleWatcher::Callback(EV_P_ ev_idle *w, int revents) { |
||||
|
IdleWatcher *idle = static_cast<IdleWatcher*>(w->data); |
||||
|
|
||||
|
assert(w == &idle->watcher_); |
||||
|
assert(revents == EV_IDLE); |
||||
|
|
||||
|
HandleScope scope; |
||||
|
|
||||
|
Local<Value> callback_v = idle->handle_->Get(callback_symbol); |
||||
|
if (!callback_v->IsFunction()) { |
||||
|
idle->Stop(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Local<Function> callback = Local<Function>::Cast(callback_v); |
||||
|
|
||||
|
TryCatch try_catch; |
||||
|
|
||||
|
callback->Call(idle->handle_, 0, NULL); |
||||
|
|
||||
|
if (try_catch.HasCaught()) { |
||||
|
FatalException(try_catch); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
//
|
||||
|
// var idle = new process.IdleWatcher();
|
||||
|
// idle.callback = function () { /* ... */ };
|
||||
|
// idle.start();
|
||||
|
//
|
||||
|
Handle<Value> IdleWatcher::New(const Arguments& args) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
IdleWatcher *s = new IdleWatcher(); |
||||
|
s->Wrap(args.This()); |
||||
|
|
||||
|
return args.This(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Handle<Value> IdleWatcher::Start(const Arguments& args) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
IdleWatcher *idle = ObjectWrap::Unwrap<IdleWatcher>(args.Holder()); |
||||
|
|
||||
|
ev_idle_start(EV_DEFAULT_UC_ &idle->watcher_); |
||||
|
|
||||
|
idle->Ref(); |
||||
|
|
||||
|
return Undefined(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Handle<Value> IdleWatcher::Stop(const Arguments& args) { |
||||
|
HandleScope scope; |
||||
|
IdleWatcher *idle = ObjectWrap::Unwrap<IdleWatcher>(args.Holder()); |
||||
|
idle->Stop(); |
||||
|
return Undefined(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void IdleWatcher::Stop () { |
||||
|
if (watcher_.active) { |
||||
|
ev_idle_stop(EV_DEFAULT_UC_ &watcher_); |
||||
|
Unref(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} // namespace node
|
@ -0,0 +1,41 @@ |
|||||
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||||
|
#ifndef NODE_IDLE_H_ |
||||
|
#define NODE_IDLE_H_ |
||||
|
|
||||
|
#include <node_object_wrap.h> |
||||
|
#include <ev.h> |
||||
|
|
||||
|
namespace node { |
||||
|
|
||||
|
class IdleWatcher : ObjectWrap { |
||||
|
public: |
||||
|
static void Initialize(v8::Handle<v8::Object> target); |
||||
|
|
||||
|
protected: |
||||
|
static v8::Persistent<v8::FunctionTemplate> constructor_template; |
||||
|
|
||||
|
IdleWatcher() : ObjectWrap() { |
||||
|
ev_idle_init(&watcher_, IdleWatcher::Callback); |
||||
|
watcher_.data = this; |
||||
|
} |
||||
|
|
||||
|
~IdleWatcher() { |
||||
|
ev_idle_stop(EV_DEFAULT_UC_ &watcher_); |
||||
|
} |
||||
|
|
||||
|
static v8::Handle<v8::Value> New(const v8::Arguments& args); |
||||
|
static v8::Handle<v8::Value> Start(const v8::Arguments& args); |
||||
|
static v8::Handle<v8::Value> Stop(const v8::Arguments& args); |
||||
|
static v8::Handle<v8::Value> SetPriority(const v8::Arguments& args); |
||||
|
|
||||
|
private: |
||||
|
static void Callback(EV_P_ ev_idle *watcher, int revents); |
||||
|
|
||||
|
void Stop(); |
||||
|
|
||||
|
ev_idle watcher_; |
||||
|
}; |
||||
|
|
||||
|
} // namespace node
|
||||
|
#endif // NODE_IDLE_H_
|
||||
|
|
@ -0,0 +1,14 @@ |
|||||
|
process.mixin(require("./common")); |
||||
|
|
||||
|
var complete = false; |
||||
|
var idle = new process.IdleWatcher(); |
||||
|
idle.callback = function () { |
||||
|
complete = true; |
||||
|
idle.stop(); |
||||
|
}; |
||||
|
idle.setPriority(process.EVMAXPRI); |
||||
|
idle.start(); |
||||
|
|
||||
|
process.addListener('exit', function () { |
||||
|
assert.ok(complete); |
||||
|
}); |
Loading…
Reference in new issue