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 5aea334
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 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": "ba7c072e2a0d810995e38c1a8d9375130b7c79f6",
"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
43 changes: 21 additions & 22 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,24 @@ public class ConfidenceFeatureProvider: FeatureProvider {
self.confidence = confidence
}

public func initialize(initialContext: OpenFeature.EvaluationContext?) {
public func initialize(initialContext: OpenFeature.EvaluationContext?) 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()
}
} else {
let semaphore = DispatchSemaphore(value: 0)
Task {
do {
try await confidence.fetchAndActivate()
} catch {
}
semaphore.signal()
}
} catch {
eventHandler.send(.error)
semaphore.wait()
}
}

Expand All @@ -80,9 +76,12 @@ public class ConfidenceFeatureProvider: FeatureProvider {
Array($0.asMap().filter { key, _ in !newContext.asMap().keys.contains(key) }.keys)
} ?? []

let semaphore = DispatchSemaphore(value: 0)
Task {
confidence.putContext(context: ConfidenceTypeMapper.from(ctx: newContext), removedKeys: removedKeys)
await confidence.putContextAndWait(context: ConfidenceTypeMapper.from(ctx: newContext), removedKeys: removedKeys)

Check failure on line 81 in Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 125 characters (line_length)
semaphore.signal()
}
semaphore.wait()
}

public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws
Expand Down Expand Up @@ -115,7 +114,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 5aea334

Please sign in to comment.