mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-05-05 04:33:50 +00:00
better water
This commit is contained in:
parent
2e759aba84
commit
a6741073c9
@ -27,6 +27,22 @@ from typing import ParamSpec
|
|||||||
Params = ParamSpec("Params")
|
Params = ParamSpec("Params")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class WaiterResult:
|
||||||
|
took: float
|
||||||
|
|
||||||
|
def __float__(self) -> float:
|
||||||
|
return self.took
|
||||||
|
|
||||||
|
|
||||||
|
class WaiterTimedOut(WaiterResult):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class WaiterFinished(WaiterResult):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Waiter:
|
class Waiter:
|
||||||
"""
|
"""
|
||||||
@ -54,7 +70,7 @@ class Waiter:
|
|||||||
since_start: float = time.monotonic() - self.start_time
|
since_start: float = time.monotonic() - self.start_time
|
||||||
return self.wait_timeout != 0 and since_start > self.wait_timeout
|
return self.wait_timeout != 0 and since_start > self.wait_timeout
|
||||||
|
|
||||||
def wait(self, in_progress: Callable[Params, bool], *args: Params.args, **kwargs: Params.kwargs) -> float:
|
def wait(self, in_progress: Callable[Params, bool], *args: Params.args, **kwargs: Params.kwargs) -> WaiterResult:
|
||||||
"""
|
"""
|
||||||
wait until requirements are not met
|
wait until requirements are not met
|
||||||
|
|
||||||
@ -66,7 +82,9 @@ class Waiter:
|
|||||||
Returns:
|
Returns:
|
||||||
float: consumed time in seconds
|
float: consumed time in seconds
|
||||||
"""
|
"""
|
||||||
while not self.is_timed_out() and in_progress(*args, **kwargs):
|
while not (timed_out := self.is_timed_out()) and in_progress(*args, **kwargs):
|
||||||
time.sleep(self.interval)
|
time.sleep(self.interval)
|
||||||
|
|
||||||
return time.monotonic() - self.start_time
|
if timed_out:
|
||||||
|
return WaiterTimedOut(time.monotonic() - self.start_time)
|
||||||
|
return WaiterFinished(time.monotonic() - self.start_time)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user