From ee9df04badaffe305b821a45a419f2797202b530 Mon Sep 17 00:00:00 2001 From: "reiley.kim" Date: Mon, 21 Nov 2022 14:09:04 +0900 Subject: [PATCH] Fixed issue with CurrentValueRelay and PassthroughRelay subscription --- Sources/Relays/CurrentValueRelay.swift | 8 -------- Sources/Relays/PassthroughRelay.swift | 8 -------- Sources/Relays/Relay.swift | 12 ++++-------- Tests/CurrentValueRelayTests.swift | 5 +++++ Tests/PassthroughRelayTests.swift | 7 ++++++- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/Sources/Relays/CurrentValueRelay.swift b/Sources/Relays/CurrentValueRelay.swift index 234a263..6740fc2 100644 --- a/Sources/Relays/CurrentValueRelay.swift +++ b/Sources/Relays/CurrentValueRelay.swift @@ -42,14 +42,6 @@ public class CurrentValueRelay: Relay { subscriber.receive(subscription: subscription) } - public func subscribe(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never { - publisher.subscribe(storage) - } - - public func subscribe(_ publisher: P) -> AnyCancellable where Output == P.Output { - publisher.subscribe(storage) - } - deinit { // Send a finished event upon dealloation subscriptions.forEach { $0.forceFinish() } diff --git a/Sources/Relays/PassthroughRelay.swift b/Sources/Relays/PassthroughRelay.swift index f6e1231..a0d2085 100644 --- a/Sources/Relays/PassthroughRelay.swift +++ b/Sources/Relays/PassthroughRelay.swift @@ -41,14 +41,6 @@ public class PassthroughRelay: Relay { subscriber.receive(subscription: subscription) } - public func subscribe(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never { - publisher.subscribe(storage) - } - - public func subscribe(_ publisher: P) -> AnyCancellable where Output == P.Output { - publisher.subscribe(storage) - } - deinit { // Send a finished event upon dealloation subscriptions.forEach { $0.forceFinish() } diff --git a/Sources/Relays/Relay.swift b/Sources/Relays/Relay.swift index 22c7f3b..0aa430c 100644 --- a/Sources/Relays/Relay.swift +++ b/Sources/Relays/Relay.swift @@ -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(_ publisher: P) -> AnyCancellable where P.Failure == Failure, P.Output == Output } @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) @@ -36,7 +29,10 @@ public extension Publisher where Failure == Never { /// /// - returns: `AnyCancellable` func subscribe(_ relay: R) -> AnyCancellable where R.Output == Output { - relay.subscribe(self) + sink(receiveValue: { output in + relay.accept(output) + }) + } } diff --git a/Tests/CurrentValueRelayTests.swift b/Tests/CurrentValueRelayTests.swift index d9a2579..43cd09a 100644 --- a/Tests/CurrentValueRelayTests.swift +++ b/Tests/CurrentValueRelayTests.swift @@ -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() { diff --git a/Tests/PassthroughRelayTests.swift b/Tests/PassthroughRelayTests.swift index ba62128..e1c39a7 100644 --- a/Tests/PassthroughRelayTests.swift +++ b/Tests/PassthroughRelayTests.swift @@ -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() { @@ -133,7 +138,7 @@ class PassthroughRelayTests: XCTestCase { input.accept("3") XCTAssertFalse(completed) - XCTAssertEqual(values, ["initial", "1", "2", "3"]) + XCTAssertEqual(values, ["1", "2", "3"]) } } #endif