From 18287c226291df88fb1518d184d5838455515ce7 Mon Sep 17 00:00:00 2001 From: vahid torkaman Date: Tue, 23 Jan 2024 17:46:02 +0100 Subject: [PATCH] map to empty stream in case of setting provider to nil Signed-off-by: vahid torkaman --- Sources/OpenFeature/EventHandler.swift | 6 +++--- Sources/OpenFeature/OpenFeatureAPI.swift | 14 +++++++++++--- Sources/OpenFeature/Provider/NoOpProvider.swift | 2 +- .../DeveloperExperienceTests.swift | 4 +++- .../Helpers/AlwaysBrokenProvider.swift | 2 +- .../Helpers/DoSomethingProvider.swift | 2 +- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Sources/OpenFeature/EventHandler.swift b/Sources/OpenFeature/EventHandler.swift index dad6388..f1a5680 100644 --- a/Sources/OpenFeature/EventHandler.swift +++ b/Sources/OpenFeature/EventHandler.swift @@ -8,8 +8,8 @@ public class EventHandler: EventSender, EventPublisher { eventState = CurrentValueSubject(ProviderEvent.stale) } - public func observe() -> CurrentValueSubject { - return eventState + public func observe() -> AnyPublisher { + return eventState.eraseToAnyPublisher() } public func send( @@ -20,7 +20,7 @@ public class EventHandler: EventSender, EventPublisher { } public protocol EventPublisher { - func observe() -> CurrentValueSubject + func observe() -> AnyPublisher } public protocol EventSender { diff --git a/Sources/OpenFeature/OpenFeatureAPI.swift b/Sources/OpenFeature/OpenFeatureAPI.swift index ec18e86..8e0c2fc 100644 --- a/Sources/OpenFeature/OpenFeatureAPI.swift +++ b/Sources/OpenFeature/OpenFeatureAPI.swift @@ -73,11 +73,19 @@ public class OpenFeatureAPI { self.hooks.removeAll() } - public func observe() -> any Publisher { + public func observe() -> AnyPublisher { return providerSubject - .compactMap{ $0 } - .map({ provider in provider.observe()}) + .map({ provider in + if let provider = provider { + return provider.observe() + } else { + return Empty() + .eraseToAnyPublisher() + } + }) .switchToLatest() + .eraseToAnyPublisher() + } struct Handler { diff --git a/Sources/OpenFeature/Provider/NoOpProvider.swift b/Sources/OpenFeature/Provider/NoOpProvider.swift index 858228e..6f35cbc 100644 --- a/Sources/OpenFeature/Provider/NoOpProvider.swift +++ b/Sources/OpenFeature/Provider/NoOpProvider.swift @@ -67,7 +67,7 @@ class NoOpProvider: FeatureProvider { value: defaultValue, variant: NoOpProvider.passedInDefault, reason: Reason.defaultReason.rawValue) } - func observe() -> CurrentValueSubject { + func observe() -> AnyPublisher { return eventHandler.observe() } } diff --git a/Tests/OpenFeatureTests/DeveloperExperienceTests.swift b/Tests/OpenFeatureTests/DeveloperExperienceTests.swift index ac4d500..f43b957 100644 --- a/Tests/OpenFeatureTests/DeveloperExperienceTests.swift +++ b/Tests/OpenFeatureTests/DeveloperExperienceTests.swift @@ -39,11 +39,13 @@ final class DeveloperExperienceTests: XCTestCase { OpenFeatureAPI.shared.setProvider(provider: provider) wait(for: [readyExpectation], timeout: 5) + OpenFeatureAPI.shared.clearProvider() + // Clearing the Provider shouldn't send further global events from it eventState = OpenFeatureAPI.shared.observe().sink { _ in XCTFail("Unexpected event") } - OpenFeatureAPI.shared.clearProvider() + provider.initialize(initialContext: MutableContext(attributes: ["Test": Value.string("Test")])) XCTAssertNotNil(eventState) diff --git a/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift b/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift index ecefc8d..f660e7a 100644 --- a/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift +++ b/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift @@ -46,7 +46,7 @@ class AlwaysBrokenProvider: FeatureProvider { throw OpenFeatureError.flagNotFoundError(key: key) } - func observe() -> CurrentValueSubject { + func observe() -> AnyPublisher { eventHandler.observe() } } diff --git a/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift b/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift index 444a0e5..c58a4e2 100644 --- a/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift +++ b/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift @@ -58,7 +58,7 @@ class DoSomethingProvider: FeatureProvider { return ProviderEvaluation(value: .null) } - func observe() -> CurrentValueSubject { + func observe() -> AnyPublisher { eventHandler.observe() }