-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// PrefixWhileBehavior.swift | ||
// CombineExt | ||
// | ||
// Created by Jasdev Singh on 29/12/2020. | ||
// Copyright © 2020 Combine Community. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
import Foundation | ||
|
||
/// Whether to include the first element that doesn’t pass | ||
/// the `while` predicate passed to `Combine.Publisher.prefix(while:behavior:)`. | ||
public enum PrefixWhileBehavior { | ||
/// Include the first element that doesn’t pass the `while` predicate. | ||
case inclusive | ||
|
||
/// Exclude the first element that doesn’t pass the `while` predicate. | ||
case exclusive | ||
} | ||
|
||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public extension Publisher { | ||
/// An overload on `Publisher.prefix(while:)` that allows for inclusion of the first element that doesn’t pass the `while` predicate. | ||
/// | ||
/// - parameters: | ||
/// - predicate: A closure that takes an element as its parameter and returns a Boolean value that indicates whether publishing should continue. | ||
/// - behavior: Whether or not to include the first element that doesn’t pass `predicate`. | ||
/// | ||
/// - returns: A publisher that passes through elements until the predicate indicates publishing should finish — and optionally that first `predicate`-failing element. | ||
func prefix( | ||
while predicate: @escaping (Output) -> Bool, | ||
behavior: PrefixWhileBehavior = .exclusive | ||
) -> AnyPublisher<Output, Failure> { | ||
switch behavior { | ||
case .exclusive: | ||
return prefix(while: predicate) | ||
.eraseToAnyPublisher() | ||
case .inclusive: | ||
return flatMap { next in | ||
Just(PrefixInclusiveEvent.whileValueOrIncluded(next)) | ||
.append(!predicate(next) ? [.end] : []) | ||
.setFailureType(to: Failure.self) | ||
} | ||
.prefix(while: \.isWhileValueOrIncluded) | ||
.compactMap(\.value) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
// MARK: - Helpers | ||
|
||
private enum PrefixInclusiveEvent<Output> { | ||
case end | ||
case whileValueOrIncluded(Output) | ||
|
||
var isWhileValueOrIncluded: Bool { | ||
switch self { | ||
case .end: | ||
return false | ||
case .whileValueOrIncluded: | ||
return true | ||
} | ||
} | ||
|
||
var value: Output? { | ||
switch self { | ||
case .end: | ||
return nil | ||
case let .whileValueOrIncluded(inner): | ||
return inner | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// | ||
// PrefixWhileBehaviorTests.swift | ||
// CombineExt | ||
// | ||
// Created by Jasdev Singh on 29/12/2020. | ||
// Copyright © 2020 Combine Community. All rights reserved. | ||
// | ||
|
||
#if !os(watchOS) | ||
import Combine | ||
import CombineExt | ||
import XCTest | ||
|
||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
final class PrefixWhileBehaviorTests: XCTestCase { | ||
private struct SomeError: Error, Equatable {} | ||
|
||
private var cancellable: AnyCancellable! | ||
|
||
func testExclusiveValueEventsWithFinished() { | ||
let intSubject = PassthroughSubject<Int, Never>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<Never>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .exclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4, 5] | ||
.forEach(intSubject.send) | ||
|
||
XCTAssertEqual(values, [0, 2, 4]) | ||
XCTAssertEqual(completions, [.finished]) | ||
} | ||
|
||
func testExclusiveValueEventsWithError() { | ||
let intSubject = PassthroughSubject<Int, SomeError>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<SomeError>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .exclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4] | ||
.forEach(intSubject.send) | ||
|
||
intSubject.send(completion: .failure(.init())) | ||
|
||
XCTAssertEqual(values, [0, 2, 4]) | ||
XCTAssertEqual(completions, [.failure(.init())]) | ||
} | ||
|
||
func testInclusiveValueEventsWithStopElement() { | ||
let intSubject = PassthroughSubject<Int, Never>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<Never>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .inclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4, 5] | ||
.forEach(intSubject.send) | ||
|
||
XCTAssertEqual(values, [0, 2, 4, 5]) | ||
XCTAssertEqual(completions, [.finished]) | ||
} | ||
|
||
func testInclusiveValueEventsWithErrorAfterStopElement() { | ||
let intSubject = PassthroughSubject<Int, SomeError>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<SomeError>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .inclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4, 5] | ||
.forEach(intSubject.send) | ||
|
||
intSubject.send(completion: .failure(.init())) | ||
|
||
XCTAssertEqual(values, [0, 2, 4, 5]) | ||
XCTAssertEqual(completions, [.finished]) | ||
} | ||
|
||
func testInclusiveValueEventsWithErrorBeforeStop() { | ||
let intSubject = PassthroughSubject<Int, SomeError>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<SomeError>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .inclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4] | ||
.forEach(intSubject.send) | ||
|
||
intSubject.send(completion: .failure(.init())) | ||
|
||
XCTAssertEqual(values, [0, 2, 4]) | ||
XCTAssertEqual(completions, [.failure(.init())]) | ||
} | ||
|
||
func testInclusiveEarlyCompletion() { | ||
let intSubject = PassthroughSubject<Int, SomeError>() | ||
|
||
var values = [Int]() | ||
var completions = [Subscribers.Completion<SomeError>]() | ||
|
||
cancellable = intSubject | ||
.prefix( | ||
while: { $0 % 2 == 0 }, | ||
behavior: .inclusive | ||
) | ||
.sink( | ||
receiveCompletion: { completions.append($0) }, | ||
receiveValue: { values.append($0) } | ||
) | ||
|
||
[0, 2, 4] | ||
.forEach(intSubject.send) | ||
|
||
intSubject.send(completion: .finished) | ||
|
||
XCTAssertEqual(values, [0, 2, 4]) | ||
XCTAssertEqual(completions, [.finished]) | ||
} | ||
} | ||
#endif |