Skip to content

Commit e72b072

Browse files
committed
Decouple timer from EventEmitter
1 parent 6d60d2d commit e72b072

3 files changed

Lines changed: 42 additions & 21 deletions

File tree

src/node.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,35 +246,33 @@ function addTimerListener (callback) {
246246
// Special case the no param case to avoid the extra object creation.
247247
if (arguments.length > 2) {
248248
var args = Array.prototype.slice.call(arguments, 2);
249-
timer.addListener("timeout", function(){
250-
callback.apply(timer, args);
251-
});
249+
timer.callback = function () { callback.apply(timer, args); };
252250
} else {
253-
timer.addListener("timeout", callback);
251+
timer.callback = callback;
254252
}
255253
}
256254

257-
GLOBAL.setTimeout = function (callback, after) {
255+
global.setTimeout = function (callback, after) {
258256
var timer = new process.Timer();
259257
addTimerListener.apply(timer, arguments);
260258
timer.start(after, 0);
261259
return timer;
262260
};
263261

264-
GLOBAL.setInterval = function (callback, repeat) {
262+
global.setInterval = function (callback, repeat) {
265263
var timer = new process.Timer();
266264
addTimerListener.apply(timer, arguments);
267265
timer.start(repeat, repeat);
268266
return timer;
269267
};
270268

271-
GLOBAL.clearTimeout = function (timer) {
269+
global.clearTimeout = function (timer) {
272270
if (timer instanceof process.Timer) {
273271
timer.stop();
274272
}
275273
};
276274

277-
GLOBAL.clearInterval = GLOBAL.clearTimeout;
275+
global.clearInterval = global.clearTimeout;
278276

279277

280278

src/node_timer.cc

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Persistent<FunctionTemplate> Timer::constructor_template;
99

1010
static Persistent<String> timeout_symbol;
1111
static Persistent<String> repeat_symbol;
12+
static Persistent<String> callback_symbol;
1213

1314
void
1415
Timer::Initialize (Handle<Object> target)
@@ -17,12 +18,12 @@ Timer::Initialize (Handle<Object> target)
1718

1819
Local<FunctionTemplate> t = FunctionTemplate::New(Timer::New);
1920
constructor_template = Persistent<FunctionTemplate>::New(t);
20-
constructor_template->Inherit(EventEmitter::constructor_template);
2121
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
2222
constructor_template->SetClassName(String::NewSymbol("Timer"));
2323

2424
timeout_symbol = NODE_PSYMBOL("timeout");
2525
repeat_symbol = NODE_PSYMBOL("repeat");
26+
callback_symbol = NODE_PSYMBOL("callback");
2627

2728
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", Timer::Start);
2829
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", Timer::Stop);
@@ -66,7 +67,23 @@ Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
6667

6768
assert(revents == EV_TIMEOUT);
6869

69-
timer->Emit(timeout_symbol, 0, NULL);
70+
HandleScope scope;
71+
72+
Local<Value> callback_v = timer->handle_->Get(callback_symbol);
73+
if (!callback_v->IsFunction()) {
74+
timer->Stop();
75+
return;
76+
}
77+
78+
Local<Function> callback = Local<Function>::Cast(callback_v);
79+
80+
TryCatch try_catch;
81+
82+
callback->Call(timer->handle_, 0, NULL);
83+
84+
if (try_catch.HasCaught()) {
85+
FatalException(try_catch);
86+
}
7087

7188
if (timer->watcher_.repeat == 0) timer->Unref();
7289
}
@@ -90,8 +107,8 @@ Timer::New (const Arguments& args)
90107
Handle<Value>
91108
Timer::Start (const Arguments& args)
92109
{
93-
Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
94110
HandleScope scope;
111+
Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
95112

96113
if (args.Length() != 2)
97114
return ThrowException(String::New("Bad arguments"));
@@ -108,13 +125,18 @@ Timer::Start (const Arguments& args)
108125
return Undefined();
109126
}
110127

111-
Handle<Value>
112-
Timer::Stop (const Arguments& args)
113-
{
128+
129+
Handle<Value> Timer::Stop(const Arguments& args) {
130+
HandleScope scope;
114131
Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
115-
if (ev_is_active(&timer->watcher_)) {
116-
ev_timer_stop(EV_DEFAULT_UC_ &timer->watcher_);
117-
timer->Unref();
118-
}
132+
timer->Stop();
119133
return Undefined();
120134
}
135+
136+
137+
void Timer::Stop () {
138+
if (watcher_.active) {
139+
ev_timer_stop(EV_DEFAULT_UC_ &watcher_);
140+
Unref();
141+
}
142+
}

src/node_timer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
#define node_timer_h
33

44
#include <node.h>
5-
#include <node_events.h>
5+
#include <node_object_wrap.h>
66
#include <v8.h>
77
#include <ev.h>
88

99
namespace node {
1010

11-
class Timer : EventEmitter {
11+
class Timer : ObjectWrap {
1212
public:
1313
static void Initialize (v8::Handle<v8::Object> target);
1414

1515
protected:
1616
static v8::Persistent<v8::FunctionTemplate> constructor_template;
1717

18-
Timer () : EventEmitter () { }
18+
Timer () : ObjectWrap () { }
1919
~Timer();
2020

2121
static v8::Handle<v8::Value> New (const v8::Arguments& args);
@@ -26,6 +26,7 @@ class Timer : EventEmitter {
2626

2727
private:
2828
static void OnTimeout (EV_P_ ev_timer *watcher, int revents);
29+
void Stop ();
2930
ev_timer watcher_;
3031
};
3132

0 commit comments

Comments
 (0)