From 251a59ae85883035c0fe4792577e9d600b6d5ce8 Mon Sep 17 00:00:00 2001 From: Aleš Mrázek Date: Fri, 27 Dec 2024 16:05:51 +0100 Subject: manager: config_store: added only_on_no_changes_update decorator This decorator will only trigger the function if the selected config has not been changed. --- python/knot_resolver/manager/config_store.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/python/knot_resolver/manager/config_store.py b/python/knot_resolver/manager/config_store.py index 214062b2..70a9d942 100644 --- a/python/knot_resolver/manager/config_store.py +++ b/python/knot_resolver/manager/config_store.py @@ -59,6 +59,25 @@ class ConfigStore: return self._config +def only_on_no_changes_update(selector: Callable[[KresConfig], Any]) -> Callable[[UpdateCallback], UpdateCallback]: + def decorator(orig_func: UpdateCallback) -> UpdateCallback: + original_value_set: Any = False + original_value: Any = None + + async def new_func_update(config: KresConfig) -> None: + nonlocal original_value_set + nonlocal original_value + if not original_value_set: + original_value_set = True + elif original_value == selector(config): + await orig_func(config) + original_value = selector(config) + + return new_func_update + + return decorator + + def only_on_real_changes_update(selector: Callable[[KresConfig], Any]) -> Callable[[UpdateCallback], UpdateCallback]: def decorator(orig_func: UpdateCallback) -> UpdateCallback: original_value_set: Any = False -- cgit v1.2.3