Skip to content

Commit 652d5f0

Browse files
feat: Adopt latest OF SDK (#184)
* feat!: Adopt newer OF SDK Signed-off-by: Fabrizio Demaria <[email protected]> * deps: Pin OF SDK version Signed-off-by: Fabrizio Demaria <[email protected]> * fix: Small demo app fix Signed-off-by: Fabrizio Demaria <[email protected]> * deps: Remove OF dependency from demo app Signed-off-by: Fabrizio Demaria <[email protected]> --------- Signed-off-by: Fabrizio Demaria <[email protected]>
1 parent 3588ae8 commit 652d5f0

File tree

7 files changed

+43
-52
lines changed

7 files changed

+43
-52
lines changed

ConfidenceDemoApp/ConfidenceDemoApp.xcodeproj/project.pbxproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
733219BF2BE3C11100747AC2 /* ConfidenceOpenFeature in Frameworks */ = {isa = PBXBuildFile; productRef = 733219BE2BE3C11100747AC2 /* ConfidenceOpenFeature */; };
10+
734EBDDC2D565CAC006D3435 /* Confidence in Frameworks */ = {isa = PBXBuildFile; productRef = 734EBDDB2D565CAC006D3435 /* Confidence */; };
1111
735EADF52CF9B64E007BC42C /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 735EADF42CF9B64E007BC42C /* LoginView.swift */; };
1212
C770C99A2A739FBC00C2AC8C /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C770C9962A739FBC00C2AC8C /* Preview Assets.xcassets */; };
1313
C770C99B2A739FBC00C2AC8C /* ConfidenceDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C770C9972A739FBC00C2AC8C /* ConfidenceDemoApp.swift */; };
@@ -56,7 +56,7 @@
5656
isa = PBXFrameworksBuildPhase;
5757
buildActionMask = 2147483647;
5858
files = (
59-
733219BF2BE3C11100747AC2 /* ConfidenceOpenFeature in Frameworks */,
59+
734EBDDC2D565CAC006D3435 /* Confidence in Frameworks */,
6060
);
6161
runOnlyForDeploymentPostprocessing = 0;
6262
};
@@ -169,7 +169,7 @@
169169
);
170170
name = ConfidenceDemoApp;
171171
packageProductDependencies = (
172-
733219BE2BE3C11100747AC2 /* ConfidenceOpenFeature */,
172+
734EBDDB2D565CAC006D3435 /* Confidence */,
173173
);
174174
productName = ConfidenceDemoApp;
175175
productReference = C770C9682A739FA000C2AC8C /* ConfidenceDemoApp.app */;
@@ -646,9 +646,9 @@
646646
/* End XCConfigurationList section */
647647

648648
/* Begin XCSwiftPackageProductDependency section */
649-
733219BE2BE3C11100747AC2 /* ConfidenceOpenFeature */ = {
649+
734EBDDB2D565CAC006D3435 /* Confidence */ = {
650650
isa = XCSwiftPackageProductDependency;
651-
productName = ConfidenceOpenFeature;
651+
productName = Confidence;
652652
};
653653
/* End XCSwiftPackageProductDependency section */
654654
};

ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct ConfidenceDemoApp: App {
2626
withAppInfo: true,
2727
withOsInfo: true,
2828
withLocale: true
29-
).decorated(context: [:]);
29+
).decorated(context: context);
3030

3131
confidence = Confidence
3232
.Builder(clientSecret: secret, loggerLevel: .TRACE)

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"repositoryURL": "[email protected]:open-feature/swift-sdk.git",
77
"state": {
88
"branch": null,
9-
"revision": "02b033c954766e86d5706bfc8ee5248244c11e77",
10-
"version": "0.1.0"
9+
"revision": "e2be5852827d7d6b837b9a4e577bb52bea6322d7",
10+
"version": "0.3.0"
1111
}
1212
}
1313
]

Package.swift

+1-1
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", .exact("0.3.0")),
2222
],
2323
targets: [
2424
.target(

Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift

+15-28
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,41 +26,29 @@ 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(
3333
confidence: Confidence,
34-
initializationStrategy: InitializationStrategy = .fetchAndActivate,
34+
initializationStrategy: InitializationStrategy,
3535
session: URLSession?
3636
) {
3737
self.metadata = Metadata(name: ConfidenceFeatureProvider.providerId)
3838
self.initializationStrategy = initializationStrategy
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

+16-12
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)

api/ConfidenceProvider_public_api.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
},
99
{
1010
"name": "initialize(initialContext:)",
11-
"declaration": "public func initialize(initialContext: OpenFeature.EvaluationContext?)"
11+
"declaration": "public func initialize(initialContext: OpenFeature.EvaluationContext?) async throws"
1212
},
1313
{
1414
"name": "onContextSet(oldContext:newContext:)",
15-
"declaration": "public func onContextSet(\n oldContext: OpenFeature.EvaluationContext?,\n newContext: OpenFeature.EvaluationContext\n)"
15+
"declaration": "public func onContextSet(\n oldContext: OpenFeature.EvaluationContext?,\n newContext: OpenFeature.EvaluationContext\n) async"
1616
},
1717
{
1818
"name": "getBooleanEvaluation(key:defaultValue:context:)",
@@ -36,7 +36,7 @@
3636
},
3737
{
3838
"name": "observe()",
39-
"declaration": "public func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never>"
39+
"declaration": "public func observe() -> AnyPublisher<OpenFeature.ProviderEvent?, Never>"
4040
}
4141
]
4242
}

0 commit comments

Comments
 (0)