summaryrefslogtreecommitdiff
path: root/src/timers.cc
blob: 88154ed81dcf46005b839e8f9235b0b8bbd32f51 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "node.h"
#include "timers.h"
#include <assert.h>

using namespace v8;

class Timer {
 public:
  Timer(Handle<Function> callback, int argc, Handle<Value> argv[], ev_tstamp after, ev_tstamp repeat);
  ~Timer();
  Local<External> CreateTimeoutID ();
  void CallCallback ();
 private:
  ev_timer watcher;
  Persistent<External> timeoutID;
  Persistent<Function> callback;
  int argc;
  Persistent<Value> argv[];
};

static void
onTimeout (struct ev_loop *loop, ev_timer *watcher, int revents)
{
  Timer *timer = static_cast<Timer*>(watcher->data);

  timer->CallCallback();

  // use ev_is_active instead?
  if(watcher->repeat == 0.) 
    delete timer;
}

Timer::Timer (Handle<Function> _callback, int _argc, Handle<Value> _argv[], ev_tstamp after, ev_tstamp repeat)
{
  HandleScope scope;
  callback = Persistent<Function>::New(_callback);
  argc = _argc;

  ev_timer_init (&watcher, onTimeout, after, repeat);
  watcher.data = this;
  ev_timer_start (node_loop(), &watcher);
}

Timer::~Timer ()
{
  ev_timer_stop (node_loop(), &watcher);

  callback.Dispose();

  timeoutID.Dispose();
  timeoutID.Clear();
}

void
Timer::CallCallback ()
{
  HandleScope scope;

  TryCatch try_catch;

  callback->Call (Context::GetCurrent()->Global(), argc, argv);

  if(try_catch.HasCaught())
    node_fatal_exception(try_catch);
}

Local<External>
Timer::CreateTimeoutID ()
{
  HandleScope scope;

  Local<External> timeoutID_local = External::New(this);

  timeoutID = Persistent<External>::New(timeoutID_local);

  return scope.Close(timeoutID_local);
}

static Timer *
UnwrapTimeoutID (Handle<External> timeoutID)
{
  HandleScope scope;

  Timer *timer = static_cast<Timer*>(timeoutID->Value());

  return timer;
}

// timeoutID = setTimeout(func, delay, [param1, param2, ...]);
// timeoutID = setTimeout(code, delay);
// 
// * timeoutID is the ID of the timeout, which can be used with
//   clearTimeout.
//
// * func is the function you want to execute after delay milliseconds.
//
// * code in the alternate syntax, is a string of code you want to execute
//   after delay milliseconds. (not recommended)
//
// * delay is the number of milliseconds (thousandths of a second) that the
//   function call should be delayed by. 
NODE_METHOD(setTimeout)
{
  if (args.Length() < 2)
    return Undefined();

  HandleScope scope;

  Local<Function> callback = Local<Function>::Cast(args[0]);
  int delay = args[1]->IntegerValue();

  ev_tstamp after = (double)delay / 1000.0;

  if (args.Length() > 2)
    assert(0 && "extra params to setTimeout not yet implemented.");
  int argc = 0;
  Handle<Value> argv[] = {};
  /*
  int argc = args.Length() - 2;
  Handle<Value> argv[] = new Handle<Value>[argc];
  // the rest of the arguments, if any arg parameters for the callback
  for(int i = 2; i < args.Length(); i++)
    argv[i - 2] = args[i];
  */

  Timer *timer = new Timer(callback, argc, argv, after, 0.0);

  Local<External> timeoutID = timer->CreateTimeoutID();

  return scope.Close(timeoutID);
}

// clearTimeout(timeoutID)
NODE_METHOD(clearTimeout)
{
  if (args.Length() < 1)
    return Undefined();

  Handle<External> timeoutID = Handle<External>::Cast(args[0]);
  Timer *timer = UnwrapTimeoutID(timeoutID);

  delete timer;

  return Undefined();
}

// intervalID = setInterval(func, delay[, param1, param2, ...]);
// intervalID = setInterval(code, delay);
// 
// where
// 
// * intervalID is a unique interval ID you can pass to clearInterval().
//
// * func is the function you want to be called repeatedly.
//
// * code in the alternate syntax, is a string of code you want to be executed
//   repeatedly.
//
// * delay is the number of milliseconds (thousandths of a second) that the
//   setInterval() function should wait before each call to func.
NODE_METHOD(setInterval) 
{
  if (args.Length() < 2)
    return Undefined();

  HandleScope scope;

  Local<Function> callback = Local<Function>::Cast(args[0]);
  int delay = args[1]->IntegerValue();

  ev_tstamp after = (double)delay / 1000.0;

  if (args.Length() > 2)
    assert(0 && "extra params to setInterval not yet implemented.");
  int argc = 0;
  Handle<Value> argv[] = {};

  Timer *timer = new Timer(callback, argc, argv, after, after);

  Local<External> timeoutID = timer->CreateTimeoutID();

  return scope.Close(timeoutID);
}

void
NodeInit_timers (Handle<Object> target)
{
  HandleScope scope;

  NODE_SET_METHOD(target, "setTimeout", setTimeout);
  NODE_SET_METHOD(target, "clearTimeout", clearTimeout);
  NODE_SET_METHOD(target, "setInterval", setInterval);
  NODE_SET_METHOD(target, "clearInterval", clearTimeout);
}