Skip to content

Commit

Permalink
Inclusive variant of prefix(while:)
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed Apr 16, 2020
1 parent 12b359d commit 2a3f398
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Sources/SignalProtocol+Filtering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,21 @@ extension SignalProtocol {

/// Emit elements until the given closure returns first `false`.
///
/// - parameter shouldContinue: A closure that tests whethere the given element should complete the signal.
/// - parameter inclusive: When `true`, the element that triggered the completion will also be sent along the signal. Defaults to `false`.
///
/// Check out interactive example at [https://rxmarbles.com/#takeWhile](https://rxmarbles.com/#takeWhile)
public func prefix(while shouldContinue: @escaping (Element) -> Bool) -> Signal<Element, Error> {
public func prefix(while shouldContinue: @escaping (Element) -> Bool, inclusive: Bool = false) -> Signal<Element, Error> {
return Signal { observer in
return self.observe { event in
switch event {
case .next(let element):
if shouldContinue(element) {
observer.receive(element)
} else {
if inclusive {
observer.receive(element)
}
observer.receive(completion: .finished)
}
default:
Expand Down

0 comments on commit 2a3f398

Please sign in to comment.