From 9f5fff50b93d611c1add3bf1fa842de5745f0dd0 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 29 Mar 2018 17:44:21 -0500 Subject: [PATCH] =?UTF-8?q?Issue#186=C2=A0:=20updated=20take(until:)=20des?= =?UTF-8?q?cription=20and=20added=20unit=20test.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this change `take(until:)` was described as terminating the receiver only for a completed event on the given signal. This has been updated to accurately state that it is for _any_ event that was sent on the provided signal. Unit Tests provided and full unit test suite passed. --- Sources/SignalProtocol.swift | 7 ++++--- Tests/ReactiveKitTests/SignalTests.swift | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Sources/SignalProtocol.swift b/Sources/SignalProtocol.swift index fea698e..5090558 100644 --- a/Sources/SignalProtocol.swift +++ b/Sources/SignalProtocol.swift @@ -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(until signal: S) -> Signal { return Signal { observer in let disposable = CompositeDisposable() - - disposable += signal.observe { event in + + disposable += signal.observe { _ in observer.completed() } diff --git a/Tests/ReactiveKitTests/SignalTests.swift b/Tests/ReactiveKitTests/SignalTests.swift index 8a521d7..00f212e 100644 --- a/Tests/ReactiveKitTests/SignalTests.swift +++ b/Tests/ReactiveKitTests/SignalTests.swift @@ -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.sequence([1, 2, 3, 4]).observeIn(bob.context) + let interrupt = Signal.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.interval(0.4, queue: Queue.global).take(5)