|
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// A provider that combines multiple providers into a single provider. |
| 5 | +public class MultiProvider: FeatureProvider { |
| 6 | + |
| 7 | + public var hooks: [any Hook] { |
| 8 | + [] |
| 9 | + } |
| 10 | + |
| 11 | + public static let name = "MultiProvider" |
| 12 | + public var metadata: ProviderMetadata = MultiProvider.MultiProviderMetadata() |
| 13 | + |
| 14 | + private let providers: [FeatureProvider] |
| 15 | + private let strategy: Strategy |
| 16 | + |
| 17 | + /// Initialize a MultiProvider with a list of providers and a strategy. |
| 18 | + /// - Parameters: |
| 19 | + /// - providers: A list of providers to evaluate. |
| 20 | + /// - strategy: A strategy to evaluate the providers. Defaults to FirstMatchStrategy. |
| 21 | + public init( |
| 22 | + providers: [FeatureProvider], |
| 23 | + strategy: Strategy = FirstMatchStrategy() |
| 24 | + ) { |
| 25 | + self.providers = providers |
| 26 | + self.strategy = strategy |
| 27 | + } |
| 28 | + |
| 29 | + public func initialize(initialContext: EvaluationContext?) async throws { |
| 30 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 31 | + for provider in providers { |
| 32 | + group.addTask { |
| 33 | + try await provider.initialize(initialContext: initialContext) |
| 34 | + } |
| 35 | + } |
| 36 | + try await group.waitForAll() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public func onContextSet(oldContext: EvaluationContext?, newContext: EvaluationContext) async throws { |
| 41 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 42 | + for provider in providers { |
| 43 | + group.addTask { |
| 44 | + try await provider.onContextSet(oldContext: oldContext, newContext: newContext) |
| 45 | + } |
| 46 | + } |
| 47 | + try await group.waitForAll() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws |
| 52 | + -> ProviderEvaluation<Bool> |
| 53 | + { |
| 54 | + return try strategy.evaluate( |
| 55 | + providers: providers, key: key, defaultValue: defaultValue, evaluationContext: context, |
| 56 | + flagEvaluation: { provider in |
| 57 | + return provider.getBooleanEvaluation(key:defaultValue:context:) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + public func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws |
| 62 | + -> ProviderEvaluation<String> |
| 63 | + { |
| 64 | + return try strategy.evaluate( |
| 65 | + providers: providers, key: key, defaultValue: defaultValue, evaluationContext: context, |
| 66 | + flagEvaluation: { provider in |
| 67 | + return provider.getStringEvaluation(key:defaultValue:context:) |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + public func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws |
| 72 | + -> ProviderEvaluation<Int64> |
| 73 | + { |
| 74 | + return try strategy.evaluate( |
| 75 | + providers: providers, key: key, defaultValue: defaultValue, evaluationContext: context, |
| 76 | + flagEvaluation: { provider in |
| 77 | + return provider.getIntegerEvaluation(key:defaultValue:context:) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + public func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws |
| 82 | + -> ProviderEvaluation<Double> |
| 83 | + { |
| 84 | + return try strategy.evaluate( |
| 85 | + providers: providers, key: key, defaultValue: defaultValue, evaluationContext: context, |
| 86 | + flagEvaluation: { provider in |
| 87 | + return provider.getDoubleEvaluation(key:defaultValue:context:) |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + public func getObjectEvaluation(key: String, defaultValue: Value, context: EvaluationContext?) throws |
| 92 | + -> ProviderEvaluation<Value> |
| 93 | + { |
| 94 | + return try strategy.evaluate( |
| 95 | + providers: providers, key: key, defaultValue: defaultValue, evaluationContext: context, |
| 96 | + flagEvaluation: { provider in |
| 97 | + return provider.getObjectEvaluation(key:defaultValue:context:) |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + public func observe() -> AnyPublisher<ProviderEvent?, Never> { |
| 102 | + return Publishers.MergeMany(providers.map { $0.observe() }).eraseToAnyPublisher() |
| 103 | + } |
| 104 | + |
| 105 | + public struct MultiProviderMetadata: ProviderMetadata { |
| 106 | + public var name: String? = MultiProvider.name |
| 107 | + } |
| 108 | +} |
0 commit comments