Skip to content

Commit

Permalink
map to empty stream in case of setting provider to nil
Browse files Browse the repository at this point in the history
Signed-off-by: vahid torkaman <[email protected]>
  • Loading branch information
vahidlazio committed Jan 23, 2024
1 parent e688f09 commit 43980b7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Sources/OpenFeature/EventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class EventHandler: EventSender, EventPublisher {
eventState = CurrentValueSubject<ProviderEvent, Never>(ProviderEvent.stale)
}

public func observe() -> CurrentValueSubject<ProviderEvent, Never> {
return eventState
public func observe() -> AnyPublisher<ProviderEvent, Never> {
return eventState.eraseToAnyPublisher()
}

public func send(
Expand All @@ -20,7 +20,7 @@ public class EventHandler: EventSender, EventPublisher {
}

public protocol EventPublisher {
func observe() -> CurrentValueSubject<ProviderEvent, Never>
func observe() -> AnyPublisher<ProviderEvent, Never>
}

public protocol EventSender {
Expand Down
14 changes: 11 additions & 3 deletions Sources/OpenFeature/OpenFeatureAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ public class OpenFeatureAPI {
self.hooks.removeAll()
}

public func observe() -> any Publisher<ProviderEvent, Never> {
public func observe() -> AnyPublisher<ProviderEvent, Never> {
return providerSubject

Check warning on line 77 in Sources/OpenFeature/OpenFeatureAPI.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Closure Violation: Trailing closure syntax should be used whenever possible (trailing_closure)
.compactMap{ $0 }
.map({ provider in provider.observe()})
.map({ provider in
if let provider = provider {
return provider.observe()
} else {
return Empty<ProviderEvent, Never>()
.eraseToAnyPublisher()
}
})
.switchToLatest()
.eraseToAnyPublisher()

Check warning on line 88 in Sources/OpenFeature/OpenFeatureAPI.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace before Closing Braces Violation: Don't include vertical whitespace (empty line) before closing braces (vertical_whitespace_closing_braces)
}

struct Handler {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenFeature/Provider/NoOpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NoOpProvider: FeatureProvider {
value: defaultValue, variant: NoOpProvider.passedInDefault, reason: Reason.defaultReason.rawValue)
}

func observe() -> CurrentValueSubject<ProviderEvent, Never> {
func observe() -> AnyPublisher<ProviderEvent, Never> {
return eventHandler.observe()
}
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/OpenFeatureTests/DeveloperExperienceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AlwaysBrokenProvider: FeatureProvider {
throw OpenFeatureError.flagNotFoundError(key: key)
}

func observe() -> CurrentValueSubject<ProviderEvent, Never> {
func observe() -> AnyPublisher<ProviderEvent, Never> {
eventHandler.observe()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DoSomethingProvider: FeatureProvider {
return ProviderEvaluation(value: .null)
}

func observe() -> CurrentValueSubject<ProviderEvent, Never> {
func observe() -> AnyPublisher<ProviderEvent, Never> {
eventHandler.observe()
}

Expand Down

0 comments on commit 43980b7

Please sign in to comment.