Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion + Implementation] Add combine operator and tests #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Receiver/Sources/Receiver+Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,60 @@ extension Receiver where Wave: OptionalProtocol {
return receiver
}
}

/// Combines a receiver of `A` and a reveiver of `B` to produce a receiver of (A,B)
/// ```
/// let (intTransmitter, intReceiver) = Receiver<Int>.make()
/// let (stringTransmitter, stringReceiver) = Receiver<String>.make()
///
/// let intAndStringReceiver = combine(intReceiver,stringReceiver)
/// }
///
/// intAndStringReceiver.listen { value in
/// /// `value` == (1,"1")
/// /// `value` == (2,"1")
/// /// `value` == (2,"2")
/// }
/// /// Value is not sent yet since we do not have a value from stringReceiver
/// intTransmitter.broadcast(1)
/// /// Value is sent to the listener now as (1,"1")
/// stringTransmitter.broadcast("1")
/// /// Value is sent to the listener now as (2,"2")
/// intTransmitter.broadcast(2)
/// /// Value is sent to the listener now as (2,"2")
/// stringTransmitter.broadcast("2")
/// ```
/// - parameters:
/// - ra: A receiver of type Receiver<A>
/// - rb: A receiver of type Receiver<B>
///
/// - returns: A `receiver` that produces values of <(A,B)> for every value emitted from either
public func combine<A,B>(_ ra: Receiver<A>, _ rb: Receiver<B>) -> Receiver<(A,B)> {
let (transmitter, receiver) = Receiver<(A?,B?)>.make()
let ab = Atomic<(A?,B?)>( (nil,nil) )

ra.listen { a in
ab.apply {
$0.0 = a
transmitter.broadcast($0)
}
}

rb.listen { b in
ab.apply {
$0.1 = b
transmitter.broadcast($0)
}
}

let newReceiver = receiver.map { (ab) -> (A,B)? in
if let next = ab as? (A,B) {
return next
} else {
return nil
}
}.skipNil()

return newReceiver

}
22 changes: 22 additions & 0 deletions ReceiverTests/ReceiverTests+Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,26 @@ class ReceiverTests_Operators: XCTestCase {

XCTAssertTrue(called == 3)
}

func test_combine() {
let (intTransmitter, intReceiver) = Receiver<Int>.make()
let (stringTransmitter, stringReceiver) = Receiver<String>.make()
let newReceiver = combine(intReceiver, stringReceiver)
let expectedValues = [(1,"1"),(2,"1"),(2,"2")]
var values = [(Int,String)]()

newReceiver.listen { wave in
values.append(wave)
}

intTransmitter.broadcast(1)
stringTransmitter.broadcast("1")
intTransmitter.broadcast(2)
stringTransmitter.broadcast("2")

XCTAssertEqual(values.map{$0.0}, expectedValues.map{$0.0})
XCTAssertEqual(values.map{$0.1}, expectedValues.map{$0.1})

}

}