Skip to content

Commit

Permalink
Fixed issue with CurrentValueRelay and PassthroughRelay subscription (C…
Browse files Browse the repository at this point in the history
  • Loading branch information
dsk1306 authored Sep 7, 2020
1 parent 7f49594 commit efa607b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Relays/CurrentValueRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class CurrentValueRelay<Output>: Relay {
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
4 changes: 4 additions & 0 deletions Sources/Relays/PassthroughRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class PassthroughRelay<Output>: Relay {
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
22 changes: 22 additions & 0 deletions Tests/CurrentValueRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,27 @@ class CurrentValueRelayTests: XCTestCase {
XCTAssertFalse(completed)
XCTAssertEqual(values, ["initial", "1", "2", "3"])
}

func testSubscribePublisher2() {
var completed = false

let input = CurrentValueRelay<String>("initial")
let output = CurrentValueRelay<String>("initial")

input
.subscribe(output)
.store(in: &subscriptions)
output
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { self.values.append($0) })
.store(in: &subscriptions)

input.accept("1")
input.accept("2")
input.accept("3")

XCTAssertFalse(completed)
XCTAssertEqual(values, ["initial", "1", "2", "3"])
}
}
#endif
22 changes: 22 additions & 0 deletions Tests/PassthroughRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,27 @@ class PassthroughRelayTests: XCTestCase {
XCTAssertFalse(completed)
XCTAssertEqual(values, ["1", "2", "3"])
}

func testSubscribePublisher2() {
var completed = false

let input = PassthroughRelay<String>()
let output = PassthroughRelay<String>()

input
.subscribe(output)
.store(in: &subscriptions)
output
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { self.values.append($0) })
.store(in: &subscriptions)

input.accept("1")
input.accept("2")
input.accept("3")

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

0 comments on commit efa607b

Please sign in to comment.