Skip to content

Commit

Permalink
Fixed issue with CurrentValueRelay and PassthroughRelay subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
reiley.kim authored and reiley kim committed Nov 21, 2022
1 parent d7b896f commit ee9df04
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
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

0 comments on commit ee9df04

Please sign in to comment.