-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AsPublisher Failure Type Alignment with RxSwift (#2)
* Add additional asObservable tests * Fix spelling * Fix alignment * Move and rename assertBridgeBufferDoesNotOverflowIfPossible * Align asPublisher Error type with RxSwift `Error` * Make assertBridgeBufferDoesNotOverflowIfPossible testable and add missing tests
- Loading branch information
1 parent
acea143
commit a92daaa
Showing
7 changed files
with
255 additions
and
26 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
28 changes: 28 additions & 0 deletions
28
...perability/RxSwift to Combine/Publisher+AssertBridgeBufferDoesNotOverflowIfPossible.swift
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,28 @@ | ||
// | ||
// Publisher+AssertBridgeBufferDoesNotOverflowIfPossible.swift | ||
// Copyright © 2021 Notonthehighstreet Enterprises Limited. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
extension Publisher { | ||
|
||
/// Convenience method whose default implementation causes a `preconditionFailure` when an upstream `BridgePublisher`'s buffer overflows. | ||
/// In the event this does not occur, the failure type is mapped to the `Error` type of the upstream `Observable`. | ||
/// | ||
/// This function can be used at any point following the `asPublisher` function if you want to ensure a buffer overflow never occurs. | ||
/// | ||
/// - Returns: A publisher that maps any upstream error or fatal errors in the event of a `bufferOverflow`. | ||
/// | ||
public func assertBridgeBufferDoesNotOverflowIfPossible(onBufferOverflow: @escaping () -> Error = { preconditionFailure("Bridge buffer overflowed.") }) -> Publishers.MapError<Self, Error> { | ||
return mapError { error -> Error in | ||
guard let bridgeFailure = error as? BridgeFailure else { return error } | ||
|
||
switch bridgeFailure { | ||
case .bufferOverflow: return onBufferOverflow() | ||
case .upstreamError(let upstreamError): return upstreamError | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.