“How to Do It in RxJava” Series - Part 1
If the event eventToCheck
is present, skip the current event event
and wait for the trigger event eventToContinue
; if the event is not present, then continue execution:
/**
* Use with flatMap(...)
*
* @param skipTrigger skips first event if any event popped from skipTrigger
* @param continueTrigger continue event passing if any event popped from continueTrigger
* @param <T> type of passing event
* @return Observable, put it inside flatMap operator
*/
public static <T> Function<T, ObservableSource<? extends T>> skipFirstIfTriggerAndContinue(
final Observable<?> skipTrigger,
final ObservableSource<?> continueTrigger) {
return t -> Observable.merge(
Observable.just(t).takeUntil(skipTrigger.take(1)),
skipTrigger
.take(1)
.zipWith(continueTrigger, (e1, e2) -> t)
).take(1);
}
Now how to use it:
Observable.just(event)
.flatMap(skipFirstIfTriggerAndContinue(
Observable.just(eventToCheck),
Observable.just(eventToContinue)
))
.doOnNext(/*event passed...*/e->Log.i("hello", "event passed:"+event)