Skip to content

Commit

Permalink
feat: [wip] Adopt newer OF SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Dec 18, 2024
1 parent 848d9ae commit 0ce146b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"package": "OpenFeature",
"repositoryURL": "[email protected]:open-feature/swift-sdk.git",
"state": {
"branch": null,
"revision": "02b033c954766e86d5706bfc8ee5248244c11e77",
"version": "0.1.0"
"branch": "fatal-state",
"revision": "ac150987ad2ff7657e7d976cd304880bc6d33784",
"version": null
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
targets: ["Confidence"])
],
dependencies: [
.package(url: "[email protected]:open-feature/swift-sdk.git", from: "0.1.0"),
.package(url: "[email protected]:open-feature/swift-sdk.git", branch: "fatal-state"),
],
targets: [
.target(
Expand Down
41 changes: 14 additions & 27 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
public var hooks: [any Hook] = []
private let lock = UnfairLock()
private let initializationStrategy: InitializationStrategy
private let eventHandler = EventHandler(ProviderEvent.notReady)
private let eventHandler = EventHandler()
private let confidence: Confidence
private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue")
private var cancellables = Set<AnyCancellable>()
Expand All @@ -26,7 +26,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
The `initializationStrategy` defines when the Provider is ready to read flags, before or after a refresh of the flag evaluation fata.
*/
public convenience init(confidence: Confidence, initializationStrategy: InitializationStrategy = .fetchAndActivate) {
self.init(confidence: confidence, session: nil)
self.init(confidence: confidence, initializationStrategy: initializationStrategy, session: nil)
}

internal init(
Expand All @@ -39,28 +39,16 @@ public class ConfidenceFeatureProvider: FeatureProvider {
self.confidence = confidence
}

public func initialize(initialContext: OpenFeature.EvaluationContext?) {
public func initialize(initialContext: OpenFeature.EvaluationContext?) async throws {
let context = ConfidenceTypeMapper.from(ctx: initialContext ?? MutableContext(attributes: [:]))
confidence.putContextLocal(context: context)
do {
if initializationStrategy == .activateAndFetchAsync {
try confidence.activate()
eventHandler.send(.ready)
Task {
await confidence.asyncFetch()
}
} else {
Task {
do {
try await confidence.fetchAndActivate()
eventHandler.send(.ready)
} catch {
eventHandler.send(.error)
}
}
if initializationStrategy == .activateAndFetchAsync {
try confidence.activate()
Task {
await confidence.asyncFetch()
}
} catch {
eventHandler.send(.error)
} else {
try await confidence.fetchAndActivate()
}
}

Expand All @@ -75,14 +63,13 @@ public class ConfidenceFeatureProvider: FeatureProvider {
public func onContextSet(
oldContext: OpenFeature.EvaluationContext?,
newContext: OpenFeature.EvaluationContext
) {
) async {
let removedKeys: [String] = oldContext.map {
Array($0.asMap().filter { key, _ in !newContext.asMap().keys.contains(key) }.keys)
} ?? []

Task {
confidence.putContext(context: ConfidenceTypeMapper.from(ctx: newContext), removedKeys: removedKeys)
}
await confidence.putContextAndWait(
context: ConfidenceTypeMapper.from(ctx: newContext),
removedKeys: removedKeys)
}

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

public func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> {
public func observe() -> AnyPublisher<OpenFeature.ProviderEvent?, Never> {
return eventHandler.observe()
}

Expand Down
28 changes: 16 additions & 12 deletions Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ class ConfidenceProviderTest: XCTestCase {
.withFlagResolverClient(flagResolver: client)
.build()

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

let cancellable = OpenFeatureAPI.shared.observe().sink { event in
if event == .ready {
readyExpectation.fulfill()
} else {
print(event)
print(event.debugDescription)
}
}

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

await fulfillment(of: [readyExpectation], timeout: 5.0)
cancellable.cancel()
}
Expand Down Expand Up @@ -60,16 +61,19 @@ class ConfidenceProviderTest: XCTestCase {
.withStorage(storage: FakeStorage())
.build()

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

let cancellable = OpenFeatureAPI.shared.observe().sink { event in
if event == .error {
errorExpectation.fulfill()
} else {
print(event)
if let event = event {
if case .error = event {
errorExpectation.fulfill()
} else {
// no-op
}
}
}

let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync)
OpenFeatureAPI.shared.setProvider(provider: provider)

await fulfillment(of: [errorExpectation], timeout: 5.0)
cancellable.cancel()
}
Expand Down Expand Up @@ -103,7 +107,7 @@ class ConfidenceProviderTest: XCTestCase {
if event == .ready {
readyExpectation.fulfill()
} else {
print(event)
print(event.debugDescription)
}
}
await fulfillment(of: [readyExpectation], timeout: 1.0)
Expand Down

0 comments on commit 0ce146b

Please sign in to comment.