@@ -9,6 +9,7 @@ Persistent<FunctionTemplate> Timer::constructor_template;
99
1010static Persistent<String> timeout_symbol;
1111static Persistent<String> repeat_symbol;
12+ static Persistent<String> callback_symbol;
1213
1314void
1415Timer::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)
90107Handle<Value>
91108Timer::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+ }
0 commit comments