Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with CurrentValueRelay and PassthroughRelay subscription #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Sources/Relays/CurrentValueRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ public class CurrentValueRelay<Output>: Relay {
subscriber.receive(subscription: subscription)
}

public func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never {
publisher.subscribe(storage)
}

public func subscribe<P: Relay>(_ publisher: P) -> AnyCancellable where Output == P.Output {
publisher.subscribe(storage)
}

deinit {
// Send a finished event upon dealloation
subscriptions.forEach { $0.forceFinish() }
Expand Down
8 changes: 0 additions & 8 deletions Sources/Relays/PassthroughRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public class PassthroughRelay<Output>: Relay {
subscriber.receive(subscription: subscription)
}

public func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never {
publisher.subscribe(storage)
}

public func subscribe<P: Relay>(_ publisher: P) -> AnyCancellable where Output == P.Output {
publisher.subscribe(storage)
}

deinit {
// Send a finished event upon dealloation
subscriptions.forEach { $0.forceFinish() }
Expand Down
12 changes: 4 additions & 8 deletions Sources/Relays/Relay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ public protocol Relay: Publisher where Failure == Never {
///
/// - Parameter value: The value to send.
func accept(_ value: Output)

/// Attaches the specified publisher to this relay.
///
/// - parameter publisher: An infallible publisher with the relay's Output type
///
/// - returns: `AnyCancellable`
func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where P.Failure == Failure, P.Output == Output
}

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand All @@ -36,7 +29,10 @@ public extension Publisher where Failure == Never {
///
/// - returns: `AnyCancellable`
func subscribe<R: Relay>(_ relay: R) -> AnyCancellable where R.Output == Output {
relay.subscribe(self)
sink(receiveValue: { output in
relay.accept(output)
})

}
}

Expand Down
5 changes: 5 additions & 0 deletions Tests/CurrentValueRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class CurrentValueRelayTests: XCTestCase {

XCTAssertFalse(completed)
XCTAssertEqual(values, ["initial", "1", "2", "3"])

relay!.accept("4")

XCTAssertFalse(completed)
XCTAssertEqual(values, ["initial", "1", "2", "3", "4"])
}

func testSubscribeRelay_CurrentValues() {
Expand Down
7 changes: 6 additions & 1 deletion Tests/PassthroughRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class PassthroughRelayTests: XCTestCase {

XCTAssertFalse(completed)
XCTAssertEqual(values, ["1", "2", "3"])

relay!.accept("4")

XCTAssertFalse(completed)
XCTAssertEqual(values, ["1", "2", "3", "4"])
}

func testSubscribeRelay_Passthroughs() {
Expand Down Expand Up @@ -133,7 +138,7 @@ class PassthroughRelayTests: XCTestCase {
input.accept("3")

XCTAssertFalse(completed)
XCTAssertEqual(values, ["initial", "1", "2", "3"])
XCTAssertEqual(values, ["1", "2", "3"])
}
}
#endif