mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-09 01:37:17 +00:00
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
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)
|