Skip to content

Commit

Permalink
src: remove user_data from TimerWrap
Browse files Browse the repository at this point in the history
There’s no point in having an opaque user data pointer when we’re
already using `std::function`.

PR-URL: #34252
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
  • Loading branch information
addaleax committed Jul 14, 2020
1 parent 18667ac commit e2f9dc6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class NodeInspectorClient : public V8InspectorClient {
void* data) override {
auto result =
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
std::make_tuple(env_, callback, data));
std::make_tuple(env_, [=]() { callback(data); }));
CHECK(result.second);
uint64_t interval = 1000 * interval_s;
result.first->second.Update(interval, interval);
Expand Down
4 changes: 2 additions & 2 deletions src/quic/node_quic_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,8 @@ QuicSession::QuicSession(
socket_(socket),
alpn_(alpn),
hostname_(hostname),
idle_(socket->env(), [this](void* data) { OnIdleTimeout(); }),
retransmit_(socket->env(), [this](void* data) { MaybeTimeout(); }),
idle_(socket->env(), [this]() { OnIdleTimeout(); }),
retransmit_(socket->env(), [this]() { MaybeTimeout(); }),
dcid_(dcid),
state_(env()->isolate()),
quic_state_(socket->quic_state()) {
Expand Down
12 changes: 5 additions & 7 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

namespace node {

TimerWrap::TimerWrap(Environment* env, TimerCb fn, void* user_data)
TimerWrap::TimerWrap(Environment* env, const TimerCb& fn)
: env_(env),
fn_(fn),
user_data_(user_data) {
fn_(fn) {
uv_timer_init(env->event_loop(), &timer_);
timer_.data = this;
}
Expand Down Expand Up @@ -45,14 +44,13 @@ void TimerWrap::Unref() {

void TimerWrap::OnTimeout(uv_timer_t* timer) {
TimerWrap* t = ContainerOf(&TimerWrap::timer_, timer);
t->fn_(t->user_data_);
t->fn_();
}

TimerWrapHandle::TimerWrapHandle(
Environment* env,
TimerWrap::TimerCb fn,
void* user_data) {
timer_ = new TimerWrap(env, fn, user_data);
const TimerWrap::TimerCb& fn) {
timer_ = new TimerWrap(env, fn);
env->AddCleanupHook(CleanupHook, this);
}

Expand Down
8 changes: 3 additions & 5 deletions src/timer_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace node {
// Utility class that makes working with libuv timers a bit easier.
class TimerWrap final : public MemoryRetainer {
public:
using TimerCb = std::function<void(void*)>;
using TimerCb = std::function<void()>;

TimerWrap(Environment* env, TimerCb fn, void* user_data);
TimerWrap(Environment* env, const TimerCb& fn);
TimerWrap(const TimerWrap&) = delete;

inline Environment* env() const { return env_; }
Expand All @@ -43,7 +43,6 @@ class TimerWrap final : public MemoryRetainer {
Environment* env_;
TimerCb fn_;
uv_timer_t timer_;
void* user_data_ = nullptr;

friend std::unique_ptr<TimerWrap>::deleter_type;
};
Expand All @@ -52,8 +51,7 @@ class TimerWrapHandle : public MemoryRetainer {
public:
TimerWrapHandle(
Environment* env,
TimerWrap::TimerCb fn,
void* user_data = nullptr);
const TimerWrap::TimerCb& fn);

TimerWrapHandle(const TimerWrapHandle&) = delete;

Expand Down

0 comments on commit e2f9dc6

Please sign in to comment.