forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_idle_watcher.cc
125 lines (85 loc) · 2.8 KB
/
node_idle_watcher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2009 Ryan Dahl <[email protected]>
#include <node_idle_watcher.h>
#include <node.h>
#include <v8.h>
#include <assert.h>
namespace node {
using namespace v8;
Persistent<FunctionTemplate> IdleWatcher::constructor_template;
static 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) {
if (!args.IsConstructCall()) {
return FromConstructorTemplate(constructor_template, 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());
idle->Start();
return Undefined();
}
void IdleWatcher::Start () {
if (!watcher_.active) {
ev_idle_start(EV_DEFAULT_UC_ &watcher_);
Ref();
}
}
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