It would be nice if I could just ask V8 or Libuv or something else
You cannot directly ask libuv, but it certainly offers a way to find out how many active timers exist.
To do this, you can call uv_walk
with a valid loop to get all active descriptors. You can then check each descriptor with the given callback and count those for which the type
data element (which has the uv_handle_type
type) is equal to UV_TIMER
.
The result is the number of active timers.
See the descriptor data structure for more details.
As a trivial example, consider the following structure:
struct Counter { static int count; static void callback(uv_handle_t* handle, void*) { if(handle.type == uv_handle_type::UV_TIMER) count++; } };
You can use it as follows:
Counter::count = 0; uv_walk(my_loop_ptr, &Counter::callback);
Of course, this is not production-ready code. In any case, I hope this gives an idea of ββthe proposed solution.
See here for libuv documentation.
source share