@@ -15,7 +15,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
15
15
public var hooks : [ any Hook ] = [ ]
16
16
private let lock = UnfairLock ( )
17
17
private let initializationStrategy : InitializationStrategy
18
- private let eventHandler = EventHandler ( ProviderEvent . notReady )
18
+ private let eventHandler = EventHandler ( )
19
19
private let confidence : Confidence
20
20
private let confidenceFeatureProviderQueue = DispatchQueue ( label: " com.provider.queue " )
21
21
private var cancellables = Set < AnyCancellable > ( )
@@ -26,41 +26,29 @@ public class ConfidenceFeatureProvider: FeatureProvider {
26
26
The `initializationStrategy` defines when the Provider is ready to read flags, before or after a refresh of the flag evaluation fata.
27
27
*/
28
28
public convenience init ( confidence: Confidence , initializationStrategy: InitializationStrategy = . fetchAndActivate) {
29
- self . init ( confidence: confidence, session: nil )
29
+ self . init ( confidence: confidence, initializationStrategy : initializationStrategy , session: nil )
30
30
}
31
31
32
32
internal init (
33
33
confidence: Confidence ,
34
- initializationStrategy: InitializationStrategy = . fetchAndActivate ,
34
+ initializationStrategy: InitializationStrategy ,
35
35
session: URLSession ?
36
36
) {
37
37
self . metadata = Metadata ( name: ConfidenceFeatureProvider . providerId)
38
38
self . initializationStrategy = initializationStrategy
39
39
self . confidence = confidence
40
40
}
41
41
42
- public func initialize( initialContext: OpenFeature . EvaluationContext ? ) {
42
+ public func initialize( initialContext: OpenFeature . EvaluationContext ? ) async throws {
43
43
let context = ConfidenceTypeMapper . from ( ctx: initialContext ?? MutableContext ( attributes: [ : ] ) )
44
44
confidence. putContextLocal ( context: context)
45
- do {
46
- if initializationStrategy == . activateAndFetchAsync {
47
- try confidence. activate ( )
48
- eventHandler. send ( . ready)
49
- Task {
50
- await confidence. asyncFetch ( )
51
- }
52
- } else {
53
- Task {
54
- do {
55
- try await confidence. fetchAndActivate ( )
56
- eventHandler. send ( . ready)
57
- } catch {
58
- eventHandler. send ( . error)
59
- }
60
- }
45
+ if initializationStrategy == . activateAndFetchAsync {
46
+ try confidence. activate ( )
47
+ Task {
48
+ await confidence. asyncFetch ( )
61
49
}
62
- } catch {
63
- eventHandler . send ( . error )
50
+ } else {
51
+ try await confidence . fetchAndActivate ( )
64
52
}
65
53
}
66
54
@@ -75,14 +63,13 @@ public class ConfidenceFeatureProvider: FeatureProvider {
75
63
public func onContextSet(
76
64
oldContext: OpenFeature . EvaluationContext ? ,
77
65
newContext: OpenFeature . EvaluationContext
78
- ) {
66
+ ) async {
79
67
let removedKeys : [ String ] = oldContext. map {
80
68
Array ( $0. asMap ( ) . filter { key, _ in !newContext. asMap ( ) . keys. contains ( key) } . keys)
81
69
} ?? [ ]
82
-
83
- Task {
84
- confidence. putContext ( context: ConfidenceTypeMapper . from ( ctx: newContext) , removedKeys: removedKeys)
85
- }
70
+ await confidence. putContextAndWait (
71
+ context: ConfidenceTypeMapper . from ( ctx: newContext) ,
72
+ removedKeys: removedKeys)
86
73
}
87
74
88
75
public func getBooleanEvaluation( key: String , defaultValue: Bool , context: EvaluationContext ? ) throws
@@ -115,7 +102,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
115
102
try confidence. getEvaluation ( key: key, defaultValue: defaultValue) . toProviderEvaluation ( )
116
103
}
117
104
118
- public func observe( ) -> AnyPublisher < OpenFeature . ProviderEvent , Never > {
105
+ public func observe( ) -> AnyPublisher < OpenFeature . ProviderEvent ? , Never > {
119
106
return eventHandler. observe ( )
120
107
}
121
108
0 commit comments