├── .github └── workflows │ ├── autoinvite.yml │ └── tests.yml ├── .gitignore ├── Carthage.xcconfig ├── ExampleApp ├── ExampleApp.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist ├── ExampleApp │ ├── AppDelegate.swift │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── Example.swift │ ├── Info.plist │ ├── SceneDelegate.swift │ └── ViewController.swift ├── Podfile └── Podfile.lock ├── Helpers └── make_project.rb ├── LICENSE ├── Makefile ├── Package.swift ├── README.md ├── Resources ├── example.gif └── logo.png ├── RxCombine.podspec ├── Sources ├── Combine+Rx │ ├── Publisher+Rx.swift │ └── Subject+Rx.swift ├── Common │ └── DemandBuffer.swift ├── Info.plist └── Rx+Combine │ ├── BehaviorRelay+Combine.swift │ ├── BehaviorSubject+Combine.swift │ ├── Observable+Combine.swift │ ├── PublishRelay+Combine.swift │ ├── PublishSubject+Combine.swift │ ├── Relays+Combine.swift │ └── RxSubscription.swift ├── Tests ├── Info.plist ├── ObservableAsPublisherTests.swift ├── PublisherAsObservableTests.swift ├── RxRelaysToCombineTests.swift └── RxSubjectsToCombineTests.swift ├── codecov.yml └── scripts └── carthage-archive.sh /.github/workflows/autoinvite.yml: -------------------------------------------------------------------------------- 1 | name: Inclusive Organization 2 | on: 3 | push: 4 | branches: main 5 | jobs: 6 | invite: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Invite contributor to the organization 10 | uses: lekterable/inclusive-organization-action@v1.1.0 11 | with: 12 | organization: CombineCommunity 13 | team: Contributors 14 | comment: | 15 | Thank you for your contribution — You Rock 🤘! 16 | 17 | I've invited you to join the [CombineCommunity](https://github.com/CombineCommunity) organization – no pressure to accept! 18 | 19 | If you'd like more information on what this means, check out our [contributor](https://github.com/CombineCommunity/contributors) guidelines and feel free to reach out with any questions. 20 | env: 21 | ACCESS_TOKEN: ${{ secrets.INVITE_TOKEN }} -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: RxCombine 2 | 3 | on: 4 | push: 5 | branches: main 6 | pull_request: 7 | branches: '**' 8 | 9 | jobs: 10 | macOS: 11 | name: "macOS" 12 | runs-on: macOS-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Make Project 17 | run: make project 18 | - name: Run tests 19 | run: set -o pipefail && xcodebuild -project RxCombine.xcodeproj -scheme RxCombine-Package -enableCodeCoverage YES -sdk macosx -destination "arch=x86_64" test | xcpretty -c -r html --output logs/macOS.html 20 | - uses: actions/upload-artifact@v1 21 | with: 22 | name: build-logs-${{ github.run_id }} 23 | path: logs 24 | 25 | iOS: 26 | name: "iOS" 27 | runs-on: macOS-latest 28 | 29 | steps: 30 | - uses: actions/checkout@v2 31 | - name: Make Project 32 | run: make project 33 | - name: Run tests 34 | run: set -o pipefail && xcodebuild -project RxCombine.xcodeproj -scheme RxCombine-Package -enableCodeCoverage YES -sdk iphonesimulator -destination "name=iPhone 11" test | xcpretty -c -r html --output logs/iOS.html 35 | - uses: codecov/codecov-action@v1.0.5 36 | with: 37 | token: 3116b02c-4202-4fe6-bde2-b148f6432f17 38 | name: RxCombine 39 | - uses: actions/upload-artifact@v1 40 | with: 41 | name: build-logs-${{ github.run_id }} 42 | path: logs 43 | tvOS: 44 | name: "tvOS" 45 | runs-on: macOS-latest 46 | 47 | steps: 48 | - uses: actions/checkout@v2 49 | - name: Make Project 50 | run: make project 51 | - name: Run tests 52 | run: set -o pipefail && xcodebuild -project RxCombine.xcodeproj -scheme RxCombine-Package -enableCodeCoverage YES -sdk appletvsimulator -destination "name=Apple TV" test | xcpretty -c -r html --output logs/tvOS.html 53 | - uses: actions/upload-artifact@v1 54 | with: 55 | name: build-logs-${{ github.run_id }} 56 | path: logs 57 | SPM: 58 | name: "SPM" 59 | runs-on: macOS-latest 60 | 61 | steps: 62 | - uses: actions/checkout@v2 63 | - name: Run tests 64 | run: set -o pipefail && swift test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | Package.resolved 20 | RxCombine.xcodeproj 21 | 22 | ## Other 23 | *.moved-aside 24 | *.xccheckout 25 | *.xcscmblueprint 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | .build/ 44 | ExampleApp/Pods 45 | ExampleApp/*.xcworkspace 46 | 47 | # CocoaPods 48 | # 49 | # We recommend against adding the Pods directory to your .gitignore. However 50 | # you should judge for yourself, the pros and cons are mentioned at: 51 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 52 | # 53 | # Pods/ 54 | 55 | # Carthage 56 | # 57 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 58 | # Carthage/Checkouts 59 | 60 | Carthage/Build 61 | 62 | # fastlane 63 | # 64 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 65 | # screenshots whenever they are needed. 66 | # For more information about the recommended setup visit: 67 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 68 | 69 | fastlane/report.xml 70 | fastlane/Preview.html 71 | fastlane/screenshots/**/*.png 72 | fastlane/test_output 73 | -------------------------------------------------------------------------------- /Carthage.xcconfig: -------------------------------------------------------------------------------- 1 | // xconfig for Carthage builds using autogenerated xcodeproj from SwiftPM 2 | 3 | PRODUCT_BUNDLE_IDENTIFIER = com.CombineCommunity.$(PRODUCT_NAME) 4 | CURRENT_PROJECT_VERSION = 1 5 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 51; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 16A290BC2E27CF47E632A3B8 /* Pods_ExampleApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B704C183DF3C49484D0FC40A /* Pods_ExampleApp.framework */; }; 11 | A26EB6C022B0397A006A57F9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A26EB6BF22B0397A006A57F9 /* AppDelegate.swift */; }; 12 | A26EB6C222B0397A006A57F9 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A26EB6C122B0397A006A57F9 /* SceneDelegate.swift */; }; 13 | A26EB6C422B0397A006A57F9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A26EB6C322B0397A006A57F9 /* ViewController.swift */; }; 14 | A26EB6C722B0397A006A57F9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A26EB6C522B0397A006A57F9 /* Main.storyboard */; }; 15 | A26EB6C922B0397C006A57F9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A26EB6C822B0397C006A57F9 /* Assets.xcassets */; }; 16 | A26EB6CC22B0397C006A57F9 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A26EB6CA22B0397C006A57F9 /* LaunchScreen.storyboard */; }; 17 | A2838BCA22B03AA500808FB2 /* Example.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2838BC922B03AA500808FB2 /* Example.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 04E5A60733794FBFF91FD10C /* Pods-ExampleApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExampleApp.release.xcconfig"; path = "Target Support Files/Pods-ExampleApp/Pods-ExampleApp.release.xcconfig"; sourceTree = ""; }; 22 | 53C7367904650D29CE5478A7 /* Pods-ExampleApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExampleApp.debug.xcconfig"; path = "Target Support Files/Pods-ExampleApp/Pods-ExampleApp.debug.xcconfig"; sourceTree = ""; }; 23 | A26EB6BC22B0397A006A57F9 /* ExampleApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 24 | A26EB6BF22B0397A006A57F9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 25 | A26EB6C122B0397A006A57F9 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 26 | A26EB6C322B0397A006A57F9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 27 | A26EB6C622B0397A006A57F9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | A26EB6C822B0397C006A57F9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | A26EB6CB22B0397C006A57F9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | A26EB6CD22B0397C006A57F9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | A2838BC922B03AA500808FB2 /* Example.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Example.swift; sourceTree = ""; }; 32 | B704C183DF3C49484D0FC40A /* Pods_ExampleApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ExampleApp.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | A26EB6B922B0397A006A57F9 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | 16A290BC2E27CF47E632A3B8 /* Pods_ExampleApp.framework in Frameworks */, 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 4C5FF8CD362E3039D6EDF5AC /* Frameworks */ = { 48 | isa = PBXGroup; 49 | children = ( 50 | B704C183DF3C49484D0FC40A /* Pods_ExampleApp.framework */, 51 | ); 52 | name = Frameworks; 53 | sourceTree = ""; 54 | }; 55 | A26EB6B322B0397A006A57F9 = { 56 | isa = PBXGroup; 57 | children = ( 58 | A26EB6BE22B0397A006A57F9 /* ExampleApp */, 59 | A26EB6BD22B0397A006A57F9 /* Products */, 60 | B389CCE8590A639BECE24C62 /* Pods */, 61 | 4C5FF8CD362E3039D6EDF5AC /* Frameworks */, 62 | ); 63 | sourceTree = ""; 64 | }; 65 | A26EB6BD22B0397A006A57F9 /* Products */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | A26EB6BC22B0397A006A57F9 /* ExampleApp.app */, 69 | ); 70 | name = Products; 71 | sourceTree = ""; 72 | }; 73 | A26EB6BE22B0397A006A57F9 /* ExampleApp */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | A2838BC922B03AA500808FB2 /* Example.swift */, 77 | A26EB6BF22B0397A006A57F9 /* AppDelegate.swift */, 78 | A26EB6C122B0397A006A57F9 /* SceneDelegate.swift */, 79 | A26EB6C322B0397A006A57F9 /* ViewController.swift */, 80 | A26EB6C522B0397A006A57F9 /* Main.storyboard */, 81 | A26EB6C822B0397C006A57F9 /* Assets.xcassets */, 82 | A26EB6CA22B0397C006A57F9 /* LaunchScreen.storyboard */, 83 | A26EB6CD22B0397C006A57F9 /* Info.plist */, 84 | ); 85 | path = ExampleApp; 86 | sourceTree = ""; 87 | }; 88 | B389CCE8590A639BECE24C62 /* Pods */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | 53C7367904650D29CE5478A7 /* Pods-ExampleApp.debug.xcconfig */, 92 | 04E5A60733794FBFF91FD10C /* Pods-ExampleApp.release.xcconfig */, 93 | ); 94 | path = Pods; 95 | sourceTree = ""; 96 | }; 97 | /* End PBXGroup section */ 98 | 99 | /* Begin PBXNativeTarget section */ 100 | A26EB6BB22B0397A006A57F9 /* ExampleApp */ = { 101 | isa = PBXNativeTarget; 102 | buildConfigurationList = A26EB6D022B0397C006A57F9 /* Build configuration list for PBXNativeTarget "ExampleApp" */; 103 | buildPhases = ( 104 | 6E3ABA54040574885F3331BD /* [CP] Check Pods Manifest.lock */, 105 | A26EB6B822B0397A006A57F9 /* Sources */, 106 | A26EB6B922B0397A006A57F9 /* Frameworks */, 107 | A26EB6BA22B0397A006A57F9 /* Resources */, 108 | B2F64257D3AC062630759C93 /* [CP] Embed Pods Frameworks */, 109 | ); 110 | buildRules = ( 111 | ); 112 | dependencies = ( 113 | ); 114 | name = ExampleApp; 115 | productName = ExampleApp; 116 | productReference = A26EB6BC22B0397A006A57F9 /* ExampleApp.app */; 117 | productType = "com.apple.product-type.application"; 118 | }; 119 | /* End PBXNativeTarget section */ 120 | 121 | /* Begin PBXProject section */ 122 | A26EB6B422B0397A006A57F9 /* Project object */ = { 123 | isa = PBXProject; 124 | attributes = { 125 | LastSwiftUpdateCheck = 1100; 126 | LastUpgradeCheck = 1100; 127 | ORGANIZATIONNAME = freak4pc; 128 | TargetAttributes = { 129 | A26EB6BB22B0397A006A57F9 = { 130 | CreatedOnToolsVersion = 11.0; 131 | }; 132 | }; 133 | }; 134 | buildConfigurationList = A26EB6B722B0397A006A57F9 /* Build configuration list for PBXProject "ExampleApp" */; 135 | compatibilityVersion = "Xcode 9.3"; 136 | developmentRegion = en; 137 | hasScannedForEncodings = 0; 138 | knownRegions = ( 139 | en, 140 | Base, 141 | ); 142 | mainGroup = A26EB6B322B0397A006A57F9; 143 | productRefGroup = A26EB6BD22B0397A006A57F9 /* Products */; 144 | projectDirPath = ""; 145 | projectRoot = ""; 146 | targets = ( 147 | A26EB6BB22B0397A006A57F9 /* ExampleApp */, 148 | ); 149 | }; 150 | /* End PBXProject section */ 151 | 152 | /* Begin PBXResourcesBuildPhase section */ 153 | A26EB6BA22B0397A006A57F9 /* Resources */ = { 154 | isa = PBXResourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | A26EB6CC22B0397C006A57F9 /* LaunchScreen.storyboard in Resources */, 158 | A26EB6C922B0397C006A57F9 /* Assets.xcassets in Resources */, 159 | A26EB6C722B0397A006A57F9 /* Main.storyboard in Resources */, 160 | ); 161 | runOnlyForDeploymentPostprocessing = 0; 162 | }; 163 | /* End PBXResourcesBuildPhase section */ 164 | 165 | /* Begin PBXShellScriptBuildPhase section */ 166 | 6E3ABA54040574885F3331BD /* [CP] Check Pods Manifest.lock */ = { 167 | isa = PBXShellScriptBuildPhase; 168 | buildActionMask = 2147483647; 169 | files = ( 170 | ); 171 | inputFileListPaths = ( 172 | ); 173 | inputPaths = ( 174 | "${PODS_PODFILE_DIR_PATH}/Podfile.lock", 175 | "${PODS_ROOT}/Manifest.lock", 176 | ); 177 | name = "[CP] Check Pods Manifest.lock"; 178 | outputFileListPaths = ( 179 | ); 180 | outputPaths = ( 181 | "$(DERIVED_FILE_DIR)/Pods-ExampleApp-checkManifestLockResult.txt", 182 | ); 183 | runOnlyForDeploymentPostprocessing = 0; 184 | shellPath = /bin/sh; 185 | shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; 186 | showEnvVarsInLog = 0; 187 | }; 188 | B2F64257D3AC062630759C93 /* [CP] Embed Pods Frameworks */ = { 189 | isa = PBXShellScriptBuildPhase; 190 | buildActionMask = 2147483647; 191 | files = ( 192 | ); 193 | inputFileListPaths = ( 194 | "${PODS_ROOT}/Target Support Files/Pods-ExampleApp/Pods-ExampleApp-frameworks-${CONFIGURATION}-input-files.xcfilelist", 195 | ); 196 | name = "[CP] Embed Pods Frameworks"; 197 | outputFileListPaths = ( 198 | "${PODS_ROOT}/Target Support Files/Pods-ExampleApp/Pods-ExampleApp-frameworks-${CONFIGURATION}-output-files.xcfilelist", 199 | ); 200 | runOnlyForDeploymentPostprocessing = 0; 201 | shellPath = /bin/sh; 202 | shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-ExampleApp/Pods-ExampleApp-frameworks.sh\"\n"; 203 | showEnvVarsInLog = 0; 204 | }; 205 | /* End PBXShellScriptBuildPhase section */ 206 | 207 | /* Begin PBXSourcesBuildPhase section */ 208 | A26EB6B822B0397A006A57F9 /* Sources */ = { 209 | isa = PBXSourcesBuildPhase; 210 | buildActionMask = 2147483647; 211 | files = ( 212 | A2838BCA22B03AA500808FB2 /* Example.swift in Sources */, 213 | A26EB6C422B0397A006A57F9 /* ViewController.swift in Sources */, 214 | A26EB6C022B0397A006A57F9 /* AppDelegate.swift in Sources */, 215 | A26EB6C222B0397A006A57F9 /* SceneDelegate.swift in Sources */, 216 | ); 217 | runOnlyForDeploymentPostprocessing = 0; 218 | }; 219 | /* End PBXSourcesBuildPhase section */ 220 | 221 | /* Begin PBXVariantGroup section */ 222 | A26EB6C522B0397A006A57F9 /* Main.storyboard */ = { 223 | isa = PBXVariantGroup; 224 | children = ( 225 | A26EB6C622B0397A006A57F9 /* Base */, 226 | ); 227 | name = Main.storyboard; 228 | sourceTree = ""; 229 | }; 230 | A26EB6CA22B0397C006A57F9 /* LaunchScreen.storyboard */ = { 231 | isa = PBXVariantGroup; 232 | children = ( 233 | A26EB6CB22B0397C006A57F9 /* Base */, 234 | ); 235 | name = LaunchScreen.storyboard; 236 | sourceTree = ""; 237 | }; 238 | /* End PBXVariantGroup section */ 239 | 240 | /* Begin XCBuildConfiguration section */ 241 | A26EB6CE22B0397C006A57F9 /* Debug */ = { 242 | isa = XCBuildConfiguration; 243 | buildSettings = { 244 | ALWAYS_SEARCH_USER_PATHS = NO; 245 | CLANG_ANALYZER_NONNULL = YES; 246 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_ENABLE_OBJC_WEAK = YES; 252 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 253 | CLANG_WARN_BOOL_CONVERSION = YES; 254 | CLANG_WARN_COMMA = YES; 255 | CLANG_WARN_CONSTANT_CONVERSION = YES; 256 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 257 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 258 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 259 | CLANG_WARN_EMPTY_BODY = YES; 260 | CLANG_WARN_ENUM_CONVERSION = YES; 261 | CLANG_WARN_INFINITE_RECURSION = YES; 262 | CLANG_WARN_INT_CONVERSION = YES; 263 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 264 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 265 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 266 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 267 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 268 | CLANG_WARN_STRICT_PROTOTYPES = YES; 269 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 270 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 271 | CLANG_WARN_UNREACHABLE_CODE = YES; 272 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 273 | COPY_PHASE_STRIP = NO; 274 | DEBUG_INFORMATION_FORMAT = dwarf; 275 | ENABLE_STRICT_OBJC_MSGSEND = YES; 276 | ENABLE_TESTABILITY = YES; 277 | GCC_C_LANGUAGE_STANDARD = gnu11; 278 | GCC_DYNAMIC_NO_PIC = NO; 279 | GCC_NO_COMMON_BLOCKS = YES; 280 | GCC_OPTIMIZATION_LEVEL = 0; 281 | GCC_PREPROCESSOR_DEFINITIONS = ( 282 | "DEBUG=1", 283 | "$(inherited)", 284 | ); 285 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 286 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 287 | GCC_WARN_UNDECLARED_SELECTOR = YES; 288 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 289 | GCC_WARN_UNUSED_FUNCTION = YES; 290 | GCC_WARN_UNUSED_VARIABLE = YES; 291 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 292 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 293 | MTL_FAST_MATH = YES; 294 | ONLY_ACTIVE_ARCH = YES; 295 | SDKROOT = iphoneos; 296 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 297 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 298 | }; 299 | name = Debug; 300 | }; 301 | A26EB6CF22B0397C006A57F9 /* Release */ = { 302 | isa = XCBuildConfiguration; 303 | buildSettings = { 304 | ALWAYS_SEARCH_USER_PATHS = NO; 305 | CLANG_ANALYZER_NONNULL = YES; 306 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 307 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 308 | CLANG_CXX_LIBRARY = "libc++"; 309 | CLANG_ENABLE_MODULES = YES; 310 | CLANG_ENABLE_OBJC_ARC = YES; 311 | CLANG_ENABLE_OBJC_WEAK = YES; 312 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 313 | CLANG_WARN_BOOL_CONVERSION = YES; 314 | CLANG_WARN_COMMA = YES; 315 | CLANG_WARN_CONSTANT_CONVERSION = YES; 316 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 317 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 318 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 319 | CLANG_WARN_EMPTY_BODY = YES; 320 | CLANG_WARN_ENUM_CONVERSION = YES; 321 | CLANG_WARN_INFINITE_RECURSION = YES; 322 | CLANG_WARN_INT_CONVERSION = YES; 323 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 324 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 325 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 326 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 327 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 328 | CLANG_WARN_STRICT_PROTOTYPES = YES; 329 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 330 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 331 | CLANG_WARN_UNREACHABLE_CODE = YES; 332 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 333 | COPY_PHASE_STRIP = NO; 334 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 335 | ENABLE_NS_ASSERTIONS = NO; 336 | ENABLE_STRICT_OBJC_MSGSEND = YES; 337 | GCC_C_LANGUAGE_STANDARD = gnu11; 338 | GCC_NO_COMMON_BLOCKS = YES; 339 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 340 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 341 | GCC_WARN_UNDECLARED_SELECTOR = YES; 342 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 343 | GCC_WARN_UNUSED_FUNCTION = YES; 344 | GCC_WARN_UNUSED_VARIABLE = YES; 345 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 346 | MTL_ENABLE_DEBUG_INFO = NO; 347 | MTL_FAST_MATH = YES; 348 | SDKROOT = iphoneos; 349 | SWIFT_COMPILATION_MODE = wholemodule; 350 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 351 | VALIDATE_PRODUCT = YES; 352 | }; 353 | name = Release; 354 | }; 355 | A26EB6D122B0397C006A57F9 /* Debug */ = { 356 | isa = XCBuildConfiguration; 357 | baseConfigurationReference = 53C7367904650D29CE5478A7 /* Pods-ExampleApp.debug.xcconfig */; 358 | buildSettings = { 359 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 360 | CODE_SIGN_STYLE = Automatic; 361 | DEVELOPMENT_TEAM = 7374UMDQL2; 362 | INFOPLIST_FILE = ExampleApp/Info.plist; 363 | LD_RUNPATH_SEARCH_PATHS = ( 364 | "$(inherited)", 365 | "@executable_path/Frameworks", 366 | ); 367 | PRODUCT_BUNDLE_IDENTIFIER = com.freak4pc.ExampleApp; 368 | PRODUCT_NAME = "$(TARGET_NAME)"; 369 | SWIFT_VERSION = 5.0; 370 | TARGETED_DEVICE_FAMILY = "1,2"; 371 | }; 372 | name = Debug; 373 | }; 374 | A26EB6D222B0397C006A57F9 /* Release */ = { 375 | isa = XCBuildConfiguration; 376 | baseConfigurationReference = 04E5A60733794FBFF91FD10C /* Pods-ExampleApp.release.xcconfig */; 377 | buildSettings = { 378 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 379 | CODE_SIGN_STYLE = Automatic; 380 | DEVELOPMENT_TEAM = 7374UMDQL2; 381 | INFOPLIST_FILE = ExampleApp/Info.plist; 382 | LD_RUNPATH_SEARCH_PATHS = ( 383 | "$(inherited)", 384 | "@executable_path/Frameworks", 385 | ); 386 | PRODUCT_BUNDLE_IDENTIFIER = com.freak4pc.ExampleApp; 387 | PRODUCT_NAME = "$(TARGET_NAME)"; 388 | SWIFT_VERSION = 5.0; 389 | TARGETED_DEVICE_FAMILY = "1,2"; 390 | }; 391 | name = Release; 392 | }; 393 | /* End XCBuildConfiguration section */ 394 | 395 | /* Begin XCConfigurationList section */ 396 | A26EB6B722B0397A006A57F9 /* Build configuration list for PBXProject "ExampleApp" */ = { 397 | isa = XCConfigurationList; 398 | buildConfigurations = ( 399 | A26EB6CE22B0397C006A57F9 /* Debug */, 400 | A26EB6CF22B0397C006A57F9 /* Release */, 401 | ); 402 | defaultConfigurationIsVisible = 0; 403 | defaultConfigurationName = Release; 404 | }; 405 | A26EB6D022B0397C006A57F9 /* Build configuration list for PBXNativeTarget "ExampleApp" */ = { 406 | isa = XCConfigurationList; 407 | buildConfigurations = ( 408 | A26EB6D122B0397C006A57F9 /* Debug */, 409 | A26EB6D222B0397C006A57F9 /* Release */, 410 | ); 411 | defaultConfigurationIsVisible = 0; 412 | defaultConfigurationName = Release; 413 | }; 414 | /* End XCConfigurationList section */ 415 | }; 416 | rootObject = A26EB6B422B0397A006A57F9 /* Project object */; 417 | } 418 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // ExampleApp 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 36 | 44 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Example.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Example.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Combine 11 | import RxSwift 12 | import RxRelay 13 | 14 | enum Example: Int { 15 | case observableAsPublisher = 101 16 | case publisherAsObservable 17 | case relaysZippedInCombine 18 | 19 | func play(with textView: UITextView) { 20 | textView.text = "" 21 | textView.contentOffset = .zero 22 | 23 | switch self { 24 | case .observableAsPublisher: 25 | observableAsPublisher(with: textView) 26 | case .publisherAsObservable: 27 | publisherAsObservable(with: textView) 28 | case .relaysZippedInCombine: 29 | relaysZippedInCombine(with: textView) 30 | } 31 | } 32 | } 33 | 34 | private extension Example { 35 | func observableAsPublisher(with textView: UITextView) { 36 | let stream = Observable.from(Array(0...100)) 37 | 38 | let id = "Observable as Publisher" 39 | 40 | textView.append(line: "🗞 \(id)") 41 | textView.append(line: "=====================") 42 | 43 | _ = stream 44 | .publisher 45 | .sink( 46 | receiveCompletion: { completion in 47 | switch completion { 48 | case .finished: 49 | textView.append(line: "\(id) -> receive finished") 50 | textView.append(line: "=========================\n") 51 | case .failure(let error): 52 | textView.append(line: "\(id) -> receive failure: \(error)") 53 | } 54 | }, 55 | receiveValue: { value in 56 | textView.append(line: "\(id) -> receive value: \(value)") 57 | } 58 | ) 59 | } 60 | 61 | func publisherAsObservable(with textView: UITextView) { 62 | let publisher = PassthroughSubject() 63 | 64 | let id = "Publisher as Observable" 65 | 66 | textView.append(line: "👀 \(id)") 67 | textView.append(line: "=====================") 68 | 69 | _ = publisher 70 | .asObservable() 71 | .do(onDispose: { 72 | textView.append(line: "\(id) -> disposed") 73 | textView.append(line: "=========================\n") 74 | }) 75 | .subscribe { event in 76 | switch event { 77 | case .next(let element): 78 | textView.append(line: "\(id) -> next(\(element))") 79 | case .error(let error): 80 | textView.append(line: "\(id) -> error(\(error))") 81 | case .completed: 82 | textView.append(line: "\(id) -> completed") 83 | } 84 | } 85 | 86 | (0...100).forEach { publisher.send($0) } 87 | publisher.send(completion: .finished) 88 | } 89 | 90 | func relaysZippedInCombine(with textView: UITextView) { 91 | let relay1 = PublishRelay() 92 | let relay2 = BehaviorRelay(value: 0) 93 | 94 | let id = "Zipped Relays in Combine" 95 | 96 | textView.append(line: "🤐 \(id)") 97 | textView.append(line: "=====================") 98 | 99 | let subscription = Publishers.Zip(relay1.publisher, relay2.publisher) 100 | .dropFirst() 101 | .sink( 102 | receiveCompletion: { completion in 103 | switch completion { 104 | case .finished: 105 | textView.append(line: "\(id) -> receive finished") 106 | textView.append(line: "=========================\n") 107 | case .failure(let error): 108 | textView.append(line: "\(id) -> receive failure: \(error)") 109 | } 110 | }, 111 | receiveValue: { value in 112 | textView.append(line: "\(id) -> receive value: \(value)") 113 | } 114 | ) 115 | 116 | let p1 = PassthroughSubject() 117 | let p2 = PassthroughSubject() 118 | 119 | _ = p1.asObservable().bind(to: relay1) 120 | _ = p2.asObservable().bind(to: relay2) 121 | 122 | 123 | (0...50).forEach { p1.send($0) } 124 | p1.send(completion: .finished) 125 | 126 | (0...50).reversed().forEach { p2.send($0) } 127 | p2.send(completion: .finished) 128 | 129 | subscription.cancel() 130 | } 131 | } 132 | 133 | private extension UITextView { 134 | func append(line: String) { 135 | text = text + "\n" + line 136 | let bottom = NSRange(location: text.count - 1, length: 1) 137 | scrollRangeToVisible(bottom) 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UILaunchStoryboardName 33 | LaunchScreen 34 | UISceneConfigurationName 35 | Default Configuration 36 | UISceneDelegateClassName 37 | $(PRODUCT_MODULE_NAME).SceneDelegate 38 | UISceneStoryboardFile 39 | Main 40 | 41 | 42 | 43 | 44 | UILaunchStoryboardName 45 | LaunchScreen 46 | UIMainStoryboardFile 47 | Main 48 | UIRequiredDeviceCapabilities 49 | 50 | armv7 51 | 52 | UISupportedInterfaceOrientations 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationLandscapeLeft 56 | UIInterfaceOrientationLandscapeRight 57 | 58 | UISupportedInterfaceOrientations~ipad 59 | 60 | UIInterfaceOrientationPortrait 61 | UIInterfaceOrientationPortraitUpsideDown 62 | UIInterfaceOrientationLandscapeLeft 63 | UIInterfaceOrientationLandscapeRight 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // ExampleApp 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 12 | var window: UIWindow? 13 | } 14 | 15 | -------------------------------------------------------------------------------- /ExampleApp/ExampleApp/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // ExampleApp 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import RxCombine 11 | 12 | class ViewController: UIViewController { 13 | @IBOutlet private var textView: UITextView! 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | textView.text = "Tap any of the buttons below ... ⬇️" 18 | } 19 | 20 | @IBAction private func tappedExample(_ sender: UIButton) { 21 | guard let example = Example(rawValue: sender.tag) else { return } 22 | example.play(with: textView) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExampleApp/Podfile: -------------------------------------------------------------------------------- 1 | platform :ios, '13.0' 2 | 3 | target 'ExampleApp' do 4 | use_frameworks! 5 | 6 | pod 'RxCombine', :path => '../' 7 | end 8 | -------------------------------------------------------------------------------- /ExampleApp/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - RxCombine (2.0.0): 3 | - RxRelay (~> 6) 4 | - RxSwift (~> 6) 5 | - RxRelay (6.0.0): 6 | - RxSwift (= 6.0.0) 7 | - RxSwift (6.0.0) 8 | 9 | DEPENDENCIES: 10 | - RxCombine (from `../`) 11 | 12 | SPEC REPOS: 13 | trunk: 14 | - RxRelay 15 | - RxSwift 16 | 17 | EXTERNAL SOURCES: 18 | RxCombine: 19 | :path: "../" 20 | 21 | SPEC CHECKSUMS: 22 | RxCombine: 1d88b0392e4c9ccfc5187af3dbc8b10791a4b128 23 | RxRelay: 8d593be109c06ea850df027351beba614b012ffb 24 | RxSwift: c14e798c59b9f6e9a2df8fd235602e85cc044295 25 | 26 | PODFILE CHECKSUM: 27ab59783b75a3dbb4fec21bad1cf7a4348abaa3 27 | 28 | COCOAPODS: 1.10.0 29 | -------------------------------------------------------------------------------- /Helpers/make_project.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'xcodeproj' 4 | 5 | ### This script creates an Xcode project using Swift Package Manager 6 | ### and then applies every needed configurations and other changes. 7 | ### 8 | ### Written by Shai Mishali, June 1st 2019. 9 | 10 | # Make sure SPM is Installed 11 | system("swift package > /dev/null 2>&1") 12 | unless $?.exitstatus == 0 13 | puts "SPM is not installed" 14 | exit 1 15 | end 16 | 17 | # Make sure we have a Package.swift file 18 | abort("Can't locate Package.swift") unless File.exist?("Package.swift") 19 | 20 | # Attempt generating Xcode Project 21 | system("swift package generate-xcodeproj --enable-code-coverage") 22 | 23 | project = Xcodeproj::Project.open('RxCombine.xcodeproj') 24 | targets = ['RxCombine', 'RxCombinePackageDescription', 'RxCombineTests', 'RxCombinePackageTests'] 25 | project.targets.each do |target| 26 | if targets.include?(target.name) 27 | # swiftlint = target.new_shell_script_build_phase('SwiftLint') 28 | # swiftlint.shell_script = <<-SwiftLint 29 | # if which swiftlint >/dev/null; then 30 | # swiftlint 31 | # else 32 | # echo "warning: SwiftLint not installed" 33 | # fi 34 | # SwiftLint 35 | 36 | # index = target.build_phases.index { |phase| (defined? phase.name) && phase.name == 'SwiftLint' } 37 | # target.build_phases.move_from(index, 0) 38 | else 39 | target.build_configurations.each do |config| 40 | config.build_settings['GCC_WARN_INHIBIT_ALL_WARNINGS'] = 'YES' 41 | config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -suppress-warnings' 42 | end 43 | end 44 | end 45 | 46 | project::save() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Shai Mishali 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | archive: 2 | scripts/carthage-archive.sh 3 | project: 4 | ruby Helpers/make_project.rb -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "RxCombine", 7 | platforms: [ 8 | .macOS(.v10_10), .iOS(.v9), .tvOS(.v9), .watchOS(.v3) 9 | ], 10 | products: [ 11 | .library( 12 | name: "RxCombine", 13 | targets: ["RxCombine"]), 14 | ], 15 | dependencies: [ 16 | .package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.0.0") 17 | ], 18 | targets: [ 19 | .target( 20 | name: "RxCombine", 21 | dependencies: ["RxSwift", "RxRelay"], 22 | path: "Sources"), 23 | .testTarget( 24 | name: "RxCombineTests", 25 | dependencies: ["RxCombine"], 26 | path: "Tests" 27 | ) 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxCombine 2 | 3 |

4 | 5 |

6 | Build Status 7 | Code Coverage for RxCombine on codecov 8 |
9 | RxCombine supports CocoaPods 10 | RxCombine supports Swift Package Manager (SPM) 11 | RxCombine supports Carthage 12 |
13 | 14 |

15 | 16 | RxCombine provides bi-directional type bridging between [RxSwift](https://github.com/ReactiveX/RxSwift.git) and Apple's [Combine](https://developer.apple.com/documentation/combine) framework. 17 | 18 | **Note**: This is highly experimental, and basically just a quickly-put-together PoC. I gladly accept PRs, ideas, opinions, or improvements. Thank you ! :) 19 | 20 | ## Basic Examples 21 | 22 | Check out the Example App in the **ExampleApp** folder. Run `pod install` before opening the project. 23 | 24 |

25 | 26 | ## Installation 27 | 28 | ### CocoaPods 29 | 30 | Add the following line to your **Podfile**: 31 | 32 | ```rb 33 | pod 'RxCombine' 34 | ``` 35 | 36 | ### Swift Package Manager 37 | 38 | Add the following dependency to your **Package.swift** file: 39 | 40 | ```swift 41 | .package(url: "https://github.com/CombineCommunity/RxCombine.git", from: "1.6.0") 42 | ``` 43 | 44 | ### Carthage 45 | 46 | Carthage support is offered as a prebuilt binary. 47 | 48 | Add the following to your **Cartfile**: 49 | 50 | ``` 51 | github "CombineCommunity/RxCombine" 52 | ``` 53 | 54 | ## I want to ... 55 | 56 | ### Use RxSwift in my Combine code 57 | 58 | RxCombine provides several helpers and conversions to help you bridge your existing RxSwift types to Combine. 59 | 60 | **Note**: If you want to learn more about the parallel operators in Combine from RxSwift, check out my [RxSwift to Combine Cheat Sheet](https://medium.com/gett-engineering/rxswift-to-apples-combine-cheat-sheet-e9ce32b14c5b) *(or on [GitHub](https://github.com/freak4pc/rxswift-to-combine-cheatsheet))*. 61 | 62 | * `Observable` (and other `ObservableConvertibleType`s) have a `publisher` property which returns a `AnyPublisher` mirroring the underlying `Observable`. 63 | 64 | ```swift 65 | let observable = Observable.just("Hello, Combine!") 66 | 67 | observable 68 | .publisher // AnyPublisher 69 | .sink(receiveValue: { value in ... }) 70 | ``` 71 | 72 | * `Relays` and `Subjects` can be converted to their Combine-counterparts using the `toCombine()` method, so you can use them as if they are regular Combine Subjects, and have them connected to your existing subjects. 73 | 74 | ```swift 75 | let relay = BehaviorRelay(value: 0) 76 | 77 | // Use `sink` on RxSwift relay 78 | let combineSubject = relay.toCombine() 79 | 80 | combineSubject.sink(receiveValue: { value in ... }) 81 | 82 | // Use `send(value:)` on RxSwift relay 83 | combineSubject.send(1) 84 | combineSubject.send(2) 85 | combineSubject.send(3) 86 | ``` 87 | 88 | ### Use Combine in my RxSwift code 89 | 90 | RxCombine provides several helpers and conversions to help you bridge Combine code and types into your existing RxSwift codebase. 91 | 92 | * `Publisher`s have a `asObservable()` method, providing an `Observable` mirroring the underlying `Publisher`. 93 | ```swift 94 | // A publisher publishing numbers from 0 to 100. 95 | let publisher = AnyPublisher { subscriber in 96 | (0...100).forEach { _ = subscriber.receive($0) } 97 | subscriber.receive(completion: .finished) 98 | } 99 | 100 | publisher 101 | .asObservable() // Observable 102 | .subscribe(onNext: { num in ... }) 103 | ``` 104 | 105 | * `PassthroughSubject` and `CurrentValueSubject` both have a `asAnyObserver()` method which returns a `AnyObserver`. Binding to it from your RxSwift code pushes the events to the underlying Combine Subject. 106 | 107 | ```swift 108 | // Combine Subject 109 | let subject = PassthroughSubject() 110 | 111 | // A publisher publishing numbers from 0 to 100. 112 | let publisher = AnyPublisher { subscriber in 113 | (0...100).forEach { _ = subscriber.receive($0) } 114 | subscriber.receive(completion: .finished) 115 | } 116 | 117 | // Convert a Publisher to an Observable and bind it 118 | // back to a Combine Subject 🤯🤯🤯 119 | publisher.asObservable() 120 | .bind(to: subject) 121 | 122 | Observable.of(10, 5, 7, 4, 1, 6) 123 | .subscribe(subject.asAnyObserver()) 124 | ``` 125 | 126 | ## Future ideas 127 | 128 | * ~~Add CI / Tests~~ 129 | * ~~Carthage Support~~ 130 | * Bridge SwiftUI with RxCocoa/RxSwift 131 | * ~~Partial Backpressure support, perhaps?~~ 132 | * ... your ideas? :) 133 | 134 | ## License 135 | 136 | MIT, of course ;-) See the [LICENSE](LICENSE) file. 137 | 138 | The Apple logo and the Combine framework are property of Apple Inc. 139 | -------------------------------------------------------------------------------- /Resources/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CombineCommunity/RxCombine/010ef6fcca69cbbabe8ed7f52b31485b70c3a22d/Resources/example.gif -------------------------------------------------------------------------------- /Resources/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CombineCommunity/RxCombine/010ef6fcca69cbbabe8ed7f52b31485b70c3a22d/Resources/logo.png -------------------------------------------------------------------------------- /RxCombine.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "RxCombine" 3 | s.version = "2.0.1" 4 | s.summary = "RxSwift is a Swift implementation of Reactive Extensions" 5 | s.description = <<-DESC 6 | Bi-directional type conversions between RxSwift and Apple's Combine framework. 7 | ``` 8 | DESC 9 | s.homepage = "https://github.com/freak4pc/RxCombine" 10 | s.license = 'MIT' 11 | s.author = { "Shai Mishali" => "freak4pc@gmail.com" } 12 | s.source = { :git => "https://github.com/freak4pc/RxCombine.git", :tag => s.version.to_s } 13 | 14 | s.requires_arc = true 15 | 16 | s.ios.deployment_target = '9.0' 17 | s.osx.deployment_target = '10.9' 18 | s.watchos.deployment_target = '3.0' 19 | s.tvos.deployment_target = '9.0' 20 | 21 | s.source_files = 'Sources/**/*.swift' 22 | s.frameworks = ['Combine'] 23 | s.dependency 'RxSwift', '~> 6' 24 | s.dependency 'RxRelay', '~> 6' 25 | 26 | s.swift_version = '5.1' 27 | end -------------------------------------------------------------------------------- /Sources/Combine+Rx/Publisher+Rx.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Publisher+Rx.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | 13 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 14 | public extension Publisher { 15 | /// Returns an Observable representing the underlying 16 | /// Publisher. Upon subscription, the Publisher's sink pushes 17 | /// events into the Observable. Upon disposing of the subscription, 18 | /// the sink is cancelled. 19 | /// 20 | /// - returns: Observable 21 | func asObservable() -> Observable { 22 | Observable.create { observer in 23 | let cancellable = self.sink( 24 | receiveCompletion: { completion in 25 | switch completion { 26 | case .finished: 27 | observer.onCompleted() 28 | case .failure(let error): 29 | observer.onError(error) 30 | } 31 | }, 32 | receiveValue: { value in 33 | observer.onNext(value) 34 | }) 35 | 36 | return Disposables.create { cancellable.cancel() } 37 | } 38 | } 39 | } 40 | 41 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 42 | public extension Publisher where Failure == Never { 43 | /// Returns an Observable representing the underlying 44 | /// Publisher. Upon subscription, the Publisher's sink pushes 45 | /// events into the Observable. Upon disposing of the subscription, 46 | /// the sink is cancelled. 47 | /// 48 | /// - returns: Observable 49 | func asInfallible() -> Infallible { 50 | Infallible.create { observer in 51 | let cancellable = self.sink( 52 | receiveCompletion: { completion in 53 | observer(.completed) 54 | }, 55 | receiveValue: { value in 56 | observer(.next(value)) 57 | }) 58 | 59 | return Disposables.create { cancellable.cancel() } 60 | } 61 | } 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /Sources/Combine+Rx/Subject+Rx.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Subject+Rx.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | import RxRelay 13 | 14 | /// Represents a Combine Subject that can be converted 15 | /// to a RxSwift AnyObserver of the underlying Output type. 16 | /// 17 | /// - note: This only works when the underlying Failure is Swift.Error, 18 | /// since RxSwift has no typed errors. 19 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 20 | public protocol AnyObserverConvertible: Combine.Subject where Failure == Swift.Error { 21 | associatedtype Output 22 | 23 | /// Returns a RxSwift `AnyObserver` wrapping the Subject 24 | /// 25 | /// - returns: AnyObserver 26 | func asAnyObserver() -> AnyObserver 27 | } 28 | 29 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 30 | public extension AnyObserverConvertible { 31 | /// Returns a RxSwift AnyObserver wrapping the Subject 32 | /// 33 | /// - returns: AnyObserver 34 | func asAnyObserver() -> AnyObserver { 35 | AnyObserver { [weak self] event in 36 | guard let self = self else { return } 37 | switch event { 38 | case .next(let value): 39 | self.send(value) 40 | case .error(let error): 41 | self.send(completion: .failure(error)) 42 | case .completed: 43 | self.send(completion: .finished) 44 | } 45 | } 46 | } 47 | } 48 | 49 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 50 | extension PassthroughSubject: AnyObserverConvertible where Failure == Swift.Error {} 51 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 52 | extension CurrentValueSubject: AnyObserverConvertible where Failure == Swift.Error {} 53 | 54 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 55 | public extension ObservableConvertibleType { 56 | /** 57 | Creates new subscription and sends elements to a Combine Subject. 58 | 59 | - parameter to: Combine subject to receives events. 60 | - returns: Disposable object that can be used to unsubscribe the observers. 61 | - seealso: `AnyOserverConvertible` 62 | */ 63 | func bind(to subject: S) -> Disposable where S.Output == Element { 64 | asObservable().subscribe(subject.asAnyObserver()) 65 | } 66 | } 67 | #endif 68 | -------------------------------------------------------------------------------- /Sources/Common/DemandBuffer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DemandBuffer.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 21/02/2020. 6 | // Copyright © 2020 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import class Foundation.NSRecursiveLock 12 | 13 | /// A buffer responsible for managing the demand of a downstream 14 | /// subscriber for an upstream publisher 15 | /// 16 | /// It buffers values and completion events and forwards them dynamically 17 | /// according to the demand requested by the downstream 18 | /// 19 | /// In a sense, the subscription only relays the requests for demand, as well 20 | /// the events emitted by the upstream — to this buffer, which manages 21 | /// the entire behavior and backpressure contract 22 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 23 | class DemandBuffer { 24 | private let lock = NSRecursiveLock() 25 | private var buffer = [S.Input]() 26 | private let subscriber: S 27 | private var completion: Subscribers.Completion? 28 | private var demandState = Demand() 29 | 30 | /// Initialize a new demand buffer for a provided downstream subscriber 31 | /// 32 | /// - parameter subscriber: The downstream subscriber demanding events 33 | init(subscriber: S) { 34 | self.subscriber = subscriber 35 | } 36 | 37 | /// Buffer an upstream value to later be forwarded to 38 | /// the downstream subscriber, once it demands it 39 | /// 40 | /// - parameter value: Upstream value to buffer 41 | /// 42 | /// - returns: The demand fulfilled by the bufferr 43 | func buffer(value: S.Input) -> Subscribers.Demand { 44 | precondition(self.completion == nil, 45 | "How could a completed publisher sent values?! Beats me 🤷‍♂️") 46 | 47 | switch demandState.requested { 48 | case .unlimited: 49 | return subscriber.receive(value) 50 | default: 51 | buffer.append(value) 52 | return flush() 53 | } 54 | } 55 | 56 | /// Complete the demand buffer with an upstream completion event 57 | /// 58 | /// This method will deplete the buffer immediately, 59 | /// based on the currently accumulated demand, and relay the 60 | /// completion event down as soon as demand is fulfilled 61 | /// 62 | /// - parameter completion: Completion event 63 | func complete(completion: Subscribers.Completion) { 64 | precondition(self.completion == nil, 65 | "Completion have already occured, which is quite awkward 🥺") 66 | 67 | self.completion = completion 68 | _ = flush() 69 | } 70 | 71 | /// Signal to the buffer that the downstream requested new demand 72 | /// 73 | /// - note: The buffer will attempt to flush as many events rqeuested 74 | /// by the downstream at this point 75 | func demand(_ demand: Subscribers.Demand) -> Subscribers.Demand { 76 | flush(adding: demand) 77 | } 78 | 79 | /// Flush buffered events to the downstream based on the current 80 | /// state of the downstream's demand 81 | /// 82 | /// - parameter newDemand: The new demand to add. If `nil`, the flush isn't the 83 | /// result of an explicit demand change 84 | /// 85 | /// - note: After fulfilling the downstream's request, if completion 86 | /// has already occured, the buffer will be cleared and the 87 | /// completion event will be sent to the downstream subscriber 88 | private func flush(adding newDemand: Subscribers.Demand? = nil) -> Subscribers.Demand { 89 | lock.lock() 90 | defer { lock.unlock() } 91 | 92 | if let newDemand = newDemand { 93 | demandState.requested += newDemand 94 | } 95 | 96 | // If buffer isn't ready for flushing, return immediately 97 | guard demandState.requested > 0 || newDemand == Subscribers.Demand.none else { return .none } 98 | 99 | while !buffer.isEmpty && demandState.processed < demandState.requested { 100 | demandState.requested += subscriber.receive(buffer.remove(at: 0)) 101 | demandState.processed += 1 102 | } 103 | 104 | if let completion = completion { 105 | // Completion event was already sent 106 | buffer = [] 107 | demandState = .init() 108 | self.completion = nil 109 | subscriber.receive(completion: completion) 110 | return .none 111 | } 112 | 113 | let sentDemand = demandState.requested - demandState.sent 114 | demandState.sent += sentDemand 115 | return sentDemand 116 | } 117 | } 118 | 119 | // MARK: - Private Helpers 120 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 121 | private extension DemandBuffer { 122 | /// A model that tracks the downstream's 123 | /// accumulated demand state 124 | struct Demand { 125 | var processed: Subscribers.Demand = .none 126 | var requested: Subscribers.Demand = .none 127 | var sent: Subscribers.Demand = .none 128 | } 129 | } 130 | #endif 131 | -------------------------------------------------------------------------------- /Sources/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 2.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/BehaviorRelay+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BehaviorRelay+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 10/05/2020. 6 | // 7 | 8 | #if canImport(Combine) 9 | import Combine 10 | import RxSwift 11 | import RxRelay 12 | 13 | // MARK: - Behavior Relay as Combine Subject 14 | 15 | /// A bi-directional wrapper for a RxSwift Behavior Relay 16 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 17 | public final class RxCurrentValueRelay: Combine.Subject { 18 | private let rxRelay: BehaviorRelay 19 | private let subject: CurrentValueSubject 20 | private let subscription: AnyCancellable? 21 | 22 | public var value: Output { 23 | get { subject.value } 24 | set { rxRelay.accept(newValue) } 25 | } 26 | 27 | init(rxRelay: BehaviorRelay) { 28 | self.rxRelay = rxRelay 29 | self.subject = CurrentValueSubject(rxRelay.value) 30 | subscription = rxRelay.publisher.subscribe(subject) 31 | } 32 | 33 | public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { 34 | subject.receive(subscriber: subscriber) 35 | } 36 | 37 | public func send(_ value: Output) { 38 | rxRelay.accept(value) 39 | } 40 | 41 | public func send(completion: Subscribers.Completion) { 42 | // Relays can't complete or fail 43 | } 44 | 45 | public func send(subscription: Subscription) { 46 | subject.send(subscription: subscription) 47 | } 48 | 49 | deinit { subscription?.cancel() } 50 | 51 | public typealias Failure = Never 52 | } 53 | 54 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 55 | public extension BehaviorRelay { 56 | func toCombine() -> RxCurrentValueRelay { 57 | RxCurrentValueRelay(rxRelay: self) 58 | } 59 | } 60 | #endif 61 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/BehaviorSubject+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // BehaviorSubject+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | 13 | // MARK: - Behavior Subject as Combine Subject 14 | 15 | /// A bi-directional wrapper for a RxSwift Behavior Subject 16 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 17 | public final class RxCurrentValueSubject: Combine.Subject { 18 | private let rxSubject: BehaviorSubject 19 | private let subject: CurrentValueSubject 20 | private let subscription: AnyCancellable? 21 | 22 | public var value: Output { 23 | get { subject.value } 24 | set { rxSubject.onNext(newValue) } 25 | } 26 | 27 | init(rxSubject: BehaviorSubject) { 28 | self.rxSubject = rxSubject 29 | self.subject = CurrentValueSubject(try! rxSubject.value()) 30 | subscription = rxSubject.publisher.subscribe(subject) 31 | } 32 | 33 | public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { 34 | subject.receive(subscriber: subscriber) 35 | } 36 | 37 | public func send(_ value: Output) { 38 | rxSubject.onNext(value) 39 | } 40 | 41 | public func send(completion: Subscribers.Completion) { 42 | switch completion { 43 | case .finished: 44 | rxSubject.onCompleted() 45 | case .failure(let error): 46 | rxSubject.onError(error) 47 | } 48 | } 49 | 50 | public func send(subscription: Subscription) { 51 | subject.send(subscription: subscription) 52 | } 53 | 54 | deinit { subscription?.cancel() } 55 | 56 | public typealias Failure = Swift.Error 57 | } 58 | 59 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 60 | public extension BehaviorSubject { 61 | func toCombine() -> RxCurrentValueSubject { 62 | RxCurrentValueSubject(rxSubject: self) 63 | } 64 | } 65 | #endif 66 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/Observable+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Observable+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | 13 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 14 | public extension ObservableConvertibleType { 15 | /// An `AnyPublisher` of the underlying Observable's Element type 16 | /// so the Observable pushes events to the Publisher. 17 | var publisher: AnyPublisher { 18 | RxPublisher(upstream: self).eraseToAnyPublisher() 19 | } 20 | 21 | /// Returns a `AnyPublisher` of the underlying Observable's Element type 22 | /// so the Observable pushes events to the Publisher. 23 | /// 24 | /// - returns: AnyPublisher of the underlying Observable's Element type. 25 | /// - note: This is an alias for the `publisher` property. 26 | func asPublisher() -> AnyPublisher { 27 | publisher 28 | } 29 | } 30 | 31 | /// A Publisher pushing RxSwift events to a Downstream Combine subscriber. 32 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 33 | public class RxPublisher: Publisher { 34 | public typealias Output = Upstream.Element 35 | public typealias Failure = Swift.Error 36 | 37 | private let upstream: Upstream 38 | 39 | init(upstream: Upstream) { 40 | self.upstream = upstream 41 | } 42 | 43 | public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { 44 | subscriber.receive(subscription: RxSubscription(upstream: upstream, 45 | downstream: subscriber)) 46 | } 47 | } 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/PublishRelay+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PublishRelay+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 10/05/2020. 6 | // 7 | 8 | #if canImport(Combine) 9 | import Combine 10 | import RxSwift 11 | import RxRelay 12 | 13 | // MARK: - Publish Relay as Combine Subject 14 | 15 | /// A bi-directional wrapper for a RxSwift Publish Relay 16 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 17 | public final class RxPassthroughRelay: Combine.Subject { 18 | private let rxRelay: PublishRelay 19 | private let subject = PassthroughSubject() 20 | private let subscription: AnyCancellable? 21 | 22 | init(rxRelay: PublishRelay) { 23 | self.rxRelay = rxRelay 24 | subscription = rxRelay.publisher.subscribe(subject) 25 | } 26 | 27 | public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { 28 | subject.receive(subscriber: subscriber) 29 | } 30 | 31 | public func send(_ value: Output) { 32 | rxRelay.accept(value) 33 | } 34 | 35 | public func send(completion: Subscribers.Completion) { 36 | // Relays can't complete or fail 37 | } 38 | 39 | public func send(subscription: Subscription) { 40 | subject.send(subscription: subscription) 41 | } 42 | 43 | deinit { subscription?.cancel() } 44 | 45 | public typealias Failure = Never 46 | } 47 | 48 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 49 | public extension PublishRelay { 50 | func toCombine() -> RxPassthroughRelay { 51 | RxPassthroughRelay(rxRelay: self) 52 | } 53 | } 54 | #endif 55 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/PublishSubject+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PublishSubject+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 10/05/2020. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | 13 | // MARK: - Behavior Subject as Combine Subject 14 | 15 | /// A bi-directional wrapper for a RxSwift Publish Subject 16 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 17 | public final class RxPassthroughSubject: Combine.Subject { 18 | private let rxSubject: PublishSubject 19 | private let subject = PassthroughSubject() 20 | private let subscription: AnyCancellable? 21 | 22 | init(rxSubject: PublishSubject) { 23 | self.rxSubject = rxSubject 24 | subscription = rxSubject.publisher.subscribe(subject) 25 | } 26 | 27 | public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { 28 | subject.receive(subscriber: subscriber) 29 | } 30 | 31 | public func send(_ value: Output) { 32 | rxSubject.onNext(value) 33 | } 34 | 35 | public func send(completion: Subscribers.Completion) { 36 | switch completion { 37 | case .finished: 38 | rxSubject.onCompleted() 39 | case .failure(let error): 40 | rxSubject.onError(error) 41 | } 42 | } 43 | 44 | public func send(subscription: Subscription) { 45 | subject.send(subscription: subscription) 46 | } 47 | 48 | deinit { subscription?.cancel() } 49 | 50 | public typealias Failure = Swift.Error 51 | } 52 | 53 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 54 | public extension PublishSubject { 55 | func toCombine() -> RxPassthroughSubject { 56 | RxPassthroughSubject(rxSubject: self) 57 | } 58 | } 59 | #endif 60 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/Relays+Combine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Relays+Combine.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 11/06/2019. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | import RxRelay 13 | 14 | // MARK: - Behavior Relay as Publisher 15 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 16 | extension BehaviorRelay { 17 | /// An `AnyPublisher` of the underlying Relay's Element type 18 | /// so the relay pushes events to the Publisher. 19 | var publisher: AnyPublisher { 20 | RxPublisher(upstream: self).assertNoFailure().eraseToAnyPublisher() 21 | } 22 | 23 | /// An `AnyPublisher` of the underlying Relay's Element type 24 | /// so the relay pushes events to the Publisher. 25 | /// 26 | /// - returns: AnyPublisher of the underlying Relay's Element type. 27 | /// - note: This is an alias for the `publisher` property. 28 | func asPublisher() -> AnyPublisher { 29 | publisher 30 | } 31 | } 32 | 33 | // MARK: - Publish Relay as Publisher 34 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 35 | extension PublishRelay { 36 | /// An `AnyPublisher` of the underlying Relay's Element type 37 | /// so the relay pushes events to the Publisher. 38 | var publisher: AnyPublisher { 39 | RxPublisher(upstream: self).assertNoFailure().eraseToAnyPublisher() 40 | } 41 | 42 | /// An `AnyPublisher` of the underlying Relay's Element type 43 | /// so the relay pushes events to the Publisher. 44 | /// 45 | /// - returns: AnyPublisher of the underlying Relay's Element type. 46 | /// - note: This is an alias for the `publisher` property. 47 | func asPublisher() -> AnyPublisher { 48 | publisher 49 | } 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /Sources/Rx+Combine/RxSubscription.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RxSubscription.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 21/03/2020. 6 | // Copyright © 2020 Combine Community. All rights reserved. 7 | // 8 | 9 | #if canImport(Combine) 10 | import Combine 11 | import RxSwift 12 | 13 | // MARK: - Fallible 14 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 15 | class RxSubscription: Combine.Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Swift.Error { 16 | private var disposable: Disposable? 17 | private let buffer: DemandBuffer 18 | 19 | init(upstream: Upstream, 20 | downstream: Downstream) { 21 | buffer = DemandBuffer(subscriber: downstream) 22 | disposable = upstream.asObservable().subscribe(bufferRxEvents) 23 | } 24 | 25 | private func bufferRxEvents(_ event: RxSwift.Event) { 26 | switch event { 27 | case .next(let element): 28 | _ = buffer.buffer(value: element) 29 | case .error(let error): 30 | buffer.complete(completion: .failure(error)) 31 | case .completed: 32 | buffer.complete(completion: .finished) 33 | } 34 | } 35 | 36 | func request(_ demand: Subscribers.Demand) { 37 | _ = self.buffer.demand(demand) 38 | } 39 | 40 | func cancel() { 41 | disposable?.dispose() 42 | disposable = nil 43 | } 44 | } 45 | 46 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 47 | extension RxSubscription: CustomStringConvertible { 48 | var description: String { 49 | return "RxSubscription<\(Upstream.self)>" 50 | } 51 | } 52 | 53 | // MARK: - Infallible 54 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 55 | class RxInfallibleSubscription: Combine.Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Never { 56 | private var disposable: Disposable? 57 | private let buffer: DemandBuffer 58 | 59 | init(upstream: Upstream, 60 | downstream: Downstream) { 61 | buffer = DemandBuffer(subscriber: downstream) 62 | disposable = upstream.asObservable().subscribe(bufferRxEvents) 63 | } 64 | 65 | private func bufferRxEvents(_ event: RxSwift.Event) { 66 | switch event { 67 | case .next(let element): 68 | _ = buffer.buffer(value: element) 69 | case .error(let error): 70 | preconditionFailure("Your downstream cannot accept errors, as it has a `Never` failure (Got \(error))") 71 | case .completed: 72 | buffer.complete(completion: .finished) 73 | } 74 | } 75 | 76 | func request(_ demand: Subscribers.Demand) { 77 | _ = self.buffer.demand(demand) 78 | } 79 | 80 | func cancel() { 81 | disposable?.dispose() 82 | disposable = nil 83 | } 84 | } 85 | 86 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 87 | extension RxInfallibleSubscription: CustomStringConvertible { 88 | var description: String { 89 | return "RxInfallibleSubscription<\(Upstream.self)>" 90 | } 91 | } 92 | #endif 93 | -------------------------------------------------------------------------------- /Tests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | -------------------------------------------------------------------------------- /Tests/ObservableAsPublisherTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ObservableAsPublisherTests.swift 3 | // RxCombine 4 | // 5 | // Created by Shai Mishali on 21/03/2020. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if !os(watchOS) 10 | import XCTest 11 | import RxCombine 12 | import RxSwift 13 | import Combine 14 | 15 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 16 | class ObservableAsPublisherTests: XCTestCase { 17 | private var subscription: AnyCancellable! 18 | 19 | func testIntObservable() { 20 | let source = Observable.range(start: 1, count: 100) 21 | var values = [Int]() 22 | var completed = false 23 | 24 | subscription = source 25 | .asPublisher() 26 | .sink(receiveCompletion: { _ in completed = true }, 27 | receiveValue: { values.append($0) }) 28 | 29 | XCTAssertEqual(values, Array(1...100)) 30 | XCTAssertTrue(completed) 31 | } 32 | 33 | func testStringObservable() { 34 | let input = "Hello world I'm a RxSwift Observable".components(separatedBy: " ") 35 | let source = Observable.from(input) 36 | var values = [String]() 37 | var completed = false 38 | 39 | subscription = source 40 | .asPublisher() 41 | .sink(receiveCompletion: { _ in completed = true }, 42 | receiveValue: { values.append($0) }) 43 | 44 | XCTAssertEqual(values, input) 45 | XCTAssertTrue(completed) 46 | } 47 | 48 | func testFailingObservable() { 49 | let source = Observable.range(start: 1, count: 100) 50 | var values = [Int]() 51 | var completion: Subscribers.Completion? 52 | 53 | subscription = source 54 | .map { val in 55 | guard val < 15 else { throw FakeError.ohNo } 56 | return val 57 | } 58 | .asPublisher() 59 | .sink(receiveCompletion: { completion = $0 }, 60 | receiveValue: { values.append($0) }) 61 | 62 | XCTAssertEqual(values, Array(1...14)) 63 | XCTAssertNotNil(completion) 64 | guard case .failure(FakeError.ohNo) = completion else { 65 | XCTFail("Expected .failure(FakeError.ohNo), got \(String(describing: completion))") 66 | return 67 | } 68 | } 69 | 70 | func testDelayedEmissionsObservable() { 71 | let expect = expectation(description: "completion") 72 | var values = [Int]() 73 | var completed = false 74 | let source = Observable 75 | .from(1...10) 76 | .delay(.milliseconds(200), scheduler: MainScheduler.instance) 77 | .do(onCompleted: { expect.fulfill() }) 78 | 79 | subscription = source 80 | .asPublisher() 81 | .sink(receiveCompletion: { _ in completed = true }, 82 | receiveValue: { values.append($0) }) 83 | 84 | wait(for: [expect], timeout: 1.5) 85 | 86 | XCTAssertEqual(values, Array(1...10)) 87 | XCTAssertTrue(completed) 88 | } 89 | } 90 | 91 | enum FakeError: Swift.Error { 92 | case ohNo 93 | } 94 | #endif 95 | -------------------------------------------------------------------------------- /Tests/PublisherAsObservableTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PublisherAsObservableTests.swift 3 | // RxCombineTests 4 | // 5 | // Created by Shai Mishali on 21/03/2020. 6 | // 7 | 8 | #if !os(watchOS) 9 | import XCTest 10 | import RxCombine 11 | import RxSwift 12 | import Combine 13 | 14 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 15 | class PublisherAsObservableTests: XCTestCase { 16 | private var disposeBag = DisposeBag() 17 | 18 | override func setUp() { 19 | disposeBag = .init() 20 | } 21 | 22 | func testIntPublisher() { 23 | let source = (1...100).publisher 24 | var events = [RxSwift.Event]() 25 | 26 | source 27 | .asObservable() 28 | .subscribe { events.append($0) } 29 | .disposed(by: disposeBag) 30 | 31 | XCTAssertEqual(events, 32 | (1...100).map { .next($0) } + [.completed]) 33 | } 34 | 35 | func testStringPublisher() { 36 | let input = "Hello world I'm a RxSwift Observable".components(separatedBy: " ") 37 | let source = input.publisher 38 | var events = [RxSwift.Event]() 39 | 40 | source 41 | .asObservable() 42 | .subscribe { events.append($0) } 43 | .disposed(by: disposeBag) 44 | 45 | XCTAssertEqual(events, input.map { .next($0) } + [.completed]) 46 | } 47 | 48 | func testFailingPublisher() { 49 | let source = (1...100).publisher 50 | var events = [RxSwift.Event]() 51 | 52 | source 53 | .setFailureType(to: FakeError.self) 54 | .tryMap { val -> Int in 55 | guard val < 15 else { throw FakeError.ohNo } 56 | return val 57 | } 58 | .asObservable() 59 | .subscribe { events.append($0) } 60 | .disposed(by: disposeBag) 61 | 62 | 63 | XCTAssertEqual(events, (1...14).map { .next($0) } + [.error(FakeError.ohNo)]) 64 | } 65 | } 66 | 67 | 68 | extension RxSwift.Event: Equatable where Element: Equatable { 69 | public static func == (lhs: Event, rhs: Event) -> Bool { 70 | switch (lhs, rhs) { 71 | case let (.next(l), .next(r)): 72 | return l == r 73 | case (.error, .error), 74 | (.completed, .completed): 75 | return true 76 | default: 77 | return false 78 | } 79 | } 80 | } 81 | #endif 82 | -------------------------------------------------------------------------------- /Tests/RxRelaysToCombineTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RxRelaysToCombineTests.swift 3 | // RxCombineTests 4 | // 5 | // Created by Shai Mishali on 21/03/2020. 6 | // 7 | 8 | #if !os(watchOS) 9 | import XCTest 10 | import RxCombine 11 | import RxSwift 12 | import RxRelay 13 | import Combine 14 | 15 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 16 | class RxRelaysToCombineTests: XCTestCase { 17 | private var subscriptions = Set() 18 | private var disposeBag = DisposeBag() 19 | 20 | override func setUp() { 21 | subscriptions = .init() 22 | disposeBag = DisposeBag() 23 | } 24 | 25 | // MARK: - Behavior Subject 26 | func testBehaviorRelayInitialReplay() { 27 | var completed = false 28 | var values = [Int]() 29 | 30 | let relay = BehaviorRelay(value: 1).toCombine() 31 | 32 | relay 33 | .sink(receiveCompletion: { _ in completed = true }, 34 | receiveValue: { values.append($0) }) 35 | .store(in: &subscriptions) 36 | 37 | XCTAssertEqual(values, [1]) 38 | XCTAssertFalse(completed) 39 | } 40 | 41 | func testBehaviorRelaysIgnoresCompletion() { 42 | var completed = false 43 | var completed2 = false 44 | var values = [Int]() 45 | var values2 = [Int]() 46 | 47 | let relay = BehaviorRelay(value: 1) 48 | let comb = relay.toCombine() 49 | 50 | comb 51 | .sink(receiveCompletion: { _ in completed = true }, 52 | receiveValue: { values.append($0) }) 53 | .store(in: &subscriptions) 54 | 55 | comb.send(2) 56 | relay.accept(3) 57 | 58 | relay 59 | .subscribe(onNext: { values2.append($0) }, 60 | onCompleted: { completed2 = true }) 61 | .disposed(by: disposeBag) 62 | 63 | relay.accept(4) 64 | comb.send(5) 65 | relay.accept(6) 66 | 67 | XCTAssertEqual(values, [1, 2, 3, 4, 5, 6]) 68 | XCTAssertEqual(values2, [3, 4, 5, 6]) 69 | 70 | XCTAssertFalse(completed) 71 | XCTAssertFalse(completed2) 72 | 73 | /// These should still emit as relays don't complete 74 | comb.send(4) 75 | relay.accept(4) 76 | relay.accept(4) 77 | 78 | XCTAssertEqual(values, [1, 2, 3, 4, 5, 6, 4, 4, 4]) 79 | XCTAssertEqual(values2, [3, 4, 5, 6, 4, 4, 4]) 80 | } 81 | 82 | func testBehaviorRelayBind() { 83 | let combineSubject = CurrentValueSubject(-1) 84 | let source = BehaviorRelay(value: 0) 85 | var completed = false 86 | var values = [Int]() 87 | 88 | combineSubject 89 | .sink(receiveCompletion: { _ in completed = true }, 90 | receiveValue: { values.append($0) }) 91 | .store(in: &subscriptions) 92 | 93 | source 94 | .bind(to: combineSubject) 95 | .disposed(by: disposeBag) 96 | 97 | let comb = source.toCombine() 98 | 99 | comb.send(1) 100 | source.accept(2) 101 | source.accept(3) 102 | comb.send(4) 103 | 104 | XCTAssertFalse(completed) 105 | 106 | XCTAssertEqual(values, [-1, 0, 1, 2, 3, 4]) 107 | } 108 | 109 | // MARK: - Publish Relay 110 | func testPublishRelays() { 111 | var completed = false 112 | var completed2 = false 113 | var values = [Int]() 114 | var values2 = [Int]() 115 | 116 | let relay = PublishRelay() 117 | let comb = relay.toCombine() 118 | 119 | comb 120 | .sink(receiveCompletion: { _ in completed = true }, 121 | receiveValue: { values.append($0) }) 122 | .store(in: &subscriptions) 123 | 124 | relay.accept(1) 125 | relay.accept(2) 126 | comb.send(3) 127 | 128 | relay 129 | .subscribe(onNext: { values2.append($0) }, 130 | onCompleted: { completed2 = true }) 131 | .disposed(by: disposeBag) 132 | 133 | relay.accept(4) 134 | comb.send(5) 135 | relay.accept(6) 136 | 137 | XCTAssertEqual(values, [1, 2, 3, 4, 5, 6]) 138 | XCTAssertEqual(values2, [4, 5, 6]) 139 | 140 | XCTAssertFalse(completed) 141 | XCTAssertFalse(completed2) 142 | } 143 | 144 | func testPublishRelayBind() { 145 | let combineSubject = PassthroughSubject() 146 | let source = PublishRelay() 147 | var completed = false 148 | var values = [Int]() 149 | 150 | combineSubject 151 | .sink(receiveCompletion: { _ in completed = true }, 152 | receiveValue: { values.append($0) }) 153 | .store(in: &subscriptions) 154 | 155 | source 156 | .bind(to: combineSubject) 157 | .disposed(by: disposeBag) 158 | 159 | let comb = source.toCombine() 160 | 161 | source.accept(1) 162 | source.accept(2) 163 | comb.send(3) 164 | source.accept(4) 165 | 166 | XCTAssertFalse(completed) 167 | XCTAssertEqual(values, [1, 2, 3, 4]) 168 | } 169 | } 170 | #endif 171 | -------------------------------------------------------------------------------- /Tests/RxSubjectsToCombineTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RxBehaviorSubjectToCombineTests.swift 3 | // RxCombineTests 4 | // 5 | // Created by Shai Mishali on 21/03/2020. 6 | // Copyright © 2019 Combine Community. All rights reserved. 7 | // 8 | 9 | #if !os(watchOS) 10 | import XCTest 11 | import RxCombine 12 | import RxSwift 13 | import Combine 14 | 15 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 16 | class RxBehaviorSubjectToCombineTests: XCTestCase { 17 | private var subscriptions = Set() 18 | private var disposeBag = DisposeBag() 19 | 20 | override func setUp() { 21 | subscriptions = .init() 22 | disposeBag = DisposeBag() 23 | } 24 | 25 | func testBehaviorSubjectRxCombineInterop() { 26 | var rxValues = [Int]() 27 | var combValues = [Int]() 28 | 29 | var rxCompletion: RxSwift.Event? 30 | var combCompletion = false 31 | 32 | let rx = BehaviorSubject(value: 1) 33 | let combine = rx 34 | .toCombine() 35 | 36 | combine 37 | .map { $0 * $0 } 38 | .sink(receiveCompletion: { _ in combCompletion = true }, 39 | receiveValue: { combValues.append($0) }) 40 | .store(in: &subscriptions) 41 | 42 | rx 43 | .map { $0 * $0 } 44 | .subscribe(onNext: { rxValues.append($0) }, 45 | onError: { rxCompletion = .error($0) }, 46 | onCompleted: { rxCompletion = .completed }) 47 | .disposed(by: disposeBag) 48 | 49 | combine.send(10) 50 | combine.send(7) 51 | rx.onNext(2) 52 | rx.onNext(5) 53 | combine.send(11) 54 | combine.value = 60 55 | 56 | XCTAssertNil(rxCompletion) 57 | XCTAssertFalse(combCompletion) 58 | 59 | combine.send(completion: .finished) 60 | XCTAssertEqual(rxCompletion, .completed) 61 | XCTAssertTrue(combCompletion) 62 | 63 | /// These should do nothing since observable was 64 | /// already terminated by error 65 | rx.onNext(4) 66 | rx.onNext(4) 67 | rx.onNext(4) 68 | 69 | XCTAssertEqual(combValues, [1, 100, 49, 4, 25, 121, 3600]) 70 | XCTAssertEqual(combValues, rxValues) 71 | } 72 | 73 | // MARK: - Behavior Subject 74 | func testBehaviorSubjectInitialReplay() { 75 | var completion: Subscribers.Completion? 76 | var values = [Int]() 77 | 78 | let subject = BehaviorSubject(value: 1).toCombine() 79 | 80 | subject 81 | .sink(receiveCompletion: { completion = $0 }, 82 | receiveValue: { values.append($0) }) 83 | .store(in: &subscriptions) 84 | 85 | XCTAssertEqual(values, [1]) 86 | XCTAssertNil(completion) 87 | } 88 | 89 | func testBehaviorSubjectsCompleted() { 90 | var completion: Subscribers.Completion? 91 | var completion2: Subscribers.Completion? 92 | var values = [Int]() 93 | var values2 = [Int]() 94 | 95 | let rx = BehaviorSubject(value: 1) 96 | let comb = rx.toCombine() 97 | 98 | comb 99 | .sink(receiveCompletion: { completion = $0 }, 100 | receiveValue: { values.append($0) }) 101 | .store(in: &subscriptions) 102 | 103 | comb.send(2) 104 | rx.onNext(3) 105 | 106 | rx 107 | .subscribe(onNext: { values2.append($0) }, 108 | onCompleted: { completion2 = .finished }) 109 | .disposed(by: disposeBag) 110 | 111 | rx.onNext(4) 112 | rx.onNext(5) 113 | comb.send(6) 114 | 115 | XCTAssertEqual(values, [1, 2, 3, 4, 5, 6]) 116 | XCTAssertEqual(values2, [3, 4, 5, 6]) 117 | 118 | XCTAssertNil(completion) 119 | XCTAssertNil(completion2) 120 | 121 | rx.onCompleted() 122 | XCTAssertNotNil(completion) 123 | XCTAssertNotNil(completion2) 124 | } 125 | 126 | func testBehaviorSubjectsError() { 127 | var completion: Subscribers.Completion? 128 | var completion2: Subscribers.Completion? 129 | var values = [Int]() 130 | var values2 = [Int]() 131 | 132 | let rx = BehaviorSubject(value: 1) 133 | let comb = rx.toCombine() 134 | 135 | comb 136 | .sink(receiveCompletion: { completion = $0 }, 137 | receiveValue: { values.append($0) }) 138 | .store(in: &subscriptions) 139 | 140 | comb.send(2) 141 | rx.onNext(3) 142 | 143 | rx 144 | .subscribe(onNext: { values2.append($0) }, 145 | onError: { completion2 = .failure($0) }, 146 | onCompleted: { completion2 = .finished }) 147 | .disposed(by: disposeBag) 148 | 149 | rx.onNext(4) 150 | rx.onNext(5) 151 | comb.send(6) 152 | 153 | XCTAssertNil(completion) 154 | XCTAssertNil(completion2) 155 | 156 | comb.send(completion: .failure(FakeError.ohNo)) 157 | 158 | guard case .failure(FakeError.ohNo) = completion else { 159 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 160 | return 161 | } 162 | 163 | guard case .failure(FakeError.ohNo) = completion2 else { 164 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 165 | return 166 | } 167 | 168 | /// These should do nothing since observable was 169 | /// already terminated by error 170 | rx.onNext(4) 171 | rx.onNext(4) 172 | comb.send(4) 173 | 174 | XCTAssertEqual(values, [1, 2, 3, 4, 5, 6]) 175 | XCTAssertEqual(values2, [3, 4, 5, 6]) 176 | } 177 | 178 | func testBehaviorSubjectBind() { 179 | let combineSubject = CurrentValueSubject(-1) 180 | let source = BehaviorSubject(value: 0) 181 | var completion: Subscribers.Completion? 182 | var values = [Int]() 183 | 184 | combineSubject 185 | .sink(receiveCompletion: { completion = $0 }, 186 | receiveValue: { values.append($0) }) 187 | .store(in: &subscriptions) 188 | 189 | source 190 | .bind(to: combineSubject) 191 | .disposed(by: disposeBag) 192 | 193 | source.onNext(1) 194 | source.onNext(2) 195 | source.onNext(3) 196 | source.onNext(4) 197 | 198 | XCTAssertNil(completion) 199 | source.onError(FakeError.ohNo) 200 | 201 | XCTAssertEqual(values, [-1, 0, 1, 2, 3, 4]) 202 | guard case .failure(FakeError.ohNo) = completion else { 203 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 204 | return 205 | } 206 | } 207 | 208 | // MARK: - Publish Relay 209 | func testPublishSubjectRxCombineInterop() { 210 | var rxValues = [Int]() 211 | var combValues = [Int]() 212 | 213 | var rxCompletion: RxSwift.Event? 214 | var combCompletion = false 215 | 216 | let rx = PublishSubject() 217 | let combine = rx.toCombine() 218 | 219 | combine 220 | .map { $0 * $0 } 221 | .sink(receiveCompletion: { _ in combCompletion = true }, 222 | receiveValue: { combValues.append($0) }) 223 | .store(in: &subscriptions) 224 | 225 | rx 226 | .map { $0 * $0 } 227 | .subscribe(onNext: { rxValues.append($0) }, 228 | onError: { rxCompletion = .error($0) }, 229 | onCompleted: { rxCompletion = .completed }) 230 | .disposed(by: disposeBag) 231 | 232 | combine.send(10) 233 | combine.send(7) 234 | rx.onNext(2) 235 | rx.onNext(5) 236 | combine.send(11) 237 | 238 | XCTAssertNil(rxCompletion) 239 | XCTAssertFalse(combCompletion) 240 | 241 | combine.send(completion: .finished) 242 | XCTAssertEqual(rxCompletion, .completed) 243 | XCTAssertTrue(combCompletion) 244 | 245 | /// These should do nothing since observable was 246 | /// already terminated by error 247 | rx.onNext(4) 248 | rx.onNext(4) 249 | rx.onNext(4) 250 | 251 | XCTAssertEqual(combValues, [100, 49, 4, 25, 121]) 252 | XCTAssertEqual(combValues, rxValues) 253 | } 254 | 255 | func testPublishSubjectsCompleted() { 256 | var completion: Subscribers.Completion? 257 | var completion2: Subscribers.Completion? 258 | var values = [Int]() 259 | var values2 = [Int]() 260 | 261 | let rx = PublishSubject() 262 | let comb = rx.toCombine() 263 | 264 | comb 265 | .sink(receiveCompletion: { completion = $0 }, 266 | receiveValue: { values.append($0) }) 267 | .store(in: &subscriptions) 268 | 269 | comb.send(2) 270 | rx.onNext(3) 271 | 272 | rx 273 | .subscribe(onNext: { values2.append($0) }, 274 | onCompleted: { completion2 = .finished }) 275 | .disposed(by: disposeBag) 276 | 277 | rx.onNext(4) 278 | rx.onNext(5) 279 | comb.send(6) 280 | 281 | XCTAssertEqual(values, [2, 3, 4, 5, 6]) 282 | XCTAssertEqual(values2, [4, 5, 6]) 283 | 284 | XCTAssertNil(completion) 285 | XCTAssertNil(completion2) 286 | 287 | rx.onCompleted() 288 | XCTAssertNotNil(completion) 289 | XCTAssertNotNil(completion2) 290 | } 291 | 292 | func testPublishSubjectsError() { 293 | var completion: Subscribers.Completion? 294 | var completion2: Subscribers.Completion? 295 | var values = [Int]() 296 | var values2 = [Int]() 297 | 298 | let rx = PublishSubject() 299 | let comb = rx.toCombine() 300 | 301 | comb 302 | .sink(receiveCompletion: { completion = $0 }, 303 | receiveValue: { values.append($0) }) 304 | .store(in: &subscriptions) 305 | 306 | comb.send(2) 307 | rx.onNext(3) 308 | 309 | rx 310 | .subscribe(onNext: { values2.append($0) }, 311 | onError: { completion2 = .failure($0) }, 312 | onCompleted: { completion2 = .finished }) 313 | .disposed(by: disposeBag) 314 | 315 | rx.onNext(4) 316 | rx.onNext(5) 317 | comb.send(6) 318 | 319 | XCTAssertNil(completion) 320 | XCTAssertNil(completion2) 321 | 322 | comb.send(completion: .failure(FakeError.ohNo)) 323 | 324 | guard case .failure(FakeError.ohNo) = completion else { 325 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 326 | return 327 | } 328 | 329 | guard case .failure(FakeError.ohNo) = completion2 else { 330 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 331 | return 332 | } 333 | 334 | /// These should do nothing since observable was 335 | /// already terminated by error 336 | rx.onNext(4) 337 | rx.onNext(4) 338 | comb.send(4) 339 | 340 | XCTAssertEqual(values, [2, 3, 4, 5, 6]) 341 | XCTAssertEqual(values2, [4, 5, 6]) 342 | } 343 | 344 | func testPublishSubjectBind() { 345 | let combineSubject = PassthroughSubject() 346 | let source = PublishSubject() 347 | var completion: Subscribers.Completion? 348 | var values = [Int]() 349 | 350 | combineSubject 351 | .sink(receiveCompletion: { completion = $0 }, 352 | receiveValue: { values.append($0) }) 353 | .store(in: &subscriptions) 354 | 355 | source 356 | .bind(to: combineSubject) 357 | .disposed(by: disposeBag) 358 | 359 | source.onNext(1) 360 | source.onNext(2) 361 | source.onNext(3) 362 | source.onNext(4) 363 | 364 | XCTAssertNil(completion) 365 | source.onError(FakeError.ohNo) 366 | 367 | XCTAssertEqual(values, [1, 2, 3, 4]) 368 | guard case .failure(FakeError.ohNo) = completion else { 369 | XCTFail("Expected \(FakeError.ohNo), got \(String(describing: completion))") 370 | return 371 | } 372 | } 373 | 374 | func testPublishSubjectBindToCurrentValue() { 375 | let combineSubject = CurrentValueSubject(0) 376 | let source = PublishSubject() 377 | var completion: Subscribers.Completion? 378 | var values = [Int]() 379 | 380 | combineSubject 381 | .sink(receiveCompletion: { completion = $0 }, 382 | receiveValue: { values.append($0) }) 383 | .store(in: &subscriptions) 384 | 385 | source 386 | .bind(to: combineSubject) 387 | .disposed(by: disposeBag) 388 | 389 | source.onNext(1) 390 | source.onNext(2) 391 | source.onNext(3) 392 | source.onNext(4) 393 | 394 | XCTAssertNil(completion) 395 | source.onCompleted() 396 | 397 | XCTAssertEqual(values, [0, 1, 2, 3, 4]) 398 | guard case .finished = completion else { 399 | XCTFail("Expected .finished, got \(String(describing: completion))") 400 | return 401 | } 402 | } 403 | } 404 | #endif 405 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | ignore: 2 | - "Tests/**/*" 3 | 4 | coverage: 5 | status: 6 | project: 7 | default: 8 | target: auto 9 | threshold: 1% 10 | base: auto -------------------------------------------------------------------------------- /scripts/carthage-archive.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if ! which carthage > /dev/null; then 4 | echo 'Error: Carthage is not installed' >&2 5 | exit 1 6 | fi 7 | 8 | if [ ! -f Package.swift ]; then 9 | echo "Package.swift can't be found, please make sure you run scripts/carthage-archive.sh from the root folder" >&2 10 | exit 1 11 | fi 12 | 13 | if ! which swift > /dev/null; then 14 | echo 'Swift is not installed' >&2 15 | exit 1 16 | fi 17 | 18 | REQUIRED_SWIFT_TOOLING="5.1.0" 19 | TOOLS_VERSION=`swift package tools-version` 20 | XCODE_XCCONFIG_FILE=$(pwd)/Carthage.xcconfig 21 | 22 | if [ ! -f ${XCODE_XCCONFIG_FILE} ]; then 23 | echo 'Carthage.xcconfig does not exist' 24 | exit 1 25 | fi 26 | 27 | if [ ! "$(printf '%s\n' "$REQUIRED_SWIFT_TOOLING" "$TOOLS_VERSION" | sort -V | head -n1)" = "$REQUIRED_SWIFT_TOOLING" ]; then 28 | echo 'You must have Swift Package Manager 5.1.0 or later.' 29 | exit 1 30 | fi 31 | 32 | swift package generate-xcodeproj 33 | export XCODE_XCCONFIG_FILE 34 | carthage build --no-skip-current 35 | carthage archive 36 | unset XCODE_XCCONFIG_FILE 37 | 38 | echo "Upload RxCombine.framework.zip to the latest release" 39 | --------------------------------------------------------------------------------