Skip to content

Commit 0ce146b

Browse files
feat: [wip] Adopt newer OF SDK
1 parent 848d9ae commit 0ce146b

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"package": "OpenFeature",
66
"repositoryURL": "[email protected]:open-feature/swift-sdk.git",
77
"state": {
8-
"branch": null,
9-
"revision": "02b033c954766e86d5706bfc8ee5248244c11e77",
10-
"version": "0.1.0"
8+
"branch": "fatal-state",
9+
"revision": "ac150987ad2ff7657e7d976cd304880bc6d33784",
10+
"version": null
1111
}
1212
}
1313
]

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
targets: ["Confidence"])
1919
],
2020
dependencies: [
21-
.package(url: "[email protected]:open-feature/swift-sdk.git", from: "0.1.0"),
21+
.package(url: "[email protected]:open-feature/swift-sdk.git", branch: "fatal-state"),
2222
],
2323
targets: [
2424
.target(

Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
1515
public var hooks: [any Hook] = []
1616
private let lock = UnfairLock()
1717
private let initializationStrategy: InitializationStrategy
18-
private let eventHandler = EventHandler(ProviderEvent.notReady)
18+
private let eventHandler = EventHandler()
1919
private let confidence: Confidence
2020
private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue")
2121
private var cancellables = Set<AnyCancellable>()
@@ -26,7 +26,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
2626
The `initializationStrategy` defines when the Provider is ready to read flags, before or after a refresh of the flag evaluation fata.
2727
*/
2828
public convenience init(confidence: Confidence, initializationStrategy: InitializationStrategy = .fetchAndActivate) {
29-
self.init(confidence: confidence, session: nil)
29+
self.init(confidence: confidence, initializationStrategy: initializationStrategy, session: nil)
3030
}
3131

3232
internal init(
@@ -39,28 +39,16 @@ public class ConfidenceFeatureProvider: FeatureProvider {
3939
self.confidence = confidence
4040
}
4141

42-
public func initialize(initialContext: OpenFeature.EvaluationContext?) {
42+
public func initialize(initialContext: OpenFeature.EvaluationContext?) async throws {
4343
let context = ConfidenceTypeMapper.from(ctx: initialContext ?? MutableContext(attributes: [:]))
4444
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()
6149
}
62-
} catch {
63-
eventHandler.send(.error)
50+
} else {
51+
try await confidence.fetchAndActivate()
6452
}
6553
}
6654

@@ -75,14 +63,13 @@ public class ConfidenceFeatureProvider: FeatureProvider {
7563
public func onContextSet(
7664
oldContext: OpenFeature.EvaluationContext?,
7765
newContext: OpenFeature.EvaluationContext
78-
) {
66+
) async {
7967
let removedKeys: [String] = oldContext.map {
8068
Array($0.asMap().filter { key, _ in !newContext.asMap().keys.contains(key) }.keys)
8169
} ?? []
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)
8673
}
8774

8875
public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws
@@ -115,7 +102,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
115102
try confidence.getEvaluation(key: key, defaultValue: defaultValue).toProviderEvaluation()
116103
}
117104

118-
public func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> {
105+
public func observe() -> AnyPublisher<OpenFeature.ProviderEvent?, Never> {
119106
return eventHandler.observe()
120107
}
121108

Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ class ConfidenceProviderTest: XCTestCase {
2121
.withFlagResolverClient(flagResolver: client)
2222
.build()
2323

24-
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
25-
OpenFeatureAPI.shared.setProvider(provider: provider)
26-
2724
let cancellable = OpenFeatureAPI.shared.observe().sink { event in
2825
if event == .ready {
2926
readyExpectation.fulfill()
3027
} else {
31-
print(event)
28+
print(event.debugDescription)
3229
}
3330
}
31+
32+
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
33+
OpenFeatureAPI.shared.setProvider(provider: provider)
34+
3435
await fulfillment(of: [readyExpectation], timeout: 5.0)
3536
cancellable.cancel()
3637
}
@@ -60,16 +61,19 @@ class ConfidenceProviderTest: XCTestCase {
6061
.withStorage(storage: FakeStorage())
6162
.build()
6263

63-
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
64-
OpenFeatureAPI.shared.setProvider(provider: provider)
65-
6664
let cancellable = OpenFeatureAPI.shared.observe().sink { event in
67-
if event == .error {
68-
errorExpectation.fulfill()
69-
} else {
70-
print(event)
65+
if let event = event {
66+
if case .error = event {
67+
errorExpectation.fulfill()
68+
} else {
69+
// no-op
70+
}
7171
}
7272
}
73+
74+
let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
75+
OpenFeatureAPI.shared.setProvider(provider: provider)
76+
7377
await fulfillment(of: [errorExpectation], timeout: 5.0)
7478
cancellable.cancel()
7579
}
@@ -103,7 +107,7 @@ class ConfidenceProviderTest: XCTestCase {
103107
if event == .ready {
104108
readyExpectation.fulfill()
105109
} else {
106-
print(event)
110+
print(event.debugDescription)
107111
}
108112
}
109113
await fulfillment(of: [readyExpectation], timeout: 1.0)

0 commit comments

Comments
 (0)