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 <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
Reviewed-By: David Carlier <[email protected]>
  • Loading branch information
addaleax authored and targos committed May 1, 2021
1 parent 8e1e3b5 commit b8bccc3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 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
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 b8bccc3

Please sign in to comment.