-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from trello/dlew/completable-support
Add Completable support
- Loading branch information
Showing
13 changed files
with
446 additions
and
22 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
rxlifecycle/src/main/java/com/trello/rxlifecycle/Functions.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,40 @@ | ||
package com.trello.rxlifecycle; | ||
|
||
import rx.Observable; | ||
import rx.exceptions.Exceptions; | ||
import rx.functions.Func1; | ||
|
||
import java.util.concurrent.CancellationException; | ||
|
||
final class Functions { | ||
|
||
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; | ||
} | ||
}; | ||
|
||
static final Func1<Boolean, Boolean> SHOULD_COMPLETE = new Func1<Boolean, Boolean>() { | ||
@Override | ||
public Boolean call(Boolean shouldComplete) { | ||
return shouldComplete; | ||
} | ||
}; | ||
|
||
static final Func1<Object, Observable<Object>> CANCEL_COMPLETABLE = new Func1<Object, Observable<Object>>() { | ||
@Override | ||
public Observable<Object> call(Object ignore) { | ||
return Observable.error(new CancellationException()); | ||
} | ||
}; | ||
|
||
private Functions() { | ||
throw new AssertionError("No instances!"); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleTransformer.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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package com.trello.rxlifecycle; | ||
|
||
import rx.Completable; | ||
import rx.Observable; | ||
import rx.Single; | ||
|
||
public interface LifecycleTransformer<T> extends Observable.Transformer<T, T> { | ||
|
||
Single.Transformer<T, T> forSingle(); | ||
|
||
Completable.CompletableTransformer forCompletable(); | ||
|
||
} |
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
62 changes: 62 additions & 0 deletions
62
...e/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventCompletableTransformer.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.NonNull; | ||
import rx.Completable; | ||
import rx.Observable; | ||
import rx.functions.Func1; | ||
|
||
import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilCorrespondingEvent; | ||
|
||
/** | ||
* Continues a subscription until it sees a particular lifecycle event. | ||
* | ||
* That lifecycle event is determined based on what stage we're at in | ||
* the current lifecycle. | ||
*/ | ||
final class UntilCorrespondingEventCompletableTransformer<T> implements Completable.CompletableTransformer { | ||
|
||
final Observable<T> sharedLifecycle; | ||
final Func1<T, T> correspondingEvents; | ||
|
||
public UntilCorrespondingEventCompletableTransformer(@NonNull Observable<T> sharedLifecycle, | ||
@NonNull Func1<T, T> correspondingEvents) { | ||
this.sharedLifecycle = sharedLifecycle; | ||
this.correspondingEvents = correspondingEvents; | ||
} | ||
|
||
@Override | ||
public Completable call(Completable source) { | ||
return Completable.amb( | ||
source, | ||
takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents) | ||
.flatMap(Functions.CANCEL_COMPLETABLE) | ||
.toCompletable() | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { return true; } | ||
if (o == null || getClass() != o.getClass()) { return false; } | ||
|
||
UntilCorrespondingEventCompletableTransformer<?> that = (UntilCorrespondingEventCompletableTransformer<?>) o; | ||
|
||
if (!sharedLifecycle.equals(that.sharedLifecycle)) { return false; } | ||
return correspondingEvents.equals(that.correspondingEvents); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = sharedLifecycle.hashCode(); | ||
result = 31 * result + correspondingEvents.hashCode(); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UntilCorrespondingEventCompletableTransformer{" + | ||
"sharedLifecycle=" + sharedLifecycle + | ||
", correspondingEvents=" + correspondingEvents + | ||
'}'; | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventCompletableTransformer.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,50 @@ | ||
package com.trello.rxlifecycle; | ||
|
||
import android.support.annotation.NonNull; | ||
import rx.Completable; | ||
import rx.Observable; | ||
|
||
import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilEvent; | ||
|
||
/** | ||
* Continues a subscription until it sees a particular lifecycle event. | ||
*/ | ||
final class UntilEventCompletableTransformer<T> implements Completable.CompletableTransformer { | ||
|
||
final Observable<T> lifecycle; | ||
final T event; | ||
|
||
public UntilEventCompletableTransformer(@NonNull Observable<T> lifecycle, @NonNull T event) { | ||
this.lifecycle = lifecycle; | ||
this.event = event; | ||
} | ||
|
||
@Override | ||
public Completable call(Completable source) { | ||
return Completable.amb( | ||
source, | ||
takeUntilEvent(lifecycle, event) | ||
.flatMap(Functions.CANCEL_COMPLETABLE) | ||
.toCompletable() | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { return true; } | ||
if (o == null || getClass() != o.getClass()) { return false; } | ||
|
||
UntilEventCompletableTransformer<?> that = (UntilEventCompletableTransformer<?>) o; | ||
|
||
if (!lifecycle.equals(that.lifecycle)) { return false; } | ||
return event.equals(that.event); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = lifecycle.hashCode(); | ||
result = 31 * result + event.hashCode(); | ||
return result; | ||
} | ||
|
||
} |
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
49 changes: 49 additions & 0 deletions
49
rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleCompletableTransformer.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,49 @@ | ||
package com.trello.rxlifecycle; | ||
|
||
import android.support.annotation.NonNull; | ||
import rx.Completable; | ||
import rx.Observable; | ||
|
||
/** | ||
* Continues a subscription until it sees *any* lifecycle event. | ||
*/ | ||
final class UntilLifecycleCompletableTransformer<T> implements Completable.CompletableTransformer { | ||
|
||
final Observable<T> lifecycle; | ||
|
||
public UntilLifecycleCompletableTransformer(@NonNull Observable<T> lifecycle) { | ||
this.lifecycle = lifecycle; | ||
} | ||
|
||
@Override | ||
public Completable call(Completable source) { | ||
return Completable.amb( | ||
source, | ||
lifecycle | ||
.flatMap(Functions.CANCEL_COMPLETABLE) | ||
.toCompletable() | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { return true; } | ||
if (o == null || getClass() != o.getClass()) { return false; } | ||
|
||
UntilLifecycleCompletableTransformer<?> that = (UntilLifecycleCompletableTransformer<?>) o; | ||
|
||
return lifecycle.equals(that.lifecycle); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return lifecycle.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UntilLifecycleCompletableTransformer{" + | ||
"lifecycle=" + lifecycle + | ||
'}'; | ||
} | ||
} |
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
Oops, something went wrong.