1 #include "timer_wrap.h" // NOLINT(build/include_inline)
2 #include "timer_wrap-inl.h"
3
4 #include "env-inl.h"
5 #include "memory_tracker-inl.h"
6 #include "uv.h"
7
8 namespace node {
9
Stop()10 void TimerWrap::Stop() {
11 if (timer_.data == nullptr) return;
12 uv_timer_stop(&timer_);
13 }
14
Close()15 void TimerWrap::Close() {
16 timer_.data = nullptr;
17 env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
18 }
19
TimerClosedCb(uv_handle_t* handle)20 void TimerWrap::TimerClosedCb(uv_handle_t* handle) {
21 std::unique_ptr<TimerWrap> ptr(
22 ContainerOf(&TimerWrap::timer_,
23 reinterpret_cast<uv_timer_t*>(handle)));
24 }
25
Update(uint64_t interval, uint64_t repeat)26 void TimerWrap::Update(uint64_t interval, uint64_t repeat) {
27 if (timer_.data == nullptr) return;
28 uv_timer_start(&timer_, OnTimeout, interval, repeat);
29 }
30
Ref()31 void TimerWrap::Ref() {
32 if (timer_.data == nullptr) return;
33 uv_ref(reinterpret_cast<uv_handle_t*>(&timer_));
34 }
35
Unref()36 void TimerWrap::Unref() {
37 if (timer_.data == nullptr) return;
38 uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
39 }
40
OnTimeout(uv_timer_t* timer)41 void TimerWrap::OnTimeout(uv_timer_t* timer) {
42 TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer);
43 t->fn_();
44 }
45
Stop()46 void TimerWrapHandle::Stop() {
47 if (timer_ != nullptr)
48 return timer_->Stop();
49 }
50
Close()51 void TimerWrapHandle::Close() {
52 if (timer_ != nullptr) {
53 timer_->env()->RemoveCleanupHook(CleanupHook, this);
54 timer_->Close();
55 }
56 timer_ = nullptr;
57 }
58
Ref()59 void TimerWrapHandle::Ref() {
60 if (timer_ != nullptr)
61 timer_->Ref();
62 }
63
Unref()64 void TimerWrapHandle::Unref() {
65 if (timer_ != nullptr)
66 timer_->Unref();
67 }
68
Update(uint64_t interval, uint64_t repeat)69 void TimerWrapHandle::Update(uint64_t interval, uint64_t repeat) {
70 if (timer_ != nullptr)
71 timer_->Update(interval, repeat);
72 }
73
MemoryInfo(MemoryTracker* tracker) const74 void TimerWrapHandle::MemoryInfo(MemoryTracker* tracker) const {
75 if (timer_ != nullptr)
76 tracker->TrackField("timer", *timer_);
77 }
78
CleanupHook(void* data)79 void TimerWrapHandle::CleanupHook(void* data) {
80 static_cast<TimerWrapHandle*>(data)->Close();
81 }
82
83 } // namespace node
84