After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 741136 - Replace observer-pattern with event system
Replace observer-pattern with event system
Status: RESOLVED WONTFIX
Product: mousetrap
Classification: Other
Component: General
3.17.x
Other All
: Normal enhancement
: ---
Assigned To: mousetrap-maint
mousetrap-maint
gnome[unmaintained]
Depends on:
Blocks:
 
 
Reported: 2014-12-04 22:29 UTC by Stoney Jackson
Modified: 2018-08-17 19:45 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Initial implementation of Bus and Event (12.21 KB, patch)
2015-06-02 14:18 UTC, Stoney Jackson
none Details | Review

Description Stoney Jackson 2014-12-04 22:29:47 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?
Comment 1 Stoney Jackson 2015-02-24 21:22:27 UTC
We're getting our versions straightened out. All bugs associated with gnome3-wip are now against 3.17.x.
Comment 2 Stoney Jackson 2015-06-02 14:18:52 UTC
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.
Comment 3 André Klapper 2018-08-17 19:45:16 UTC
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.