|
| 1 | + |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import Any, Callable |
| 4 | +from fbclient.common_types import FBUser |
| 5 | +from fbclient.interfaces import Notice |
| 6 | +from fbclient.notice_broadcaster import NoticeBroadcater |
| 7 | + |
| 8 | +FLAG_CHANGE_NOTICE_TYPE = 'flag_change_notice' |
| 9 | + |
| 10 | + |
| 11 | +class FlagChangedNotice(Notice): |
| 12 | + def __init__(self, flag_key: str): |
| 13 | + self.__flag_key = flag_key |
| 14 | + |
| 15 | + @property |
| 16 | + def notice_type(self) -> str: |
| 17 | + return FLAG_CHANGE_NOTICE_TYPE |
| 18 | + |
| 19 | + @property |
| 20 | + def flag_key(self) -> str: |
| 21 | + return self.__flag_key |
| 22 | + |
| 23 | + |
| 24 | +class FlagChangedListener(ABC): |
| 25 | + """ |
| 26 | + A notice listener that is notified when a feature flag's configuration has changed. |
| 27 | +
|
| 28 | + This is an abstract class. You need to implement your own listener by overriding the :func:`on_flag_change` method. |
| 29 | +
|
| 30 | + """ |
| 31 | + @abstractmethod |
| 32 | + def on_flag_change(self, notice: FlagChangedNotice): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class FlagValueChangedListener(FlagChangedListener): |
| 37 | + def __init__(self, |
| 38 | + flag_key: str, |
| 39 | + user: dict, |
| 40 | + evaluate_fn: Callable[[str, dict, Any], Any], |
| 41 | + flag_value_changed_fn: Callable[[str, Any], None]): |
| 42 | + self.__flag_key = flag_key |
| 43 | + self.__user = user |
| 44 | + self.__evaluate_fn = evaluate_fn |
| 45 | + self.__fn = flag_value_changed_fn |
| 46 | + # record the flag value when the listener is created |
| 47 | + self.__prvious_flag_value = self.__evaluate_fn(self.__flag_key, self.__user, None) |
| 48 | + |
| 49 | + def on_flag_change(self, notice: FlagChangedNotice): |
| 50 | + if notice.flag_key == self.__flag_key: |
| 51 | + prev_flag_value = self.__prvious_flag_value |
| 52 | + curr_flag_value = self.__evaluate_fn(self.__flag_key, self.__user, None) |
| 53 | + if prev_flag_value != curr_flag_value: |
| 54 | + self.__fn(self.__flag_key, curr_flag_value) |
| 55 | + self.__prvious_flag_value = curr_flag_value |
| 56 | + |
| 57 | + |
| 58 | +class FlagValueMayChangedListener(FlagChangedListener): |
| 59 | + def __init__(self, |
| 60 | + flag_key: str, |
| 61 | + user: dict, |
| 62 | + evaluate_fn: Callable[[str, dict, Any], Any], |
| 63 | + flag_value_changed_fn: Callable[[str, Any], None]): |
| 64 | + self.__flag_key = flag_key |
| 65 | + self.__user = user |
| 66 | + self.__evaluate_fn = evaluate_fn |
| 67 | + self.__fn = flag_value_changed_fn |
| 68 | + |
| 69 | + def on_flag_change(self, notice: FlagChangedNotice): |
| 70 | + if notice.flag_key == self.__flag_key: |
| 71 | + curr_flag_value = self.__evaluate_fn(self.__flag_key, self.__user, None) |
| 72 | + self.__fn(self.__flag_key, curr_flag_value) |
| 73 | + |
| 74 | + |
| 75 | +class FlagTracker: |
| 76 | + """ |
| 77 | + A registry to register the flag change listeners in order to track changes in feature flag configurations. |
| 78 | +
|
| 79 | + The registered listerners only work if the SDK is actually connecting to FeatBit feature flag center. |
| 80 | + If the SDK is only in offline mode then it cannot know when there is a change, because flags are read on an as-needed basis. |
| 81 | +
|
| 82 | + Application code never needs to initialize or extend this class directly. |
| 83 | + """ |
| 84 | + |
| 85 | + def __init__(self, |
| 86 | + flag_change_broadcaster: NoticeBroadcater, |
| 87 | + evaluate_fn: Callable[[str, dict, Any], Any]): |
| 88 | + """ |
| 89 | + :param flag_change_broadcaster: The broadcaster that broadcasts the flag change notices |
| 90 | + :param evaluate_fn: The function to evaluate the flag value |
| 91 | + """ |
| 92 | + self.__broadcater = flag_change_broadcaster |
| 93 | + self.__evaluate_fn = evaluate_fn |
| 94 | + |
| 95 | + def add_flag_value_changed_listener(self, |
| 96 | + flag_key: str, |
| 97 | + user: dict, |
| 98 | + flag_value_changed_fn: Callable[[str, Any], None]) -> FlagValueChangedListener: |
| 99 | + """ |
| 100 | + Registers a listener to be notified of a change in a specific feature flag's value for a specific FeatBit user. |
| 101 | +
|
| 102 | + The listener will be notified whenever the SDK receives any change to any feature flag's configuration, |
| 103 | + or to a user segment that is referenced by a feature flag. |
| 104 | +
|
| 105 | + When you call this method, it first immediately evaluates the feature flag. It then uses :class:`FlagChangeListener` to start listening for feature flag configuration |
| 106 | + changes, and whenever the specified feature flag changes, it re-evaluates the flag for the same user. It then calls :class:`FlagValueChangeListener` |
| 107 | + if and only if the resulting value has changed. |
| 108 | +
|
| 109 | + :param flag_key: The key of the feature flag to track |
| 110 | + :param user: The user to evaluate the flag value |
| 111 | + :param flag_value_changed_fn: The function to be called only when this flag value changes |
| 112 | + * the first argument is the flag key |
| 113 | + * the second argument is the latest flag value, this value must be different from the previous value |
| 114 | +
|
| 115 | + :return: A listener object that can be used to remove it later on. |
| 116 | + """ |
| 117 | + |
| 118 | + # check flag key |
| 119 | + if not isinstance(flag_key, str) or not flag_key: |
| 120 | + raise ValueError('flag_key must be a non-empty string') |
| 121 | + # check user |
| 122 | + FBUser.from_dict(user) |
| 123 | + # check flag_value_changed_fn |
| 124 | + if not isinstance(flag_value_changed_fn, Callable) or not flag_value_changed_fn: |
| 125 | + raise ValueError('flag_value_changed_fn must be a callable function') |
| 126 | + |
| 127 | + listener = FlagValueChangedListener(flag_key, user, self.__evaluate_fn, flag_value_changed_fn) |
| 128 | + self.add_flag_changed_listener(listener) |
| 129 | + return listener |
| 130 | + |
| 131 | + def add_flag_value_may_changed_listener(self, |
| 132 | + flag_key: str, |
| 133 | + user: dict, |
| 134 | + flag_value_changed_fn: Callable[[str, Any], None]) -> FlagValueMayChangedListener: |
| 135 | + """ |
| 136 | + Registers a listener to be notified of a change in a specific feature flag's value for a specific FeatBit user. |
| 137 | +
|
| 138 | + The listener will be notified whenever the SDK receives any change to any feature flag's configuration, |
| 139 | + or to a user segment that is referenced by a feature flag. |
| 140 | +
|
| 141 | + Note that this does not necessarily mean the flag's value has changed for any particular flag, |
| 142 | + only that some part of the flag configuration was changed so that it may return a different value than it previously returned for some user. |
| 143 | +
|
| 144 | + If you want to track flag value changes,use :func:`add_flag_value_changed_listener instead. |
| 145 | +
|
| 146 | + :param flag_key: The key of the feature flag to track |
| 147 | + :param user: The user to evaluate the flag value |
| 148 | + :param flag_value_changed_fn: The function to be called only if any changes to a specific flag |
| 149 | + * the first argument is the flag key |
| 150 | + * the second argument is the latest flag value, this value may be same as the previous value |
| 151 | +
|
| 152 | + :return: A listener object that can be used to remove it later on. |
| 153 | +
|
| 154 | + """ |
| 155 | + |
| 156 | + # check flag key |
| 157 | + if not isinstance(flag_key, str) or not flag_key: |
| 158 | + raise ValueError('flag_key must be a non-empty string') |
| 159 | + # check user |
| 160 | + FBUser.from_dict(user) |
| 161 | + # check flag_value_changed_fn |
| 162 | + if not isinstance(flag_value_changed_fn, Callable) or not flag_value_changed_fn: |
| 163 | + raise ValueError('flag_value_changed_fn must be a callable function') |
| 164 | + |
| 165 | + listener = FlagValueMayChangedListener(flag_key, user, self.__evaluate_fn, flag_value_changed_fn) |
| 166 | + self.add_flag_changed_listener(listener) |
| 167 | + return listener |
| 168 | + |
| 169 | + def add_flag_changed_listener(self, listener: FlagChangedListener): |
| 170 | + """ |
| 171 | + Registers a listener to be notified of feature flag changes in general. |
| 172 | +
|
| 173 | + The listener will be notified whenever the SDK receives any change to any feature flag's configuration, |
| 174 | + or to a user segment that is referenced by a feature flag. |
| 175 | +
|
| 176 | + :param listener: The listener to be registered. The :class:`FlagChangedListner` is an abstract class. You need to implement your own listener. |
| 177 | + """ |
| 178 | + self.__broadcater.add_listener(FLAG_CHANGE_NOTICE_TYPE, listener.on_flag_change) # type: ignore |
| 179 | + |
| 180 | + def remove_flag_change_notifier(self, listener: FlagChangedListener): |
| 181 | + """ |
| 182 | + Unregisters a listener so that it will no longer be notified of feature flag changes. |
| 183 | +
|
| 184 | + :param listener: The listener to be unregistered. The listener must be the same object that was passed to :func:`add_flag_changed_listner` or :func:`add_flag_value_changed_listerner` |
| 185 | + """ |
| 186 | + self.__broadcater.remove_listener(FLAG_CHANGE_NOTICE_TYPE, listener.on_flag_change) # type: ignore |
0 commit comments