boing.core.observer
— Observables and observers¶The module boing.core.observer
provides an implementation of
the Observer design pattern.
Rather than the standard behaviour, this implementation enables the
Observable
objects to trigger only a subset of all the
current registered Observer
objects.
Note
The notification mechanism relies on the SIGNAL-SLOT mechanism of the Qt eventloop, thus a QApplication must be running in order to ensure that notifications are processed.
boing.core.observer.
Observable
(parent=None)¶Observable
objects can be subscribed by a list of
Observer
instances. An Observable can trigger all or only
a subset of the subscribed observers by invoking the method
trigger()
. The attribute parent defines the parent object
of the observable.
observerAdded
¶Signal emitted when a new observer is added.
observerRemoved
¶Signal emitted when a registered observer is removed.
observers
()¶Return an iterator over the subscribed observers.
addObserver
(observer, mode=QtCore.Qt.QueuedConnection)¶Subscribe observer as a new observer. Return whether observer has been correctly added.
removeObserver
(observer)¶Unsubscribe observer. Return whether observer has been correctly removed.
clear
()¶Unsubscribe all registered observers.
notify
(*restrictions)¶Trigger all the subscribed observers if restrictions is empty, otherwise trigger only the registered observers in restrictions.
Note
The list of the subscribed observers is composed by weak references, so it is necessary to keep both observables and observers alive.
boing.core.observer.
Observer
(react=None, hz=None, parent=None)¶Observer
objects can be subscribed to many
Observable
instances in order to listen to their
notifications. The argument react can be set to the handler
function (it must accept one argument) that will be called as
consequence of an observable notification. If react is None, the
member method _react()
will be called. hz defines the rate
at which the observer will react to notifications. Available
values are:
parent defines the observers parent.
observableAdded
¶Signal emitted when the observer is subscribed to a new observable.
observableRemoved
¶Signal emitted when the observer is unsubscribed from an observable.
observed
()¶Return an iterator over the observables it is subscribed to.
subscribeTo
(observable, mode=QtCore.Qt.QueuedConnection)¶Subscribe to observable. Return whether observer has been successfully subscribed to.
unsubscribeFrom
(observable)¶Unsubscribe from observable. Return whether observable has been successfully found and removed.
clear
()¶Unsubscribe from all observed observables.
hz
()¶Return when the observer will react to the notifications. Possible values:
queue
()¶Return an iterator over the observables that have triggered without having being reacted to yet.
_react
(observable)¶Handler method invoked as a result of the notification of
observable, but only if the Observer
instance has
not been created with a custom react handler.
Note
The list of the subscribed observables is composed by weak references, so it is necessary to keep both observables and observers alive.