Bläddra i källkod

convert timer into an element

Falko Schindler 1 år sedan
förälder
incheckning
0ba11eaabf
2 ändrade filer med 10 tillägg och 14 borttagningar
  1. 1 0
      nicegui/functions/timer.js
  2. 9 14
      nicegui/functions/timer.py

+ 1 - 0
nicegui/functions/timer.js

@@ -0,0 +1 @@
+export default {};

+ 9 - 14
nicegui/functions/timer.py

@@ -4,10 +4,10 @@ from typing import Any, Awaitable, Callable, Optional
 
 
 from .. import background_tasks, globals  # pylint: disable=redefined-builtin
 from .. import background_tasks, globals  # pylint: disable=redefined-builtin
 from ..binding import BindableProperty
 from ..binding import BindableProperty
-from ..slot import Slot
+from ..element import Element
 
 
 
 
-class Timer:
+class Timer(Element, component='timer.js'):
     active = BindableProperty()
     active = BindableProperty()
     interval = BindableProperty()
     interval = BindableProperty()
 
 
@@ -28,10 +28,10 @@ class Timer:
         :param active: whether the callback should be executed or not (can be changed during runtime)
         :param active: whether the callback should be executed or not (can be changed during runtime)
         :param once: whether the callback is only executed once after a delay specified by `interval` (default: `False`)
         :param once: whether the callback is only executed once after a delay specified by `interval` (default: `False`)
         """
         """
+        super().__init__()
         self.interval = interval
         self.interval = interval
         self.callback: Optional[Callable[..., Any]] = callback
         self.callback: Optional[Callable[..., Any]] = callback
         self.active = active
         self.active = active
-        self.slot: Optional[Slot] = globals.get_slot()
         self._is_canceled: bool = False
         self._is_canceled: bool = False
 
 
         coroutine = self._run_once if once else self._run_in_loop
         coroutine = self._run_once if once else self._run_in_loop
@@ -57,8 +57,7 @@ class Timer:
         try:
         try:
             if not await self._connected():
             if not await self._connected():
                 return
                 return
-            assert self.slot is not None
-            with self.slot:
+            with self.parent_slot:
                 await asyncio.sleep(self.interval)
                 await asyncio.sleep(self.interval)
                 if self.active and not self._should_stop():
                 if self.active and not self._should_stop():
                     await self._invoke_callback()
                     await self._invoke_callback()
@@ -69,8 +68,7 @@ class Timer:
         try:
         try:
             if not await self._connected():
             if not await self._connected():
                 return
                 return
-            assert self.slot is not None
-            with self.slot:
+            with self.parent_slot:
                 while not self._should_stop():
                 while not self._should_stop():
                     try:
                     try:
                         start = time.time()
                         start = time.time()
@@ -101,27 +99,24 @@ class Timer:
         See https://github.com/zauberzeug/nicegui/issues/206 for details.
         See https://github.com/zauberzeug/nicegui/issues/206 for details.
         Returns True if the client is connected, False if the client is not connected and the timer should be cancelled.
         Returns True if the client is connected, False if the client is not connected and the timer should be cancelled.
         """
         """
-        assert self.slot is not None
-        if self.slot.parent.client.shared:
+        if self.client.shared:
             return True
             return True
 
 
         # ignore served pages which do not reconnect to backend (e.g. monitoring requests, scrapers etc.)
         # ignore served pages which do not reconnect to backend (e.g. monitoring requests, scrapers etc.)
         try:
         try:
-            await self.slot.parent.client.connected(timeout=timeout)
+            await self.client.connected(timeout=timeout)
             return True
             return True
         except TimeoutError:
         except TimeoutError:
             globals.log.error(f'Timer cancelled because client is not connected after {timeout} seconds')
             globals.log.error(f'Timer cancelled because client is not connected after {timeout} seconds')
             return False
             return False
 
 
     def _should_stop(self) -> bool:
     def _should_stop(self) -> bool:
-        assert self.slot is not None
         return (
         return (
-            self.slot.parent.is_deleted or
-            self.slot.parent.client.id not in globals.clients or
+            self.is_deleted or
+            self.client.id not in globals.clients or
             self._is_canceled or
             self._is_canceled or
             globals.state in {globals.State.STOPPING, globals.State.STOPPED}
             globals.state in {globals.State.STOPPING, globals.State.STOPPED}
         )
         )
 
 
     def _cleanup(self) -> None:
     def _cleanup(self) -> None:
-        self.slot = None
         self.callback = None
         self.callback = None