mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-06 01:51:21 +00:00
30 lines
887 B
Python
30 lines
887 B
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from functools import partial
|
|
|
|
from tfw.decorators import lazy_property
|
|
|
|
|
|
class CallbackMixin:
|
|
@lazy_property
|
|
def _callbacks(self):
|
|
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 unsubscribe_callback(self, callback):
|
|
self._callbacks.remove(callback)
|
|
|
|
def _execute_callbacks(self, *args, **kwargs):
|
|
for callback in self._callbacks:
|
|
callback(*args, **kwargs)
|