Refactor forwarder and implement clock service
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Callable
|
||||
|
||||
|
||||
class Clock:
|
||||
|
||||
millis : int
|
||||
increment : int
|
||||
|
||||
on_update : Callable[[str], None]
|
||||
on_terminated : Callable[[], None]
|
||||
|
||||
def __init__(self, value: int, increment: int) -> None:
|
||||
self.millis = value * 1000
|
||||
self.increment = increment * 1000
|
||||
@@ -14,8 +22,6 @@ class Clock:
|
||||
|
||||
self.thread = threading.Thread(target=self._update_clock, daemon=True)
|
||||
|
||||
# ---------- time computation ----------
|
||||
|
||||
def _compute_seconds(self) -> int:
|
||||
return (self.millis // 1000) % 60
|
||||
|
||||
@@ -25,8 +31,6 @@ class Clock:
|
||||
def _add_increment(self) -> None:
|
||||
self.millis += self.increment
|
||||
|
||||
# ---------- thread logic ----------
|
||||
|
||||
def _update_clock(self):
|
||||
while not self._terminate_event.is_set():
|
||||
self._run_event.wait()
|
||||
@@ -35,12 +39,26 @@ class Clock:
|
||||
if self.millis <= 0:
|
||||
self.millis = 0
|
||||
self._run_event.clear()
|
||||
self.__notify_update()
|
||||
break
|
||||
|
||||
self.millis -= 1
|
||||
self.__notify_update()
|
||||
|
||||
time.sleep(0.001)
|
||||
# ---------- public API ----------
|
||||
|
||||
def __notify_update(self):
|
||||
self.on_update(self.clock_to_str())
|
||||
|
||||
def __notify_terminated(self):
|
||||
self.on_terminated()
|
||||
|
||||
def set_on_update(self, callback : Callable[[str], None]):
|
||||
self.on_update = callback
|
||||
|
||||
def set_on_terminated(self, callback: Callable[[], None]):
|
||||
self.on_terminated = callback
|
||||
|
||||
def is_running(self) -> bool:
|
||||
return self._run_event.is_set()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user