Allow Timer objects to be cancelled
This commit is contained in:
parent
b692bc7e88
commit
32fa999c78
1 changed files with 5 additions and 3 deletions
|
@ -18,7 +18,7 @@ class Timer(object):
|
||||||
def set_next_due(self):
|
def set_next_due(self):
|
||||||
self.next_due = time.time()+self.delay
|
self.next_due = time.time()+self.delay
|
||||||
def due(self) -> bool:
|
def due(self) -> bool:
|
||||||
return self.time_left() <= 0
|
return not self.done() and self.time_left() <= 0
|
||||||
def time_left(self) -> float:
|
def time_left(self) -> float:
|
||||||
return self.next_due-time.time()
|
return self.next_due-time.time()
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ class Timer(object):
|
||||||
self.set_next_due()
|
self.set_next_due()
|
||||||
def finish(self):
|
def finish(self):
|
||||||
self._done = True
|
self._done = True
|
||||||
|
def cancel(self):
|
||||||
|
self.finish()
|
||||||
def done(self) -> bool:
|
def done(self) -> bool:
|
||||||
return self._done
|
return self._done
|
||||||
|
|
||||||
|
@ -108,8 +110,8 @@ class Timers(object):
|
||||||
timer.finish()
|
timer.finish()
|
||||||
self.events.on("timer.%s" % timer.name).call(timer=timer,
|
self.events.on("timer.%s" % timer.name).call(timer=timer,
|
||||||
**timer.kwargs)
|
**timer.kwargs)
|
||||||
if timer.done():
|
if timer.done():
|
||||||
self._remove(timer)
|
self._remove(timer)
|
||||||
|
|
||||||
def purge_context(self, context: str):
|
def purge_context(self, context: str):
|
||||||
if context in self.context_timers:
|
if context in self.context_timers:
|
||||||
|
|
Loading…
Reference in a new issue