New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposing Observable.subscribeWith { ... } #17
Conversation
Travis has failed because "The command "eval git clone --depth=50 git://github.com/ReactiveX/RxKotlin.git ReactiveX/RxKotlin" failed 3 times." but this PR does pass all unit tests |
You have to note that you generally don't need to create subscriber in this case. You can just do observable.onError {.... } .subscribe { ... } So you need this builder if you need to make subscriber and use it later and subscribe dynamically. In this case you will need to specify type anyway. |
Unfortunately not. That was indeed my first try, but when using The correct syntax would be: observable
.onErrorResumeNext {
it.printStackTrace()
/* Handle the error */
emptyObservable()
}
.subscribe {
/* Handle next value */
} That is why I propose the syntax exposed in this PR ;) |
With pull #16 subscribeWith could be implemented simply like this public inline fun <T> Observable<T>.subscribeWith( body : FunctionSubscriber<T>.() -> FunctionSubscriber<T>) : Subscription {
return subscribe(subscriber<T>().body())
} |
@cy6erGn0m I don't think so since, even in #16, the setter function do not set the value of the current subscriber but create a new copied subscriber with the updated function |
@SalomonBrys I see, you will need dots in this case |
#16 was merged. You need a rebase |
Done ;) (I did a merge, not a rebase) |
Hi there. Any news on this PR? |
@cy6erGn0m any other comments on this PR? A 👍 from me |
I think we can go with it. Lets merge it and release then |
Proposing Observable.subscribeWith { ... }
I find the current subscriber syntax cumbersome. Here's an example:
The problem I have is with
subscriber<String>()
. Because it creates a subscriber that is later modified with.onNext
and.onError
, the Kotlin compiler cannot infer it's type.For
subscriber<String>
, this is not such an issue, but forsubscriber<Map<String, User.Status>>
this can quickly become cumbersome.Beside, we could better use Kotlin's amazing type inference system and use a more "Kotlin-esque" way.
This PR introduces a new way of subscribing to an observable with this proposed API:
myObservable.subscribeWith { onNext { println(it) } onError { it.printStackTrace() } }
The API is using Kotlin's type-safe builder pattern.
Let me know what you think ;)