-
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.
feat: state and event for onContextChanged
Signed-off-by: Fabrizio Demaria <[email protected]>
- Loading branch information
1 parent
96f51a7
commit 40d0812
Showing
4 changed files
with
113 additions
and
5 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
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,79 @@ | ||
import Combine | ||
import Foundation | ||
import OpenFeature | ||
|
||
class StaggeredProvider: FeatureProvider { | ||
public static let name = "Something" | ||
private let eventHandler = EventHandler() | ||
private let onContextSetSemaphore: DispatchSemaphore? | ||
|
||
init(onContextSetSemaphore: DispatchSemaphore?) { | ||
self.onContextSetSemaphore = onContextSetSemaphore | ||
} | ||
|
||
func onContextSet(oldContext: OpenFeature.EvaluationContext?, newContext: OpenFeature.EvaluationContext) { | ||
onContextSetSemaphore?.wait() | ||
} | ||
|
||
func initialize(initialContext: OpenFeature.EvaluationContext?) { | ||
} | ||
|
||
var hooks: [any OpenFeature.Hook] = [] | ||
var metadata: OpenFeature.ProviderMetadata = DoMetadata() | ||
|
||
func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Bool | ||
> | ||
{ | ||
return ProviderEvaluation(value: !defaultValue, flagMetadata: DoSomethingProvider.flagMetadataMap) | ||
} | ||
|
||
func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
String | ||
> | ||
{ | ||
return ProviderEvaluation( | ||
value: String(defaultValue.reversed()), flagMetadata: DoSomethingProvider.flagMetadataMap) | ||
} | ||
|
||
func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Int64 | ||
> | ||
{ | ||
return ProviderEvaluation(value: defaultValue * 100, flagMetadata: DoSomethingProvider.flagMetadataMap) | ||
} | ||
|
||
func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Double | ||
> | ||
{ | ||
return ProviderEvaluation(value: defaultValue * 100, flagMetadata: DoSomethingProvider.flagMetadataMap) | ||
} | ||
|
||
func getObjectEvaluation(key: String, defaultValue: Value, context: EvaluationContext?) throws | ||
-> ProviderEvaluation< | ||
Value | ||
> | ||
{ | ||
return ProviderEvaluation(value: .null, flagMetadata: DoSomethingProvider.flagMetadataMap) | ||
} | ||
|
||
func observe() -> AnyPublisher<ProviderEvent?, Never> { | ||
eventHandler.observe() | ||
} | ||
|
||
public struct DoMetadata: ProviderMetadata { | ||
public var name: String? = DoSomethingProvider.name | ||
} | ||
|
||
public static let flagMetadataMap = [ | ||
"int-metadata": FlagMetadataValue.integer(99), | ||
"double-metadata": FlagMetadataValue.double(98.4), | ||
"string-metadata": FlagMetadataValue.string("hello-world"), | ||
"boolean-metadata": FlagMetadataValue.boolean(true), | ||
] | ||
} |