Skip to content

Commit a1be343

Browse files
reiley-kimreiley kim
authored and
reiley kim
committed
Fixed issue with CurrentValueRelay and PassthroughRelay subscription
1 parent d7b896f commit a1be343

5 files changed

+15
-25
lines changed

Sources/Relays/CurrentValueRelay.swift

-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ public class CurrentValueRelay<Output>: Relay {
4242
subscriber.receive(subscription: subscription)
4343
}
4444

45-
public func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never {
46-
publisher.subscribe(storage)
47-
}
48-
49-
public func subscribe<P: Relay>(_ publisher: P) -> AnyCancellable where Output == P.Output {
50-
publisher.subscribe(storage)
51-
}
52-
5345
deinit {
5446
// Send a finished event upon dealloation
5547
subscriptions.forEach { $0.forceFinish() }

Sources/Relays/PassthroughRelay.swift

-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ public class PassthroughRelay<Output>: Relay {
4141
subscriber.receive(subscription: subscription)
4242
}
4343

44-
public func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where Output == P.Output, P.Failure == Never {
45-
publisher.subscribe(storage)
46-
}
47-
48-
public func subscribe<P: Relay>(_ publisher: P) -> AnyCancellable where Output == P.Output {
49-
publisher.subscribe(storage)
50-
}
51-
5244
deinit {
5345
// Send a finished event upon dealloation
5446
subscriptions.forEach { $0.forceFinish() }

Sources/Relays/Relay.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ public protocol Relay: Publisher where Failure == Never {
1919
///
2020
/// - Parameter value: The value to send.
2121
func accept(_ value: Output)
22-
23-
/// Attaches the specified publisher to this relay.
24-
///
25-
/// - parameter publisher: An infallible publisher with the relay's Output type
26-
///
27-
/// - returns: `AnyCancellable`
28-
func subscribe<P: Publisher>(_ publisher: P) -> AnyCancellable where P.Failure == Failure, P.Output == Output
2922
}
3023

3124
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@@ -36,7 +29,10 @@ public extension Publisher where Failure == Never {
3629
///
3730
/// - returns: `AnyCancellable`
3831
func subscribe<R: Relay>(_ relay: R) -> AnyCancellable where R.Output == Output {
39-
relay.subscribe(self)
32+
sink(receiveValue: { output in
33+
relay.accept(output)
34+
})
35+
4036
}
4137
}
4238

Tests/CurrentValueRelayTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class CurrentValueRelayTests: XCTestCase {
7575

7676
XCTAssertFalse(completed)
7777
XCTAssertEqual(values, ["initial", "1", "2", "3"])
78+
79+
relay!.accept("4")
80+
81+
XCTAssertFalse(completed)
82+
XCTAssertEqual(values, ["initial", "1", "2", "3", "4"])
7883
}
7984

8085
func testSubscribeRelay_CurrentValues() {

Tests/PassthroughRelayTests.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class PassthroughRelayTests: XCTestCase {
9090

9191
XCTAssertFalse(completed)
9292
XCTAssertEqual(values, ["1", "2", "3"])
93+
94+
relay!.accept("4")
95+
96+
XCTAssertFalse(completed)
97+
XCTAssertEqual(values, ["1", "2", "3", "4"])
9398
}
9499

95100
func testSubscribeRelay_Passthroughs() {
@@ -133,7 +138,7 @@ class PassthroughRelayTests: XCTestCase {
133138
input.accept("3")
134139

135140
XCTAssertFalse(completed)
136-
XCTAssertEqual(values, ["initial", "1", "2", "3"])
141+
XCTAssertEqual(values, ["1", "2", "3"])
137142
}
138143
}
139144
#endif

0 commit comments

Comments
 (0)