From b0c324e4c2b5e666485f1f571be79d8cf4e7d25d Mon Sep 17 00:00:00 2001 From: Jack Stone Date: Fri, 20 Sep 2024 13:39:01 +0100 Subject: [PATCH] Update README file (#14) * Update readme to include Infallible examples * Update readme to show latest major version --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3dae0c2..15af8a7 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,30 @@ import CombineRx let myBridgedObservable = Just(0).asObservable() ``` +It is also possible to convert Combine `Publisher`s to RxSwift `Infallible`s using `.asInfallible()`. +This can be done as follows: +```swift +import Combine +import RxSwift +import CombineRx + +let myBridgedInfallible = Just(0) + .setFailureType(to: Never.self) + .asInfallible() // Works when `Failure` type is `Never`. + +let myBridgedInfallible = Just(0) + .setFailureType(to: Error.self) + .asInfallible(onErrorRecover: { error in ... }) // Handle the error + +let myBridgedInfallible = Just(0) + .setFailureType(to: Error.self) + .asInfallible(onErrorFallbackTo: Just(42).eraseToAnyPublisher()) // Use a fallback `Publisher` + +let myBridgedInfallible = Just(0) + .setFailureType(to: Error.self) + .asInfallible(onErrorJustReturn: 42) // Use a fallback value +``` + ### RxSwift to Combine In order to convert RxSwift `Observable`s to Combine `Publisher`s, you can make use of the `asPublisher(withBufferSize:andBridgeBufferingStrategy:)` function. This can be done as follows: @@ -30,6 +54,19 @@ let myBridgedPublisher1 = Observable.just(0).asPublisher(withBufferSize: 1, andB let myBridgedPublisher2 = Observable.from([0, 1, 2, 3]).asPublisher(withBufferSize: 4, andBridgeBufferingStrategy: .error) ``` +It is also possible to convert RxSwift `Infallible`s to Combine `Publisher`s +using `asPublisher(withBufferSize:andBridgeBufferingStrategy:)`. +This can be done as follows: +```swift +import Combine +import RxSwift +import CombineRx + +let myBridgedPublisher1 = Infallible.just(0).asPublisher(withBufferSize: 1, andBridgeBufferingStrategy: .dropOldest) +let myBridgedPublisher2 = Infallible.from([0, 1, 2, 3]).asPublisher(withBufferSize: 4, andBridgeBufferingStrategy: .dropOldest) +``` + + One difference between RxSwift and Combine is that Combine adheres to the mechanism of backpressure in order to ensure that `Publisher`s only produce as many elements that `Subscriber`s have requested. This prevents the case where elements might build up in a `Publisher`s buffer faster than they can be processed downstream by a subscriber as this could lead to out-of-memory errors and degradation in performance due to high system resource consumption. Combine applies this backpressure upstream through a contractual obligation by `Publisher`s to only emit an element when it is requested by `Subscriber`s through `Subscribers.Demand` requests. RxSwift `Observable`s differ in this regard as they rely on a source with an unbounded rate of production and therefore when bridging to a Combine `Publisher`, we must maintain a buffer or drop elements accordingly in order to satisfy the requirements of downstream subscribers. @@ -47,7 +84,7 @@ import PackageDescription let package = Package( ... dependencies: [ - .package(url: "https://github.com/Jackstone92/CombineRx", .upToNextMajor(from: "0.1.0")), + .package(url: "https://github.com/Jackstone92/CombineRx", .upToNextMajor(from: "2.0.0")), ], ... targets: [