Browse Source

Move Timer class into node namespace.

v0.7.4-release
Ryan 16 years ago
parent
commit
f6c955b7f4
  1. 4
      src/main.js
  2. 2
      src/node.cc
  3. 34
      src/timer.cc
  4. 22
      src/timer.h

4
src/main.js

@ -35,13 +35,13 @@ node.path = new function () {
// Timers // Timers
function setTimeout (callback, delay) { function setTimeout (callback, delay) {
var timer = new Timer(callback, delay, 0); var timer = new node.Timer(callback, delay, 0);
timer.start(); timer.start();
return timer; return timer;
}; };
function setInterval (callback, delay) { function setInterval (callback, delay) {
var timer = new Timer(callback, delay, delay); var timer = new node.Timer(callback, delay, delay);
timer.start(); timer.start();
return timer; return timer;
}; };

2
src/node.cc

@ -266,7 +266,7 @@ main (int argc, char *argv[])
// BUILT-IN MODULES // BUILT-IN MODULES
node::Init_timer(g); Timer::Initialize(node);
File::Initialize(g); File::Initialize(g);

34
src/timer.cc

@ -5,19 +5,18 @@
using namespace v8; using namespace v8;
using namespace node; using namespace node;
class Timer : ObjectWrap { void
public: Timer::Initialize (Handle<Object> target)
Timer(Handle<Object> handle, Handle<Function> callback, ev_tstamp after, ev_tstamp repeat); {
~Timer(); HandleScope scope;
static Handle<Value> New (const Arguments& args); Local<FunctionTemplate> timer_template = FunctionTemplate::New(Timer::New);
static Handle<Value> Start (const Arguments& args); timer_template->InstanceTemplate()->SetInternalFieldCount(1);
static Handle<Value> Stop (const Arguments& args); target->Set(String::NewSymbol("Timer"), timer_template->GetFunction());
private: NODE_SET_METHOD(timer_template->InstanceTemplate(), "start", Timer::Start);
static void OnTimeout (EV_P_ ev_timer *watcher, int revents); NODE_SET_METHOD(timer_template->InstanceTemplate(), "stop", Timer::Stop);
ev_timer watcher_; }
};
void void
Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents) Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
@ -99,16 +98,3 @@ Timer::Stop (const Arguments& args)
timer->Detach(); timer->Detach();
return Undefined(); return Undefined();
} }
void
node::Init_timer (Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> timer_template = FunctionTemplate::New(Timer::New);
timer_template->InstanceTemplate()->SetInternalFieldCount(1);
target->Set(String::NewSymbol("Timer"), timer_template->GetFunction());
NODE_SET_METHOD(timer_template->InstanceTemplate(), "start", Timer::Start);
NODE_SET_METHOD(timer_template->InstanceTemplate(), "stop", Timer::Stop);
}

22
src/timer.h

@ -1,11 +1,31 @@
#ifndef node_timer_h #ifndef node_timer_h
#define node_timer_h #define node_timer_h
#include "node.h"
#include <v8.h> #include <v8.h>
#include <ev.h>
namespace node { namespace node {
void Init_timer (v8::Handle<v8::Object> target); class Timer : ObjectWrap {
public:
static void Initialize (v8::Handle<v8::Object> target);
protected:
Timer(v8::Handle<v8::Object> handle,
v8::Handle<v8::Function> callback,
ev_tstamp after,
ev_tstamp repeat);
~Timer();
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);
private:
static void OnTimeout (EV_P_ ev_timer *watcher, int revents);
ev_timer watcher_;
};
} // namespace node } // namespace node
#endif // node_timer_h #endif // node_timer_h

Loading…
Cancel
Save