FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_timer.cc at master · BevanR/node · GitHub
BevanR
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node_timer.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
176 lines (127 loc) · 4.13 KB
Breadcrumbs
node
/
src
/
node_timer.cc
Copy path
File metadata and controls
176 lines (127 loc) · 4.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
node.h
>
#
include
<
node_timer.h
>
#
include
<
assert.h
>
using
namespace
v8
;
using
namespace
node
;
Persistent<FunctionTemplate> Timer::constructor_template;
static
Persistent<String> timeout_symbol;
static
Persistent<String> repeat_symbol;
static
Persistent<String> callback_symbol;
void
Timer::Initialize
(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t =
FunctionTemplate::New
(Timer::New);
constructor_template = Persistent<FunctionTemplate>::
New
(t);
constructor_template->
InstanceTemplate
()->
SetInternalFieldCount
(
1
);
constructor_template->
SetClassName
(
String::NewSymbol
(
"
Timer
"
));
timeout_symbol =
NODE_PSYMBOL
(
"
timeout
"
);
repeat_symbol =
NODE_PSYMBOL
(
"
repeat
"
);
callback_symbol =
NODE_PSYMBOL
(
"
callback
"
);
NODE_SET_PROTOTYPE_METHOD
(constructor_template,
"
start
"
, Timer::Start);
NODE_SET_PROTOTYPE_METHOD
(constructor_template,
"
stop
"
, Timer::Stop);
NODE_SET_PROTOTYPE_METHOD
(constructor_template,
"
again
"
, Timer::Again);
constructor_template->
InstanceTemplate
()->
SetAccessor
(repeat_symbol,
RepeatGetter, RepeatSetter);
target->
Set
(
String::NewSymbol
(
"
Timer
"
), constructor_template->
GetFunction
());
}
Handle<Value>
Timer::RepeatGetter
(Local<String> property,
const
AccessorInfo& info)
{
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(info.
This
());
assert
(timer);
assert
(property == repeat_symbol);
Local<Integer> v =
Integer::New
(timer->
watcher_
.
repeat
);
return
scope.
Close
(v);
}
void
Timer::RepeatSetter
(Local<String> property, Local<Value> value,
const
AccessorInfo& info)
{
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(info.
This
());
assert
(timer);
assert
(property == repeat_symbol);
timer->
watcher_
.
repeat
=
NODE_V8_UNIXTIME
(value);
}
void
Timer::OnTimeout
(
EV_P_
ev_timer *watcher,
int
revents)
{
Timer *timer =
static_cast
<Timer*>(watcher->
data
);
assert
(revents ==
EV_TIMEOUT
);
HandleScope scope;
Local<Value> callback_v = timer->
handle_
->
Get
(callback_symbol);
if
(!callback_v->
IsFunction
()) {
timer->
Stop
();
return
;
}
Local<Function> callback = Local<Function>::
Cast
(callback_v);
TryCatch try_catch;
callback->
Call
(timer->
handle_
,
0
,
NULL
);
if
(try_catch.
HasCaught
()) {
FatalException
(try_catch);
}
if
(timer->
watcher_
.
repeat
==
0
) timer->
Unref
();
}
Timer::~Timer
()
{
ev_timer_stop
(
EV_DEFAULT_UC_
&watcher_);
}
Handle<Value>
Timer::New
(
const
Arguments& args)
{
HandleScope scope;
Timer *t =
new
Timer
();
t->
Wrap
(args.
Holder
());
return
args.
This
();
}
Handle<Value>
Timer::Start
(
const
Arguments& args)
{
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(args.
Holder
());
if
(args.
Length
() !=
2
)
return
ThrowException
(
String::New
(
"
Bad arguments
"
));
bool
was_active =
ev_is_active
(&timer->
watcher_
);
ev_tstamp after =
NODE_V8_UNIXTIME
(args[
0
]);
ev_tstamp repeat =
NODE_V8_UNIXTIME
(args[
1
]);
ev_timer_init
(&timer->
watcher_
, Timer::OnTimeout, after, repeat);
timer->
watcher_
.
data
= timer;
//
Update the event loop time. Need to call this because processing JS can
//
take non-negligible amounts of time.
ev_now_update
(
EV_DEFAULT_UC
);
ev_timer_start
(
EV_DEFAULT_UC_
&timer->
watcher_
);
if
(!was_active) timer->
Ref
();
return
Undefined
();
}
Handle<Value>
Timer::Stop
(
const
Arguments& args) {
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(args.
Holder
());
timer->
Stop
();
return
Undefined
();
}
void
Timer::Stop
() {
if
(watcher_.
active
) {
ev_timer_stop
(
EV_DEFAULT_UC_
&watcher_);
Unref
();
}
}
Handle<Value>
Timer::Again
(
const
Arguments& args) {
HandleScope scope;
Timer *timer = ObjectWrap::Unwrap<Timer>(args.
Holder
());
int
was_active =
ev_is_active
(&timer->
watcher_
);
if
(args.
Length
() >
0
) {
ev_tstamp repeat =
NODE_V8_UNIXTIME
(args[
0
]);
if
(repeat >
0
) timer->
watcher_
.
repeat
= repeat;
}
ev_timer_again
(
EV_DEFAULT_UC_
&timer->
watcher_
);
//
ev_timer_again can start or stop the watcher.
//
So we need to check what happened and adjust the ref count
//
appropriately.
if
(
ev_is_active
(&timer->
watcher_
)) {
if
(!was_active) timer->
Ref
();
}
else
{
if
(was_active) timer->
Unref
();
}
return
Undefined
();
}
Back
|
FazBrowse Home
|
New Git URL