The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / timers.cc @ 90fc8d36
History | View | Annotate | Download (3.44 KB)
1 |
#include "node.h" |
---|---|
2 |
#include "timers.h" |
3 |
#include <assert.h> |
4 |
|
5 |
using namespace v8; |
6 |
|
7 |
static Persistent<ObjectTemplate> timer_template;
|
8 |
|
9 |
class Timer { |
10 |
public:
|
11 |
Timer(Handle<Function> callback, ev_tstamp after, ev_tstamp repeat); |
12 |
~Timer(); |
13 |
|
14 |
static Handle<Value> setTimeout (const Arguments& args); |
15 |
static Handle<Value> setInterval (const Arguments& args); |
16 |
static Handle<Value> clearTimeout (const Arguments& args); |
17 |
|
18 |
Persistent<Object> handle_; |
19 |
|
20 |
private:
|
21 |
static Timer* Unwrap (Handle<Object> handle);
|
22 |
static void OnTimeout (EV_P_ ev_timer *watcher, int revents); |
23 |
ev_timer watcher_; |
24 |
}; |
25 |
|
26 |
Timer* |
27 |
Timer::Unwrap (Handle<Object> handle) |
28 |
{ |
29 |
HandleScope scope; |
30 |
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
|
31 |
Timer* timer = static_cast<Timer*>(field->Value());
|
32 |
return timer;
|
33 |
} |
34 |
|
35 |
void
|
36 |
Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
|
37 |
{ |
38 |
Timer *timer = static_cast<Timer*>(watcher->data);
|
39 |
|
40 |
HandleScope scope; |
41 |
|
42 |
Local<Value> callback_value = timer->handle_->Get(String::NewSymbol("callback"));
|
43 |
if (!callback_value->IsFunction())
|
44 |
return;
|
45 |
|
46 |
Local<Function> callback = Local<Function>::Cast(callback_value); |
47 |
|
48 |
TryCatch try_catch; |
49 |
callback->Call (Context::GetCurrent()->Global(), 0, NULL); |
50 |
if(try_catch.HasCaught())
|
51 |
node_fatal_exception(try_catch); |
52 |
|
53 |
// use ev_is_active instead?
|
54 |
if(watcher->repeat == 0.) |
55 |
delete timer;
|
56 |
} |
57 |
|
58 |
Timer::Timer (Handle<Function> callback, ev_tstamp after, ev_tstamp repeat) |
59 |
{ |
60 |
HandleScope scope; |
61 |
|
62 |
handle_ = Persistent<Object>::New(timer_template->NewInstance()); |
63 |
handle_->Set(String::NewSymbol("callback"), callback);
|
64 |
|
65 |
Local<External> external = External::New(this);
|
66 |
handle_->SetInternalField(0, external);
|
67 |
|
68 |
ev_timer_init(&watcher_, Timer::OnTimeout, after, repeat); |
69 |
watcher_.data = this;
|
70 |
|
71 |
ev_timer_start(EV_DEFAULT_UC_ &watcher_); |
72 |
} |
73 |
|
74 |
Timer::~Timer () |
75 |
{ |
76 |
ev_timer_stop (EV_DEFAULT_UC_ &watcher_); |
77 |
handle_->SetInternalField(0, Undefined());
|
78 |
handle_.Dispose(); |
79 |
} |
80 |
|
81 |
Handle<Value> |
82 |
Timer::setTimeout (const Arguments& args)
|
83 |
{ |
84 |
if (args.Length() < 2) |
85 |
return Undefined();
|
86 |
|
87 |
HandleScope scope; |
88 |
|
89 |
Local<Function> callback = Local<Function>::Cast(args[0]);
|
90 |
int delay = args[1]->IntegerValue(); |
91 |
|
92 |
ev_tstamp after = (double)delay / 1000.0; |
93 |
|
94 |
if (args.Length() > 2) |
95 |
assert(0 && "extra params to setTimeout not yet implemented."); |
96 |
|
97 |
Timer *timer = new Timer(callback, after, 0.0); |
98 |
|
99 |
return scope.Close(timer->handle_);
|
100 |
} |
101 |
|
102 |
Handle<Value> |
103 |
Timer::setInterval (const Arguments& args)
|
104 |
{ |
105 |
if (args.Length() < 2) |
106 |
return Undefined();
|
107 |
|
108 |
HandleScope scope; |
109 |
|
110 |
Local<Function> callback = Local<Function>::Cast(args[0]);
|
111 |
int delay = args[1]->IntegerValue(); |
112 |
|
113 |
ev_tstamp after = (double)delay / 1000.0; |
114 |
|
115 |
Timer *timer = new Timer(callback, after, after);
|
116 |
|
117 |
return scope.Close(timer->handle_);
|
118 |
} |
119 |
|
120 |
Handle<Value> |
121 |
Timer::clearTimeout (const Arguments& args)
|
122 |
{ |
123 |
if (args.Length() < 1) |
124 |
return Undefined();
|
125 |
|
126 |
HandleScope scope; |
127 |
Local<Object> handle = Local<Object>::Cast(args[0]);
|
128 |
Timer *timer = Timer::Unwrap(handle); |
129 |
delete timer;
|
130 |
|
131 |
return Undefined();
|
132 |
} |
133 |
|
134 |
void
|
135 |
NodeInit_timers (Handle<Object> target) |
136 |
{ |
137 |
HandleScope scope; |
138 |
|
139 |
timer_template = Persistent<ObjectTemplate>::New(ObjectTemplate::New()); |
140 |
timer_template->SetInternalFieldCount(1);
|
141 |
|
142 |
NODE_SET_METHOD(target, "setTimeout", Timer::setTimeout);
|
143 |
NODE_SET_METHOD(target, "setInterval", Timer::setInterval);
|
144 |
NODE_SET_METHOD(target, "clearTimeout", Timer::clearTimeout);
|
145 |
NODE_SET_METHOD(target, "clearInterval", Timer::clearTimeout);
|
146 |
} |