瀏覽代碼

Merge pull request #1829 from zauberzeug/timer-element

Timer element
Rodja Trappe 1 年之前
父節點
當前提交
40257cd109
共有 3 個文件被更改,包括 12 次插入16 次删除
  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 .. 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

+ 2 - 2
nicegui/ui.py

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