forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveX#438 from MarioAriasC/master
Kotlin Language Adaptor
- Loading branch information
Showing
6 changed files
with
837 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Kotlin Adaptor for RxJava | ||
|
||
Kotlin has support for SAM (Single Abstract Method) Interfaces as Functions (i.e. Java 8 Lambdas). So you could use Kotlin in RxJava whitout this adaptor | ||
|
||
```kotlin | ||
Observable.create<String>{ observer -> | ||
observer!!.onNext("Hello") | ||
observer.onCompleted() | ||
Subscriptions.empty() | ||
}!!.subscribe { result -> | ||
a!!.received(result) | ||
} | ||
``` | ||
|
||
This adaptor exposes a set of Extension functions that allow a more idiomatic Kotlin usage | ||
|
||
```kotlin | ||
import rx.lang.kotlin.* | ||
|
||
{(observer: Observer<in String>) -> | ||
observer.onNext("Hello") | ||
observer.onCompleted() | ||
Subscriptions.empty()!! | ||
}.asObservable().subscribe { result -> | ||
a!!.received(result) | ||
} | ||
``` | ||
|
||
## Binaries | ||
|
||
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%7Ca%3A%22rxjava-kotlin%22). | ||
|
||
Example for Maven: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.netflix.rxjava</groupId> | ||
<artifactId>rxjava-kotlin</artifactId> | ||
<version>x.y.z</version> | ||
</dependency> | ||
``` | ||
|
||
and for Ivy: | ||
|
||
```xml | ||
<dependency org="com.netflix.rxjava" name="rxjava-kotlin" rev="x.y.z" /> | ||
``` | ||
|
||
and for Gradle: | ||
|
||
```groovy | ||
compile 'com.netflix.rxjava:rxjava-kotlin:x.y.z' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
buildscript { | ||
repositories() { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.800' | ||
} | ||
} | ||
|
||
apply plugin: 'kotlin' | ||
apply plugin: 'osgi' | ||
|
||
dependencies { | ||
compile project(':rxjava-core') | ||
compile 'org.jetbrains.kotlin:kotlin-stdlib:0.6.800' | ||
provided 'junit:junit-dep:4.10' | ||
provided 'org.mockito:mockito-core:1.8.5' | ||
} | ||
|
||
jar { | ||
manifest { | ||
name = 'rxjava-kotlin' | ||
instruction 'Bundle-Vendor', 'Netflix' | ||
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava' | ||
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*' | ||
instruction 'Fragment-Host', 'com.netflix.rxjava.core' | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
language-adaptors/rxjava-kotlin/src/main/kotlin/rx/lang/kotlin/namespace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package rx.lang.kotlin | ||
|
||
import rx.Subscription | ||
import rx.Observer | ||
import rx.Observable | ||
|
||
public fun<T> Function1<Observer<in T>, Subscription>.asObservable(): Observable<T> { | ||
return Observable.create { this(it!!) }!! | ||
} | ||
|
||
public fun<T> Function0<Observable<out T>>.defer(): Observable<T> { | ||
return Observable.defer(this)!! | ||
} | ||
|
||
public fun<T> Iterable<T>.asObservable(): Observable<T> { | ||
return Observable.from(this)!! | ||
} | ||
|
||
public fun<T> T.asObservable(): Observable<T> { | ||
return Observable.from(this)!! | ||
} | ||
|
||
public fun<T> Throwable.asObservable(): Observable<T> { | ||
return Observable.error(this)!! | ||
} | ||
|
||
public fun<T> Pair<T, T>.asObservable(): Observable<T> { | ||
return Observable.from(this.component1(), this.component2())!! | ||
} | ||
|
||
public fun<T> Triple<T, T, T>.asObservable(): Observable<T> { | ||
return Observable.from(this.component1(), this.component2(), this.component3())!! | ||
} | ||
|
||
public fun<T> Pair<Observable<T>, Observable<T>>.merge(): Observable<T> { | ||
return Observable.merge(this.component1(), this.component2())!! | ||
} | ||
|
||
public fun<T> Triple<Observable<T>, Observable<T>, Observable<T>>.merge(): Observable<T> { | ||
return Observable.merge(this.component1(), this.component2(), this.component3())!! | ||
} | ||
|
||
public fun<T> Pair<Observable<T>, Observable<T>>.mergeDelayError(): Observable<T> { | ||
return Observable.mergeDelayError(this.component1(), this.component2())!! | ||
} | ||
|
||
public fun<T> Triple<Observable<T>, Observable<T>, Observable<T>>.mergeDelayError(): Observable<T> { | ||
return Observable.mergeDelayError(this.component1(), this.component2(), this.component3())!! | ||
} |
Oops, something went wrong.