GNOME Bugzilla – Bug 741136
Replace observer-pattern with event system
Last modified: 2018-08-17 19:45:16 UTC
## Current Situation Mousetrap uses an observer-pattern to allow plugins to register with mousetrap.core.Loop. There are several problems with the current implementation: ## The Problem(s) 1. PlugIns must know the Loop object. 2. PlugIns must share state, and that state is stored on the App object. 3. Observable is overcomplicated making it somewhat challenging to reuse. 4. The observer pattern is not very pythonic. 5. PlugIns cannot easily talk to each other except through shared state. ## Suggestion Discussions with Kevin inspired the following "solution". We could have a central events object that provides event registration, event firing, and event notification. Below shows a hypothetical CameraPlugin making use of this new events object. In the example, the camera plugin registers its handle_pulse method to be called on any "pulse" event. More, it fires a "capture_image" event every time it captures a new image. This would allow objects/plugins registered for the 'capture_image' to get the new image. class CameraPlugin: def __init__(self, config, events): self._image = None self._events = events self._events.on('pulse', notify=self.handle_pulse) def handle_pulse(self, event): # event may have a source and/or data attribute depending on the event. if self._is_time_to_capture_image(): self._image = self._capture_image() def _is_time_to_capture_image(self): ... def _capture_image(self): self._image = ... # keyword parameters will become attributes of the event object that # observers receive. self._event_registry.fire('capture_image', source=self, data=self._image) def get_image(self): return self._image I'm still struggling with the passing of data and/or sources for different events. Suggestions welcome. This design doesn't suffer from the aforementioned problems. But I'm sure there are others. Let me know what you think?
We're getting our versions straightened out. All bugs associated with gnome3-wip are now against 3.17.x.
Created attachment 304430 [details] [review] Initial implementation of Bus and Event This is not a complete solution. It has not been integrated into the existing code base. But I wanted to get this up for feedback.
mousetrap is not under active development anymore since 2015. Its codebase has been archived: https://gitlab.gnome.org/Archive/mousetrap/commits/master Closing this report as WONTFIX as part of Bugzilla Housekeeping to reflect reality. Please feel free to reopen this ticket (or rather transfer the project to GNOME Gitlab, as GNOME Bugzilla is deprecated) if anyone takes the responsibility for active development again.