|
| 1 | +import Foundation |
| 2 | +import OpenFeature |
| 3 | +import Combine |
| 4 | + |
| 5 | +class SlowProvider: FeatureProvider { |
| 6 | + public static let name = "Slow" |
| 7 | + private let eventHandler = EventHandler(.stale) |
| 8 | + private var holdit: AnyCancellable? |
| 9 | + |
| 10 | + func onContextSet(oldContext: OpenFeature.EvaluationContext?, newContext: OpenFeature.EvaluationContext) { |
| 11 | + eventHandler.send(.ready) |
| 12 | + } |
| 13 | + |
| 14 | + func initialize(initialContext: OpenFeature.EvaluationContext?) { |
| 15 | + Task { |
| 16 | + // TODO Remove the sleep in the tests |
| 17 | + print(">> Sleeping") |
| 18 | + try await Task.sleep(nanoseconds: 1_000_000_000) |
| 19 | + print(">> Emitting Ready") |
| 20 | + eventHandler.send(.ready) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + var hooks: [any OpenFeature.Hook] = [] |
| 25 | + var metadata: OpenFeature.ProviderMetadata = DoMetadata() |
| 26 | + |
| 27 | + func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws |
| 28 | + -> ProviderEvaluation< |
| 29 | + Bool |
| 30 | + > |
| 31 | + { |
| 32 | + return ProviderEvaluation(value: !defaultValue) |
| 33 | + } |
| 34 | + |
| 35 | + func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws |
| 36 | + -> ProviderEvaluation< |
| 37 | + String |
| 38 | + > |
| 39 | + { |
| 40 | + return ProviderEvaluation(value: String(defaultValue.reversed())) |
| 41 | + } |
| 42 | + |
| 43 | + func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws |
| 44 | + -> ProviderEvaluation< |
| 45 | + Int64 |
| 46 | + > |
| 47 | + { |
| 48 | + return ProviderEvaluation(value: defaultValue * 100) |
| 49 | + } |
| 50 | + |
| 51 | + func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws |
| 52 | + -> ProviderEvaluation< |
| 53 | + Double |
| 54 | + > |
| 55 | + { |
| 56 | + return ProviderEvaluation(value: defaultValue * 100) |
| 57 | + } |
| 58 | + |
| 59 | + func getObjectEvaluation(key: String, defaultValue: Value, context: EvaluationContext?) throws |
| 60 | + -> ProviderEvaluation< |
| 61 | + Value |
| 62 | + > |
| 63 | + { |
| 64 | + return ProviderEvaluation(value: .null) |
| 65 | + } |
| 66 | + |
| 67 | + func observe() -> AnyPublisher<OpenFeature.ProviderEvent, Never> { |
| 68 | + eventHandler.observe() |
| 69 | + } |
| 70 | + |
| 71 | + public struct DoMetadata: ProviderMetadata { |
| 72 | + public var name: String? = DoSomethingProvider.name |
| 73 | + } |
| 74 | +} |
0 commit comments