-
Notifications
You must be signed in to change notification settings - Fork 161
Adds Publisher.prefix(while:behavior:). #70
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.