class _Event(object): def __init__(self, loop, callback, timeout, starttime): self.loop = loop self.callback = WeakMethod(callback) self.timeout = timeout self._last_dt = starttime self._dt = 0. def do(self, dt): if self.callback.is_dead(): return False self.callback()(dt) def tick(self, curtime): # timeout happen ? if curtime - self._last_dt < self.timeout: return True # calculate current timediff for this event self._dt = curtime - self._last_dt self._last_dt = curtime # call the callback if self.callback.is_dead(): return False ret = self.callback()(self._dt) # if it's a once event, don't care about the result # just remove the event if not self.loop: return False # if user return an explicit false, # remove the event if ret == False: return False return True
def set_handler(self, name, handler): '''Attach a single event handler. :Parameters: `name` : str Name of the event type to attach to. `handler` : callable Event handler to attach. ''' # Create event stack if necessary if self._event_stack is None: self._event_stack = [{}] self._event_stack[0][name] = WeakMethod(handler)
def __init__(self, loop, callback, timeout, starttime): self.loop = loop self.callback = WeakMethod(callback) self.timeout = timeout self._last_dt = starttime self._dt = 0.