├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── RELEASING.md ├── build.gradle.kts ├── gradle.properties ├── gradle ├── buildViaTravis.sh └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle.kts └── src ├── examples └── kotlin │ └── io │ └── reactivex │ └── rxjava3 │ └── kotlin │ └── examples │ ├── examples.kt │ └── retrofit │ └── retrofit.kt ├── main └── kotlin │ └── io │ └── reactivex │ └── rxjava3 │ └── kotlin │ ├── Flowables.kt │ ├── Maybes.kt │ ├── Observables.kt │ ├── Singles.kt │ ├── completable.kt │ ├── disposable.kt │ ├── flowable.kt │ ├── maybe.kt │ ├── observable.kt │ ├── single.kt │ └── subscribers.kt └── test └── kotlin └── io └── reactivex └── rxjava3 └── kotlin ├── BasicKotlinTests.kt ├── CompletableTest.kt ├── ExtensionTests.kt ├── FlowableTest.kt ├── KotlinTests.kt ├── MaybeTest.kt ├── ObservableTest.kt ├── ObservablesTest.kt ├── SingleTest.kt ├── SinglesTest.kt └── SubscriptionTests.kt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | # Packages # 11 | ############ 12 | # it's better to unpack these files and commit the raw source 13 | # git has its own built in compression methods 14 | *.7z 15 | *.dmg 16 | *.gz 17 | *.iso 18 | *.jar 19 | *.rar 20 | *.tar 21 | *.zip 22 | 23 | # Logs and databases # 24 | ###################### 25 | *.log 26 | 27 | # OS generated files # 28 | ###################### 29 | .DS_Store* 30 | ehthumbs.db 31 | Icon? 32 | Thumbs.db 33 | 34 | # Editor Files # 35 | ################ 36 | *~ 37 | *.swp 38 | 39 | # Gradle Files # 40 | ################ 41 | .gradle 42 | .gradletasknamecache 43 | .m2 44 | 45 | # Build output directies 46 | target/ 47 | build/ 48 | 49 | # IntelliJ specific files/directories 50 | out 51 | .idea 52 | *.ipr 53 | *.iws 54 | *.iml 55 | atlassian-ide-plugin.xml 56 | 57 | # Eclipse specific files/directories 58 | .classpath 59 | .project 60 | .settings 61 | .metadata 62 | bin/ 63 | 64 | # NetBeans specific files/directories 65 | .nbattrs 66 | /.nb-gradle/profiles/private/ 67 | .nb-gradle-properties 68 | 69 | # Scala build 70 | *.cache 71 | /.nb-gradle/private/ 72 | local.properties 73 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: 4 | - openjdk8 5 | 6 | sudo: false 7 | # as per http://blog.travis-ci.com/2014-12-17-faster-builds-with-container-based-infrastructure/ 8 | 9 | # script for build and release via Travis to Bintray 10 | script: gradle/buildViaTravis.sh 11 | 12 | # cache between builds 13 | cache: 14 | directories: 15 | - $HOME/.m2 16 | - $HOME/.gradle 17 | 18 | # secure environment variables for release to Bintray 19 | env: 20 | global: 21 | - secure: "MZHSWADYDupKbv8QTF1G93pgBEqNOnDcAguWhZwImXSMGk9YbfdL41riGvG7iapMsU1PfItoEbhOk+iquWasDDVLde+5ulZfYU1nxPODQYr8/e0Jwljv7voD+LeMaLlYD6UMr9q/DbiuSaWowLPbJ2R9CncO+KaHAASHLAQNuqs=" 22 | - secure: "fya8ma3RQexNggnrEQymbIiyT3ELohTNNwOycpxKsWvuMu8c1AgYwUYtfa+OD4SMyBkgHR+ora20s1v8Kx/uaa1ZxyzVjZliq06OSUqvzM0STRHQn3UvXuNhf8bpcrXTyA7aMJ7WxvjRvWpBeJnUSb6ILDZaT/NY8sdEyQ0+IRk=" 23 | - secure: "IGFb9GZLJ5Co9iEqiT/xkA8CuAIYDrggMTLNEgaGD+WwWv/fffK6/NXG0dKdqZTbW7FryclWolbJ7tQCbqKU+B/N+BgqG9zV49L2UdvYqhg7QT4aml/0HDaebzuDrlGUBCNb084yt50OOpf1kvi25LVKwwRgXbg3J0BvM7mgJtk=" 24 | - secure: "LirFHG2+tqF+l3GrH/jdAxs09ByhZeT/CqxMmszrQNuS9Qbh9qeNP3ZkMdWrlsZt0gCVGJAD5LU42kvAVbyyxmIAVkldDleNxiMZcb86LHW/cL7DwYaK+E34J70HuhN17hwhCit+34BauStVagBSU71M+fm7lz4qCZQ8LznEzsk=" 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to RxKotlin 2 | 3 | If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request (on a branch other than `master` or `gh-pages`). 4 | 5 | When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. 6 | 7 | ## License 8 | 9 | By contributing your code, you agree to license your contribution under the terms of the APLv2: https://github.com/ReactiveX/RxKotlin/blob/2.x/LICENSE 10 | 11 | All files are released with the Apache 2.0 license. 12 | 13 | If you are adding a new file it should have a header like this: 14 | 15 | ``` 16 | /** 17 | * Copyright 2014 Netflix, Inc. 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); 20 | * you may not use this file except in compliance with the License. 21 | * You may obtain a copy of the License at 22 | * 23 | * http://www.apache.org/licenses/LICENSE-2.0 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, 27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 28 | * See the License for the specific language governing permissions and 29 | * limitations under the License. 30 | */ 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2012 Netflix, Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxKotlin 2 | 3 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxkotlin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.reactivex.rxjava3/rxkotlin) 4 | 5 | ## Kotlin Extensions for RxJava 6 | 7 | RxKotlin is a lightweight library that adds convenient extension functions to [RxJava](https://github.com/ReactiveX/RxJava). You can use RxJava with Kotlin out-of-the-box, but Kotlin has language features (such as [extension functions](https://kotlinlang.org/docs/reference/extensions.html)) that can streamline usage of RxJava even more. RxKotlin aims to conservatively collect these conveniences in one centralized library, and standardize conventions for using RxJava with Kotlin. 8 | 9 | 10 | ```kotlin 11 | import io.reactivex.rxjava3.kotlin.subscribeBy 12 | import io.reactivex.rxjava3.kotlin.toObservable 13 | 14 | fun main() { 15 | 16 | val list = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 17 | 18 | list.toObservable() // extension function for Iterables 19 | .filter { it.length >= 5 } 20 | .subscribeBy( // named arguments for lambda Subscribers 21 | onNext = { println(it) }, 22 | onError = { it.printStackTrace() }, 23 | onComplete = { println("Done!") } 24 | ) 25 | 26 | } 27 | ``` 28 | 29 | ## Contributing 30 | 31 | :warning: (16/10/2023) We are currently not accepting contributions due to lack of developers capable of handling them in a reasonable manner. 32 | 33 | Since Kotlin makes it easy to implement extensions for anything and everything, this project has to be conservative in what features are in scope. Intentions to create syntactic sugar can quickly regress into [syntactic saccharin](https://en.wikipedia.org/wiki/Syntactic_sugar#Syntactic_saccharin), and such personal preferences belong in one's internal domain rather than an OSS library. 34 | 35 | Here are some basic guidelines to determine whether your contribution might be in scope for RxKotlin: 36 | 37 | * Is this intended feature already in RxJava? 38 | - If no, propose the operator in RxJava. 39 | - If yes, can Kotlin streamline the operator further? 40 | 41 | * Does this substantially reduce the amount of boilerplate code? 42 | * Does this make an existing operator easier to use? 43 | * Does RxJava not contain this feature due to Java language limitations, or because of a deliberate decision to not include it? 44 | 45 | ### Kotlin Slack Channel 46 | 47 | Join us on the #rx channel in Kotlin Slack! 48 | 49 | https://kotlinlang.slack.com/messages/rx 50 | 51 | 52 | ## Support for RxJava 3.x, RxJava 2.x and RxJava 1.x 53 | 54 | **Use RxKotlin 3.x versions to target RxJava 3.x.** 55 | - The 3.x version is active. 56 | 57 | Use RxKotlin 2.x versions to target RxJava 2.x. 58 | - The 2.x version of RxJava and RxKotlin is in maintenance mode and will be supported only through bugfixes. No new features or behavior changes will be accepted or applied. 59 | 60 | Use RxKotlin 1.x versions to target RxJava 1.x. 61 | - The 1.x version of RxJava and RxKotlin reached end-of-life. No further development, support, maintenance, PRs or updates will happen. 62 | 63 | The maintainers do not update the RxJava dependency version for every minor or patch RxJava release, so you should explicitly add the desired RxJava dependency version to your `pom.xml` or `build.gradle(.kts)`. 64 | 65 | ## Binaries 66 | 67 | Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Crxkotlin). 68 | 69 | ### RxKotlin 3.x [![Build Status](https://travis-ci.org/ReactiveX/RxKotlin.svg?branch=3.x)](https://travis-ci.org/ReactiveX/RxKotlin) 70 | 71 | Example for Maven: 72 | 73 | ```xml 74 | 75 | io.reactivex.rxjava3 76 | rxkotlin 77 | 3.x.y 78 | 79 | ``` 80 | 81 | Example for Gradle: 82 | 83 | ```kotlin 84 | implementation("io.reactivex.rxjava3:rxkotlin:3.x.y") 85 | ``` 86 | 87 | ### RxKotlin 2.x [![Build Status](https://travis-ci.org/ReactiveX/RxKotlin.svg?branch=2.x)](https://travis-ci.org/ReactiveX/RxKotlin) 88 | 89 | Example for Maven: 90 | 91 | ```xml 92 | 93 | io.reactivex.rxjava2 94 | rxkotlin 95 | 2.x.y 96 | 97 | ``` 98 | 99 | Example for Gradle: 100 | 101 | ```kotlin 102 | implementation("io.reactivex.rxjava2:rxkotlin:2.x.y") 103 | ``` 104 | 105 | ### RxKotlin 1.x 106 | 107 | Example for Maven: 108 | 109 | ```xml 110 | 111 | io.reactivex 112 | rxkotlin 113 | 1.x.y 114 | 115 | ``` 116 | 117 | Example for Gradle: 118 | 119 | ```kotlin 120 | implementation("io.reactivex:rxkotlin:1.x.y") 121 | ``` 122 | 123 | ### Building with JitPack 124 | 125 | You can also use Gradle or Maven with [JitPack](https://jitpack.io/) to build directly off a snapshot, branch, or commit of this repository. 126 | 127 | For example, to build off the 3.x branch, use this setup for Gradle: 128 | 129 | ```groovy 130 | repositories { 131 | maven { url 'https://jitpack.io' } 132 | } 133 | 134 | dependencies { 135 | implementation 'com.github.ReactiveX:RxKotlin:3.x-SNAPSHOT' 136 | } 137 | ``` 138 | 139 | Use this setup for Maven: 140 | 141 | ```xml 142 | 143 | 144 | jitpack.io 145 | https://jitpack.io 146 | 147 | 148 | 149 | 150 | com.github.ReactiveX 151 | RxKotlin 152 | 3.x-SNAPSHOT 153 | 154 | ``` 155 | 156 | Learn more about building this project with JitPack [here](https://jitpack.io/#ReactiveX/RxKotlin). 157 | 158 | 159 | 160 | ## Extensions 161 | 162 | |Target Type|Method|Return Type|Description| 163 | |---|---|---|---| 164 | |BooleanArray|toObservable()|Observable|Turns a Boolean array into an Observable| 165 | |ByteArray|toObservable()|Observable|Turns a Byte array into an Observable| 166 | |ShortArray|toObservable()|Observable|Turns a Short array into an Observable| 167 | |IntArray|toObservable()|Observable|Turns an Int array into an Observable| 168 | |LongArray|toObservable()|Observable|Turns a Long array into an Observable| 169 | |FloatArray|toObservable()|Observable|Turns a Float array into an Observable| 170 | |DoubleArray|toObservable()|Observable|Turns a Double array into an Observable| 171 | |Array|toObservable()|Observable|Turns a `T` array into an Observable| 172 | |IntProgression|toObservable()|Observable|Turns an `IntProgression` into an Observable| 173 | |Iterable|toObservable()|Observable|Turns an `Iterable` into an Observable| 174 | |Iterator|toObservable()|Observable|Turns an `Iterator` into an Observable| 175 | |Observable|flatMapSequence()|Observable|Flat maps each `T` emission to a `Sequence`| 176 | |Observable>|toMap()|Single>|Collects `Pair` emissions into a `Map`| 177 | |Observable>|toMultimap()|`Single>`|Collects `Pair` emissions into a `Map>`| 178 | |Observable>|mergeAll()|Observable|Merges all Observables emitted from an Observable| 179 | |Observable>|concatAll()|Observable|Concatenates all Observables emitted from an Observable| 180 | |Observable>|switchLatest()|Observable|Emits from the last emitted Observable| 181 | |Observable<*>|cast()|Observable|Casts all emissions to the reified type| 182 | |Observable<*>|ofType()|Observable|Filters all emissions to only the reified type| 183 | |Iterable>|merge()|Observable|Merges an Iterable of Observables into a single Observable| 184 | |Iterable>|mergeDelayError()|Observable|Merges an Iterable of Observables into a single Observable, but delays any error| 185 | |BooleanArray|toFlowable()|Flowable|Turns a Boolean array into an Flowable| 186 | |ByteArray|toFlowable()|Flowable|Turns a Byte array into an Flowable| 187 | |ShortArray|toFlowable()|Flowable|Turns a Short array into an Flowable| 188 | |IntArray|toFlowable()|Flowable|Turns an Int array into an Flowable| 189 | |LongArray|toFlowable()|Flowable|Turns a Long array into an Flowable| 190 | |FloatArray|toFlowable()|Flowable|Turns a Float array into an Flowable| 191 | |DoubleArray|toFlowable()|Flowable|Turns a Double array into an Flowable| 192 | |Array|toFlowable()|Flowable|Turns a `T` array into an Flowable| 193 | |IntProgression|toFlowable()|Flowable|Turns an `IntProgression` into an Flowable| 194 | |Iterable|toFlowable()|Flowable|Turns an `Iterable` into an Flowable| 195 | |Iterator|toFlowable()|Flowable|Turns an `Iterator` into an Flowable| 196 | |Flowable|flatMapSequence()|Flowable|Flat maps each `T` emission to a `Sequence`| 197 | |Flowable>|toMap()|Single>|Collects `Pair` emissions into a `Map`| 198 | |Flowable>|toMultimap()|`Single>>`|Collects `Pair` emissions into a `Map>`| 199 | |Flowable>|mergeAll()|Flowable|Merges all Flowables emitted from an Flowable| 200 | |Flowable>|concatAll()|Flowable|Concatenates all Flowables emitted from an Flowable| 201 | |Flowable>|switchLatest()|Flowable|Emits from the last emitted Flowable| 202 | |Flowable|cast()|Flowable|Casts all emissions to the reified type| 203 | |Flowable|ofType()|Flowable|Filters all emissions to only the reified type| 204 | |Iterable>|merge()|Flowable|Merges an Iterable of Flowables into a single Flowable| 205 | |Iterable>|mergeDelayError()|Flowable|Merges an Iterable of Flowables into a single Flowable, but delays any error| 206 | |Single|cast()|Single|Casts all emissions to the reified type| 207 | |Observable>|mergeAllSingles()|Observable|Merges all Singles emitted from an Observable| 208 | |Flowable>|mergeAllSingles()|Flowable|Merges all Singles emitted from a Flowable| 209 | |Maybe|cast()|Maybe|Casts any emissions to the reified type| 210 | |Maybe|ofType()|Maybe|Filters any emission that is the reified type| 211 | |Observable>|mergeAllMaybes()|Observable|Merges all emitted Maybes| 212 | |Flowable>|mergeAllMaybes()|Flowable|Merges all emitted Maybes| 213 | |Action|toCompletable()|Completable|Turns an `Action` into a `Completable`| 214 | |Callable|toCompletable()|Completable|Turns a `Callable` into a `Completable`| 215 | |Future|toCompletable()|Completable|Turns a `Future` into a `Completable`| 216 | |(() -> Any)|toCompletable()|Completable|Turns a `(() -> Any)` into a `Completable`| 217 | |Observable|mergeAllCompletables()|Completable>|Merges all emitted Completables| 218 | |Flowable|mergeAllCompletables()|Completable|Merges all emitted Completables| 219 | |Observable|subscribeBy()|Disposable|Allows named arguments to construct an Observer| 220 | |Flowable|subscribeBy()|Disposable|Allows named arguments to construct a Subscriber| 221 | |Single|subscribeBy()|Disposable|Allows named arguments to construct a SingleObserver| 222 | |Maybe|subscribeBy()|Disposable|Allows named arguments to construct a MaybeObserver| 223 | |Completable|subscribeBy()|Disposable|Allows named arguments to construct a CompletableObserver| 224 | |Observable|blockingSubscribeBy()|Unit|Allows named arguments to construct a blocking Observer| 225 | |Flowable|blockingSubscribeBy()|Unit|Allows named arguments to construct a blocking Subscriber| 226 | |Single|blockingSubscribeBy()|Unit|Allows named arguments to construct a blocking SingleObserver| 227 | |Maybe|blockingSubscribeBy()|Unit|Allows named arguments to construct a blocking MaybeObserver| 228 | |Completable|blockingSubscribeBy()|Unit|Allows named arguments to construct a blocking CompletableObserver| 229 | |Disposable|addTo()|Disposable|Adds a `Disposable` to the specified `CompositeDisposable`| 230 | |CompositeDisposable|plusAssign()|Disposable|Operator function to add a `Disposable` to this`CompositeDisposable`| 231 | 232 | 233 | 234 | ## SAM Helpers (made obsolete since Kotlin 1.4) 235 | 236 | _These methods have been made obsolete with new type inference algorithm in Kotlin 1.4. 237 | They will be removed in some future RxKotlin version._ 238 | 239 | To help cope with the [SAM ambiguity issue](https://youtrack.jetbrains.com/issue/KT-14984) when using RxJava with Kotlin, there are a number of helper factories and extension functions to workaround the affected operators. 240 | 241 | ``` 242 | Observables.zip() 243 | Observables.combineLatest() 244 | Observable#zipWith() 245 | Observable#withLatestFrom() 246 | Flowables.zip() 247 | Flowables.combineLatest() 248 | Flowable#zipWith() 249 | Flowable#withLatestFrom() 250 | Singles.zip() 251 | Single#zipWith() 252 | Maybes.zip() 253 | ``` 254 | 255 | ## Usage with Other Rx Libraries 256 | 257 | RxKotlin can be used in conjunction with other Rx and Kotlin libraries, such as [RxAndroid](https://github.com/ReactiveX/RxAndroid), [RxBinding](https://github.com/JakeWharton/RxBinding), and [TornadoFX](https://github.com/edvin/tornadofx)/[RxKotlinFX](https://github.com/thomasnield/RxKotlinFX). These libraries and RxKotlin are modular, and RxKotlin is merely a set of extension functions to RxJava that can be used with these other libraries. There should be no overlap or dependency issues. 258 | 259 | 260 | ## Other Resources 261 | 262 | ### _Learning RxJava_ Packt Book 263 | 264 | Chapter 12 of [_Learning RxJava_](https://www.packtpub.com/application-development/learning-rxjava) covers RxKotlin and Kotlin idioms with RxJava. 265 | 266 | [![](https://d255esdrn735hr.cloudfront.net/sites/default/files/imagecache/ppv4_main_book_cover/B06263_cover.png)](https://www.packtpub.com/application-development/learning-rxjava) 267 | 268 | ### _Reactive Programming in Kotlin_ Packt Book 269 | 270 | The book [_Reactive Programming in Kotlin_](https://www.packtpub.com/application-development/reactive-programming-kotlin) mainly focuses on RxKotlin, as well as learning reactive programming with Kotlin. 271 | 272 | [![](https://i.imgur.com/0GjGMn5.png)](https://www.packtpub.com/application-development/reactive-programming-kotlin) 273 | 274 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | Release Process 2 | =============== 3 | 4 | 1. Ensure `version` in `gradle.properties` is set to the version you want to release. 5 | 2. Add an entry in `CHANGELOG.md` with the changes for the release. 6 | 3. Update `README.md` with the version about to be released. Also update the RxJava version in 7 | this file to its latest. 8 | 4. Update the RxJava version in `rxkotlin/build.gradle.kts` to its latest. (We tell people that we 9 | won't be tracking RxJava releases, and we don't, but we do it anyway when we are releasing for 10 | those who ignore the advice.) 11 | 5. Commit: `git commit -am "Prepare version X.Y.X"` 12 | 6. Tag: `git tag -a X.Y.Z -m "Version X.Y.Z"` 13 | 7. Update `VERSION_NAME` in `gradle.properties` to the next development version. For example, if 14 | you just tagged version 1.0.4 you would set this value to 1.0.5. Do NOT append "-SNAPSHOT" to 15 | this value, it will be added automatically. 16 | 8. Commit: `git commit -am "Prepare next development version."` 17 | 9. Push: `git push --follow-tags` 18 | 10. Paste the `CHANGELOG.md` contents for this version into a Release on GitHub along with the 19 | Groovy for depending on the new version (https://github.com/ReactiveX/RxKotlin/releases). 20 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | @file:Suppress("UNUSED_VARIABLE", "HasPlatformType") 2 | 3 | import org.gradle.api.publish.maven.MavenPom 4 | import org.jetbrains.dokka.gradle.DokkaTask 5 | 6 | buildscript { 7 | repositories { 8 | jcenter() 9 | } 10 | } 11 | 12 | plugins { 13 | id("java-library") 14 | kotlin("jvm") version "1.4.10" 15 | id("org.jetbrains.dokka") version "0.9.18" 16 | id("maven-publish") 17 | id("com.jfrog.bintray") version "1.8.4" 18 | } 19 | 20 | repositories { 21 | jcenter() 22 | } 23 | 24 | group = "io.reactivex.rxjava3" 25 | 26 | //additional source sets 27 | sourceSets { 28 | val examples by creating { 29 | java { 30 | compileClasspath += sourceSets.main.get().output 31 | runtimeClasspath += sourceSets.main.get().output 32 | } 33 | } 34 | } 35 | 36 | //examples configuration 37 | val examplesImplementation by configurations.getting { 38 | extendsFrom(configurations.implementation.get()) 39 | } 40 | 41 | dependencies { 42 | api("io.reactivex.rxjava3:rxjava:3.0.6") 43 | implementation(kotlin("stdlib")) 44 | 45 | testImplementation("org.funktionale:funktionale-partials:1.0.0-final") 46 | testImplementation("junit:junit:4.12") 47 | testImplementation("org.mockito:mockito-core:1.10.19") 48 | 49 | examplesImplementation("com.squareup.retrofit2:retrofit:2.9.0") 50 | examplesImplementation("com.squareup.retrofit2:adapter-rxjava3:2.9.0") 51 | examplesImplementation("com.squareup.retrofit2:converter-moshi:2.9.0") 52 | } 53 | 54 | //sources 55 | val sourcesJar by tasks.creating(Jar::class) { 56 | from(sourceSets.main.get().allSource) 57 | archiveClassifier.set("sources") 58 | } 59 | 60 | //documentation 61 | val dokka by tasks.getting(DokkaTask::class) { 62 | outputFormat = "html" 63 | outputDirectory = "$buildDir/javadoc" 64 | 65 | } 66 | 67 | //documentation 68 | val dokkaJavadoc by tasks.creating(DokkaTask::class) { 69 | outputFormat = "javadoc" 70 | outputDirectory = "$buildDir/javadoc" 71 | } 72 | 73 | //documentation 74 | val javadocJar by tasks.creating(Jar::class) { 75 | dependsOn(dokkaJavadoc) 76 | archiveClassifier.set("javadoc") 77 | from("$buildDir/javadoc") 78 | } 79 | 80 | //publications 81 | val snapshot = "snapshot" 82 | val release = "release" 83 | 84 | publishing { 85 | 86 | fun MavenPom.initPom() { 87 | name.set("RxKotlin") 88 | description.set("RxJava bindings for Kotlin") 89 | url.set("https://github.com/ReactiveX/RxKotlin") 90 | 91 | licenses { 92 | license { 93 | name.set("The Apache License, Version 2.0") 94 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") 95 | } 96 | } 97 | scm { 98 | url.set("https://github.com/ReactiveX/RxKotlin.git") 99 | } 100 | developers { 101 | developer { 102 | id.set("thomasnield") 103 | name.set("Thomas Nield") 104 | email.set("thomasnield@live.com") 105 | organization.set("ReactiveX") 106 | organizationUrl.set("http://reactivex.io/") 107 | } 108 | developer { 109 | id.set("vpriscan") 110 | name.set("Vedran Prišćan") 111 | email.set("priscan.vedran@gmail.com") 112 | } 113 | } 114 | } 115 | 116 | publications { 117 | create(snapshot) { 118 | artifactId = "rxkotlin" 119 | version = "${project.version}-SNAPSHOT" 120 | 121 | from(components["java"]) 122 | 123 | pom.initPom() 124 | } 125 | create(release) { 126 | artifactId = "rxkotlin" 127 | version = "${project.version}" 128 | 129 | from(components["java"]) 130 | artifact(sourcesJar) 131 | artifact(javadocJar) 132 | 133 | pom.initPom() 134 | } 135 | } 136 | } 137 | 138 | bintray { 139 | user = project.findProperty("bintrayUser") as? String 140 | key = project.findProperty("bintrayKey") as? String 141 | 142 | val isRelease = project.findProperty("release") == "true" 143 | 144 | publish = isRelease 145 | override = false 146 | 147 | setPublications(if (isRelease) release else snapshot) 148 | 149 | // dryRun = true 150 | 151 | with(pkg) { 152 | userOrg = "reactivex" 153 | repo = "RxJava" 154 | name = "RxKotlin" 155 | setLicenses("Apache-2.0") 156 | setLabels("reactivex", "rxjava", "rxkotlin") 157 | websiteUrl = "https://github.com/ReactiveX/RxKotlin" 158 | issueTrackerUrl = "https://github.com/ReactiveX/RxKotlin/issues" 159 | vcsUrl = "https://github.com/ReactiveX/RxKotlin.git" 160 | 161 | with(version) { 162 | name = project.version.toString() 163 | vcsTag = project.version.toString() 164 | 165 | with(gpg){ 166 | sign = true 167 | } 168 | 169 | with(mavenCentralSync) { 170 | sync = true 171 | user = project.findProperty("sonatypeUsername") as? String 172 | password = project.findProperty("sonatypePassword") as? String 173 | close = "1" 174 | } 175 | } 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=3.0.1 2 | org.gradle.jvmargs=-Xms256m -Xmx1024m -XX:MaxPermSize=256m 3 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/buildViaTravis.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script will build the project. 3 | 4 | if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then 5 | echo -e "Build Pull Request #$TRAVIS_PULL_REQUEST => Branch [$TRAVIS_BRANCH]" 6 | ./gradlew build 7 | elif [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" == "" ]; then 8 | echo -e 'Build Branch with Snapshot => Branch ['$TRAVIS_BRANCH']' 9 | ./gradlew build 10 | echo -e 'To publish snapshot version to your local maven repo, execute: ./gradlew clean build publishSnapshotPublicationToMavenLocal' 11 | elif [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then 12 | echo -e 'Build Branch for Release => Branch ['$TRAVIS_BRANCH'] Tag ['$TRAVIS_TAG']' 13 | ./gradlew -Prelease.travisci=true -PbintrayUser="${bintrayUser}" -PbintrayKey="${bintrayKey}" -PsonatypeUsername="${sonatypeUsername}" -PsonatypePassword="${sonatypePassword}" \ 14 | -Prelease=true build bintrayUpload --stacktrace 15 | else 16 | echo -e 'WARN: Should not be here => Branch ['$TRAVIS_BRANCH'] Tag ['$TRAVIS_TAG'] Pull Request ['$TRAVIS_PULL_REQUEST']' 17 | ./gradlew build 18 | fi 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ReactiveX/RxKotlin/88176f6691de8de39d092f144671341bec30d4f8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin, switch paths to Windows format before running java 129 | if $cygwin ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem http://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name="rxkotlin" 2 | -------------------------------------------------------------------------------- /src/examples/kotlin/io/reactivex/rxjava3/kotlin/examples/examples.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin.examples 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import io.reactivex.rxjava3.disposables.CompositeDisposable 5 | import io.reactivex.rxjava3.kotlin.* 6 | import java.net.URL 7 | import java.util.* 8 | import java.util.concurrent.TimeUnit 9 | import kotlin.concurrent.thread 10 | 11 | fun main(args: Array) { 12 | 13 | val subscription = CompositeDisposable() 14 | 15 | val printArticle = { art: String -> 16 | println("--- Article ---\n${art.substring(0, 125)}") 17 | } 18 | 19 | @Suppress("ConvertLambdaToReference") 20 | val printIt = { it: String -> println(it) } 21 | 22 | subscription += asyncObservable().subscribe(printIt) 23 | 24 | subscription += syncObservable().subscribe(printIt) 25 | 26 | subscription.clear() 27 | 28 | simpleComposition() 29 | 30 | asyncWiki("Tiger", "Elephant").subscribe(printArticle) 31 | 32 | asyncWikiWithErrorHandling("Tiger", "Elephant").subscribe(printArticle) { e -> 33 | println("--- Error ---\n${e.message}") 34 | } 35 | 36 | combineLatest(listOfObservables()) 37 | 38 | zip(listOfObservables()) 39 | 40 | simpleObservable().subscribeBy( 41 | onNext = { s: String -> println("1st onNext => $s") } andThen { println("2nd onNext => $it") } 42 | ) 43 | 44 | addToCompositeSubscription() 45 | } 46 | 47 | private fun URL.toScannerObservable() = Observable.create { s -> 48 | this.openStream().use { stream -> 49 | Scanner(stream).useDelimiter("\\A") 50 | .toObservable() 51 | .subscribe { s.onNext(it) } 52 | } 53 | } 54 | 55 | fun syncObservable(): Observable = Observable.create { subscriber -> 56 | (0..75).toObservable() 57 | .map { "Sync value_$it" } 58 | .subscribe { subscriber.onNext(it) } 59 | } 60 | 61 | fun asyncObservable(): Observable = Observable.create { subscriber -> 62 | thread { 63 | (0..75).toObservable() 64 | .map { "Async value_$it" } 65 | .subscribe { subscriber.onNext(it) } 66 | } 67 | } 68 | 69 | fun asyncWiki(vararg articleNames: String): Observable = Observable.create { subscriber -> 70 | thread { 71 | articleNames.toObservable() 72 | .flatMapMaybe { name -> URL("http://en.wikipedia.org/wiki/$name").toScannerObservable().firstElement() } 73 | .subscribe { subscriber.onNext(it) } 74 | } 75 | } 76 | 77 | fun asyncWikiWithErrorHandling(vararg articleNames: String): Observable = Observable.create { subscriber -> 78 | thread { 79 | articleNames.toObservable() 80 | .flatMapMaybe { name -> URL("http://en.wikipedia.org/wiki/$name").toScannerObservable().firstElement() } 81 | .subscribe({ subscriber.onNext(it) }, { subscriber.onError(it) }) 82 | } 83 | } 84 | 85 | fun simpleComposition() { 86 | asyncObservable() 87 | .skip(10) 88 | .take(5) 89 | .map { "${it}_xform" } 90 | .subscribe { println("onNext => $it") } 91 | } 92 | 93 | fun listOfObservables(): List> = listOf(syncObservable(), syncObservable()) 94 | 95 | fun combineLatest(observables: List>) { 96 | observables.combineLatest { it.reduce { one, two -> one + two } }.subscribe(::println) 97 | } 98 | 99 | fun zip(observables: List>) { 100 | observables.zip { it.reduce { one, two -> one + two } }.subscribe(::println) 101 | } 102 | 103 | fun simpleObservable(): Observable = (0..17).toObservable().map { "Simple $it" } 104 | 105 | fun addToCompositeSubscription() { 106 | val compositeSubscription = CompositeDisposable() 107 | 108 | Observable.just("test") 109 | .delay(100, TimeUnit.MILLISECONDS) 110 | .subscribe() 111 | .addTo(compositeSubscription) 112 | 113 | compositeSubscription.dispose() 114 | } 115 | 116 | infix inline fun ((T) -> Unit).andThen(crossinline block: (T) -> Unit): (T) -> Unit = { this(it); block(it) } -------------------------------------------------------------------------------- /src/examples/kotlin/io/reactivex/rxjava3/kotlin/examples/retrofit/retrofit.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin.examples.retrofit 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import retrofit2.Retrofit 5 | import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory 6 | import retrofit2.converter.moshi.MoshiConverterFactory 7 | import retrofit2.http.GET 8 | import retrofit2.http.Query 9 | 10 | data class SearchResultEntry(val id : String, val latestVersion : String) 11 | data class SearchResults(val docs : List) 12 | data class MavenSearchResponse(val response : SearchResults) 13 | 14 | interface MavenSearchService { 15 | @GET("/solrsearch/select?wt=json") 16 | fun search(@Query("q") s : String, @Query("rows") rows : Int = 20) : Observable 17 | } 18 | 19 | fun main(args: Array) { 20 | val service = Retrofit.Builder(). 21 | baseUrl("http://search.maven.org"). 22 | addCallAdapterFactory(RxJava3CallAdapterFactory.create()). 23 | addConverterFactory(MoshiConverterFactory.create()). 24 | build(). 25 | create(MavenSearchService::class.java) 26 | 27 | service.search("rxkotlin"). 28 | flatMapIterable { it.response.docs }. 29 | subscribe { artifact -> 30 | println("${artifact.id} (${artifact.latestVersion})") 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/Flowables.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.BackpressureKind 6 | import io.reactivex.rxjava3.annotations.BackpressureSupport 7 | import io.reactivex.rxjava3.annotations.CheckReturnValue 8 | import io.reactivex.rxjava3.annotations.SchedulerSupport 9 | import io.reactivex.rxjava3.core.BackpressureStrategy 10 | import io.reactivex.rxjava3.core.Flowable 11 | import io.reactivex.rxjava3.core.FlowableEmitter 12 | import io.reactivex.rxjava3.functions.* 13 | import org.reactivestreams.Publisher 14 | 15 | 16 | object Flowables { 17 | 18 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 19 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, combineFunction)", "io.reactivex.Flowable"), 20 | level = DeprecationLevel.WARNING) 21 | @CheckReturnValue 22 | @BackpressureSupport(BackpressureKind.FULL) 23 | @SchedulerSupport(SchedulerSupport.NONE) 24 | inline fun combineLatest( 25 | source1: Flowable, 26 | source2: Flowable, 27 | crossinline combineFunction: (T1, T2) -> R 28 | ): Flowable = Flowable.combineLatest(source1, source2, 29 | BiFunction { t1, t2 -> combineFunction(t1, t2) }) 30 | 31 | /** 32 | * Emits `Pair` 33 | */ 34 | @CheckReturnValue 35 | @BackpressureSupport(BackpressureKind.FULL) 36 | @SchedulerSupport(SchedulerSupport.NONE) 37 | fun combineLatest( 38 | source1: Flowable, 39 | source2: Flowable 40 | ): Flowable> = Flowable.combineLatest(source1, source2, 41 | BiFunction> { t1, t2 -> t1 to t2 }) 42 | 43 | 44 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 45 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, combineFunction)", "io.reactivex.Flowable"), 46 | level = DeprecationLevel.WARNING) 47 | @CheckReturnValue 48 | @BackpressureSupport(BackpressureKind.FULL) 49 | @SchedulerSupport(SchedulerSupport.NONE) 50 | inline fun combineLatest( 51 | source1: Flowable, 52 | source2: Flowable, 53 | source3: Flowable, 54 | crossinline combineFunction: (T1, T2, T3) -> R 55 | ): Flowable = Flowable.combineLatest(source1, source2, source3, 56 | Function3 { t1: T1, t2: T2, t3: T3 -> combineFunction(t1, t2, t3) }) 57 | 58 | /** 59 | * Emits `Triple` 60 | */ 61 | @CheckReturnValue 62 | @BackpressureSupport(BackpressureKind.FULL) 63 | @SchedulerSupport(SchedulerSupport.NONE) 64 | fun combineLatest( 65 | source1: Flowable, 66 | source2: Flowable, 67 | source3: Flowable 68 | ): Flowable> = Flowable.combineLatest(source1, source2, source3, 69 | Function3> { t1, t2, t3 -> Triple(t1, t2, t3) }) 70 | 71 | 72 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 73 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, combineFunction)", "io.reactivex.Flowable"), 74 | level = DeprecationLevel.WARNING) 75 | @CheckReturnValue 76 | @BackpressureSupport(BackpressureKind.FULL) 77 | @SchedulerSupport(SchedulerSupport.NONE) 78 | inline fun combineLatest( 79 | source1: Flowable, 80 | source2: Flowable, 81 | source3: Flowable, 82 | source4: Flowable, 83 | crossinline combineFunction: (T1, T2, T3, T4) -> R 84 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, 85 | Function4 { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1, t2, t3, t4) }) 86 | 87 | 88 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 89 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, source5, combineFunction)", "io.reactivex.Flowable"), 90 | level = DeprecationLevel.WARNING) 91 | @CheckReturnValue 92 | @BackpressureSupport(BackpressureKind.FULL) 93 | @SchedulerSupport(SchedulerSupport.NONE) 94 | inline fun combineLatest( 95 | source1: Flowable, 96 | source2: Flowable, 97 | source3: Flowable, 98 | source4: Flowable, 99 | source5: Flowable, 100 | crossinline combineFunction: (T1, T2, T3, T4, T5) -> R 101 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, source5, 102 | Function5 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1, t2, t3, t4, t5) }) 103 | 104 | 105 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 106 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, source5, source6, combineFunction)", "io.reactivex.Flowable"), 107 | level = DeprecationLevel.WARNING) 108 | @CheckReturnValue 109 | @BackpressureSupport(BackpressureKind.FULL) 110 | @SchedulerSupport(SchedulerSupport.NONE) 111 | inline fun combineLatest( 112 | source1: Flowable, 113 | source2: Flowable, 114 | source3: Flowable, 115 | source4: Flowable, 116 | source5: Flowable, 117 | source6: Flowable, 118 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6) -> R 119 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, source5, source6, 120 | Function6 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6 -> combineFunction(t1, t2, t3, t4, t5, t6) }) 121 | 122 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 123 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, combineFunction)", "io.reactivex.Flowable"), 124 | level = DeprecationLevel.WARNING) 125 | @CheckReturnValue 126 | @BackpressureSupport(BackpressureKind.FULL) 127 | @SchedulerSupport(SchedulerSupport.NONE) 128 | inline fun combineLatest( 129 | source1: Flowable, 130 | source2: Flowable, 131 | source3: Flowable, 132 | source4: Flowable, 133 | source5: Flowable, 134 | source6: Flowable, 135 | source7: Flowable, 136 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7) -> R 137 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, 138 | Function7 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7 -> combineFunction(t1, t2, t3, t4, t5, t6, t7) }) 139 | 140 | 141 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 142 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, combineFunction)", "io.reactivex.Flowable"), 143 | level = DeprecationLevel.WARNING) 144 | @CheckReturnValue 145 | @BackpressureSupport(BackpressureKind.FULL) 146 | @SchedulerSupport(SchedulerSupport.NONE) 147 | inline fun combineLatest( 148 | source1: Flowable, 149 | source2: Flowable, 150 | source3: Flowable, 151 | source4: Flowable, 152 | source5: Flowable, 153 | source6: Flowable, 154 | source7: Flowable, 155 | source8: Flowable, 156 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 157 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, 158 | Function8 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8) }) 159 | 160 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 161 | replaceWith = ReplaceWith("Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, combineFunction)", "io.reactivex.Flowable"), 162 | level = DeprecationLevel.WARNING) 163 | @CheckReturnValue 164 | @BackpressureSupport(BackpressureKind.FULL) 165 | @SchedulerSupport(SchedulerSupport.NONE) 166 | inline fun combineLatest( 167 | source1: Flowable, 168 | source2: Flowable, 169 | source3: Flowable, 170 | source4: Flowable, 171 | source5: Flowable, 172 | source6: Flowable, 173 | source7: Flowable, 174 | source8: Flowable, 175 | source9: Flowable, 176 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 177 | ): Flowable = Flowable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, 178 | Function9 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 179 | 180 | 181 | @CheckReturnValue 182 | @BackpressureSupport(BackpressureKind.SPECIAL) 183 | @SchedulerSupport(SchedulerSupport.NONE) 184 | inline fun create( 185 | mode: BackpressureStrategy, 186 | crossinline source: (FlowableEmitter) -> Unit 187 | ): Flowable = Flowable.create({ source(it) }, mode) 188 | 189 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 190 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, combineFunction)", "io.reactivex.Flowable"), 191 | level = DeprecationLevel.WARNING) 192 | @CheckReturnValue 193 | @BackpressureSupport(BackpressureKind.FULL) 194 | @SchedulerSupport(SchedulerSupport.NONE) 195 | inline fun zip( 196 | source1: Flowable, 197 | source2: Flowable, 198 | crossinline combineFunction: (T1, T2) -> R 199 | ): Flowable = Flowable.zip(source1, source2, 200 | BiFunction { t1, t2 -> combineFunction(t1, t2) }) 201 | 202 | /** 203 | * Emits `Pair` 204 | */ 205 | @CheckReturnValue 206 | @BackpressureSupport(BackpressureKind.FULL) 207 | @SchedulerSupport(SchedulerSupport.NONE) 208 | fun zip(source1: Flowable, source2: Flowable): Flowable> = 209 | Flowable.zip(source1, source2, BiFunction> { t1, t2 -> t1 to t2 }) 210 | 211 | 212 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 213 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, combineFunction)", "io.reactivex.Flowable"), 214 | level = DeprecationLevel.WARNING) 215 | @CheckReturnValue 216 | @BackpressureSupport(BackpressureKind.FULL) 217 | @SchedulerSupport(SchedulerSupport.NONE) 218 | inline fun zip( 219 | source1: Flowable, 220 | source2: Flowable, 221 | source3: Flowable, 222 | crossinline combineFunction: (T1, T2, T3) -> R 223 | ): Flowable = Flowable.zip(source1, source2, source3, 224 | Function3 { t1: T1, t2: T2, t3: T3 -> combineFunction(t1, t2, t3) }) 225 | 226 | /** 227 | * Emits `Triple` 228 | */ 229 | @CheckReturnValue 230 | @BackpressureSupport(BackpressureKind.FULL) 231 | @SchedulerSupport(SchedulerSupport.NONE) 232 | fun zip( 233 | source1: Flowable, 234 | source2: Flowable, 235 | source3: Flowable 236 | ): Flowable> = Flowable.zip(source1, source2, source3, 237 | Function3> { t1, t2, t3 -> Triple(t1, t2, t3) }) 238 | 239 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 240 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, combineFunction)", "io.reactivex.Flowable"), 241 | level = DeprecationLevel.WARNING) 242 | @CheckReturnValue 243 | @BackpressureSupport(BackpressureKind.FULL) 244 | @SchedulerSupport(SchedulerSupport.NONE) 245 | inline fun zip( 246 | source1: Flowable, 247 | source2: Flowable, 248 | source3: Flowable, 249 | source4: Flowable, 250 | crossinline combineFunction: (T1, T2, T3, T4) -> R 251 | ): Flowable = Flowable.zip(source1, source2, source3, source4, 252 | Function4 { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1, t2, t3, t4) }) 253 | 254 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 255 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, source5, combineFunction)", "io.reactivex.Flowable"), 256 | level = DeprecationLevel.WARNING) 257 | @CheckReturnValue 258 | @BackpressureSupport(BackpressureKind.FULL) 259 | @SchedulerSupport(SchedulerSupport.NONE) 260 | inline fun zip( 261 | source1: Flowable, source2: Flowable, 262 | source3: Flowable, source4: Flowable, 263 | source5: Flowable, crossinline combineFunction: (T1, T2, T3, T4, T5) -> R 264 | ): Flowable = Flowable.zip(source1, source2, source3, source4, source5, 265 | Function5 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1, t2, t3, t4, t5) }) 266 | 267 | 268 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 269 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, source5, source6, combineFunction)", "io.reactivex.Flowable"), 270 | level = DeprecationLevel.WARNING) 271 | @CheckReturnValue 272 | @BackpressureSupport(BackpressureKind.FULL) 273 | @SchedulerSupport(SchedulerSupport.NONE) 274 | inline fun zip( 275 | source1: Flowable, source2: Flowable, 276 | source3: Flowable, source4: Flowable, 277 | source5: Flowable, source6: Flowable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6) -> R 278 | ): Flowable = Flowable.zip(source1, source2, source3, source4, source5, source6, 279 | Function6 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6 -> combineFunction(t1, t2, t3, t4, t5, t6) }) 280 | 281 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 282 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, source5, source6, source7, combineFunction)", "io.reactivex.Flowable"), 283 | level = DeprecationLevel.WARNING) 284 | @CheckReturnValue 285 | @BackpressureSupport(BackpressureKind.FULL) 286 | @SchedulerSupport(SchedulerSupport.NONE) 287 | inline fun zip( 288 | source1: Flowable, source2: Flowable, 289 | source3: Flowable, source4: Flowable, 290 | source5: Flowable, source6: Flowable, 291 | source7: Flowable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7) -> R 292 | ): Flowable = Flowable.zip(source1, source2, source3, source4, source5, source6, source7, 293 | Function7 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7 -> combineFunction(t1, t2, t3, t4, t5, t6, t7) }) 294 | 295 | 296 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 297 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, source5, source6, source7, source8, combineFunction)", "io.reactivex.Flowable"), 298 | level = DeprecationLevel.WARNING) 299 | @CheckReturnValue 300 | @BackpressureSupport(BackpressureKind.FULL) 301 | @SchedulerSupport(SchedulerSupport.NONE) 302 | inline fun zip( 303 | source1: Flowable, source2: Flowable, 304 | source3: Flowable, source4: Flowable, 305 | source5: Flowable, source6: Flowable, 306 | source7: Flowable, source8: Flowable, 307 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 308 | ): Flowable = Flowable.zip(source1, source2, source3, source4, source5, source6, source7, source8, 309 | Function8 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8) }) 310 | 311 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 312 | replaceWith = ReplaceWith("Flowable.zip(source1, source2, source3, source4, source5, source6, source7, source8, source9, combineFunction)", "io.reactivex.Flowable"), 313 | level = DeprecationLevel.WARNING) 314 | @CheckReturnValue 315 | @BackpressureSupport(BackpressureKind.FULL) 316 | @SchedulerSupport(SchedulerSupport.NONE) 317 | inline fun zip( 318 | source1: Flowable, 319 | source2: Flowable, 320 | source3: Flowable, 321 | source4: Flowable, 322 | source5: Flowable, 323 | source6: Flowable, 324 | source7: Flowable, 325 | source8: Flowable, 326 | source9: Flowable, 327 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 328 | ): Flowable = Flowable.zip(source1, source2, source3, source4, source5, source6, source7, source8, source9, 329 | Function9 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 330 | 331 | } 332 | 333 | /** 334 | * An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax. 335 | */ 336 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 337 | replaceWith = ReplaceWith("withLatestFrom(other, combiner)"), 338 | level = DeprecationLevel.WARNING) 339 | @CheckReturnValue 340 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 341 | @SchedulerSupport(SchedulerSupport.NONE) 342 | inline fun Flowable.withLatestFrom( 343 | other: Publisher, 344 | crossinline combiner: (T, U) -> R 345 | ): Flowable = withLatestFrom(other, BiFunction { t, u -> combiner.invoke(t, u) }) 346 | 347 | @CheckReturnValue 348 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 349 | @SchedulerSupport(SchedulerSupport.NONE) 350 | fun Flowable.withLatestFrom(other: Publisher): Flowable> = 351 | withLatestFrom(other, BiFunction { t, u -> Pair(t, u) }) 352 | 353 | 354 | /** 355 | * An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax. 356 | */ 357 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 358 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, combiner)"), 359 | level = DeprecationLevel.WARNING) 360 | @CheckReturnValue 361 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 362 | @SchedulerSupport(SchedulerSupport.NONE) 363 | inline fun Flowable.withLatestFrom( 364 | o1: Publisher, 365 | o2: Publisher, 366 | crossinline combiner: (T, T1, T2) -> R 367 | ): Flowable = withLatestFrom(o1, o2, Function3 { t, t1, t2 -> combiner.invoke(t, t1, t2) }) 368 | 369 | @CheckReturnValue 370 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 371 | @SchedulerSupport(SchedulerSupport.NONE) 372 | fun Flowable.withLatestFrom( 373 | o1: Publisher, 374 | o2: Publisher 375 | ): Flowable> = withLatestFrom(o1, o2, Function3 { t, t1, t2 -> Triple(t, t1, t2) }) 376 | 377 | /** 378 | * An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax. 379 | */ 380 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 381 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, o3, combiner)"), 382 | level = DeprecationLevel.WARNING) 383 | @CheckReturnValue 384 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 385 | @SchedulerSupport(SchedulerSupport.NONE) 386 | inline fun Flowable.withLatestFrom( 387 | o1: Publisher, 388 | o2: Publisher, 389 | o3: Publisher, 390 | crossinline combiner: (T, T1, T2, T3) -> R 391 | ): Flowable = withLatestFrom(o1, o2, o3, Function4 { t, t1, t2, t3 -> combiner.invoke(t, t1, t2, t3) }) 392 | 393 | /** 394 | * An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax. 395 | */ 396 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 397 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, o3, o4, combiner)"), 398 | level = DeprecationLevel.WARNING) 399 | @CheckReturnValue 400 | @BackpressureSupport(BackpressureKind.FULL) 401 | @SchedulerSupport(SchedulerSupport.NONE) 402 | inline fun Flowable.withLatestFrom( 403 | o1: Publisher, 404 | o2: Publisher, 405 | o3: Publisher, 406 | o4: Publisher, 407 | crossinline combiner: (T, T1, T2, T3, T4) -> R 408 | ): Flowable = withLatestFrom(o1, o2, o3, o4, Function5 { t, t1, t2, t3, t4 -> combiner.invoke(t, t1, t2, t3, t4) }) 409 | 410 | /** 411 | * An alias to [Flowable.zipWith], but allowing for cleaner lambda syntax. 412 | */ 413 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 414 | replaceWith = ReplaceWith("zipWith(other, zipper)"), 415 | level = DeprecationLevel.WARNING) 416 | @CheckReturnValue 417 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 418 | @SchedulerSupport(SchedulerSupport.NONE) 419 | inline fun Flowable.zipWith( 420 | other: Publisher, 421 | crossinline zipper: (T, U) -> R 422 | ): Flowable = zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) }) 423 | 424 | /** 425 | * Emits a zipped `Pair` 426 | */ 427 | @CheckReturnValue 428 | @BackpressureSupport(BackpressureKind.FULL) 429 | @SchedulerSupport(SchedulerSupport.NONE) 430 | fun Flowable.zipWith(other: Publisher): Flowable> = 431 | zipWith(other, BiFunction { t, u -> Pair(t, u) }) 432 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/Maybes.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.CheckReturnValue 6 | import io.reactivex.rxjava3.annotations.SchedulerSupport 7 | import io.reactivex.rxjava3.core.Maybe 8 | import io.reactivex.rxjava3.core.MaybeSource 9 | import io.reactivex.rxjava3.functions.* 10 | 11 | object Maybes { 12 | 13 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 14 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, zipper)", "io.reactivex.Maybe"), 15 | level = DeprecationLevel.WARNING) 16 | @CheckReturnValue 17 | @SchedulerSupport(SchedulerSupport.NONE) 18 | inline fun zip( 19 | s1: MaybeSource, 20 | s2: MaybeSource, 21 | crossinline zipper: (T, U) -> R 22 | ): Maybe = Maybe.zip(s1, s2, 23 | BiFunction { t, u -> zipper.invoke(t, u) }) 24 | 25 | @CheckReturnValue 26 | @SchedulerSupport(SchedulerSupport.NONE) 27 | fun zip( 28 | s1: MaybeSource, 29 | s2: MaybeSource 30 | ): Maybe> = Maybe.zip(s1, s2, 31 | BiFunction { t, u -> Pair(t, u) }) 32 | 33 | 34 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 35 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, zipper)", "io.reactivex.Maybe"), 36 | level = DeprecationLevel.WARNING) 37 | @CheckReturnValue 38 | @SchedulerSupport(SchedulerSupport.NONE) 39 | inline fun 40 | zip( 41 | s1: MaybeSource, s2: MaybeSource, s3: MaybeSource, 42 | crossinline zipper: (T1, T2, T3) -> R 43 | ): Maybe = Maybe.zip(s1, s2, s3, 44 | Function3 { t1, t2, t3 -> zipper.invoke(t1, t2, t3) }) 45 | 46 | @CheckReturnValue 47 | @SchedulerSupport(SchedulerSupport.NONE) 48 | fun 49 | zip( 50 | s1: MaybeSource, 51 | s2: MaybeSource, 52 | s3: MaybeSource 53 | ): Maybe> = Maybe.zip(s1, s2, s3, 54 | Function3 { t1, t2, t3 -> Triple(t1, t2, t3) }) 55 | 56 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 57 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, zipper)", "io.reactivex.Maybe"), 58 | level = DeprecationLevel.WARNING) 59 | @CheckReturnValue 60 | @SchedulerSupport(SchedulerSupport.NONE) 61 | inline fun 62 | zip( 63 | s1: MaybeSource, s2: MaybeSource, 64 | s3: MaybeSource, s4: MaybeSource, 65 | crossinline zipper: (T1, T2, T3, T4) -> R 66 | ): Maybe = Maybe.zip(s1, s2, s3, s4, 67 | Function4 { t1, t2, t3, t4 -> zipper.invoke(t1, t2, t3, t4) }) 68 | 69 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 70 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, s5, zipper)", "io.reactivex.Maybe"), 71 | level = DeprecationLevel.WARNING) 72 | @CheckReturnValue 73 | @SchedulerSupport(SchedulerSupport.NONE) 74 | inline fun 75 | zip( 76 | s1: MaybeSource, s2: MaybeSource, 77 | s3: MaybeSource, s4: MaybeSource, 78 | s5: MaybeSource, 79 | crossinline zipper: (T1, T2, T3, T4, T5) -> R 80 | ): Maybe = Maybe.zip(s1, s2, s3, s4, s5, 81 | Function5 { t1, t2, t3, t4, t5 -> zipper.invoke(t1, t2, t3, t4, t5) }) 82 | 83 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 84 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, s5, s6, zipper)", "io.reactivex.Maybe"), 85 | level = DeprecationLevel.WARNING) 86 | @CheckReturnValue 87 | @SchedulerSupport(SchedulerSupport.NONE) 88 | inline fun 89 | zip( 90 | s1: MaybeSource, s2: MaybeSource, 91 | s3: MaybeSource, s4: MaybeSource, 92 | s5: MaybeSource, s6: MaybeSource, 93 | crossinline zipper: (T1, T2, T3, T4, T5, T6) -> R 94 | ): Maybe = Maybe.zip(s1, s2, s3, s4, s5, s6, 95 | Function6 { t1, t2, t3, t4, t5, t6 -> zipper.invoke(t1, t2, t3, t4, t5, t6) }) 96 | 97 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 98 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, s5, s6, s7, zipper)", "io.reactivex.Maybe"), 99 | level = DeprecationLevel.WARNING) 100 | @CheckReturnValue 101 | @SchedulerSupport(SchedulerSupport.NONE) 102 | inline fun 103 | zip( 104 | s1: MaybeSource, s2: MaybeSource, 105 | s3: MaybeSource, s4: MaybeSource, 106 | s5: MaybeSource, s6: MaybeSource, 107 | s7: MaybeSource, 108 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7) -> R 109 | ): Maybe = Maybe.zip(s1, s2, s3, s4, s5, s6, s7, 110 | Function7 { t1, t2, t3, t4, t5, t6, t7 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7) }) 111 | 112 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 113 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, s5, s6, s7, s8, zipper)", "io.reactivex.Maybe"), 114 | level = DeprecationLevel.WARNING) 115 | @CheckReturnValue 116 | @SchedulerSupport(SchedulerSupport.NONE) 117 | inline fun 118 | zip( 119 | s1: MaybeSource, s2: MaybeSource, 120 | s3: MaybeSource, s4: MaybeSource, 121 | s5: MaybeSource, s6: MaybeSource, 122 | s7: MaybeSource, s8: MaybeSource, 123 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 124 | ): Maybe = Maybe.zip(s1, s2, s3, s4, s5, s6, s7, s8, 125 | Function8 { t1, t2, t3, t4, t5, t6, t7, t8 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7, t8) }) 126 | 127 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 128 | replaceWith = ReplaceWith("Maybe.zip(s1, s2, s3, s4, s5, s6, s7, s8, s9, zipper)", "io.reactivex.Maybe"), 129 | level = DeprecationLevel.WARNING) 130 | @CheckReturnValue 131 | @SchedulerSupport(SchedulerSupport.NONE) 132 | inline fun 133 | zip( 134 | s1: MaybeSource, s2: MaybeSource, 135 | s3: MaybeSource, s4: MaybeSource, 136 | s5: MaybeSource, s6: MaybeSource, 137 | s7: MaybeSource, s8: MaybeSource, 138 | s9: MaybeSource, 139 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 140 | ): Maybe = Maybe.zip(s1, s2, s3, s4, s5, s6, s7, s8, s9, 141 | Function9 { t1, t2, t3, t4, t5, t6, t7, t8, t9 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 142 | } 143 | 144 | /** 145 | * An alias to [Maybe.zipWith], but allowing for cleaner lambda syntax. 146 | */ 147 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 148 | replaceWith = ReplaceWith("zipWith(other, zipper)"), 149 | level = DeprecationLevel.WARNING) 150 | @CheckReturnValue 151 | @SchedulerSupport(SchedulerSupport.NONE) 152 | inline fun Maybe.zipWith( 153 | other: MaybeSource, 154 | crossinline zipper: (T, U) -> R 155 | ): Maybe = zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) }) 156 | 157 | @CheckReturnValue 158 | @SchedulerSupport(SchedulerSupport.NONE) 159 | fun Maybe.zipWith(other: MaybeSource): Maybe> = 160 | zipWith(other, BiFunction { t, u -> Pair(t, u) }) 161 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/Observables.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("unused", "HasPlatformType") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.CheckReturnValue 6 | import io.reactivex.rxjava3.annotations.SchedulerSupport 7 | import io.reactivex.rxjava3.core.Observable 8 | import io.reactivex.rxjava3.core.ObservableSource 9 | import io.reactivex.rxjava3.functions.* 10 | 11 | /** 12 | * SAM adapters to aid Kotlin lambda support 13 | */ 14 | object Observables { 15 | 16 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 17 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, combineFunction)", "io.reactivex.Observable"), 18 | level = DeprecationLevel.WARNING) 19 | @CheckReturnValue 20 | @SchedulerSupport(SchedulerSupport.NONE) 21 | inline fun combineLatest( 22 | source1: Observable, 23 | source2: Observable, 24 | crossinline combineFunction: (T1, T2) -> R 25 | ): Observable = Observable.combineLatest(source1, source2, 26 | BiFunction { t1, t2 -> combineFunction(t1, t2) }) 27 | 28 | /** 29 | * Emits `Pair` 30 | */ 31 | @CheckReturnValue 32 | @SchedulerSupport(SchedulerSupport.NONE) 33 | fun combineLatest(source1: Observable, source2: Observable): Observable> = 34 | Observable.combineLatest(source1, source2, 35 | BiFunction> { t1, t2 -> t1 to t2 }) 36 | 37 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 38 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, combineFunction)", "io.reactivex.Observable"), 39 | level = DeprecationLevel.WARNING) 40 | @CheckReturnValue 41 | @SchedulerSupport(SchedulerSupport.NONE) 42 | inline fun combineLatest( 43 | source1: Observable, 44 | source2: Observable, 45 | source3: Observable, 46 | crossinline combineFunction: (T1, T2, T3) -> R 47 | ): Observable = Observable.combineLatest(source1, source2, source3, 48 | Function3 { t1: T1, t2: T2, t3: T3 -> combineFunction(t1, t2, t3) }) 49 | 50 | /** 51 | * Emits `Triple` 52 | */ 53 | @CheckReturnValue 54 | @SchedulerSupport(SchedulerSupport.NONE) 55 | fun combineLatest( 56 | source1: Observable, 57 | source2: Observable, 58 | source3: Observable 59 | ): Observable> = Observable.combineLatest(source1, source2, source3, 60 | Function3> { t1, t2, t3 -> Triple(t1, t2, t3) }) 61 | 62 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 63 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, combineFunction)", "io.reactivex.Observable"), 64 | level = DeprecationLevel.WARNING) 65 | @CheckReturnValue 66 | @SchedulerSupport(SchedulerSupport.NONE) 67 | inline fun combineLatest( 68 | source1: Observable, source2: Observable, source3: Observable, 69 | source4: Observable, crossinline combineFunction: (T1, T2, T3, T4) -> R 70 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, 71 | Function4 { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1, t2, t3, t4) }) 72 | 73 | 74 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 75 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, source5, combineFunction)", "io.reactivex.Observable"), 76 | level = DeprecationLevel.WARNING) 77 | @CheckReturnValue 78 | @SchedulerSupport(SchedulerSupport.NONE) 79 | inline fun combineLatest( 80 | source1: Observable, source2: Observable, 81 | source3: Observable, source4: Observable, 82 | source5: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5) -> R 83 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, source5, 84 | Function5 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1, t2, t3, t4, t5) }) 85 | 86 | 87 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 88 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, source5, source6, combineFunction)", "io.reactivex.Observable"), 89 | level = DeprecationLevel.WARNING) 90 | @CheckReturnValue 91 | @SchedulerSupport(SchedulerSupport.NONE) 92 | inline fun combineLatest( 93 | source1: Observable, source2: Observable, 94 | source3: Observable, source4: Observable, 95 | source5: Observable, source6: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6) -> R 96 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, source5, source6, 97 | Function6 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6 -> combineFunction(t1, t2, t3, t4, t5, t6) }) 98 | 99 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 100 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, combineFunction)", "io.reactivex.Observable"), 101 | level = DeprecationLevel.WARNING) 102 | @CheckReturnValue 103 | @SchedulerSupport(SchedulerSupport.NONE) 104 | inline fun combineLatest( 105 | source1: Observable, source2: Observable, 106 | source3: Observable, source4: Observable, 107 | source5: Observable, source6: Observable, 108 | source7: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7) -> R 109 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, 110 | Function7 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7 -> combineFunction(t1, t2, t3, t4, t5, t6, t7) }) 111 | 112 | 113 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 114 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, combineFunction)", "io.reactivex.Observable"), 115 | level = DeprecationLevel.WARNING) 116 | @CheckReturnValue 117 | @SchedulerSupport(SchedulerSupport.NONE) 118 | inline fun combineLatest( 119 | source1: Observable, source2: Observable, 120 | source3: Observable, source4: Observable, 121 | source5: Observable, source6: Observable, 122 | source7: Observable, source8: Observable, 123 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 124 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, 125 | Function8 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8) }) 126 | 127 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 128 | replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, combineFunction)", "io.reactivex.Observable"), 129 | level = DeprecationLevel.WARNING) 130 | @CheckReturnValue 131 | @SchedulerSupport(SchedulerSupport.NONE) 132 | inline fun combineLatest( 133 | source1: Observable, source2: Observable, 134 | source3: Observable, source4: Observable, 135 | source5: Observable, source6: Observable, 136 | source7: Observable, source8: Observable, 137 | source9: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 138 | ): Observable = Observable.combineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, 139 | Function9 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 140 | 141 | 142 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 143 | replaceWith = ReplaceWith("Observable.zip(source1, source2, combineFunction)", "io.reactivex.Observable"), 144 | level = DeprecationLevel.WARNING) 145 | @CheckReturnValue 146 | @SchedulerSupport(SchedulerSupport.NONE) 147 | inline fun zip( 148 | source1: Observable, 149 | source2: Observable, 150 | crossinline combineFunction: (T1, T2) -> R 151 | ): Observable = Observable.zip(source1, source2, 152 | BiFunction { t1, t2 -> combineFunction(t1, t2) }) 153 | 154 | 155 | /** 156 | * Emits `Pair` 157 | */ 158 | @CheckReturnValue 159 | @SchedulerSupport(SchedulerSupport.NONE) 160 | fun zip(source1: Observable, source2: Observable): Observable> = 161 | Observable.zip(source1, source2, 162 | BiFunction> { t1, t2 -> t1 to t2 }) 163 | 164 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 165 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, combineFunction)", "io.reactivex.Observable"), 166 | level = DeprecationLevel.WARNING) 167 | @CheckReturnValue 168 | @SchedulerSupport(SchedulerSupport.NONE) 169 | inline fun zip( 170 | source1: Observable, 171 | source2: Observable, 172 | source3: Observable, 173 | crossinline combineFunction: (T1, T2, T3) -> R 174 | ): Observable = Observable.zip(source1, source2, source3, 175 | Function3 { t1: T1, t2: T2, t3: T3 -> combineFunction(t1, t2, t3) }) 176 | 177 | /** 178 | * Emits `Triple` 179 | */ 180 | @CheckReturnValue 181 | @SchedulerSupport(SchedulerSupport.NONE) 182 | fun zip( 183 | source1: Observable, 184 | source2: Observable, 185 | source3: Observable 186 | ): Observable> = Observable.zip(source1, source2, source3, 187 | Function3> { t1, t2, t3 -> Triple(t1, t2, t3) }) 188 | 189 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 190 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, combineFunction)", "io.reactivex.Observable"), 191 | level = DeprecationLevel.WARNING) 192 | @CheckReturnValue 193 | @SchedulerSupport(SchedulerSupport.NONE) 194 | inline fun zip( 195 | source1: Observable, 196 | source2: Observable, 197 | source3: Observable, 198 | source4: Observable, 199 | crossinline combineFunction: (T1, T2, T3, T4) -> R 200 | ): Observable = Observable.zip(source1, source2, source3, source4, 201 | Function4 { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1, t2, t3, t4) }) 202 | 203 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 204 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, source5, combineFunction)", "io.reactivex.Observable"), 205 | level = DeprecationLevel.WARNING) 206 | @CheckReturnValue 207 | @SchedulerSupport(SchedulerSupport.NONE) 208 | inline fun zip( 209 | source1: Observable, source2: Observable, 210 | source3: Observable, source4: Observable, 211 | source5: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5) -> R 212 | ): Observable = Observable.zip(source1, source2, source3, source4, source5, 213 | Function5 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1, t2, t3, t4, t5) }) 214 | 215 | 216 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 217 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, source5, source6, combineFunction)", "io.reactivex.Observable"), 218 | level = DeprecationLevel.WARNING) 219 | @CheckReturnValue 220 | @SchedulerSupport(SchedulerSupport.NONE) 221 | inline fun zip( 222 | source1: Observable, source2: Observable, 223 | source3: Observable, source4: Observable, 224 | source5: Observable, source6: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6) -> R 225 | ): Observable = Observable.zip(source1, source2, source3, source4, source5, source6, 226 | Function6 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6 -> combineFunction(t1, t2, t3, t4, t5, t6) }) 227 | 228 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 229 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, source5, source6, source7, combineFunction)", "io.reactivex.Observable"), 230 | level = DeprecationLevel.WARNING) 231 | @CheckReturnValue 232 | @SchedulerSupport(SchedulerSupport.NONE) 233 | inline fun zip( 234 | source1: Observable, source2: Observable, 235 | source3: Observable, source4: Observable, 236 | source5: Observable, source6: Observable, 237 | source7: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7) -> R 238 | ): Observable = Observable.zip(source1, source2, source3, source4, source5, source6, source7, 239 | Function7 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7 -> combineFunction(t1, t2, t3, t4, t5, t6, t7) }) 240 | 241 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 242 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, source5, source6, source7, source8, combineFunction)", "io.reactivex.Observable"), 243 | level = DeprecationLevel.WARNING) 244 | @CheckReturnValue 245 | @SchedulerSupport(SchedulerSupport.NONE) 246 | inline fun zip( 247 | source1: Observable, source2: Observable, 248 | source3: Observable, source4: Observable, 249 | source5: Observable, source6: Observable, 250 | source7: Observable, source8: Observable, 251 | crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 252 | ): Observable = Observable.zip(source1, source2, source3, source4, source5, source6, source7, source8, 253 | Function8 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8) }) 254 | 255 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 256 | replaceWith = ReplaceWith("Observable.zip(source1, source2, source3, source4, source5, source6, source7, source8, source9, combineFunction)", "io.reactivex.Observable"), 257 | level = DeprecationLevel.WARNING) 258 | @CheckReturnValue 259 | @SchedulerSupport(SchedulerSupport.NONE) 260 | inline fun zip( 261 | source1: Observable, source2: Observable, 262 | source3: Observable, source4: Observable, 263 | source5: Observable, source6: Observable, 264 | source7: Observable, source8: Observable, 265 | source9: Observable, crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 266 | ): Observable = Observable.zip(source1, source2, source3, source4, source5, source6, source7, source8, source9, 267 | Function9 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9 -> combineFunction(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 268 | 269 | } 270 | 271 | 272 | /** 273 | * An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax. 274 | */ 275 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 276 | replaceWith = ReplaceWith("withLatestFrom(other, combiner)"), 277 | level = DeprecationLevel.WARNING) 278 | @CheckReturnValue 279 | @SchedulerSupport(SchedulerSupport.NONE) 280 | inline fun Observable.withLatestFrom( 281 | other: ObservableSource, 282 | crossinline combiner: (T, U) -> R 283 | ): Observable = withLatestFrom(other, BiFunction { t, u -> combiner.invoke(t, u) }) 284 | 285 | /** 286 | * Emits a `Pair` 287 | */ 288 | @CheckReturnValue 289 | @SchedulerSupport(SchedulerSupport.NONE) 290 | fun Observable.withLatestFrom(other: ObservableSource): Observable> = 291 | withLatestFrom(other, BiFunction { t, u -> Pair(t, u) }) 292 | 293 | /** 294 | * An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax. 295 | */ 296 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 297 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, combiner)"), 298 | level = DeprecationLevel.WARNING) 299 | @CheckReturnValue 300 | @SchedulerSupport(SchedulerSupport.NONE) 301 | inline fun Observable.withLatestFrom( 302 | o1: ObservableSource, 303 | o2: ObservableSource, 304 | crossinline combiner: (T, T1, T2) -> R 305 | ): Observable = withLatestFrom(o1, o2, Function3 { t, t1, t2 -> combiner.invoke(t, t1, t2) }) 306 | 307 | @CheckReturnValue 308 | @SchedulerSupport(SchedulerSupport.NONE) 309 | fun Observable.withLatestFrom( 310 | o1: ObservableSource, 311 | o2: ObservableSource 312 | ): Observable> = withLatestFrom(o1, o2, Function3 { t, t1, t2 -> Triple(t, t1, t2) }) 313 | 314 | /** 315 | * An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax. 316 | */ 317 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 318 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, o3, combiner)"), 319 | level = DeprecationLevel.WARNING) 320 | @CheckReturnValue 321 | @SchedulerSupport(SchedulerSupport.NONE) 322 | inline fun Observable.withLatestFrom( 323 | o1: ObservableSource, 324 | o2: ObservableSource, 325 | o3: ObservableSource, 326 | crossinline combiner: (T, T1, T2, T3) -> R 327 | ): Observable = withLatestFrom(o1, o2, o3, Function4 { t, t1, t2, t3 -> combiner.invoke(t, t1, t2, t3) }) 328 | 329 | /** 330 | * An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax. 331 | */ 332 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 333 | replaceWith = ReplaceWith("withLatestFrom(o1, o2, o3, o4, combiner)"), 334 | level = DeprecationLevel.WARNING) 335 | @CheckReturnValue 336 | @SchedulerSupport(SchedulerSupport.NONE) 337 | inline fun Observable.withLatestFrom( 338 | o1: ObservableSource, 339 | o2: ObservableSource, 340 | o3: ObservableSource, 341 | o4: ObservableSource, 342 | crossinline combiner: (T, T1, T2, T3, T4) -> R 343 | ): Observable = withLatestFrom(o1, o2, o3, o4, 344 | Function5 { t, t1, t2, t3, t4 -> combiner.invoke(t, t1, t2, t3, t4) }) 345 | 346 | /** 347 | * An alias to [Observable.zipWith], but allowing for cleaner lambda syntax. 348 | */ 349 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 350 | replaceWith = ReplaceWith("zipWith(other, zipper)"), 351 | level = DeprecationLevel.WARNING) 352 | @CheckReturnValue 353 | @SchedulerSupport(SchedulerSupport.NONE) 354 | inline fun Observable.zipWith( 355 | other: ObservableSource, 356 | crossinline zipper: (T, U) -> R 357 | ): Observable = zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) }) 358 | 359 | /** 360 | * Emits a zipped `Pair` 361 | */ 362 | @CheckReturnValue 363 | @SchedulerSupport(SchedulerSupport.NONE) 364 | fun Observable.zipWith(other: ObservableSource): Observable> = 365 | zipWith(other, BiFunction { t, u -> Pair(t, u) }) 366 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/Singles.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.annotations.CheckReturnValue 4 | import io.reactivex.rxjava3.annotations.SchedulerSupport 5 | import io.reactivex.rxjava3.core.Single 6 | import io.reactivex.rxjava3.core.SingleSource 7 | import io.reactivex.rxjava3.functions.* 8 | 9 | 10 | object Singles { 11 | 12 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 13 | replaceWith = ReplaceWith("Single.zip(s1, s2, zipper)", "io.reactivex.Single"), 14 | level = DeprecationLevel.WARNING) 15 | @CheckReturnValue 16 | @SchedulerSupport(SchedulerSupport.NONE) 17 | inline fun zip( 18 | s1: SingleSource, 19 | s2: SingleSource, 20 | crossinline zipper: (T, U) -> R 21 | ): Single = Single.zip(s1, s2, BiFunction { t, u -> zipper.invoke(t, u) }) 22 | 23 | @CheckReturnValue 24 | @SchedulerSupport(SchedulerSupport.NONE) 25 | fun zip( 26 | s1: SingleSource, 27 | s2: SingleSource 28 | ): Single> = Single.zip(s1, s2, BiFunction { t, u -> Pair(t, u) }) 29 | 30 | 31 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 32 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, zipper)", "io.reactivex.Single"), 33 | level = DeprecationLevel.WARNING) 34 | @CheckReturnValue 35 | @SchedulerSupport(SchedulerSupport.NONE) 36 | inline fun 37 | zip( 38 | s1: SingleSource, s2: SingleSource, s3: SingleSource, 39 | crossinline zipper: (T1, T2, T3) -> R 40 | ): Single = Single.zip(s1, s2, s3, Function3 { t1, t2, t3 -> zipper.invoke(t1, t2, t3) }) 41 | 42 | @CheckReturnValue 43 | @SchedulerSupport(SchedulerSupport.NONE) 44 | fun 45 | zip( 46 | s1: SingleSource, 47 | s2: SingleSource, 48 | s3: SingleSource 49 | ): Single> = Single.zip(s1, s2, s3, Function3 { t1, t2, t3 -> Triple(t1, t2, t3) }) 50 | 51 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 52 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, zipper)", "io.reactivex.Single"), 53 | level = DeprecationLevel.WARNING) 54 | @CheckReturnValue 55 | @SchedulerSupport(SchedulerSupport.NONE) 56 | inline fun 57 | zip( 58 | s1: SingleSource, s2: SingleSource, 59 | s3: SingleSource, s4: SingleSource, 60 | crossinline zipper: (T1, T2, T3, T4) -> R 61 | ): Single = Single.zip(s1, s2, s3, s4, 62 | Function4 { t1, t2, t3, t4 -> zipper.invoke(t1, t2, t3, t4) }) 63 | 64 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 65 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, s5, zipper)", "io.reactivex.Single"), 66 | level = DeprecationLevel.WARNING) 67 | @CheckReturnValue 68 | @SchedulerSupport(SchedulerSupport.NONE) 69 | inline fun 70 | zip( 71 | s1: SingleSource, s2: SingleSource, 72 | s3: SingleSource, s4: SingleSource, 73 | s5: SingleSource, 74 | crossinline zipper: (T1, T2, T3, T4, T5) -> R 75 | ): Single = Single.zip(s1, s2, s3, s4, s5, 76 | Function5 { t1, t2, t3, t4, t5 -> zipper.invoke(t1, t2, t3, t4, t5) }) 77 | 78 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 79 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, s5, s6, zipper)", "io.reactivex.Single"), 80 | level = DeprecationLevel.WARNING) 81 | @CheckReturnValue 82 | @SchedulerSupport(SchedulerSupport.NONE) 83 | inline fun 84 | zip( 85 | s1: SingleSource, s2: SingleSource, 86 | s3: SingleSource, s4: SingleSource, 87 | s5: SingleSource, s6: SingleSource, 88 | crossinline zipper: (T1, T2, T3, T4, T5, T6) -> R 89 | ): Single = Single.zip(s1, s2, s3, s4, s5, s6, 90 | Function6 { t1, t2, t3, t4, t5, t6 -> zipper.invoke(t1, t2, t3, t4, t5, t6) }) 91 | 92 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 93 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, s5, s6, s7, zipper)", "io.reactivex.Single"), 94 | level = DeprecationLevel.WARNING) 95 | @CheckReturnValue 96 | @SchedulerSupport(SchedulerSupport.NONE) 97 | inline fun 98 | zip( 99 | s1: SingleSource, s2: SingleSource, 100 | s3: SingleSource, s4: SingleSource, 101 | s5: SingleSource, s6: SingleSource, 102 | s7: SingleSource, 103 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7) -> R 104 | ): Single = Single.zip(s1, s2, s3, s4, s5, s6, s7, 105 | Function7 { t1, t2, t3, t4, t5, t6, t7 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7) }) 106 | 107 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 108 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, s5, s6, s7, s8, zipper)", "io.reactivex.Single"), 109 | level = DeprecationLevel.WARNING) 110 | @CheckReturnValue 111 | @SchedulerSupport(SchedulerSupport.NONE) 112 | inline fun 113 | zip( 114 | s1: SingleSource, s2: SingleSource, 115 | s3: SingleSource, s4: SingleSource, 116 | s5: SingleSource, s6: SingleSource, 117 | s7: SingleSource, s8: SingleSource, 118 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7, T8) -> R 119 | ): Single = Single.zip(s1, s2, s3, s4, s5, s6, s7, s8, 120 | Function8 { t1, t2, t3, t4, t5, t6, t7, t8 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7, t8) }) 121 | 122 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 123 | replaceWith = ReplaceWith("Single.zip(s1, s2, s3, s4, s5, s6, s7, s8, s9, zipper)", "io.reactivex.Single"), 124 | level = DeprecationLevel.WARNING) 125 | @CheckReturnValue 126 | @SchedulerSupport(SchedulerSupport.NONE) 127 | inline fun 128 | zip( 129 | s1: SingleSource, s2: SingleSource, 130 | s3: SingleSource, s4: SingleSource, 131 | s5: SingleSource, s6: SingleSource, 132 | s7: SingleSource, s8: SingleSource, 133 | s9: SingleSource, 134 | crossinline zipper: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R 135 | ): Single = Single.zip(s1, s2, s3, s4, s5, s6, s7, s8, s9, 136 | Function9 { t1, t2, t3, t4, t5, t6, t7, t8, t9 -> zipper.invoke(t1, t2, t3, t4, t5, t6, t7, t8, t9) }) 137 | } 138 | 139 | @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", 140 | replaceWith = ReplaceWith("zipWith(other, zipper)"), 141 | level = DeprecationLevel.WARNING) 142 | @CheckReturnValue 143 | @SchedulerSupport(SchedulerSupport.NONE) 144 | inline fun Single.zipWith( 145 | other: SingleSource, 146 | crossinline zipper: (T, U) -> R 147 | ): Single = zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) }) 148 | 149 | 150 | @CheckReturnValue 151 | @SchedulerSupport(SchedulerSupport.NONE) 152 | fun Single.zipWith(other: SingleSource): Single> = 153 | zipWith(other, BiFunction { t, u -> Pair(t, u) }) 154 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/completable.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("HasPlatformType", "unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.BackpressureKind 6 | import io.reactivex.rxjava3.annotations.BackpressureSupport 7 | import io.reactivex.rxjava3.annotations.CheckReturnValue 8 | import io.reactivex.rxjava3.annotations.SchedulerSupport 9 | import io.reactivex.rxjava3.core.Completable 10 | import io.reactivex.rxjava3.core.CompletableSource 11 | import io.reactivex.rxjava3.core.Flowable 12 | import io.reactivex.rxjava3.core.Observable 13 | import io.reactivex.rxjava3.functions.Action 14 | import java.util.concurrent.Callable 15 | import java.util.concurrent.Future 16 | 17 | fun Action.toCompletable(): Completable = Completable.fromAction(this) 18 | fun Callable.toCompletable(): Completable = Completable.fromCallable(this) 19 | fun Future.toCompletable(): Completable = Completable.fromFuture(this) 20 | fun (() -> Any).toCompletable(): Completable = Completable.fromCallable(this) 21 | 22 | 23 | // EXTENSION FUNCTION OPERATORS 24 | 25 | /** 26 | * Merges the emissions of a Observable. Same as calling `flatMapSingle { it }`. 27 | */ 28 | @CheckReturnValue 29 | @SchedulerSupport(SchedulerSupport.NONE) 30 | fun Observable.mergeAllCompletables(): Completable = flatMapCompletable { it } 31 | 32 | /** 33 | * Merges the emissions of a Flowable. Same as calling `flatMap { it }`. 34 | */ 35 | @CheckReturnValue 36 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 37 | @SchedulerSupport(SchedulerSupport.NONE) 38 | fun Flowable.mergeAllCompletables(): Completable = flatMapCompletable { it } 39 | 40 | /** 41 | * Concats an Iterable of completables into flowable. Same as calling `Completable.concat(this)` 42 | */ 43 | @CheckReturnValue 44 | @SchedulerSupport(SchedulerSupport.NONE) 45 | fun Iterable.concatAll(): Completable = Completable.concat(this) 46 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/disposable.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.disposables.CompositeDisposable 4 | import io.reactivex.rxjava3.disposables.Disposable 5 | 6 | /** 7 | * disposable += observable.subscribe() 8 | */ 9 | operator fun CompositeDisposable.plusAssign(disposable: Disposable) { 10 | add(disposable) 11 | } 12 | 13 | /** 14 | * Add the disposable to a CompositeDisposable. 15 | * @param compositeDisposable CompositeDisposable to add this disposable to 16 | * @return this instance 17 | */ 18 | fun Disposable.addTo(compositeDisposable: CompositeDisposable): Disposable = 19 | apply { compositeDisposable.add(this) } 20 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/flowable.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("HasPlatformType", "unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.BackpressureKind 6 | import io.reactivex.rxjava3.annotations.BackpressureSupport 7 | import io.reactivex.rxjava3.annotations.CheckReturnValue 8 | import io.reactivex.rxjava3.annotations.SchedulerSupport 9 | import io.reactivex.rxjava3.core.Flowable 10 | import io.reactivex.rxjava3.core.Single 11 | import io.reactivex.rxjava3.functions.* 12 | import org.reactivestreams.Publisher 13 | 14 | 15 | @CheckReturnValue 16 | fun BooleanArray.toFlowable(): Flowable = asIterable().toFlowable() 17 | 18 | @CheckReturnValue 19 | fun ByteArray.toFlowable(): Flowable = asIterable().toFlowable() 20 | 21 | @CheckReturnValue 22 | fun CharArray.toFlowable(): Flowable = asIterable().toFlowable() 23 | 24 | @CheckReturnValue 25 | fun ShortArray.toFlowable(): Flowable = asIterable().toFlowable() 26 | 27 | @CheckReturnValue 28 | fun IntArray.toFlowable(): Flowable = asIterable().toFlowable() 29 | 30 | @CheckReturnValue 31 | fun LongArray.toFlowable(): Flowable = asIterable().toFlowable() 32 | 33 | @CheckReturnValue 34 | fun FloatArray.toFlowable(): Flowable = asIterable().toFlowable() 35 | 36 | @CheckReturnValue 37 | fun DoubleArray.toFlowable(): Flowable = asIterable().toFlowable() 38 | 39 | @CheckReturnValue 40 | @BackpressureSupport(BackpressureKind.FULL) 41 | @SchedulerSupport(SchedulerSupport.NONE) 42 | fun Array.toFlowable(): Flowable = Flowable.fromArray(*this) 43 | 44 | @CheckReturnValue 45 | @BackpressureSupport(BackpressureKind.FULL) 46 | @SchedulerSupport(SchedulerSupport.NONE) 47 | fun IntProgression.toFlowable(): Flowable = 48 | if (step == 1 && last.toLong() - first < Integer.MAX_VALUE) Flowable.range(first, Math.max(0, last - first + 1)) 49 | else Flowable.fromIterable(this) 50 | 51 | fun Iterator.toFlowable(): Flowable = toIterable().toFlowable() 52 | @CheckReturnValue 53 | @BackpressureSupport(BackpressureKind.FULL) 54 | @SchedulerSupport(SchedulerSupport.NONE) 55 | fun Iterable.toFlowable(): Flowable = Flowable.fromIterable(this) 56 | 57 | fun Sequence.toFlowable(): Flowable = asIterable().toFlowable() 58 | 59 | @CheckReturnValue 60 | @BackpressureSupport(BackpressureKind.FULL) 61 | @SchedulerSupport(SchedulerSupport.NONE) 62 | fun Iterable>.merge(): Flowable = Flowable.merge(this.toFlowable()) 63 | 64 | @CheckReturnValue 65 | @BackpressureSupport(BackpressureKind.FULL) 66 | @SchedulerSupport(SchedulerSupport.NONE) 67 | fun Iterable>.mergeDelayError(): Flowable = Flowable.mergeDelayError(this.toFlowable()) 68 | 69 | /** 70 | * Returns Flowable that emits objects from kotlin [Sequence] returned by function you provided by parameter [body] for 71 | * each input object and merges all produced elements into one flowable. 72 | * Works similar to [Flowable.flatMap] and [Flowable.flatMapIterable] but with [Sequence] 73 | * 74 | * @param body is a function that applied for each item emitted by source flowable that returns [Sequence] 75 | * @returns Flowable that merges all [Sequence]s produced by [body] functions 76 | */ 77 | @CheckReturnValue 78 | @BackpressureSupport(BackpressureKind.FULL) 79 | @SchedulerSupport(SchedulerSupport.NONE) 80 | inline fun Flowable.flatMapSequence(crossinline body: (T) -> Sequence): Flowable = 81 | flatMap { body(it).toFlowable() } 82 | 83 | 84 | /** 85 | * Flowable.combineLatest(List> sources, FuncN combineFunction) 86 | */ 87 | @Suppress("UNCHECKED_CAST") 88 | @SchedulerSupport(SchedulerSupport.NONE) 89 | @CheckReturnValue 90 | @BackpressureSupport(BackpressureKind.FULL) 91 | inline fun Iterable>.combineLatest(crossinline combineFunction: (args: List) -> R): Flowable = 92 | Flowable.combineLatest(this) { combineFunction(it.asList().map { it as T }) } 93 | 94 | /** 95 | * Flowable.zip(List> sources, FuncN combineFunction) 96 | */ 97 | @Suppress("UNCHECKED_CAST") 98 | @CheckReturnValue 99 | @BackpressureSupport(BackpressureKind.FULL) 100 | @SchedulerSupport(SchedulerSupport.NONE) 101 | inline fun Iterable>.zip(crossinline zipFunction: (args: List) -> R): Flowable = 102 | Flowable.zip(this) { zipFunction(it.asList().map { it as T }) } 103 | 104 | /** 105 | * Returns an Flowable that emits the items emitted by the source Flowable, converted to the specified type. 106 | */ 107 | @CheckReturnValue 108 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 109 | @SchedulerSupport(SchedulerSupport.NONE) 110 | inline fun Flowable<*>.cast(): Flowable = cast(R::class.java) 111 | 112 | /** 113 | * Filters the items emitted by an Flowable, only emitting those of the specified type. 114 | */ 115 | @CheckReturnValue 116 | @BackpressureSupport(BackpressureKind.PASS_THROUGH) 117 | @SchedulerSupport(SchedulerSupport.NONE) 118 | inline fun Flowable<*>.ofType(): Flowable = ofType(R::class.java) 119 | 120 | private fun Iterator.toIterable(): Iterable = object : Iterable { 121 | override fun iterator(): Iterator = this@toIterable 122 | } 123 | 124 | /** 125 | * Combine latest operator that produces [Pair] 126 | */ 127 | @CheckReturnValue 128 | @BackpressureSupport(BackpressureKind.FULL) 129 | @SchedulerSupport(SchedulerSupport.NONE) 130 | fun Flowable.combineLatest(flowable: Flowable): Flowable> = 131 | Flowable.combineLatest(this, flowable, BiFunction(::Pair)) 132 | 133 | /** 134 | * Combine latest operator that produces [Triple] 135 | */ 136 | @CheckReturnValue 137 | @BackpressureSupport(BackpressureKind.FULL) 138 | @SchedulerSupport(SchedulerSupport.NONE) 139 | fun Flowable.combineLatest( 140 | flowable1: Flowable, 141 | flowable2: Flowable 142 | ): Flowable> = Flowable.combineLatest(this, flowable1, flowable2, Function3(::Triple)) 143 | 144 | //EXTENSION FUNCTION OPERATORS 145 | 146 | /** 147 | * Merges the emissions of a Flowable>. Same as calling `flatMap { it }`. 148 | */ 149 | @CheckReturnValue 150 | @BackpressureSupport(BackpressureKind.FULL) 151 | @SchedulerSupport(SchedulerSupport.NONE) 152 | fun Flowable>.mergeAll(): Flowable = flatMap { it } 153 | 154 | 155 | /** 156 | * Concatenates the emissions of an Flowable>. Same as calling `concatMap { it }`. 157 | */ 158 | @CheckReturnValue 159 | @BackpressureSupport(BackpressureKind.FULL) 160 | @SchedulerSupport(SchedulerSupport.NONE) 161 | fun Flowable>.concatAll(): Flowable = concatMap { it } 162 | 163 | 164 | /** 165 | * Emits the latest `Flowable` emitted through an `Flowable>`. Same as calling `switchMap { it }`. 166 | */ 167 | @CheckReturnValue 168 | @BackpressureSupport(BackpressureKind.FULL) 169 | @SchedulerSupport(SchedulerSupport.NONE) 170 | fun Flowable>.switchLatest(): Flowable = switchMap { it } 171 | 172 | 173 | @CheckReturnValue 174 | @BackpressureSupport(BackpressureKind.FULL) 175 | @SchedulerSupport(SchedulerSupport.NONE) 176 | fun Flowable>.switchOnNext(): Flowable = Flowable.switchOnNext(this) 177 | 178 | 179 | /** 180 | * Collects `Pair` emission into a `Map` 181 | */ 182 | @CheckReturnValue 183 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 184 | @SchedulerSupport(SchedulerSupport.NONE) 185 | fun Flowable>.toMap(): Single> = 186 | toMap({ it.first }, { it.second }) 187 | 188 | /** 189 | * Collects `Pair` emission into a multimap 190 | */ 191 | @CheckReturnValue 192 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 193 | @SchedulerSupport(SchedulerSupport.NONE) 194 | fun Flowable>.toMultimap(): Single>> = 195 | toMultimap({ it.first }, { it.second }) 196 | 197 | /** 198 | * Concats an Iterable of flowables into flowable. Same as calling `Flowable.concat(this)` 199 | */ 200 | @CheckReturnValue 201 | @BackpressureSupport(BackpressureKind.FULL) 202 | @SchedulerSupport(SchedulerSupport.NONE) 203 | fun Iterable>.concatAll(): Flowable = Flowable.concat(this) 204 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/maybe.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("HasPlatformType", "unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.BackpressureKind 6 | import io.reactivex.rxjava3.annotations.BackpressureSupport 7 | import io.reactivex.rxjava3.annotations.CheckReturnValue 8 | import io.reactivex.rxjava3.annotations.SchedulerSupport 9 | import io.reactivex.rxjava3.core.Flowable 10 | import io.reactivex.rxjava3.core.Maybe 11 | import io.reactivex.rxjava3.core.MaybeSource 12 | import io.reactivex.rxjava3.core.Observable 13 | 14 | 15 | @CheckReturnValue 16 | @SchedulerSupport(SchedulerSupport.NONE) 17 | inline fun Maybe<*>.cast(): Maybe = cast(R::class.java) 18 | 19 | @CheckReturnValue 20 | @SchedulerSupport(SchedulerSupport.NONE) 21 | inline fun Maybe<*>.ofType(): Maybe = ofType(R::class.java) 22 | 23 | 24 | // EXTENSION FUNCTION OPERATORS 25 | 26 | /** 27 | * Merges the emissions of a Observable>. Same as calling `flatMapMaybe { it }`. 28 | */ 29 | @CheckReturnValue 30 | @SchedulerSupport(SchedulerSupport.NONE) 31 | fun Observable>.mergeAllMaybes(): Observable = flatMapMaybe { it } 32 | 33 | /** 34 | * Merges the emissions of a Flowable>. Same as calling `flatMap { it }`. 35 | */ 36 | @CheckReturnValue 37 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 38 | @SchedulerSupport(SchedulerSupport.NONE) 39 | fun Flowable>.mergeAllMaybes(): Flowable = flatMapMaybe { it } 40 | 41 | /** 42 | * Concats an Iterable of maybes into flowable. Same as calling `Maybe.concat(this)` 43 | */ 44 | @BackpressureSupport(BackpressureKind.FULL) 45 | @CheckReturnValue 46 | @SchedulerSupport(SchedulerSupport.NONE) 47 | fun Iterable>.concatAll(): Flowable = Maybe.concat(this) 48 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/observable.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("HasPlatformType", "unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.CheckReturnValue 6 | import io.reactivex.rxjava3.annotations.SchedulerSupport 7 | import io.reactivex.rxjava3.core.Observable 8 | import io.reactivex.rxjava3.core.ObservableSource 9 | import io.reactivex.rxjava3.core.Single 10 | 11 | 12 | @CheckReturnValue 13 | @SchedulerSupport(SchedulerSupport.NONE) 14 | fun BooleanArray.toObservable(): Observable = asIterable().toObservable() 15 | 16 | @CheckReturnValue 17 | @SchedulerSupport(SchedulerSupport.NONE) 18 | fun ByteArray.toObservable(): Observable = asIterable().toObservable() 19 | 20 | @CheckReturnValue 21 | @SchedulerSupport(SchedulerSupport.NONE) 22 | fun CharArray.toObservable(): Observable = asIterable().toObservable() 23 | 24 | @CheckReturnValue 25 | @SchedulerSupport(SchedulerSupport.NONE) 26 | fun ShortArray.toObservable(): Observable = asIterable().toObservable() 27 | 28 | @CheckReturnValue 29 | @SchedulerSupport(SchedulerSupport.NONE) 30 | fun IntArray.toObservable(): Observable = asIterable().toObservable() 31 | 32 | @CheckReturnValue 33 | @SchedulerSupport(SchedulerSupport.NONE) 34 | fun LongArray.toObservable(): Observable = asIterable().toObservable() 35 | 36 | @CheckReturnValue 37 | @SchedulerSupport(SchedulerSupport.NONE) 38 | fun FloatArray.toObservable(): Observable = asIterable().toObservable() 39 | 40 | @CheckReturnValue 41 | @SchedulerSupport(SchedulerSupport.NONE) 42 | fun DoubleArray.toObservable(): Observable = asIterable().toObservable() 43 | 44 | @CheckReturnValue 45 | @SchedulerSupport(SchedulerSupport.NONE) 46 | fun Array.toObservable(): Observable = Observable.fromArray(*this) 47 | 48 | @CheckReturnValue 49 | @SchedulerSupport(SchedulerSupport.NONE) 50 | fun IntProgression.toObservable(): Observable = 51 | if (step == 1 && last.toLong() - first < Integer.MAX_VALUE) Observable.range(first, Math.max(0, last - first + 1)) 52 | else Observable.fromIterable(this) 53 | 54 | @CheckReturnValue 55 | @SchedulerSupport(SchedulerSupport.NONE) 56 | fun Iterator.toObservable(): Observable = toIterable().toObservable() 57 | 58 | @CheckReturnValue 59 | @SchedulerSupport(SchedulerSupport.NONE) 60 | fun Iterable.toObservable(): Observable = Observable.fromIterable(this) 61 | 62 | @CheckReturnValue 63 | @SchedulerSupport(SchedulerSupport.NONE) 64 | fun Sequence.toObservable(): Observable = asIterable().toObservable() 65 | 66 | @CheckReturnValue 67 | @SchedulerSupport(SchedulerSupport.NONE) 68 | fun Iterable>.merge(): Observable = Observable.merge(this.toObservable()) 69 | 70 | @CheckReturnValue 71 | @SchedulerSupport(SchedulerSupport.NONE) 72 | fun Iterable>.mergeDelayError(): Observable = Observable.mergeDelayError(this.toObservable()) 73 | 74 | @CheckReturnValue 75 | @SchedulerSupport(SchedulerSupport.NONE) 76 | fun Observable>.flatMapIterable(): Observable = flatMapIterable { it } 77 | 78 | @CheckReturnValue 79 | @SchedulerSupport(SchedulerSupport.NONE) 80 | fun Observable>.concatMapIterable(): Observable = concatMapIterable { it } 81 | 82 | /** 83 | * Returns Observable that emits objects from kotlin [Sequence] returned by function you provided by parameter [body] for 84 | * each input object and merges all produced elements into one observable. 85 | * Works similar to [Observable.flatMap] and [Observable.flatMapIterable] but with [Sequence] 86 | * 87 | * @param body is a function that applied for each item emitted by source observable that returns [Sequence] 88 | * @returns Observable that merges all [Sequence]s produced by [body] functions 89 | */ 90 | @CheckReturnValue 91 | @SchedulerSupport(SchedulerSupport.NONE) 92 | inline fun Observable.flatMapSequence(crossinline body: (T) -> Sequence): Observable = 93 | flatMap { body(it).toObservable() } 94 | 95 | 96 | /** 97 | * Observable.combineLatest(List> sources, FuncN combineFunction) 98 | */ 99 | @Suppress("UNCHECKED_CAST") 100 | @CheckReturnValue 101 | @SchedulerSupport(SchedulerSupport.NONE) 102 | inline fun Iterable>.combineLatest(crossinline combineFunction: (args: List) -> R): Observable = 103 | Observable.combineLatest(this) { combineFunction(it.asList().map { it as T }) } 104 | 105 | /** 106 | * Observable.zip(List> sources, FuncN combineFunction) 107 | */ 108 | @Suppress("UNCHECKED_CAST") 109 | @CheckReturnValue 110 | @SchedulerSupport(SchedulerSupport.NONE) 111 | inline fun Iterable>.zip(crossinline zipFunction: (args: List) -> R): Observable = 112 | Observable.zip(this) { zipFunction(it.asList().map { it as T }) } 113 | 114 | /** 115 | * Returns an Observable that emits the items emitted by the source Observable, converted to the specified type. 116 | */ 117 | @CheckReturnValue 118 | @SchedulerSupport(SchedulerSupport.NONE) 119 | inline fun Observable<*>.cast(): Observable = cast(R::class.java) 120 | 121 | /** 122 | * Filters the items emitted by an Observable, only emitting those of the specified type. 123 | */ 124 | @CheckReturnValue 125 | @SchedulerSupport(SchedulerSupport.NONE) 126 | inline fun Observable<*>.ofType(): Observable = ofType(R::class.java) 127 | 128 | private fun Iterator.toIterable(): Iterable = object : Iterable { 129 | override fun iterator(): Iterator = this@toIterable 130 | } 131 | 132 | // EXTENSION FUNCTION OPERATORS 133 | 134 | /** 135 | * Merges the emissions of an Observable>. Same as calling `flatMap { it }`. 136 | */ 137 | @CheckReturnValue 138 | @SchedulerSupport(SchedulerSupport.NONE) 139 | fun Observable>.mergeAll(): Observable = flatMap { it } 140 | 141 | /** 142 | * Concatenates the emissions of an Observable>. Same as calling `concatMap { it }`. 143 | */ 144 | @CheckReturnValue 145 | @SchedulerSupport(SchedulerSupport.NONE) 146 | fun Observable>.concatAll(): Observable = concatMap { it } 147 | 148 | /** 149 | * Emits the latest `Observable` emitted through an `Observable>`. Same as calling `switchMap { it }`. 150 | */ 151 | @CheckReturnValue 152 | @SchedulerSupport(SchedulerSupport.NONE) 153 | fun Observable>.switchLatest(): Observable = switchMap { it } 154 | 155 | @CheckReturnValue 156 | @SchedulerSupport(SchedulerSupport.NONE) 157 | fun Observable>.switchOnNext(): Observable = 158 | Observable.switchOnNext(this) 159 | 160 | /** 161 | * Collects `Pair` emission into a `Map` 162 | */ 163 | @CheckReturnValue 164 | @SchedulerSupport(SchedulerSupport.NONE) 165 | fun Observable>.toMap(): Single> = 166 | toMap({ it.first }, { it.second }) 167 | 168 | /** 169 | * Collects `Pair` emission into a multimap 170 | */ 171 | @CheckReturnValue 172 | @SchedulerSupport(SchedulerSupport.NONE) 173 | fun Observable>.toMultimap(): Single>> = 174 | toMultimap({ it.first }, { it.second }) 175 | 176 | @CheckReturnValue 177 | @SchedulerSupport(SchedulerSupport.NONE) 178 | fun Iterable>.concatAll(): Observable = 179 | Observable.concat(this) 180 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/single.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("HasPlatformType", "unused") 2 | 3 | package io.reactivex.rxjava3.kotlin 4 | 5 | import io.reactivex.rxjava3.annotations.BackpressureKind 6 | import io.reactivex.rxjava3.annotations.BackpressureSupport 7 | import io.reactivex.rxjava3.annotations.CheckReturnValue 8 | import io.reactivex.rxjava3.annotations.SchedulerSupport 9 | import io.reactivex.rxjava3.core.Flowable 10 | import io.reactivex.rxjava3.core.Observable 11 | import io.reactivex.rxjava3.core.Single 12 | import io.reactivex.rxjava3.core.SingleSource 13 | 14 | inline fun Single<*>.cast(): Single = cast(R::class.java) 15 | 16 | 17 | // EXTENSION FUNCTION OPERATORS 18 | 19 | /** 20 | * Merges the emissions of a Observable>. Same as calling `flatMapSingle { it }`. 21 | */ 22 | @CheckReturnValue 23 | @SchedulerSupport(SchedulerSupport.NONE) 24 | fun Observable>.mergeAllSingles(): Observable = flatMapSingle { it } 25 | 26 | /** 27 | * Merges the emissions of a Flowable>. Same as calling `flatMap { it }`. 28 | */ 29 | @CheckReturnValue 30 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 31 | @SchedulerSupport(SchedulerSupport.NONE) 32 | fun Flowable>.mergeAllSingles(): Flowable = flatMapSingle { it } 33 | 34 | /** 35 | * Concats an Iterable of singles into flowable. Same as calling `Single.concat(this)` 36 | */ 37 | @CheckReturnValue 38 | @SchedulerSupport(SchedulerSupport.NONE) 39 | @BackpressureSupport(BackpressureKind.FULL) 40 | fun Iterable>.concatAll(): Flowable = Single.concat(this) 41 | -------------------------------------------------------------------------------- /src/main/kotlin/io/reactivex/rxjava3/kotlin/subscribers.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.annotations.BackpressureKind 4 | import io.reactivex.rxjava3.annotations.BackpressureSupport 5 | import io.reactivex.rxjava3.annotations.CheckReturnValue 6 | import io.reactivex.rxjava3.annotations.SchedulerSupport 7 | import io.reactivex.rxjava3.core.* 8 | import io.reactivex.rxjava3.disposables.Disposable 9 | import io.reactivex.rxjava3.functions.Action 10 | import io.reactivex.rxjava3.functions.Consumer 11 | import io.reactivex.rxjava3.internal.functions.Functions 12 | 13 | 14 | private val onNextStub: (Any) -> Unit = {} 15 | private val onErrorStub: (Throwable) -> Unit = {} 16 | private val onCompleteStub: () -> Unit = {} 17 | 18 | private fun ((T) -> Unit).asConsumer(): Consumer { 19 | return if (this === onNextStub) Functions.emptyConsumer() else Consumer(this) 20 | } 21 | 22 | private fun ((Throwable) -> Unit).asOnErrorConsumer(): Consumer { 23 | return if (this === onErrorStub) Functions.ON_ERROR_MISSING else Consumer(this) 24 | } 25 | 26 | private fun (() -> Unit).asOnCompleteAction(): Action { 27 | return if (this === onCompleteStub) Functions.EMPTY_ACTION else Action(this) 28 | } 29 | 30 | /** 31 | * Overloaded subscribe function that allows passing named parameters 32 | */ 33 | @CheckReturnValue 34 | @SchedulerSupport(SchedulerSupport.NONE) 35 | fun Observable.subscribeBy( 36 | onError: (Throwable) -> Unit = onErrorStub, 37 | onComplete: () -> Unit = onCompleteStub, 38 | onNext: (T) -> Unit = onNextStub 39 | ): Disposable = subscribe(onNext.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 40 | 41 | /** 42 | * Overloaded subscribe function that allows passing named parameters 43 | */ 44 | @CheckReturnValue 45 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 46 | @SchedulerSupport(SchedulerSupport.NONE) 47 | fun Flowable.subscribeBy( 48 | onError: (Throwable) -> Unit = onErrorStub, 49 | onComplete: () -> Unit = onCompleteStub, 50 | onNext: (T) -> Unit = onNextStub 51 | ): Disposable = subscribe(onNext.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 52 | 53 | /** 54 | * Overloaded subscribe function that allows passing named parameters 55 | */ 56 | @CheckReturnValue 57 | @SchedulerSupport(SchedulerSupport.NONE) 58 | fun Single.subscribeBy( 59 | onError: (Throwable) -> Unit = onErrorStub, 60 | onSuccess: (T) -> Unit = onNextStub 61 | ): Disposable = subscribe(onSuccess.asConsumer(), onError.asOnErrorConsumer()) 62 | 63 | /** 64 | * Overloaded subscribe function that allows passing named parameters 65 | */ 66 | @CheckReturnValue 67 | @SchedulerSupport(SchedulerSupport.NONE) 68 | fun Maybe.subscribeBy( 69 | onError: (Throwable) -> Unit = onErrorStub, 70 | onComplete: () -> Unit = onCompleteStub, 71 | onSuccess: (T) -> Unit = onNextStub 72 | ): Disposable = subscribe(onSuccess.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 73 | 74 | /** 75 | * Overloaded subscribe function that allows passing named parameters 76 | */ 77 | @CheckReturnValue 78 | @SchedulerSupport(SchedulerSupport.NONE) 79 | fun Completable.subscribeBy( 80 | onError: (Throwable) -> Unit = onErrorStub, 81 | onComplete: () -> Unit = onCompleteStub 82 | ): Disposable = when { 83 | // There are optimized versions of the completable Consumers, so we need to use the subscribe overloads 84 | // here. 85 | onError === onErrorStub && onComplete === onCompleteStub -> subscribe() 86 | onError === onErrorStub -> subscribe(onComplete) 87 | else -> subscribe(onComplete.asOnCompleteAction(), Consumer(onError)) 88 | } 89 | 90 | /** 91 | * Overloaded blockingSubscribe function that allows passing named parameters 92 | */ 93 | @SchedulerSupport(SchedulerSupport.NONE) 94 | fun Observable.blockingSubscribeBy( 95 | onError: (Throwable) -> Unit = onErrorStub, 96 | onComplete: () -> Unit = onCompleteStub, 97 | onNext: (T) -> Unit = onNextStub 98 | ): Unit = blockingSubscribe(onNext.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 99 | 100 | /** 101 | * Overloaded blockingSubscribe function that allows passing named parameters 102 | */ 103 | @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) 104 | @SchedulerSupport(SchedulerSupport.NONE) 105 | fun Flowable.blockingSubscribeBy( 106 | onError: (Throwable) -> Unit = onErrorStub, 107 | onComplete: () -> Unit = onCompleteStub, 108 | onNext: (T) -> Unit = onNextStub 109 | ): Unit = blockingSubscribe(onNext.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 110 | 111 | /** 112 | * Overloaded blockingSubscribe function that allows passing named parameters 113 | */ 114 | @SchedulerSupport(SchedulerSupport.NONE) 115 | fun Maybe.blockingSubscribeBy( 116 | onError: (Throwable) -> Unit = onErrorStub, 117 | onComplete: () -> Unit = onCompleteStub, 118 | onSuccess: (T) -> Unit = onNextStub 119 | ) : Unit = blockingSubscribe(onSuccess.asConsumer(), onError.asOnErrorConsumer(), onComplete.asOnCompleteAction()) 120 | 121 | /** 122 | * Overloaded blockingSubscribe function that allows passing named parameters 123 | */ 124 | @SchedulerSupport(SchedulerSupport.NONE) 125 | fun Single.blockingSubscribeBy( 126 | onError: (Throwable) -> Unit = onErrorStub, 127 | onSuccess: (T) -> Unit = onNextStub 128 | ) : Unit = blockingSubscribe(onSuccess.asConsumer(), onError.asOnErrorConsumer()) 129 | 130 | /** 131 | * Overloaded blockingSubscribe function that allows passing named parameters 132 | */ 133 | @SchedulerSupport(SchedulerSupport.NONE) 134 | fun Completable.blockingSubscribeBy( 135 | onError: (Throwable) -> Unit = onErrorStub, 136 | onComplete: () -> Unit = onCompleteStub 137 | ): Unit = blockingSubscribe(onComplete.asOnCompleteAction(), onError.asOnErrorConsumer()) 138 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/BasicKotlinTests.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.reactivex.rxjava3.kotlin 18 | 19 | import io.reactivex.rxjava3.core.* 20 | import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException 21 | import io.reactivex.rxjava3.functions.* 22 | import io.reactivex.rxjava3.plugins.RxJavaPlugins 23 | import org.junit.Assert.* 24 | import org.junit.Test 25 | import org.mockito.Mockito.* 26 | import kotlin.concurrent.thread 27 | 28 | /** 29 | * This class use plain Kotlin without extensions from the language adaptor 30 | */ 31 | class BasicKotlinTests : KotlinTests() { 32 | 33 | @Test fun testCreate() { 34 | Observable.create { onSubscribe -> 35 | onSubscribe.onNext("Hello") 36 | onSubscribe.onComplete() 37 | }.subscribeBy( 38 | onNext = { a.received(it) } 39 | ) 40 | 41 | verify(a, times(1)).received("Hello") 42 | } 43 | 44 | @Test fun testOnError() { 45 | class TestException : RuntimeException() 46 | val list = ArrayList() 47 | RxJavaPlugins.setErrorHandler { list.add(it) } 48 | try { 49 | Observable.error(TestException()).subscribeBy() 50 | assertEquals(1, list.size) 51 | assertTrue(list[0] is OnErrorNotImplementedException) 52 | assertTrue(list[0].cause is TestException) 53 | } finally { 54 | RxJavaPlugins.reset() 55 | } 56 | } 57 | 58 | @Test fun testFilter() { 59 | Observable.fromIterable(listOf(1, 2, 3)) 60 | .filter { it >= 2 } 61 | .subscribeBy(onNext = received()) 62 | verify(a, times(0)).received(1) 63 | verify(a, times(1)).received(2) 64 | verify(a, times(1)).received(3) 65 | } 66 | 67 | @Test fun testLast() { 68 | assertEquals("three", Observable.fromIterable(listOf("one", "two", "three")).blockingLast()) 69 | } 70 | 71 | @Test fun testMap1() { 72 | Single.just(1) 73 | .map { v -> "hello_$v" } 74 | .subscribeBy(onSuccess = received()) 75 | verify(a, times(1)).received("hello_1") 76 | } 77 | 78 | @Test fun testMap2() { 79 | Observable.fromIterable(listOf(1, 2, 3)).map { v -> "hello_$v" }.subscribe(received()) 80 | verify(a, times(1)).received("hello_1") 81 | verify(a, times(1)).received("hello_2") 82 | verify(a, times(1)).received("hello_3") 83 | } 84 | 85 | @Test fun testMaterialize() { 86 | Observable.fromIterable(listOf(1, 2, 3)).materialize().subscribe(received()) 87 | verify(a, times(4)).received(any(Notification::class.java)) 88 | verify(a, times(0)).error(any(Exception::class.java)) 89 | } 90 | 91 | @Test fun testMerge() { 92 | Observable.merge( 93 | Observable.fromIterable(listOf(1, 2, 3)), 94 | Observable.merge( 95 | Observable.just(6), 96 | Observable.error(NullPointerException()), 97 | Observable.just(7) 98 | ), 99 | Observable.fromIterable(listOf(4, 5)) 100 | ).subscribeBy( 101 | onNext = received(), 102 | onError = { a.error(it) } 103 | ) 104 | verify(a, times(1)).received(1) 105 | verify(a, times(1)).received(2) 106 | verify(a, times(1)).received(3) 107 | verify(a, times(0)).received(4) 108 | verify(a, times(0)).received(5) 109 | verify(a, times(1)).received(6) 110 | verify(a, times(0)).received(7) 111 | verify(a, times(1)).error(any(NullPointerException::class.java)) 112 | } 113 | 114 | @Test fun testScriptWithMaterialize() { 115 | TestFactory() 116 | .observable 117 | .materialize() 118 | .subscribeBy(onNext = received()) 119 | verify(a, times(2)).received(any(Notification::class.java)) 120 | } 121 | 122 | @Test fun testScriptWithMerge() { 123 | val factory = TestFactory() 124 | Observable.merge(factory.observable, factory.observable) 125 | .subscribeBy(onNext = received()) 126 | verify(a, times(1)).received("hello_1") 127 | verify(a, times(1)).received("hello_2") 128 | } 129 | 130 | @Test fun testFromWithIterable() { 131 | val list = listOf(1, 2, 3, 4, 5) 132 | assertEquals(5, Observable.fromIterable(list).count().blockingGet()) 133 | } 134 | 135 | @Test fun testFromWithObjects() { 136 | val list = listOf(1, 2, 3, 4, 5) 137 | assertEquals(2, Observable.fromIterable(listOf(list, 6)).count().blockingGet()) 138 | } 139 | 140 | @Test fun testStartWith() { 141 | val list = listOf(10, 11, 12, 13, 14) 142 | val startList = listOf(1, 2, 3, 4, 5) 143 | assertEquals(6, Observable.fromIterable(list).startWithItem(0).count().blockingGet()) 144 | assertEquals(10, Observable.fromIterable(list).startWithIterable(startList).count().blockingGet()) 145 | } 146 | 147 | @Test fun testScriptWithOnNext() { 148 | TestFactory().observable.subscribe(received()) 149 | verify(a, times(1)).received("hello_1") 150 | } 151 | 152 | @Test fun testSkipTake() { 153 | Observable.fromIterable(listOf(1, 2, 3)).skip(1).take(1).subscribe(received()) 154 | verify(a, times(0)).received(1) 155 | verify(a, times(1)).received(2) 156 | verify(a, times(0)).received(3) 157 | } 158 | 159 | @Test fun testSkip() { 160 | Observable.fromIterable(listOf(1, 2, 3)).skip(2).subscribe(received()) 161 | verify(a, times(0)).received(1) 162 | verify(a, times(0)).received(2) 163 | verify(a, times(1)).received(3) 164 | } 165 | 166 | @Test fun testTake() { 167 | Observable.fromIterable(listOf(1, 2, 3)).take(2).subscribe(received()) 168 | verify(a, times(1)).received(1) 169 | verify(a, times(1)).received(2) 170 | verify(a, times(0)).received(3) 171 | } 172 | 173 | @Test fun testTakeLast() { 174 | TestFactory().observable.takeLast(1).subscribe(received()) 175 | verify(a, times(1)).received("hello_1") 176 | } 177 | 178 | @Test fun testTakeWhile() { 179 | Observable.fromIterable(listOf(1, 2, 3)).takeWhile { x -> x < 3 }.subscribe(received()) 180 | verify(a, times(1)).received(1) 181 | verify(a, times(1)).received(2) 182 | verify(a, times(0)).received(3) 183 | } 184 | 185 | @Test fun testTakeWhileWithIndex() { 186 | Observable.fromIterable(listOf(1, 2, 3)) 187 | .takeWhile { x -> x < 3 } 188 | .zipWith(Observable.range(0, Integer.MAX_VALUE), BiFunction { x, _ -> x }) 189 | .subscribe(received()) 190 | verify(a, times(1)).received(1) 191 | verify(a, times(1)).received(2) 192 | verify(a, times(0)).received(3) 193 | } 194 | 195 | @Test fun testToSortedList() { 196 | TestFactory().numbers.toSortedList().subscribe(received()) 197 | verify(a, times(1)).received(listOf(1, 2, 3, 4, 5)) 198 | } 199 | 200 | @Test fun testForEach() { 201 | Observable.create(AsyncObservable()).blockingForEach(received()) 202 | verify(a, times(1)).received(1) 203 | verify(a, times(1)).received(2) 204 | verify(a, times(1)).received(3) 205 | } 206 | 207 | @Test(expected = RuntimeException::class) fun testForEachWithError() { 208 | Observable.create(AsyncObservable()).blockingForEach { throw RuntimeException("err") } 209 | fail("we expect an exception to be thrown") 210 | } 211 | 212 | @Test fun testLastOrDefault() { 213 | assertEquals("two", Observable.fromIterable(listOf("one", "two")).blockingLast("default")) 214 | assertEquals("default", Observable.fromIterable(listOf("one", "two")).filter { it.length > 3 }.blockingLast("default")) 215 | } 216 | 217 | @Test(expected = IllegalArgumentException::class) fun testSingle() { 218 | assertEquals("one", Observable.just("one").blockingSingle()) 219 | Observable.fromIterable(listOf("one", "two")).blockingSingle() 220 | fail() 221 | } 222 | 223 | @Test fun testDefer() { 224 | Observable.defer { Observable.fromIterable(listOf(1, 2)) }.subscribe(received()) 225 | verify(a, times(1)).received(1) 226 | verify(a, times(1)).received(2) 227 | } 228 | 229 | @Test fun testAll() { 230 | Observable.fromIterable(listOf(1, 2, 3)).all { x -> x > 0 }.subscribe(received()) 231 | verify(a, times(1)).received(true) 232 | } 233 | 234 | @Test fun testZip() { 235 | val o1 = Observable.fromIterable(listOf(1, 2, 3)) 236 | val o2 = Observable.fromIterable(listOf(4, 5, 6)) 237 | val o3 = Observable.fromIterable(listOf(7, 8, 9)) 238 | 239 | val values = Observable.zip(o1, o2, o3, Function3> { a, b, c -> listOf(a, b, c) }).toList().blockingGet() 240 | assertEquals(listOf(1, 4, 7), values[0]) 241 | assertEquals(listOf(2, 5, 8), values[1]) 242 | assertEquals(listOf(3, 6, 9), values[2]) 243 | } 244 | 245 | @Test fun testZipWithIterable() { 246 | val o1 = Observable.fromIterable(listOf(1, 2, 3)) 247 | val o2 = Observable.fromIterable(listOf(4, 5, 6)) 248 | val o3 = Observable.fromIterable(listOf(7, 8, 9)) 249 | 250 | val values = Observable.zip(listOf(o1, o2, o3), { args -> listOf(*args) }).toList().blockingGet() 251 | assertEquals(listOf(1, 4, 7), values[0]) 252 | assertEquals(listOf(2, 5, 8), values[1]) 253 | assertEquals(listOf(3, 6, 9), values[2]) 254 | } 255 | 256 | @Test fun testGroupBy() { 257 | var count = 0 258 | 259 | Observable.fromIterable(listOf("one", "two", "three", "four", "five", "six")) 260 | .groupBy(String::length) 261 | .flatMap { groupObservable -> 262 | groupObservable.map { s -> 263 | "Value: $s Group ${groupObservable.key}" 264 | } 265 | }.blockingForEach { s -> 266 | println(s) 267 | count++ 268 | } 269 | 270 | assertEquals(6, count) 271 | } 272 | 273 | 274 | class TestFactory() { 275 | var counter = 1 276 | 277 | val numbers: Observable 278 | get() { 279 | return Observable.fromIterable(listOf(1, 3, 2, 5, 4)) 280 | } 281 | 282 | val onSubscribe: TestOnSubscribe 283 | get() { 284 | return TestOnSubscribe(counter++) 285 | } 286 | 287 | val observable: Observable 288 | get() { 289 | return Observable.create(onSubscribe) 290 | } 291 | 292 | } 293 | 294 | class AsyncObservable : ObservableOnSubscribe { 295 | override fun subscribe(op: ObservableEmitter) { 296 | thread { 297 | Thread.sleep(50) 298 | op.onNext(1) 299 | op.onNext(2) 300 | op.onNext(3) 301 | op.onComplete() 302 | } 303 | } 304 | } 305 | 306 | class TestOnSubscribe(val count: Int) : ObservableOnSubscribe { 307 | override fun subscribe(op: ObservableEmitter) { 308 | op.onNext("hello_$count") 309 | op.onComplete() 310 | } 311 | } 312 | } 313 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/CompletableTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Completable 4 | import io.reactivex.rxjava3.core.Single 5 | import io.reactivex.rxjava3.functions.Action 6 | import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection 7 | import org.junit.Assert 8 | import org.junit.Assert.assertEquals 9 | import org.junit.Assert.assertNotNull 10 | import org.junit.Test 11 | import org.mockito.Mockito 12 | import java.util.* 13 | import java.util.concurrent.Callable 14 | 15 | class CompletableTest : KotlinTests() { 16 | 17 | @Test fun testCreateFromAction() { 18 | var count = 0 19 | val c1 = Action { count++ }.toCompletable() 20 | assertNotNull(c1) 21 | c1.subscribe() 22 | assertEquals(1, count) 23 | } 24 | 25 | @Test fun testCreateFromCallable() { 26 | var count = 0 27 | val c1 = Callable { count++ }.toCompletable() 28 | assertNotNull(c1) 29 | c1.subscribe() 30 | assertEquals(1, count) 31 | } 32 | 33 | @Test fun createFromLambda() { 34 | var count = 0 35 | val c2 = { count++ }.toCompletable() 36 | assertNotNull(c2) 37 | c2.subscribe() 38 | assertEquals(1, count) 39 | } 40 | 41 | @Test(expected = NoSuchElementException::class) fun testCreateFromSingle() { 42 | val c1 = Single.just("Hello World!").ignoreElement() 43 | assertNotNull(c1) 44 | c1.toObservable().blockingFirst() 45 | } 46 | 47 | @Test fun testConcatAll() { 48 | val list = emptyList().toMutableList() 49 | (0 until 10) 50 | .map { v -> Completable.create { list += v } } 51 | .concatAll() 52 | .subscribe { 53 | Assert.assertEquals((0 until 10).toList(), list) 54 | } 55 | } 56 | 57 | @Test 58 | fun testSubscribeBy() { 59 | Completable.complete() 60 | .subscribeBy { 61 | a.received(Unit) 62 | } 63 | Mockito.verify(a, Mockito.times(1)) 64 | .received(Unit) 65 | } 66 | 67 | @Test 68 | fun testSubscribeByErrorIntrospection() { 69 | val disposable = Completable.complete() 70 | .subscribeBy() as LambdaConsumerIntrospection 71 | Assert.assertFalse(disposable.hasCustomOnError()) 72 | } 73 | 74 | @Test 75 | fun testSubscribeByErrorIntrospectionCustom() { 76 | val disposable = Completable.complete() 77 | .subscribeBy(onError = {}) as LambdaConsumerIntrospection 78 | Assert.assertTrue(disposable.hasCustomOnError()) 79 | } 80 | 81 | @Test 82 | fun testBlockingSubscribeBy() { 83 | Completable.complete() 84 | .blockingSubscribeBy { 85 | a.received(Unit) 86 | } 87 | Mockito.verify(a, Mockito.times(1)) 88 | .received(Unit) 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/ExtensionTests.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.reactivex.rxjava3.kotlin 18 | 19 | import io.reactivex.rxjava3.core.Notification 20 | import io.reactivex.rxjava3.core.Observable 21 | import io.reactivex.rxjava3.core.ObservableEmitter 22 | import io.reactivex.rxjava3.schedulers.TestScheduler 23 | import io.reactivex.rxjava3.functions.* 24 | import org.funktionale.partials.invoke 25 | import org.junit.Assert.assertEquals 26 | import org.junit.Assert.fail 27 | import org.junit.Test 28 | import org.mockito.Mockito.* 29 | import java.util.concurrent.TimeUnit 30 | import kotlin.concurrent.thread 31 | 32 | /** 33 | * This class contains tests using the extension functions provided by the language adaptor. 34 | */ 35 | class ExtensionTests : KotlinTests() { 36 | 37 | 38 | @Test fun testCreate() { 39 | Observable.create { subscriber -> 40 | subscriber.onNext("Hello") 41 | subscriber.onComplete() 42 | }.subscribe { result -> 43 | a.received(result) 44 | } 45 | 46 | verify(a, times(1)).received("Hello") 47 | } 48 | 49 | @Test fun testFilter() { 50 | listOf(1, 2, 3).toObservable().filter { it >= 2 }.subscribe(received()) 51 | verify(a, times(0)).received(1) 52 | verify(a, times(1)).received(2) 53 | verify(a, times(1)).received(3) 54 | } 55 | 56 | @Test fun testLast() { 57 | assertEquals("three", listOf("one", "two", "three").toObservable().blockingLast()) 58 | } 59 | 60 | @Test fun testLastWithPredicate() { 61 | assertEquals("two", listOf("one", "two", "three").toObservable().filter { it.length == 3 }.blockingLast()) 62 | } 63 | 64 | @Test fun testMap2() { 65 | listOf(1, 2, 3).toObservable().map { v -> "hello_$v" }.subscribe(received()) 66 | verify(a, times(1)).received("hello_1") 67 | verify(a, times(1)).received("hello_2") 68 | verify(a, times(1)).received("hello_3") 69 | } 70 | 71 | @Test fun testMaterialize() { 72 | listOf(1, 2, 3).toObservable().materialize().subscribe(received()) 73 | verify(a, times(4)).received(any(Notification::class.java)) 74 | verify(a, times(0)).error(any(Exception::class.java)) 75 | } 76 | 77 | @Test fun testMerge() { 78 | listOf(listOf(1, 2, 3).toObservable(), 79 | listOf(Observable.just(6), 80 | Observable.error(NullPointerException()), 81 | Observable.just(7) 82 | ).merge(), 83 | listOf(4, 5).toObservable() 84 | ).merge().subscribe(received(), { e -> a.error(e) }) 85 | verify(a, times(1)).received(1) 86 | verify(a, times(1)).received(2) 87 | verify(a, times(1)).received(3) 88 | verify(a, times(0)).received(4) 89 | verify(a, times(0)).received(5) 90 | verify(a, times(1)).received(6) 91 | verify(a, times(0)).received(7) 92 | verify(a, times(1)).error(any(NullPointerException::class.java)) 93 | } 94 | 95 | @Test fun testScriptWithMaterialize() { 96 | TestFactory().observable.materialize().subscribe(received()) 97 | verify(a, times(2)).received(any(Notification::class.java)) 98 | } 99 | 100 | @Test fun testScriptWithMerge() { 101 | val factory = TestFactory() 102 | (factory.observable.mergeWith(factory.observable)).subscribe((received())) 103 | verify(a, times(1)).received("hello_1") 104 | verify(a, times(1)).received("hello_2") 105 | } 106 | 107 | @Test fun testFromWithIterable() { 108 | assertEquals(5, listOf(1, 2, 3, 4, 5).toObservable().count().blockingGet()) 109 | } 110 | 111 | @Test fun testStartWith() { 112 | val list = listOf(10, 11, 12, 13, 14) 113 | val startList = listOf(1, 2, 3, 4, 5) 114 | assertEquals(6, list.toObservable().startWithItem(0).count().blockingGet()) 115 | assertEquals(10, list.toObservable().startWithIterable(startList).count().blockingGet()) 116 | } 117 | 118 | @Test fun testScriptWithOnNext() { 119 | TestFactory().observable 120 | .subscribe(received()) 121 | verify(a, times(1)).received("hello_1") 122 | } 123 | 124 | @Test fun testSkipTake() { 125 | listOf(1, 2, 3) 126 | .toObservable() 127 | .skip(1) 128 | .take(1) 129 | .subscribe(received()) 130 | verify(a, times(0)).received(1) 131 | verify(a, times(1)).received(2) 132 | verify(a, times(0)).received(3) 133 | } 134 | 135 | @Test fun testSkip() { 136 | listOf(1, 2, 3) 137 | .toObservable() 138 | .skip(2) 139 | .subscribe(received()) 140 | verify(a, times(0)).received(1) 141 | verify(a, times(0)).received(2) 142 | verify(a, times(1)).received(3) 143 | } 144 | 145 | @Test fun testTake() { 146 | listOf(1, 2, 3) 147 | .toObservable() 148 | .take(2) 149 | .subscribe(received()) 150 | verify(a, times(1)).received(1) 151 | verify(a, times(1)).received(2) 152 | verify(a, times(0)).received(3) 153 | } 154 | 155 | @Test fun testTakeLast() { 156 | TestFactory().observable 157 | .takeLast(1) 158 | .subscribe(received()) 159 | verify(a, times(1)).received("hello_1") 160 | } 161 | 162 | @Test fun testTakeWhile() { 163 | listOf(1, 2, 3) 164 | .toObservable() 165 | .takeWhile { x -> x < 3 } 166 | .subscribe(received()) 167 | verify(a, times(1)).received(1) 168 | verify(a, times(1)).received(2) 169 | verify(a, times(0)).received(3) 170 | } 171 | 172 | 173 | @Test fun testToSortedList() { 174 | TestFactory().numbers.toSortedList().subscribe(received()) 175 | verify(a, times(1)).received(listOf(1, 2, 3, 4, 5)) 176 | } 177 | 178 | @Test fun testForEach() { 179 | Observable.create(asyncObservable).blockingForEach(received()) 180 | verify(a, times(1)).received(1) 181 | verify(a, times(1)).received(2) 182 | verify(a, times(1)).received(3) 183 | } 184 | 185 | @Test(expected = RuntimeException::class) fun testForEachWithError() { 186 | Observable.create(asyncObservable).blockingForEach { throw RuntimeException("err") } 187 | fail("we expect an exception to be thrown") 188 | } 189 | 190 | @Test fun testLastOrDefault() { 191 | assertEquals("two", listOf("one", "two").toObservable().blockingLast("default")) 192 | assertEquals("default", listOf("one", "two").toObservable().filter { it.length > 3 }.blockingLast("default")) 193 | } 194 | 195 | @Test fun testDefer() { 196 | Observable.defer { listOf(1, 2).toObservable() }.subscribe(received()) 197 | verify(a, times(1)).received(1) 198 | verify(a, times(1)).received(2) 199 | } 200 | 201 | @Test fun testAll() { 202 | listOf(1, 2, 3).toObservable().all { x -> x > 0 }.subscribe(received()) 203 | verify(a, times(1)).received(true) 204 | } 205 | 206 | @Test fun testZip() { 207 | val o1 = listOf(1, 2, 3).toObservable() 208 | val o2 = listOf(4, 5, 6).toObservable() 209 | val o3 = listOf(7, 8, 9).toObservable() 210 | 211 | val values = Observable.zip(o1, o2, o3, Function3 > { a, b, c -> listOf(a, b, c) }).toList().blockingGet() 212 | assertEquals(listOf(1, 4, 7), values[0]) 213 | assertEquals(listOf(2, 5, 8), values[1]) 214 | assertEquals(listOf(3, 6, 9), values[2]) 215 | } 216 | 217 | @Test fun testSwitchOnNext() { 218 | val testScheduler = TestScheduler() 219 | val worker = testScheduler.createWorker() 220 | 221 | val observable = Observable.create> { s -> 222 | fun at(delay: Long, func: () -> Unit) { 223 | worker.schedule({ 224 | func() 225 | }, delay, TimeUnit.MILLISECONDS) 226 | } 227 | 228 | val first = Observable.interval(5, TimeUnit.MILLISECONDS, testScheduler).take(3) 229 | at(0, { s.onNext(first) }) 230 | 231 | val second = Observable.interval(5, TimeUnit.MILLISECONDS, testScheduler).take(3) 232 | at(11, { s.onNext(second) }) 233 | 234 | at(40, { s.onComplete() }) 235 | } 236 | 237 | observable.switchOnNext().subscribe(received()) 238 | 239 | val inOrder = inOrder(a) 240 | testScheduler.advanceTimeTo(10, TimeUnit.MILLISECONDS) 241 | inOrder.verify(a, times(1)).received(0L) 242 | inOrder.verify(a, times(1)).received(1L) 243 | 244 | testScheduler.advanceTimeTo(40, TimeUnit.MILLISECONDS) 245 | inOrder.verify(a, times(1)).received(0L) 246 | inOrder.verify(a, times(1)).received(1L) 247 | inOrder.verify(a, times(1)).received(2L) 248 | inOrder.verifyNoMoreInteractions() 249 | } 250 | 251 | val funOnSubscribe: (Int, ObservableEmitter) -> Unit = { counter, subscriber -> 252 | subscriber.onNext("hello_$counter") 253 | subscriber.onComplete() 254 | } 255 | 256 | val asyncObservable: (ObservableEmitter) -> Unit = { subscriber -> 257 | thread { 258 | Thread.sleep(50) 259 | subscriber.onNext(1) 260 | subscriber.onNext(2) 261 | subscriber.onNext(3) 262 | subscriber.onComplete() 263 | } 264 | } 265 | 266 | inner class TestFactory { 267 | var counter = 1 268 | 269 | val numbers: Observable 270 | get() = listOf(1, 3, 2, 5, 4).toObservable() 271 | 272 | val onSubscribe: (ObservableEmitter) -> Unit 273 | get() = funOnSubscribe(p1 = counter++) // partial applied function 274 | 275 | val observable: Observable 276 | get() = Observable.create(onSubscribe) 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/FlowableTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.BackpressureStrategy 4 | import io.reactivex.rxjava3.core.Flowable 5 | import io.reactivex.rxjava3.core.FlowableEmitter 6 | import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection 7 | import io.reactivex.rxjava3.subscribers.TestSubscriber 8 | import org.junit.Assert 9 | import org.junit.Assert.assertEquals 10 | import org.junit.Assert.assertNotNull 11 | import org.junit.Ignore 12 | import org.junit.Test 13 | import java.util.concurrent.atomic.AtomicInteger 14 | import java.util.concurrent.atomic.AtomicReference 15 | 16 | class FlowableTest { 17 | 18 | private fun bufferedFlowable(source: (FlowableEmitter) -> Unit) = 19 | Flowable.create(source, BackpressureStrategy.BUFFER) 20 | 21 | @org.junit.Test fun testCreation() { 22 | val o0: Flowable = Flowable.empty() 23 | val list = bufferedFlowable { s -> 24 | s.onNext(1) 25 | s.onNext(777) 26 | s.onComplete() 27 | }.toList().blockingGet() 28 | assertEquals(listOf(1, 777), list) 29 | val o1: Flowable = listOf(1, 2, 3).toFlowable() 30 | val o2: Flowable> = Flowable.just(listOf(1, 2, 3)) 31 | 32 | val o3: Flowable = Flowable.defer { bufferedFlowable { s -> s.onNext(1) } } 33 | val o4: Flowable = Array(3) { 0 }.toFlowable() 34 | val o5: Flowable = IntArray(3).toFlowable() 35 | 36 | assertNotNull(o0) 37 | assertNotNull(o1) 38 | assertNotNull(o2) 39 | assertNotNull(o3) 40 | assertNotNull(o4) 41 | assertNotNull(o5) 42 | } 43 | 44 | @org.junit.Test fun testExampleFromReadme() { 45 | val result = bufferedFlowable { subscriber -> 46 | subscriber.onNext("H") 47 | subscriber.onNext("e") 48 | subscriber.onNext("l") 49 | subscriber.onNext("") 50 | subscriber.onNext("l") 51 | subscriber.onNext("o") 52 | subscriber.onComplete() 53 | }.filter(String::isNotEmpty). 54 | reduce(StringBuilder(), StringBuilder::append). 55 | map { it.toString() }. 56 | blockingGet() 57 | 58 | assertEquals("Hello", result) 59 | } 60 | 61 | @Test fun iteratorFlowable() { 62 | assertEquals(listOf(1, 2, 3), listOf(1, 2, 3).iterator().toFlowable().toList().blockingGet()) 63 | } 64 | 65 | @Test fun intProgressionStep1Empty() { 66 | assertEquals(listOf(1), (1..1).toFlowable().toList().blockingGet()) 67 | } 68 | 69 | @Test fun intProgressionStep1() { 70 | assertEquals((1..10).toList(), (1..10).toFlowable().toList().blockingGet()) 71 | } 72 | 73 | @Test fun intProgressionDownTo() { 74 | assertEquals((1 downTo 10).toList(), (1 downTo 10).toFlowable().toList().blockingGet()) 75 | } 76 | 77 | @Ignore("Too slow") 78 | @Test fun intProgressionOverflow() { 79 | assertEquals((0..10).toList().reversed(), (-10..Integer.MAX_VALUE).toFlowable().skip(Integer.MAX_VALUE.toLong()).map { Integer.MAX_VALUE - it }.toList().blockingGet()) 80 | } 81 | 82 | 83 | @Test fun testFold() { 84 | val result = listOf(1, 2, 3).toFlowable().reduce(0) { acc, e -> acc + e }.blockingGet() 85 | assertEquals(6, result) 86 | } 87 | 88 | @Test fun `kotlin sequence should produce expected items and flowable be able to handle em`() { 89 | generateSequence(0) { it + 1 }.toFlowable() 90 | .take(3) 91 | .toList() 92 | .test() 93 | .assertValues(listOf(0, 1, 2)) 94 | } 95 | 96 | @Test fun `infinite iterable should not hang or produce too many elements`() { 97 | val generated = AtomicInteger() 98 | generateSequence { generated.incrementAndGet() }.toFlowable(). 99 | take(100). 100 | toList(). 101 | subscribe() 102 | 103 | assertEquals(100, generated.get()) 104 | } 105 | 106 | @Test fun testFlatMapSequence() { 107 | assertEquals( 108 | listOf(1, 2, 3, 2, 3, 4, 3, 4, 5), 109 | listOf(1, 2, 3).toFlowable().flatMapSequence { listOf(it, it + 1, it + 2).asSequence() }.toList().blockingGet() 110 | ) 111 | } 112 | 113 | @Test fun testCombineLatest() { 114 | val list = listOf(1, 2, 3, 2, 3, 4, 3, 4, 5) 115 | assertEquals(list, list.map { Flowable.just(it) }.combineLatest { it }.blockingFirst()) 116 | } 117 | 118 | @Test fun testZip() { 119 | val list = listOf(1, 2, 3, 2, 3, 4, 3, 4, 5) 120 | assertEquals(list, list.map { Flowable.just(it) }.zip { it }.blockingFirst()) 121 | } 122 | 123 | @Test fun testCast() { 124 | val source = Flowable.just(1, 2) 125 | val flowable = source.cast() 126 | flowable.test() 127 | .await() 128 | .assertValues(1, 2) 129 | .assertNoErrors() 130 | .assertComplete() 131 | } 132 | 133 | @Test fun testCastWithWrongType() { 134 | val source = Flowable.just(1, 2) 135 | val flowable = source.cast() 136 | flowable.test() 137 | .assertError(ClassCastException::class.java) 138 | } 139 | 140 | @Test fun combineLatestPair() { 141 | Flowable.just(3) 142 | .combineLatest(Flowable.just(10)) 143 | .map { (x, y) -> x * y } 144 | .test() 145 | .assertValues(30) 146 | } 147 | 148 | @Test fun combineLatestTriple() { 149 | Flowable.just(3) 150 | .combineLatest(Flowable.just(10), Flowable.just(20)) 151 | .map { (x, y, z) -> x * y * z } 152 | .test() 153 | .assertValues(600) 154 | } 155 | @Test 156 | fun testSubscribeBy() { 157 | val first = AtomicReference() 158 | 159 | Flowable.just("Alpha") 160 | .subscribeBy { 161 | first.set(it) 162 | } 163 | Assert.assertTrue(first.get() == "Alpha") 164 | } 165 | 166 | @Test 167 | fun testSubscribeByErrorIntrospection() { 168 | val disposable = Flowable.just(Unit) 169 | .subscribeBy() as LambdaConsumerIntrospection 170 | Assert.assertFalse(disposable.hasCustomOnError()) 171 | } 172 | 173 | @Test 174 | fun testSubscribeByErrorIntrospectionCustom() { 175 | val disposable = Flowable.just(Unit) 176 | .subscribeBy(onError = {}) as LambdaConsumerIntrospection 177 | Assert.assertTrue(disposable.hasCustomOnError()) 178 | } 179 | 180 | @Test 181 | fun testSubscribeByErrorIntrospectionDefaultWithOnComplete() { 182 | val disposable = Flowable.just(Unit) 183 | .subscribeBy(onComplete = {}) as LambdaConsumerIntrospection 184 | Assert.assertFalse(disposable.hasCustomOnError()) 185 | } 186 | 187 | @Test 188 | fun testBlockingSubscribeBy() { 189 | val first = AtomicReference() 190 | 191 | Flowable.just("Alpha") 192 | .blockingSubscribeBy { 193 | first.set(it) 194 | } 195 | Assert.assertTrue(first.get() == "Alpha") 196 | } 197 | 198 | @Test 199 | fun testPairZip() { 200 | 201 | val testSubscriber = TestSubscriber>() 202 | 203 | Flowables.zip( 204 | Flowable.just("Alpha", "Beta", "Gamma"), 205 | Flowable.range(1,4) 206 | ).subscribe(testSubscriber) 207 | 208 | testSubscriber.assertValues(Pair("Alpha",1), Pair("Beta",2), Pair("Gamma",3)) 209 | } 210 | 211 | @Test 212 | fun testTripleZip() { 213 | 214 | val testSubscriber = TestSubscriber>() 215 | 216 | Flowables.zip( 217 | Flowable.just("Alpha", "Beta", "Gamma"), 218 | Flowable.range(1,4), 219 | Flowable.just(100,200,300) 220 | ).subscribe(testSubscriber) 221 | 222 | testSubscriber.assertValues(Triple("Alpha",1, 100), Triple("Beta",2, 200), Triple("Gamma",3, 300)) 223 | } 224 | 225 | @Test fun testConcatAll() { 226 | (0 until 10) 227 | .map { Flowable.just(it) } 228 | .concatAll() 229 | .toList() 230 | .subscribe { result -> 231 | Assert.assertEquals((0 until 10).toList(), result) 232 | } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/KotlinTests.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Netflix, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.reactivex.rxjava3.kotlin 18 | 19 | import org.junit.Before 20 | import org.mockito.Mock 21 | import org.mockito.MockitoAnnotations 22 | 23 | abstract class KotlinTests { 24 | @Mock var a: ScriptAssertion = uninitialized() 25 | 26 | @Before fun before() { 27 | MockitoAnnotations.initMocks(this) 28 | } 29 | 30 | @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") 31 | fun received() = { result: T -> a.received(result) } 32 | 33 | interface ScriptAssertion { 34 | fun error(e: Throwable?) 35 | 36 | fun received(e: Any?) 37 | } 38 | 39 | private fun uninitialized() = null as T 40 | } 41 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/MaybeTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Maybe 4 | import io.reactivex.rxjava3.core.Single 5 | import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection 6 | import org.junit.Assert 7 | import org.junit.Test 8 | import java.util.concurrent.atomic.AtomicReference 9 | 10 | class MaybeTest { 11 | @Test 12 | fun testSubscribeBy() { 13 | val first = AtomicReference() 14 | 15 | Maybe.just("Alpha") 16 | .subscribeBy { 17 | first.set(it) 18 | } 19 | Assert.assertTrue(first.get() == "Alpha") 20 | } 21 | 22 | @Test 23 | fun testSubscribeByErrorIntrospection() { 24 | val disposable = Single.just(Unit) 25 | .subscribeBy() as LambdaConsumerIntrospection 26 | Assert.assertFalse(disposable.hasCustomOnError()) 27 | } 28 | 29 | @Test 30 | fun testSubscribeByErrorIntrospectionCustom() { 31 | val disposable = Single.just(Unit) 32 | .subscribeBy(onError = {}) as LambdaConsumerIntrospection 33 | Assert.assertTrue(disposable.hasCustomOnError()) 34 | } 35 | 36 | @Test 37 | fun testBlockingSubscribeBy() { 38 | val first = AtomicReference() 39 | 40 | Maybe.just("Alpha") 41 | .blockingSubscribeBy { 42 | first.set(it) 43 | } 44 | Assert.assertTrue(first.get() == "Alpha") 45 | } 46 | 47 | @Test fun testConcatAll() { 48 | (0 until 10) 49 | .map { Maybe.just(it) } 50 | .concatAll() 51 | .toList() 52 | .subscribe { result -> 53 | Assert.assertEquals((0 until 10).toList(), result) 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/ObservableTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection 5 | import io.reactivex.rxjava3.observers.TestObserver 6 | import org.junit.Assert 7 | import org.junit.Assert.* 8 | import org.junit.Ignore 9 | import org.junit.Test 10 | import java.math.BigDecimal 11 | import java.util.concurrent.atomic.AtomicInteger 12 | import java.util.concurrent.atomic.AtomicReference 13 | 14 | class ObservableTest { 15 | 16 | @Test fun testCreation() { 17 | val observable = Observable.create { s -> 18 | s.apply { 19 | onNext(1) 20 | onNext(777) 21 | onComplete() 22 | } 23 | } 24 | 25 | assertEquals(listOf(1, 777), observable.toList().blockingGet()) 26 | 27 | val o0: Observable = Observable.empty() 28 | val o1: Observable = listOf(1, 2, 3).toObservable() 29 | val o2: Observable> = Observable.just(listOf(1, 2, 3)) 30 | 31 | val o3: Observable = Observable.defer { Observable.create { s -> s.onNext(1) } } 32 | val o4: Observable = Array(3) { 0 }.toObservable() 33 | val o5: Observable = IntArray(3).toObservable() 34 | 35 | assertNotNull(o0) 36 | assertNotNull(o1) 37 | assertNotNull(o2) 38 | assertNotNull(o3) 39 | assertNotNull(o4) 40 | assertNotNull(o5) 41 | } 42 | 43 | @Test fun testExampleFromReadme() { 44 | val observable = Observable.create { s -> 45 | s.apply { 46 | onNext("H") 47 | onNext("e") 48 | onNext("l") 49 | onNext("") 50 | onNext("l") 51 | onNext("o") 52 | onComplete() 53 | } 54 | } 55 | val result = observable 56 | .filter(String::isNotEmpty) 57 | .reduce(StringBuilder(), StringBuilder::append) 58 | .map { it.toString() } 59 | .blockingGet() 60 | 61 | assertEquals("Hello", result) 62 | } 63 | 64 | @Test fun iteratorObservable() { 65 | assertEquals(listOf(1, 2, 3), listOf(1, 2, 3).iterator().toObservable().toList().blockingGet()) 66 | } 67 | 68 | @Test fun intProgressionStep1Empty() { 69 | assertEquals(listOf(1), (1..1).toObservable().toList().blockingGet()) 70 | } 71 | 72 | @Test fun intProgressionStep1() { 73 | assertEquals((1..10).toList(), (1..10).toObservable().toList().blockingGet()) 74 | } 75 | 76 | @Test fun intProgressionDownTo() { 77 | assertEquals((1 downTo 10).toList(), (1 downTo 10).toObservable().toList().blockingGet()) 78 | } 79 | 80 | @Ignore("Too slow") 81 | @Test fun intProgressionOverflow() { 82 | val result = (-10..Integer.MAX_VALUE).toObservable() 83 | .skip(Integer.MAX_VALUE.toLong()) 84 | .map { Integer.MAX_VALUE - it } 85 | .toList() 86 | .blockingGet() 87 | assertEquals((0..10).toList().reversed(), result) 88 | } 89 | 90 | @Test fun testReduce() { 91 | val result = listOf(1, 2, 3).toObservable().reduce(0) { acc, e -> acc + e }.blockingGet() 92 | assertEquals(6, result) 93 | } 94 | 95 | @Test fun `kotlin sequence should produce expected items and observable be able to handle em`() { 96 | generateSequence(0) { it + 1 }.toObservable() 97 | .take(3) 98 | .toList() 99 | .test() 100 | .assertValues(listOf(0, 1, 2)) 101 | } 102 | 103 | @Test fun `infinite iterable should not hang or produce too many elements`() { 104 | val generated = AtomicInteger() 105 | generateSequence { generated.incrementAndGet() }.toObservable(). 106 | take(100). 107 | toList(). 108 | subscribe() 109 | 110 | assertEquals(100, generated.get()) 111 | } 112 | 113 | @Test fun testFlatMapSequence() { 114 | assertEquals( 115 | listOf(1, 2, 3, 2, 3, 4, 3, 4, 5), 116 | listOf(1, 2, 3).toObservable().flatMapSequence { listOf(it, it + 1, it + 2).asSequence() }.toList().blockingGet() 117 | ) 118 | } 119 | 120 | @Test fun testFlatMapIterable() { 121 | assertEquals( 122 | listOf(1, 2, 3), 123 | Observable.just(listOf(1, 2, 3)).flatMapIterable().toList().blockingGet() 124 | ) 125 | } 126 | 127 | @Test fun testConcatMapIterable() { 128 | assertEquals( 129 | listOf(1, 2, 3, 4), 130 | Observable.just(listOf(1, 2, 3) , listOf(4)).concatMapIterable().toList().blockingGet() 131 | ) 132 | } 133 | 134 | @Test fun testCombineLatest() { 135 | val list = listOf(1, 2, 3, 2, 3, 4, 3, 4, 5) 136 | assertEquals(list, list.map { Observable.just(it) }.combineLatest { it }.blockingFirst()) 137 | } 138 | 139 | @Test fun testZip() { 140 | val list = listOf(1, 2, 3, 2, 3, 4, 3, 4, 5) 141 | assertEquals(list, list.map { Observable.just(it) }.zip { it }.blockingFirst()) 142 | } 143 | 144 | @Test fun testCast() { 145 | val source = Observable.just(1, 2) 146 | val observable = source.cast() 147 | observable.test() 148 | .await() 149 | .assertValues(1, 2) 150 | .assertNoErrors() 151 | .assertComplete() 152 | } 153 | 154 | @Test fun testCastWithWrongType() { 155 | val source = Observable.just(1, 2) 156 | val observable = source.cast() 157 | observable.test() 158 | .assertError(ClassCastException::class.java) 159 | } 160 | 161 | @Test fun testOfType() { 162 | val source = Observable.just(BigDecimal.valueOf(15, 1), 2, BigDecimal.valueOf(42), 15) 163 | 164 | source.ofType() 165 | .test() 166 | .await() 167 | .assertValues(2, 15) 168 | .assertNoErrors() 169 | .assertComplete() 170 | 171 | source.ofType() 172 | .test() 173 | .await() 174 | .assertValues(BigDecimal.valueOf(15, 1), BigDecimal.valueOf(42)) 175 | .assertNoErrors() 176 | .assertComplete() 177 | 178 | source.ofType() 179 | .test() 180 | .await() 181 | .assertNoValues() 182 | .assertNoErrors() 183 | .assertComplete() 184 | 185 | source.ofType>() 186 | .test() 187 | .await() 188 | .assertValues(BigDecimal.valueOf(15, 1), 2, BigDecimal.valueOf(42), 15) 189 | .assertNoErrors() 190 | .assertComplete() 191 | } 192 | 193 | @Test 194 | fun testSubscribeBy() { 195 | val first = AtomicReference() 196 | 197 | Observable.just("Alpha") 198 | .subscribeBy { 199 | first.set(it) 200 | } 201 | assertTrue(first.get() == "Alpha") 202 | } 203 | 204 | @Test 205 | fun testSubscribeByErrorIntrospection() { 206 | val disposable = Observable.just(Unit) 207 | .subscribeBy() as LambdaConsumerIntrospection 208 | assertFalse(disposable.hasCustomOnError()) 209 | } 210 | 211 | @Test 212 | fun testSubscribeByErrorIntrospectionCustom() { 213 | val disposable = Observable.just(Unit) 214 | .subscribeBy(onError = {}) as LambdaConsumerIntrospection 215 | assertTrue(disposable.hasCustomOnError()) 216 | } 217 | 218 | @Test 219 | @Ignore("Fix with adding support for LambdaConsumerIntrospection - #151") 220 | fun testSubscribeByErrorIntrospectionDefaultWithOnComplete() { 221 | val disposable = Observable.just(Unit) 222 | .subscribeBy(onComplete = {}) as LambdaConsumerIntrospection 223 | assertFalse(disposable.hasCustomOnError()) 224 | } 225 | 226 | @Test 227 | fun testBlockingSubscribeBy() { 228 | val first = AtomicReference() 229 | 230 | Observable.just("Alpha") 231 | .blockingSubscribeBy { 232 | first.set(it) 233 | } 234 | assertTrue(first.get() == "Alpha") 235 | } 236 | 237 | @Test 238 | fun testPairZip() { 239 | 240 | val testObserver = TestObserver>() 241 | 242 | Observables.zip( 243 | Observable.just("Alpha", "Beta", "Gamma"), 244 | Observable.range(1,4) 245 | ).subscribe(testObserver) 246 | 247 | testObserver.assertValues(Pair("Alpha",1), Pair("Beta",2), Pair("Gamma",3)) 248 | } 249 | 250 | @Test 251 | fun testTripleZip() { 252 | 253 | val testObserver = TestObserver>() 254 | 255 | Observables.zip( 256 | Observable.just("Alpha", "Beta", "Gamma"), 257 | Observable.range(1,4), 258 | Observable.just(100,200,300) 259 | ).subscribe(testObserver) 260 | 261 | testObserver.assertValues(Triple("Alpha",1, 100), Triple("Beta",2, 200), Triple("Gamma",3, 300)) 262 | } 263 | 264 | @Test fun testConcatAll() { 265 | var counter = 0 266 | (0 until 10) 267 | .map { Observable.just(counter++, counter++, counter++) } 268 | .concatAll() 269 | .toList() 270 | .subscribe { result -> 271 | Assert.assertEquals((0 until 30).toList(), result) 272 | } 273 | } 274 | } 275 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/ObservablesTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import org.junit.Assert.assertEquals 5 | import org.junit.Test 6 | 7 | 8 | class ObservablesTest { 9 | @Test fun testCombineLatestEmitPair() { 10 | val pair = Pair(1, "a") 11 | val result = Observables.combineLatest( 12 | Observable.just(pair.first), 13 | Observable.just(pair.second) 14 | ).blockingFirst() 15 | 16 | assertEquals(pair, result) 17 | } 18 | 19 | @Test fun testCombineLatestEmitTriple() { 20 | val triple = Triple(1, "a", 1.0) 21 | val result = Observables.combineLatest( 22 | Observable.just(triple.first), 23 | Observable.just(triple.second), 24 | Observable.just(triple.third) 25 | ).blockingFirst() 26 | 27 | assertEquals(triple, result) 28 | } 29 | 30 | @Test fun testWithLatestFromEmitPair() { 31 | val pair = Pair(1, "a") 32 | val result = Observable.just(pair.first) 33 | .withLatestFrom(Observable.just(pair.second)) 34 | .blockingFirst() 35 | 36 | assertEquals(pair, result) 37 | } 38 | 39 | @Test fun testWithLatestFromEmitTriple(){ 40 | val triple = Triple(1, "a", 1.0) 41 | val result = Observable.just(triple.first) 42 | .withLatestFrom(Observable.just(triple.second), Observable.just(triple.third)) 43 | .blockingFirst() 44 | 45 | assertEquals(triple, result) 46 | } 47 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/SingleTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Single 4 | import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection 5 | import org.junit.Assert 6 | import org.junit.Test 7 | import org.mockito.Mockito 8 | import org.mockito.Mockito.verify 9 | 10 | class SingleTest : KotlinTests() { 11 | @Test fun testCreate() { 12 | Single.create { s -> 13 | s.onSuccess("Hello World!") 14 | }.subscribe { result -> 15 | a.received(result) 16 | } 17 | verify(a, Mockito.times(1)).received("Hello World!") 18 | } 19 | 20 | @Test 21 | fun testSubscribeBy() { 22 | Single.just("Alpha") 23 | .subscribeBy { 24 | a.received(it) 25 | } 26 | verify(a, Mockito.times(1)) 27 | .received("Alpha") 28 | } 29 | 30 | @Test 31 | fun testSubscribeByErrorIntrospection() { 32 | val disposable = Single.just(Unit) 33 | .subscribeBy() as LambdaConsumerIntrospection 34 | Assert.assertFalse(disposable.hasCustomOnError()) 35 | } 36 | 37 | @Test 38 | fun testSubscribeByErrorIntrospectionCustom() { 39 | val disposable = Single.just(Unit) 40 | .subscribeBy(onError = {}) as LambdaConsumerIntrospection 41 | Assert.assertTrue(disposable.hasCustomOnError()) 42 | } 43 | 44 | @Test 45 | fun testBlockingSubscribeBy() { 46 | Single.just("Alpha") 47 | .blockingSubscribeBy { 48 | a.received(it) 49 | } 50 | verify(a, Mockito.times(1)) 51 | .received("Alpha") 52 | } 53 | 54 | @Test fun testConcatAll() { 55 | (0 until 10) 56 | .map { Single.just(it) } 57 | .concatAll() 58 | .toList() 59 | .subscribe { result -> 60 | Assert.assertEquals((0 until 10).toList(), result) 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/SinglesTest.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Single 4 | import io.reactivex.rxjava3.core.SingleSource 5 | import org.junit.Test 6 | 7 | class SinglesTest : KotlinTests() { 8 | 9 | @Test fun testParameterOrder() { 10 | Singles.zip( 11 | SingleSourceInt(1), SingleSourceInt(2), 12 | {one, two -> 13 | assert(one == 1, { -> "Should equal one"}) 14 | assert(two == 2, { -> "Should equal two"}) 15 | }).blockingGet() 16 | 17 | Singles.zip( 18 | SingleSourceInt(1), SingleSourceInt(2), 19 | SingleSourceInt(3), 20 | {one, two, three -> 21 | assert(one == 1, { -> "Should equal one"}) 22 | assert(two == 2, { -> "Should equal two"}) 23 | assert(three == 3, { -> "Should equal three"}) 24 | }).blockingGet() 25 | 26 | Singles.zip( 27 | SingleSourceInt(1), SingleSourceInt(2), 28 | SingleSourceInt(3), SingleSourceInt(4), 29 | {one, two, three, four -> 30 | assert(one == 1, { -> "Should equal one"}) 31 | assert(two == 2, { -> "Should equal two"}) 32 | assert(three == 3, { -> "Should equal three"}) 33 | assert(four == 4, { -> "Should equal four"}) 34 | }).blockingGet() 35 | 36 | Singles.zip( 37 | SingleSourceInt(1), SingleSourceInt(2), 38 | SingleSourceInt(3), SingleSourceInt(4), 39 | SingleSourceInt(5), 40 | {one, two, three, four, five -> 41 | assert(one == 1, { -> "Should equal one"}) 42 | assert(two == 2, { -> "Should equal two"}) 43 | assert(three == 3, { -> "Should equal three"}) 44 | assert(four == 4, { -> "Should equal four"}) 45 | assert(five == 5, { -> "Should equal five"}) 46 | }).blockingGet() 47 | 48 | Singles.zip( 49 | SingleSourceInt(1), SingleSourceInt(2), 50 | SingleSourceInt(3), SingleSourceInt(4), 51 | SingleSourceInt(5), SingleSourceInt(6), 52 | {one, two, three, four, five, six -> 53 | assert(one == 1, { -> "Should equal one"}) 54 | assert(two == 2, { -> "Should equal two"}) 55 | assert(three == 3, { -> "Should equal three"}) 56 | assert(four == 4, { -> "Should equal four"}) 57 | assert(five == 5, { -> "Should equal five"}) 58 | assert(six == 6, { -> "Should equal six"}) 59 | }).blockingGet() 60 | 61 | Singles.zip( 62 | SingleSourceInt(1), SingleSourceInt(2), 63 | SingleSourceInt(3), SingleSourceInt(4), 64 | SingleSourceInt(5), SingleSourceInt(6), 65 | SingleSourceInt(7), 66 | {one, two, three, four, five, six, seven -> 67 | assert(one == 1, { -> "Should equal one"}) 68 | assert(two == 2, { -> "Should equal two"}) 69 | assert(three == 3, { -> "Should equal three"}) 70 | assert(four == 4, { -> "Should equal four"}) 71 | assert(five == 5, { -> "Should equal five"}) 72 | assert(six == 6, { -> "Should equal six"}) 73 | assert(seven == 7, { -> "Should equal seven"}) 74 | }).blockingGet() 75 | 76 | Singles.zip( 77 | SingleSourceInt(1), SingleSourceInt(2), 78 | SingleSourceInt(3), SingleSourceInt(4), 79 | SingleSourceInt(5), SingleSourceInt(6), 80 | SingleSourceInt(7), SingleSourceInt(8), 81 | {one, two, three, four, five, six, seven, eight -> 82 | assert(one == 1, { -> "Should equal one"}) 83 | assert(two == 2, { -> "Should equal two"}) 84 | assert(three == 3, { -> "Should equal three"}) 85 | assert(four == 4, { -> "Should equal four"}) 86 | assert(five == 5, { -> "Should equal five"}) 87 | assert(six == 6, { -> "Should equal six"}) 88 | assert(seven == 7, { -> "Should equal seven"}) 89 | assert(eight == 8, { -> "Should equal eight"}) 90 | }).blockingGet() 91 | 92 | Singles.zip( 93 | SingleSourceInt(1), SingleSourceInt(2), 94 | SingleSourceInt(3), SingleSourceInt(4), 95 | SingleSourceInt(5), SingleSourceInt(6), 96 | SingleSourceInt(7), SingleSourceInt(8), 97 | SingleSourceInt(9), 98 | {one, two, three, four, five, six, seven, eight, nine -> 99 | assert(one == 1, { -> "Should equal one"}) 100 | assert(two == 2, { -> "Should equal two"}) 101 | assert(three == 3, { -> "Should equal three"}) 102 | assert(four == 4, { -> "Should equal four"}) 103 | assert(five == 5, { -> "Should equal five"}) 104 | assert(six == 6, { -> "Should equal six"}) 105 | assert(seven == 7, { -> "Should equal seven"}) 106 | assert(eight == 8, { -> "Should equal eight"}) 107 | assert(nine == 9, { -> "Should equal nine"}) 108 | }).blockingGet() 109 | } 110 | } 111 | 112 | fun SingleSourceInt(i: Int): SingleSource { 113 | return Single.create({ s -> s.onSuccess(i)}) 114 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/reactivex/rxjava3/kotlin/SubscriptionTests.kt: -------------------------------------------------------------------------------- 1 | package io.reactivex.rxjava3.kotlin 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import io.reactivex.rxjava3.disposables.CompositeDisposable 5 | import org.junit.Test 6 | import java.util.concurrent.TimeUnit 7 | 8 | class SubscriptionTest { 9 | @Test fun testSubscriptionAddTo() { 10 | val compositeSubscription = CompositeDisposable() 11 | 12 | // Create an asynchronous subscription 13 | // The delay ensures that we don't automatically unsubscribe because data finished emitting 14 | val subscription = Observable.just("test") 15 | .delay(100, TimeUnit.MILLISECONDS) 16 | .subscribe() 17 | 18 | assert(!subscription.isDisposed) 19 | 20 | subscription.addTo(compositeSubscription) 21 | 22 | assert(compositeSubscription.size() > 0) 23 | assert(!subscription.isDisposed) 24 | 25 | compositeSubscription.dispose() 26 | 27 | assert(compositeSubscription.isDisposed) 28 | } 29 | } --------------------------------------------------------------------------------