Return new Timers

This commit is contained in:
jesopo 2019-07-09 11:16:34 +01:00
parent 32fa999c78
commit 02838e4802

View file

@ -64,14 +64,15 @@ class Timers(object):
self.timers.remove(timer) self.timers.remove(timer)
self.database.bot_settings.delete("timer-%s" % timer.id) self.database.bot_settings.delete("timer-%s" % timer.id)
def add(self, name: str, delay: float, next_due: float=None, **kwargs): def add(self, name: str, delay: float, next_due: float=None, **kwargs
self._add(None, name, delay, next_due, None, False, kwargs) ) -> Timer:
return self._add(None, name, delay, next_due, None, False, kwargs)
def add_persistent(self, name: str, delay: float, next_due: float=None, def add_persistent(self, name: str, delay: float, next_due: float=None,
**kwargs): **kwargs) -> Timer:
self._add(None, name, delay, next_due, None, True, kwargs) return self._add(None, name, delay, next_due, None, True, kwargs)
def _add(self, context: typing.Optional[str], name: str, delay: float, def _add(self, context: typing.Optional[str], name: str, delay: float,
next_due: typing.Optional[float], id: typing.Optional[str], next_due: typing.Optional[float], id: typing.Optional[str],
persist: bool, kwargs: dict): persist: bool, kwargs: dict) -> Timer:
id = id or str(uuid.uuid4()) id = id or str(uuid.uuid4())
timer = Timer(id, context, name, delay, next_due, kwargs) timer = Timer(id, context, name, delay, next_due, kwargs)
if persist: if persist:
@ -83,6 +84,7 @@ class Timers(object):
self.context_timers[context].append(timer) self.context_timers[context].append(timer)
else: else:
self.timers.append(timer) self.timers.append(timer)
return timer
def next(self) -> typing.Optional[float]: def next(self) -> typing.Optional[float]:
times = list(filter(None, times = list(filter(None,
@ -122,12 +124,12 @@ class TimersContext(object):
self._parent = parent self._parent = parent
self.context = context self.context = context
def add(self, name: str, delay: float, next_due: float=None, def add(self, name: str, delay: float, next_due: float=None,
**kwargs): **kwargs) -> Timer:
self._parent._add(self.context, name, delay, next_due, None, False, return self._parent._add(self.context, name, delay, next_due, None,
kwargs) False, kwargs)
def add_persistent(self, name: str, delay: float, next_due: float=None, def add_persistent(self, name: str, delay: float, next_due: float=None,
**kwargs): **kwargs) -> Timer:
self._parent._add(None, name, delay, next_due, None, True, return self._parent._add(None, name, delay, next_due, None, True,
kwargs) kwargs)
def find_all(self, name: str) -> typing.List[Timer]: def find_all(self, name: str) -> typing.List[Timer]:
return self._parent.find_all(name) return self._parent.find_all(name)