I am stuck at this problem, which should be fairly simple. I need subscriber to execute a code block when the Maybe
has completed as an Empty Maybe
. I found that
Maybe.isEmpty.blockingGet()
, but it is dirty too.I have tried following (Kotlin Syntax):-
fun <T> Maybe<T>.subscribeWithEmptyHandler(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onEmpty: () -> Unit) {
this.isEmpty.subscribe({ if (it) onEmpty() }, { onError(it) })
this.subscribe({ onSuccess(it) }, { onError(it) })
}
But as expected it is running subscription twice, tested here:-
Maybe.create<Int> {
println("subscribing")
//Remove line below to create Empty Maybe
it.onSuccess(5)
it.onComplete()
}
.subscribeWithEmptyHandler({println("success")},{println("error")},{println("empty")})
Could somebody please suggest neater way to solve this?
Use Maybe.doOnEvent
(java example):
Maybe
.empty()
.doOnEvent((value, error)-> {
if (value==null && error == null) {
System.out.println("empty!");
}})
.subscribe();