-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds `setProviderAndWait` extension function, exposed by this library as a user-facing API (documentation also updated). The application can now use `async/await` to wait for the Provider to be ready, before reading flags. The older alternative (still available) is for the application to call `setProvider` and listen for `.ready` event manually. --------- Signed-off-by: Fabrizio Demaria <[email protected]>
- Loading branch information
1 parent
053dabc
commit 3ce6b8d
Showing
4 changed files
with
145 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Tests/OpenFeatureTests/Helpers/InjectableEventHandlerProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Foundation | ||
import OpenFeature | ||
import Combine | ||
|
||
class InjectableEventHandlerProvider: FeatureProvider { | ||
public static let name = "InjectableEventHandler" | ||
private let eventHandler: EventHandler | ||
|
||
init(eventHandler: EventHandler) { | ||
self.eventHandler = eventHandler | ||
} | ||
|
||
func onContextSet(oldContext: OpenFeature.EvaluationContext?, newContext: OpenFeature.EvaluationContext) { | ||
// Emit stale, then let the parent test control events via eventHandler | ||
eventHandler.send(.stale) | ||
} | ||
|
||
func initialize(initialContext: OpenFeature.EvaluationContext?) { | ||
// Emit stale, then let the parent test control events via eventHandler | ||
eventHandler.send(.stale) | ||
} | ||
|
||
var hooks: [any OpenFeature.Hook] = [] | ||
var metadata: OpenFeature.ProviderMetadata = InjectableEventHandlerMetadata() | ||
|
||
func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Bool | ||
> | ||
{ | ||
return ProviderEvaluation(value: !defaultValue) | ||
} | ||
|
||
func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
String | ||
> | ||
{ | ||
return ProviderEvaluation(value: String(defaultValue.reversed())) | ||
} | ||
|
||
func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Int64 | ||
> | ||
{ | ||
return ProviderEvaluation(value: defaultValue * 100) | ||
} | ||
|
||
func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Double | ||
> | ||
{ | ||
return ProviderEvaluation(value: defaultValue * 100) | ||
} | ||
|
||
func getObjectEvaluation(key: String, defaultValue: Value, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Value | ||
> | ||
{ | ||
return ProviderEvaluation(value: .null) | ||
} | ||
|
||
func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> { | ||
eventHandler.observe() | ||
} | ||
|
||
public struct InjectableEventHandlerMetadata: ProviderMetadata { | ||
public var name: String? = InjectableEventHandlerProvider.name | ||
} | ||
} |