-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify Observable/Single implementations
- Loading branch information
Showing
5 changed files
with
74 additions
and
86 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
rxlifecycle/src/main/java/com/trello/rxlifecycle/TakeUntilGenerator.java
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,62 @@ | ||
package com.trello.rxlifecycle; | ||
|
||
import android.support.annotation.CheckResult; | ||
import android.support.annotation.NonNull; | ||
import rx.Observable; | ||
import rx.exceptions.Exceptions; | ||
import rx.functions.Func1; | ||
import rx.functions.Func2; | ||
|
||
final class TakeUntilGenerator { | ||
|
||
@NonNull | ||
@CheckResult | ||
static <T> Observable<T> takeUntilEvent(@NonNull final Observable<T> lifecycle, @NonNull final T event) { | ||
return lifecycle.takeFirst(new Func1<T, Boolean>() { | ||
@Override | ||
public Boolean call(T lifecycleEvent) { | ||
return lifecycleEvent.equals(event); | ||
} | ||
}); | ||
} | ||
|
||
@NonNull | ||
@CheckResult | ||
static <T> Observable<Boolean> takeUntilCorrespondingEvent(@NonNull final Observable<T> lifecycle, | ||
@NonNull final Func1<T, T> correspondingEvents) { | ||
return Observable.combineLatest( | ||
lifecycle.take(1).map(correspondingEvents), | ||
lifecycle.skip(1), | ||
new Func2<T, T, Boolean>() { | ||
@Override | ||
public Boolean call(T bindUntilEvent, T lifecycleEvent) { | ||
return lifecycleEvent.equals(bindUntilEvent); | ||
} | ||
}) | ||
.onErrorReturn(RESUME_FUNCTION) | ||
.takeFirst(SHOULD_COMPLETE); | ||
} | ||
|
||
private static final Func1<Throwable, Boolean> RESUME_FUNCTION = new Func1<Throwable, Boolean>() { | ||
@Override | ||
public Boolean call(Throwable throwable) { | ||
if (throwable instanceof OutsideLifecycleException) { | ||
return true; | ||
} | ||
|
||
Exceptions.propagate(throwable); | ||
return false; | ||
} | ||
}; | ||
|
||
private static final Func1<Boolean, Boolean> SHOULD_COMPLETE = new Func1<Boolean, Boolean>() { | ||
@Override | ||
public Boolean call(Boolean shouldComplete) { | ||
return shouldComplete; | ||
} | ||
}; | ||
|
||
private TakeUntilGenerator() { | ||
throw new AssertionError("No instances!"); | ||
} | ||
} |
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
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
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
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