core/events: allow handlers return some value
This commit is contained in:
parent
6052143735
commit
1cd6acd875
@ -26,6 +26,9 @@ fires events in reverse order. It is suitable for events fired before some
|
|||||||
action is performed. You may at your own responsibility raise exceptions from
|
action is performed. You may at your own responsibility raise exceptions from
|
||||||
such events to try to prevent such action.
|
such events to try to prevent such action.
|
||||||
|
|
||||||
|
Event handlers may return a value. Those values are aggregated and returned
|
||||||
|
to the caller as a list of those values. The order of this list is undefined.
|
||||||
|
:py:obj:`None` values are omitted.
|
||||||
|
|
||||||
Handling events
|
Handling events
|
||||||
---------------
|
---------------
|
||||||
|
@ -129,13 +129,17 @@ class Emitter(object):
|
|||||||
if not self.events_enabled:
|
if not self.events_enabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
effects = []
|
||||||
for cls in order:
|
for cls in order:
|
||||||
if not hasattr(cls, '__handlers__'):
|
if not hasattr(cls, '__handlers__'):
|
||||||
continue
|
continue
|
||||||
for func in sorted(cls.__handlers__[event],
|
for func in sorted(cls.__handlers__[event],
|
||||||
key=(lambda handler: hasattr(handler, 'ha_bound')),
|
key=(lambda handler: hasattr(handler, 'ha_bound')),
|
||||||
reverse=True):
|
reverse=True):
|
||||||
func(self, event, *args, **kwargs)
|
effect = func(self, event, *args, **kwargs)
|
||||||
|
if effect is not None:
|
||||||
|
effects.append(effect)
|
||||||
|
return effects
|
||||||
|
|
||||||
|
|
||||||
def fire_event(self, event, *args, **kwargs):
|
def fire_event(self, event, *args, **kwargs):
|
||||||
@ -150,13 +154,14 @@ class Emitter(object):
|
|||||||
:py:meth:`fire_event_pre`
|
:py:meth:`fire_event_pre`
|
||||||
|
|
||||||
:param str event: event identificator
|
:param str event: event identificator
|
||||||
|
:returns: list of effects
|
||||||
|
|
||||||
All *args* and *kwargs* are passed verbatim. They are different for
|
All *args* and *kwargs* are passed verbatim. They are different for
|
||||||
different events.
|
different events.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
self._fire_event_in_order(reversed(self.__class__.__mro__), event,
|
return self._fire_event_in_order(reversed(self.__class__.__mro__),
|
||||||
*args, **kwargs)
|
event, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def fire_event_pre(self, event, *args, **kwargs):
|
def fire_event_pre(self, event, *args, **kwargs):
|
||||||
@ -170,10 +175,11 @@ class Emitter(object):
|
|||||||
:py:meth:`fire_event`
|
:py:meth:`fire_event`
|
||||||
|
|
||||||
:param str event: event identificator
|
:param str event: event identificator
|
||||||
|
:returns: list of effects
|
||||||
|
|
||||||
All *args* and *kwargs* are passed verbatim. They are different for
|
All *args* and *kwargs* are passed verbatim. They are different for
|
||||||
different events.
|
different events.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
self._fire_event_in_order(self.__class__.__mro__, event,
|
return self._fire_event_in_order(self.__class__.__mro__,
|
||||||
*args, **kwargs)
|
event, *args, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user