You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. import wx
  2. from collections import defaultdict
  3. __ALL__ = ['pub']
  4. class PubSub(object):
  5. """
  6. A super simplified clone of Wx.lib.pubsub since it doesn't exist on linux
  7. """
  8. def __init__(self):
  9. self.registry = defaultdict(list)
  10. def subscribe(self, event, handler):
  11. self.registry[event].append(handler)
  12. def send_message(self, event, **kwargs):
  13. for event_handler in self.registry.get(event, []):
  14. wx.CallAfter(event_handler, **kwargs)
  15. def send_message_sync(self, event, **kwargs):
  16. """
  17. ===== THIS IS NOT THREAD SAFE =====
  18. Synchronously sends the message to all relevant consumers
  19. and blocks until a response is received.
  20. This MUST ONLY be used for communication within
  21. the same thread! It exists primarily as an escape
  22. hatch for bubbling up messages (which would be garbage
  23. collected in the CallAfter form) to interested components
  24. """
  25. for event_handler in self.registry.get(event, []):
  26. event_handler(**kwargs)
  27. pub = PubSub()