Last active
December 17, 2015 04:39
-
-
Save MarioAriasC/5552266 to your computer and use it in GitHub Desktop.
Creating a rx.Observable from a function
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
import rx.Observer | |
import rx.Subscription | |
import rx.Observable | |
import rx.util.functions.* | |
fun<T> Function1<Observer<T>, Subscription>.asObservable(): Observable<T> { | |
return Observable.create(Func1<Observer<T>, Subscription>{ | |
this(it!!) | |
})!! | |
} | |
private fun customObservableNonBlocking(): Observable<String> { | |
return {(observer: Observer<String>) -> | |
val t = Thread(Runnable{ | |
for(i in 0..75){ | |
observer.onNext("anotherValue_$i") | |
} | |
observer.onCompleted() | |
}) | |
t.start() | |
Subscription{ | |
t.interrupt() | |
} | |
}.asObservable() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment