Skip to content

Commit d0e74f9

Browse files
committed
Added CurrentValuePublisher.combineLatest(_:)
1 parent e4be13c commit d0e74f9

2 files changed

Lines changed: 74 additions & 44 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Combine
2+
3+
extension CurrentValuePublisher {
4+
5+
/// Returns a new `CurrentValuePublisher` that combines the current value and subsequent values
6+
/// from this publisher with those from another `CurrentValuePublisher`.
7+
///
8+
/// The returned publisher emits a tuple containing the latest values from both publishers
9+
/// whenever the value of either publisher changes.
10+
///
11+
/// - Parameter other: Another `CurrentValuePublisher` to combine with.
12+
/// - Returns: A `CurrentValuePublisher` that emits tuples of the latest values from both
13+
/// publishers.
14+
public func combineLatest<T>(
15+
_ other: CurrentValuePublisher<T, Failure>
16+
) -> CurrentValuePublisher<(Output, T), Failure> {
17+
switch (self.storage, other.storage) {
18+
case (.constant, .constant):
19+
return CurrentValuePublisher<(Output, T), Failure>(
20+
value: (self.value, other.value)
21+
)
22+
default:
23+
return CurrentValuePublisher<(Output, T), Failure>(
24+
initial: (self.value, other.value),
25+
upstream: Publishers.CombineLatest(self, other).dropFirst()
26+
)
27+
}
28+
}
29+
30+
}

Tests/CombineExtensionsTests/CurrentValuePublisher/CurrentValuePublisherTests.swift

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -679,50 +679,50 @@ class CurrentValuePublisherTests: XCTestCase {
679679
XCTAssertEqual(values, [1, 4, 6, 7])
680680
}
681681

682-
// // ============================================================================ //
683-
// // MARK: - CombineLatest Operator
684-
// // ============================================================================ //
685-
//
686-
// func testCombineLatest_constants() {
687-
// var cancellables = Set<AnyCancellable>()
688-
// var values = [[Int]]()
689-
//
690-
// let publisher1 = CurrentValuePublisher<Int, Never>(value: 100)
691-
// let publisher2 = CurrentValuePublisher<Int, Never>(value: 200)
692-
//
693-
// publisher1.combineLatest(publisher2)
694-
// .sink { values.append([$0, $1]) }
695-
// .store(in: &cancellables)
696-
//
697-
// XCTAssertEqual(values, [[100, 200]])
698-
// }
699-
//
700-
// func testCombineLatest_subjects() {
701-
// var cancellables = Set<AnyCancellable>()
702-
// var values = [[Int]]()
703-
//
704-
// let subject1 = CurrentValueSubject<Int, Never>(100)
705-
// let publisher1 = CurrentValuePublisher(subject1)
706-
//
707-
// let subject2 = CurrentValueSubject<Int, Never>(200)
708-
// let publisher2 = CurrentValuePublisher(subject2)
709-
//
710-
// publisher1.combineLatest(publisher2)
711-
// .sink { values.append([$0, $1]) }
712-
// .store(in: &cancellables)
713-
//
714-
// XCTAssertEqual(values, [[100, 200]])
715-
//
716-
// subject1.send(101)
717-
// XCTAssertEqual(values, [[100, 200], [101, 200]])
718-
//
719-
// subject1.send(102)
720-
// XCTAssertEqual(values, [[100, 200], [101, 200], [102, 200]])
721-
//
722-
// subject2.send(201)
723-
// XCTAssertEqual(values, [[100, 200], [101, 200], [102, 200], [102, 201]])
724-
// }
725-
//
682+
// ============================================================================ //
683+
// MARK: - CombineLatest Operator
684+
// ============================================================================ //
685+
686+
func testCombineLatest_constants() {
687+
var cancellables = Set<AnyCancellable>()
688+
var values = [[Int]]()
689+
690+
let publisher1 = CurrentValuePublisher<Int, Never>(value: 100)
691+
let publisher2 = CurrentValuePublisher<Int, Never>(value: 200)
692+
693+
publisher1.combineLatest(publisher2)
694+
.sink { values.append([$0, $1]) }
695+
.store(in: &cancellables)
696+
697+
XCTAssertEqual(values, [[100, 200]])
698+
}
699+
700+
func testCombineLatest_subjects() {
701+
var cancellables = Set<AnyCancellable>()
702+
var values = [[Int]]()
703+
704+
let subject1 = CurrentValueSubject<Int, Never>(100)
705+
let publisher1 = CurrentValuePublisher(subject1)
706+
707+
let subject2 = CurrentValueSubject<Int, Never>(200)
708+
let publisher2 = CurrentValuePublisher(subject2)
709+
710+
publisher1.combineLatest(publisher2)
711+
.sink { values.append([$0, $1]) }
712+
.store(in: &cancellables)
713+
714+
XCTAssertEqual(values, [[100, 200]])
715+
716+
subject1.send(101)
717+
XCTAssertEqual(values, [[100, 200], [101, 200]])
718+
719+
subject1.send(102)
720+
XCTAssertEqual(values, [[100, 200], [101, 200], [102, 200]])
721+
722+
subject2.send(201)
723+
XCTAssertEqual(values, [[100, 200], [101, 200], [102, 200], [102, 201]])
724+
}
725+
726726
// // ============================================================================ //
727727
// // MARK: - Boolean Operators
728728
// // ============================================================================ //

0 commit comments

Comments
 (0)