{"id":22588779,"url":"https://github.com/jackstone92/combinerx","last_synced_at":"2025-04-10T21:44:41.897Z","repository":{"id":39661793,"uuid":"318952631","full_name":"Jackstone92/CombineRx","owner":"Jackstone92","description":"Helpful bridging functions between RxSwift and Combine frameworks","archived":false,"fork":false,"pushed_at":"2024-09-20T14:01:09.000Z","size":73,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T21:44:31.292Z","etag":null,"topics":["backpressure","bridge","buffer","combine","combine-framework","pressure","rxswift","strategy","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jackstone92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-06T04:44:15.000Z","updated_at":"2024-10-09T05:58:36.000Z","dependencies_parsed_at":"2022-08-31T07:41:07.228Z","dependency_job_id":null,"html_url":"https://github.com/Jackstone92/CombineRx","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jackstone92%2FCombineRx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jackstone92%2FCombineRx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jackstone92%2FCombineRx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jackstone92%2FCombineRx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jackstone92","download_url":"https://codeload.github.com/Jackstone92/CombineRx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248305885,"owners_count":21081562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backpressure","bridge","buffer","combine","combine-framework","pressure","rxswift","strategy","swift"],"created_at":"2024-12-08T08:11:06.573Z","updated_at":"2025-04-10T21:44:41.869Z","avatar_url":"https://github.com/Jackstone92.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CombineRx\n\n![Run Tests](https://github.com/Jackstone92/CombineRx/workflows/Run%20Tests/badge.svg)\n[![License](https://img.shields.io/badge/license-mit-brightgreen.svg)](https://en.wikipedia.org/wiki/MIT_License)\n[![codecov](https://codecov.io/gh/Jackstone92/CombineRx/branch/main/graph/badge.svg?token=8XH4E4NLBD)](https://codecov.io/gh/Jackstone92/CombineRx)\n\nThe *CombineRx* library contains a series of functions that help with the interoperability between [RxSwift](https://github.com/ReactiveX/RxSwift) and Apple's [Combine](https://developer.apple.com/documentation/combine) frameworks.\n\n## Interoperability between Combine and RxSwift\n\n### Combine to RxSwift\nIn order to convert Combine `Publisher`s to RxSwift  `Observable`s, you can make use of the `asObservable()` function. This can be done as follows:\n```swift\nimport Combine\nimport RxSwift\nimport CombineRx\n\nlet myBridgedObservable = Just(0).asObservable()\n```\n\nIt is also possible to convert Combine `Publisher`s to RxSwift `Infallible`s using `.asInfallible()`.\nThis can be done as follows:\n```swift\nimport Combine\nimport RxSwift\nimport CombineRx\n\nlet myBridgedInfallible = Just(0)\n    .setFailureType(to: Never.self)\n    .asInfallible() // Works when `Failure` type is `Never`.\n\nlet myBridgedInfallible = Just(0)\n    .setFailureType(to: Error.self)\n    .asInfallible(onErrorRecover: { error in ... }) // Handle the error\n\nlet myBridgedInfallible = Just(0)\n    .setFailureType(to: Error.self)\n    .asInfallible(onErrorFallbackTo: Just\u003cInt\u003e(42).eraseToAnyPublisher()) // Use a fallback `Publisher`\n\nlet myBridgedInfallible = Just(0)\n    .setFailureType(to: Error.self)\n    .asInfallible(onErrorJustReturn: 42) // Use a fallback value\n```\n\n### RxSwift to Combine\nIn order to convert RxSwift `Observable`s to Combine `Publisher`s, you can make use of the\n`asPublisher(withBufferSize:andBridgeBufferingStrategy:)` function. This can be done as follows:\n```swift\nimport Combine\nimport RxSwift\nimport CombineRx\n\nlet myBridgedPublisher1 = Observable.just(0).asPublisher(withBufferSize: 1, andBridgeBufferingStrategy: .error)\nlet myBridgedPublisher2 = Observable.from([0, 1, 2, 3]).asPublisher(withBufferSize: 4, andBridgeBufferingStrategy: .error)\n```\n\nIt is also possible to convert RxSwift `Infallible`s to Combine `Publisher`s \nusing `asPublisher(withBufferSize:andBridgeBufferingStrategy:)`.\nThis can be done as follows:\n```swift\nimport Combine\nimport RxSwift\nimport CombineRx\n\nlet myBridgedPublisher1 = Infallible.just(0).asPublisher(withBufferSize: 1, andBridgeBufferingStrategy: .dropOldest)\nlet myBridgedPublisher2 = Infallible.from([0, 1, 2, 3]).asPublisher(withBufferSize: 4, andBridgeBufferingStrategy: .dropOldest)\n```\n\n\nOne 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.\n\nRxSwift `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.\n\nThis is the reason for the required `withBufferSize` and `andBridgeBufferingStrategy` parameters for `asPublisher(withBufferSize:andBridgeBufferingStrategy:)`. `withBufferSize` is where the buffer size should manually be set (ideally based directly on the number of expected elements in the sequence). `andBridgeBufferingStrategy` is the strategy to employ when the maximum buffer capacity is reached. Keeping in line with native Combine strategies, this can either be `error`, where any buffer overflow is treated as an error, `dropNewest` where the elements already present in the buffer are maintained and any new elements are ignored, or finally `dropOldest` where new elements are added to the buffer and replace older elements that were already present.\n\nAdditional information on Combine's use of backpressure can be found [here](https://developer.apple.com/documentation/combine/processing-published-elements-with-subscribers).\n\n## Installation\n\nIt is currently possible to install this library using Swift Package Manager. In order to do so, please add the current repository as a package dependency using Xcode or include the following in your `Package.swift` file:\n```swift\nimport PackageDescription\n\nlet package = Package(\n    ...\n    dependencies: [\n        .package(url: \"https://github.com/Jackstone92/CombineRx\", .upToNextMajor(from: \"2.0.0\")),\n    ],\n    ...\n    targets: [\n        .target(name: \"MyTarget\", dependencies: [\"CombineRx\"]),\n    ]\n)\n```\n\n## Copywrite and license information\nCopyright 2020 © Jack Stone\n\n*CombineRx* is made available under the [MIT License](https://github.com/Jackstone92/CombineRx/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackstone92%2Fcombinerx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjackstone92%2Fcombinerx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackstone92%2Fcombinerx/lists"}