From 216905ceca731b9761ee52f176b5d113363ef824 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 7 Oct 2018 12:22:10 -0400 Subject: [PATCH] klippy: Add a basic event dispatch mechanism Signed-off-by: Kevin O'Connor --- klippy/klippy.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/klippy/klippy.py b/klippy/klippy.py index e8f737f7..09a6b3ad 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -58,6 +58,7 @@ class Printer: self.is_shutdown = False self.run_result = None self.state_cb = [gc.printer_state] + self.event_handlers = {} def get_start_args(self): return self.start_args def get_reactor(self): @@ -188,6 +189,10 @@ class Printer: def invoke_async_shutdown(self, msg): self.reactor.register_async_callback( (lambda e: self.invoke_shutdown(msg))) + def register_event_handler(self, event, callback): + self.event_handlers.setdefault(event, []).append(callback) + def send_event(self, event, *params): + return [cb(*params) for cb in self.event_handlers.get(event, [])] def request_exit(self, result): self.run_result = result self.reactor.end()