From f9b56d82f708d7976da417a2f3ab9c985c81661f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Pf=C3=BCtzenreuter?= Date: Fri, 26 Apr 2019 18:54:08 -0300 Subject: [PATCH] Add cancel_timeout method This method is useful when the keymap needs to use state.set_timeout() and this timeout needs to be cancellable. Also, the set_timeout() now returns a timer handle. --- kmk/internal_state.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kmk/internal_state.py b/kmk/internal_state.py index b27454b..e552c01 100644 --- a/kmk/internal_state.py +++ b/kmk/internal_state.py @@ -67,8 +67,15 @@ class InternalState: else: timeout_key = ticks_ms() + after_ticks + while timeout_key in self.timeouts: + timeout_key += 1 + self.timeouts[timeout_key] = callback - return self + return timeout_key + + def cancel_timeout(self, timeout_key): + if timeout_key in self.timeouts: + del self.timeouts[timeout_key] def process_timeouts(self): if not self.timeouts: