|
| 1 | +import 'dart:collection'; |
| 2 | + |
| 3 | +import 'package:launchdarkly_dart_common/launchdarkly_dart_common.dart' |
| 4 | + show LDValue, LDContext, LDEvaluationDetail; |
| 5 | + |
| 6 | +import '../ld_common_client.dart' show IdentifyResult; |
| 7 | + |
| 8 | +/// Meta-data about a hook implementation. |
| 9 | +final class HookMetadata { |
| 10 | + /// The name of the hook. |
| 11 | + final String name; |
| 12 | + |
| 13 | + /// Construct a new hook metadata instance. |
| 14 | + /// Implementation note: If more fields are added then they must not be |
| 15 | + /// required constructor parameters for compatibility purposes. |
| 16 | + const HookMetadata({required this.name}); |
| 17 | + |
| 18 | + @override |
| 19 | + String toString() { |
| 20 | + return 'HookMetadata{name: $name}'; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Contextual information provided to the evaluation stages. |
| 25 | +final class EvaluationSeriesContext { |
| 26 | + /// The flag key the evaluation is for. |
| 27 | + final String flagKey; |
| 28 | + |
| 29 | + /// The context for the evaluation. Optional in case an evaluation is |
| 30 | + /// performed before the SDK has been started, or an invalid context is |
| 31 | + /// used. |
| 32 | + final LDContext? context; |
| 33 | + |
| 34 | + /// The default value that was provided for the evaluation. |
| 35 | + final LDValue defaultValue; |
| 36 | + |
| 37 | + /// The name of the method that was invoked to perform the evaluation. |
| 38 | + final String method; |
| 39 | + |
| 40 | + /// The environment ID associated with the evaluation if available. |
| 41 | + final String? environmentId; |
| 42 | + |
| 43 | + EvaluationSeriesContext.internal( |
| 44 | + {required this.flagKey, |
| 45 | + required this.context, |
| 46 | + required this.defaultValue, |
| 47 | + required this.method, |
| 48 | + required this.environmentId}); |
| 49 | + |
| 50 | + @override |
| 51 | + String toString() { |
| 52 | + return 'EvaluationSeriesContext{flagKey: $flagKey, context: $context,' |
| 53 | + ' defaultValue: $defaultValue, method: $method,' |
| 54 | + ' environmentId: $environmentId}'; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Contextual information provided to identify stages. |
| 59 | +final class IdentifySeriesContext { |
| 60 | + /// The context associated with the identify operation. |
| 61 | + final LDContext context; |
| 62 | + |
| 63 | + // Implementation note: Timeout not managed by SDK, so not included. |
| 64 | + // If the timeout does become managed by the SDK, then it should be |
| 65 | + // added here. |
| 66 | + |
| 67 | + IdentifySeriesContext.internal({required this.context}); |
| 68 | + |
| 69 | + @override |
| 70 | + String toString() { |
| 71 | + return 'IdentifySeriesContext{context: $context}'; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Contextual information provided to track stages. |
| 76 | +final class TrackSeriesContext { |
| 77 | + /// The key for the event being tracked. |
| 78 | + final String key; |
| 79 | + |
| 80 | + /// The context associated with the track operation. |
| 81 | + final LDContext context; |
| 82 | + |
| 83 | + /// The data associated with the track operation. |
| 84 | + final LDValue? data; |
| 85 | + |
| 86 | + /// The metric value associated with the track operation. |
| 87 | + final num? numericValue; |
| 88 | + |
| 89 | + TrackSeriesContext.internal( |
| 90 | + {required this.key, required this.context, this.data, this.numericValue}); |
| 91 | + |
| 92 | + @override |
| 93 | + String toString() { |
| 94 | + return 'TrackSeriesContext{key: $key, context: $context,' |
| 95 | + ' data: $data, numericValue: $numericValue}'; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Base class for extending SDK functionality via hooks. |
| 100 | +/// All hook implementations must derive from this class. |
| 101 | +/// |
| 102 | +/// Default implementations are provided for each stage and an implementer |
| 103 | +/// should override at least one of the stage methods. |
| 104 | +/// |
| 105 | +/// All implementations must implement the metadata getter. |
| 106 | +abstract base class Hook { |
| 107 | + /// Metadata associated with this hook. |
| 108 | + /// |
| 109 | + /// Hook implementations must implement this property. |
| 110 | + /// ```dart |
| 111 | + /// final _metadata = HookMetadata(name: 'MyHookName'); |
| 112 | + /// |
| 113 | + /// @override |
| 114 | + /// HookMetadata get metadata => _metadata; |
| 115 | + /// ``` |
| 116 | + HookMetadata get metadata; |
| 117 | + |
| 118 | + /// Construct a new hook instance. |
| 119 | + Hook(); |
| 120 | + |
| 121 | + /// This method is called during the execution of a variation method before |
| 122 | + /// the flag value has been determined. The method is executed synchronously. |
| 123 | + /// |
| 124 | + /// [hookContext] Contains information about the evaluation being performed. |
| 125 | + /// [data] A record associated with each stage of hook invocations. Each stage |
| 126 | + /// is called with the data of the previous stage for a series. The input |
| 127 | + /// record should not be modified. |
| 128 | + /// |
| 129 | + /// Returns data to use when executing the next state of the hook in the |
| 130 | + /// evaluation series. It is recommended to expand the previous input into the |
| 131 | + /// return. This helps ensure your stage remains compatible moving forward as |
| 132 | + /// more stages are added. |
| 133 | + /// |
| 134 | + /// ```dart |
| 135 | + /// Map<String, LDValue> newData = Map.from(data); |
| 136 | + /// newData['new-key'] = LDValue.ofString('new-value'); |
| 137 | + /// return UnmodifiableMapView(newData); |
| 138 | + /// ``` |
| 139 | + UnmodifiableMapView<String, LDValue> beforeEvaluation( |
| 140 | + EvaluationSeriesContext hookContext, |
| 141 | + UnmodifiableMapView<String, LDValue> data) { |
| 142 | + return data; |
| 143 | + } |
| 144 | + |
| 145 | + /// This method is called during the execution of the variation method |
| 146 | + /// after the flag value has been determined. The method is executed |
| 147 | + /// synchronously. |
| 148 | + /// |
| 149 | + /// [hookContext] Contains information about the evaluation being performed. |
| 150 | + /// [data] A record associated with each stage of hook invocations. Each stage |
| 151 | + /// is called with the data of the previous stage for a series. The input |
| 152 | + /// record should not be modified. |
| 153 | + /// [detail] The result of the evaluation. |
| 154 | + /// |
| 155 | + /// Returns data to use when executing the next state of the hook in the |
| 156 | + /// evaluation series. It is recommended to expand the previous input into the |
| 157 | + /// return. This helps ensure your stage remains compatible moving forward as |
| 158 | + /// more stages are added. |
| 159 | + /// |
| 160 | + /// ```dart |
| 161 | + /// Map<String, LDValue> newData = Map.from(data); |
| 162 | + /// newData['new-key'] = LDValue.ofString('new-value'); |
| 163 | + /// return UnmodifiableMapView(newData); |
| 164 | + /// ``` |
| 165 | + UnmodifiableMapView<String, LDValue> afterEvaluation( |
| 166 | + EvaluationSeriesContext hookContext, |
| 167 | + UnmodifiableMapView<String, LDValue> data, |
| 168 | + LDEvaluationDetail<LDValue> detail) { |
| 169 | + return data; |
| 170 | + } |
| 171 | + |
| 172 | + /// This method is called during the execution of the identify process before |
| 173 | + /// the operation completes, but after any context modifications are |
| 174 | + /// performed. |
| 175 | + /// |
| 176 | + /// [hookContext] Contains information about the evaluation being performed. |
| 177 | + /// [data] A record associated with each stage of hook invocations. Each stage |
| 178 | + /// is called with the data of the previous stage for a series. The input |
| 179 | + /// record should not be modified. |
| 180 | + /// |
| 181 | + /// Returns data to use when executing the next state of the hook in the |
| 182 | + /// evaluation series. It is recommended to expand the previous input into the |
| 183 | + /// return. This helps ensure your stage remains compatible moving forward as |
| 184 | + /// more stages are added. |
| 185 | + /// |
| 186 | + /// ```dart |
| 187 | + /// Map<String, LDValue> newData = Map.from(data); |
| 188 | + /// newData['new-key'] = LDValue.ofString('new-value'); |
| 189 | + /// return UnmodifiableMapView(newData); |
| 190 | + /// ``` |
| 191 | + UnmodifiableMapView<String, LDValue> beforeIdentify( |
| 192 | + IdentifySeriesContext hookContext, |
| 193 | + UnmodifiableMapView<String, LDValue> data) { |
| 194 | + return data; |
| 195 | + } |
| 196 | + |
| 197 | + /// This method is called during the execution of the identify process, after |
| 198 | + /// the operation completes. |
| 199 | + /// |
| 200 | + /// [hookContext] Contains information about the evaluation being performed. |
| 201 | + /// [data] A record associated with each stage of hook invocations. Each stage |
| 202 | + /// is called with the data of the previous stage for a series. The input |
| 203 | + /// record should not be modified. |
| 204 | + /// [result] The result of the identify operation. |
| 205 | + /// |
| 206 | + /// Returns data to use when executing the next state of the hook in the |
| 207 | + /// evaluation series. It is recommended to expand the previous input into the |
| 208 | + /// return. This helps ensure your stage remains compatible moving forward as |
| 209 | + /// more stages are added. |
| 210 | + /// |
| 211 | + /// ```dart |
| 212 | + /// Map<String, LDValue> newData = Map.from(data); |
| 213 | + /// newData['new-key'] = LDValue.ofString('new-value'); |
| 214 | + /// return UnmodifiableMapView(newData); |
| 215 | + /// ``` |
| 216 | + UnmodifiableMapView<String, LDValue> afterIdentify( |
| 217 | + IdentifySeriesContext hookContext, |
| 218 | + UnmodifiableMapView<String, LDValue> data, |
| 219 | + IdentifyResult result) { |
| 220 | + return data; |
| 221 | + } |
| 222 | + |
| 223 | + /// This method is called during the execution of the track process after the |
| 224 | + /// event has been enqueued. |
| 225 | + /// |
| 226 | + /// [hookContext] Contains information about the track operation being |
| 227 | + /// performed. This is not mutable. |
| 228 | + void afterTrack(TrackSeriesContext hookContext) {} |
| 229 | +} |
0 commit comments