From 386afa4c1c3e09c9c6a3a2c017303033dcace834 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Wed, 27 Nov 2024 11:23:02 +0100
Subject: [PATCH] Timer and Hooks now accept func and coro functions

---
 libs/lib/xaal/lib/aioengine.py | 6 +++---
 libs/lib/xaal/lib/core.py      | 8 +++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libs/lib/xaal/lib/aioengine.py b/libs/lib/xaal/lib/aioengine.py
index 400a23ed..f4f3c2bf 100644
--- a/libs/lib/xaal/lib/aioengine.py
+++ b/libs/lib/xaal/lib/aioengine.py
@@ -36,7 +36,7 @@ class HookType(Enum):
 class Hook(object):
     __slots__ = ['type', 'func', 'args', 'kwargs']
 
-    def __init__(self, type_:HookType, func: Any, *args, **kwargs):
+    def __init__(self, type_:HookType, func: core.FuncT, *args, **kwargs):
         # func has to be a callable, but it can be a coroutine or a function
         self.type = type_
         self.func = func
@@ -83,11 +83,11 @@ class AsyncEngine(core.EngineMixin):
     #####################################################
     # Hooks
     #####################################################
-    def on_start(self, func: Any, *args, **kwargs):
+    def on_start(self, func: core.FuncT, *args, **kwargs):
         hook = Hook(HookType.start, func, *args, **kwargs)
         self._hooks.append(hook)
 
-    def on_stop(self, func: Any, *args, **kwargs):
+    def on_stop(self, func: core.FuncT, *args, **kwargs):
         hook = Hook(HookType.stop, func, *args, **kwargs)
         self._hooks.append(hook)
 
diff --git a/libs/lib/xaal/lib/core.py b/libs/lib/xaal/lib/core.py
index ca2d975c..07caaaa4 100644
--- a/libs/lib/xaal/lib/core.py
+++ b/libs/lib/xaal/lib/core.py
@@ -22,7 +22,7 @@ import inspect
 import logging
 import time
 import typing
-from typing import Any, Optional, List, Callable
+from typing import Any, Awaitable, Optional, List, Callable, Union, TypeVar
 
 from .exceptions import EngineError, XAALError
 from .messages import ALIVE_ADDR, MessageAction, MessageFactory, MessageType
@@ -32,6 +32,7 @@ if typing.TYPE_CHECKING:
     from .messages import Message
 
 
+FuncT = TypeVar("FuncT", Callable[[], None], Callable[[], Awaitable[None]])
 logger = logging.getLogger(__name__)
 
 
@@ -39,7 +40,7 @@ logger = logging.getLogger(__name__)
 # Timer class
 #####################################################
 class Timer(object):
-    def __init__(self, func: Any, period: int, counter: int):
+    def __init__(self, func: FuncT, period: int, counter: int):
         # Timer function should a Callable[[],None], but it can be a coroutine too
         self.func = func
         self.period = period
@@ -208,7 +209,8 @@ class EngineMixin(object):
     #####################################################
     # timers
     #####################################################
-    def add_timer(self, func: Any, period: int, counter: int = -1):
+    from typing import Coroutine
+    def add_timer(self, func: FuncT, period: int, counter: int = -1):
         """
         func: function to call
         period: period in second
-- 
GitLab