nodepp icon indicating copy to clipboard operation
nodepp copied to clipboard

Clear Timer within Timer callback

Open sallmann opened this issue 1 year ago • 1 comments

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);
}

sallmann avatar Nov 21 '25 12:11 sallmann

Ok, look, coBegin, coEnd, CoFinish, ..., are just a fancy way to return -1, 1 or 0, ( Well, not too fancy, they manage coroutine's states too ); Now, timer::timeout() and timer::interval, weren't been designed to be canceled within it's callback function. that is why I've added timer::add() callback. since timer::add() accept coroutines and coroutines are just a function_t<int,T...>, you can return:

  • -1: to remove the task from the EV loop.
  • 0: to block the EV Loop until that task ends.
  • 1: to schedule the task and run the next task.

all without fancy macros. So, in order to cancel your timer, and keep stuffs ordered, just:


ptr_t<int> interval = new int( 10 ); // <- ptr_t is used to share memory across tasks.

// create your timer
timer::add( [=]() -> int {

     if( !running ){ return -1; }

     while( *interval > 0 ){
          console::log( "interval: ", *interval );
          *interval -= 1; 
     return  1; } // <- return 1 to execute the next task
     return -1;   // <- Remove this task from the EV Loop

}, time );

also, you can create async timers within coroutines by using coDelay

process::add( coroutine::add( COROUTINE(){
     if( !running ){ return -1; }
coBegin

      while( *interval > 0 ){ coDelay( 1000 ); 
          console::log( "interval: ", *interval );
     *interval -= 1; }

coFinish
}));

EDBCREPO avatar Nov 21 '25 16:11 EDBCREPO