Selaa lähdekoodia

Merge pull request #1829 from zauberzeug/timer-element

Timer element
Rodja Trappe 1 vuosi sitten
vanhempi
säilyke
40257cd109
3 muutettua tiedostoa jossa 12 lisäystä ja 16 poistoa
  1. 1 0
      nicegui/elements/timer.js
  2. 9 14
      nicegui/elements/timer.py
  3. 2 2
      nicegui/ui.py

+ 1 - 0
nicegui/elements/timer.js

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

+ 9 - 14
nicegui/functions/timer.py → nicegui/elements/timer.py

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

+ 2 - 2
nicegui/ui.py

@@ -70,6 +70,7 @@ __all__ = [
     'tabs',
     'textarea',
     'time',
+    'timer',
     'timeline',
     'timeline_entry',
     'toggle',
@@ -84,7 +85,6 @@ __all__ = [
     'notify',
     'open',
     'refreshable',
-    'timer',
     'update',
     'page',
     'drawer',
@@ -170,6 +170,7 @@ from .elements.textarea import Textarea as textarea
 from .elements.time import Time as time
 from .elements.timeline import Timeline as timeline
 from .elements.timeline import TimelineEntry as timeline_entry
+from .elements.timer import Timer as timer
 from .elements.toggle import Toggle as toggle
 from .elements.tooltip import Tooltip as tooltip
 from .elements.tree import Tree as tree
@@ -181,7 +182,6 @@ from .functions.javascript import run_javascript
 from .functions.notify import notify
 from .functions.open import open  # pylint: disable=redefined-builtin
 from .functions.refreshable import refreshable
-from .functions.timer import Timer as timer
 from .functions.update import update
 from .page import page
 from .page_layout import Drawer as drawer