Skip to content

Commit

Permalink
Merge pull request #188 from npvisual/issue186
Browse files Browse the repository at this point in the history
Issue#186 : updated take(until:) description and added unit test.
  • Loading branch information
srdanrasic authored Apr 1, 2018
2 parents 0adc6d3 + 9f5fff5 commit dacbf74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Sources/SignalProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,13 @@ public extension SignalProtocol {
}
}

/// Emit elements of the reciver until given signal completes and then complete the receiver.
/// Emit elements of the receiver until the given signal sends an event (of any kind)
/// and then completes the receiver (subsequent events on the receiver are ignored).
public func take<S: SignalProtocol>(until signal: S) -> Signal<Element, Error> {
return Signal { observer in
let disposable = CompositeDisposable()

disposable += signal.observe { event in
disposable += signal.observe { _ in
observer.completed()
}

Expand Down
22 changes: 22 additions & 0 deletions Tests/ReactiveKitTests/SignalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ class SignalTests: XCTestCase {
let takenLast2 = operation.take(last: 2)
takenLast2.expectComplete(after: [2, 3])
}

func testTakeUntil() {
let bob = Scheduler()
let eve = Scheduler()

let operation = Signal<Int, TestError>.sequence([1, 2, 3, 4]).observeIn(bob.context)
let interrupt = Signal<String, TestError>.sequence(["A", "B"]).observeIn(eve.context)

let takeuntil = operation.take(until: interrupt)

let exp = expectation(description: "completed")
takeuntil.expectAsyncComplete(after: [1, 2], expectation: exp)

bob.runOne() // Sends 1.
bob.runOne() // Sends 2.
eve.runOne() // Sends A, effectively stopping the receiver.
bob.runOne() // Ignored.
eve.runRemaining() // Ignored. Sends B, with termination.
bob.runRemaining() // Ignored.

waitForExpectations(timeout: 1, handler: nil)
}

// func testThrottle() {
// let operation = Signal<Int, TestError>.interval(0.4, queue: Queue.global).take(5)
Expand Down

0 comments on commit dacbf74

Please sign in to comment.