-
-
Notifications
You must be signed in to change notification settings - Fork 15
Closed
Description
From the examples I see there is a way of clearing timers previously started with
timer::clear(A);
But I do not see a way stopping a timer from within the callback, which I think would be a good way of keeping code compact and ordered.
Here is a working patch implementing a possible solution:
diff --git a/include/nodepp/timer.h b/include/nodepp/timer.h
index 2f4a67a..e6f13a7 100644
--- a/include/nodepp/timer.h
+++ b/include/nodepp/timer.h
@@ -16,6 +16,9 @@
#include "generator.h"
+#include <type_traits>
+#include <utility>
+
/*────────────────────────────────────────────────────────────────────────────*/
namespace nodepp { namespace timer {
@@ -46,14 +49,35 @@ namespace nodepp { namespace timer {
/*─······································································─*/
- template< class V, class... T >
- void* interval ( V func, ulong* time, const T&... args ){
- return timer::add([=]( T... args ){ func(args...); return 1; }, time, args... );
+ template<class V, class... T>
+ using result_t = std::invoke_result_t<V, T...>;
+
+ template<class V, class... T>
+ std::enable_if_t<std::is_void_v<result_t<V, T...>>, void*>
+ interval(V func, ulong* time, const T&... args)
+ {
+ return timer::add([=](T... a){ func(a...); return 1; }, time, args...);
}
- template< class V, class... T >
- void* interval( V func, ulong time, const T&... args ){
- return timer::add([=]( T... args ){ func(args...); return 1; }, time, args... );
+ template<class V, class... T>
+ std::enable_if_t<std::is_void_v<result_t<V, T...>>, void*>
+ interval(V func, ulong time, const T&... args)
+ {
+ return timer::add([=](T... a){ func(a...); return 1; }, time, args...);
+ }
+
+ template<class V, class... T>
+ std::enable_if_t<!std::is_void_v<result_t<V, T...>>, void*>
+ interval(V func, ulong* time, const T&... args)
+ {
+ return timer::add([=](T... a){ return func(a...); }, time, args...);
+ }
+
+ template<class V, class... T>
+ std::enable_if_t<!std::is_void_v<result_t<V, T...>>, void*>
+ interval(V func, ulong time, const T&... args)
+ {
+ return timer::add([=](T... a){ return func(a...); }, time, args...);
}
/*─······································································─*/
#include <nodepp/nodepp.h>
#include <nodepp/timer.h>
using namespace nodepp;
bool running = true;
void onMain(){
// add: a Coroutine Based Timer
[[maybe_unused]] auto C = timer::add( coroutine::add( COROUTINE() {
// static uchar x = 100;
static uchar x = 4;
coBegin
while( x-->0 ){
console::log(" timer task ");
coNext;
}
running = false;
coFinish
}) ,1000);
// Interval: a setInterval equivalent
[[maybe_unused]] auto A = timer::interval([=](){
console::done("set interval: ", running);
if(!running) return -1;
return 1;
},1000);
// Timeout: a setTimeout equivalent
[[maybe_unused]] auto B = timer::timeout([=](){
console::error("set timeout");
},1000);
// Turn off an specific timer
// timer::clear(A);
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels