mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-08 23:54:09 +00:00
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from functools import partial
|
|
|
|
from tfw.decorators.lazy_property import lazy_property
|
|
|
|
|
|
class CallbackMixin:
|
|
@lazy_property
|
|
def _callbacks(self):
|
|
# pylint: disable=no-self-use
|
|
return []
|
|
|
|
def subscribe_callback(self, callback, *args, **kwargs):
|
|
"""
|
|
Subscribe a callable to invoke once an event is triggered.
|
|
:param callback: callable to be executed on events
|
|
:param args: arguments passed to callable
|
|
:param kwargs: kwargs passed to callable
|
|
"""
|
|
fun = partial(callback, *args, **kwargs)
|
|
self._callbacks.append(fun)
|
|
|
|
def subscribe_callbacks(self, *callbacks):
|
|
"""
|
|
Subscribe a list of callbacks to incoke once an event is triggered.
|
|
:param callbacks: callbacks to be subscribed
|
|
"""
|
|
for callback in callbacks:
|
|
self.subscribe_callback(callback)
|
|
|
|
def unsubscribe_callback(self, callback):
|
|
self._callbacks.remove(callback)
|
|
|
|
def _execute_callbacks(self, *args, **kwargs):
|
|
for callback in self._callbacks:
|
|
callback(*args, **kwargs)
|