Skip to content

Commit

Permalink
Fix/tweak some things in the RxLifecycle module (#120)
Browse files Browse the repository at this point in the history
Formatting, fix up docs and make the APIs `api` configurations as they should be
  • Loading branch information
ZacSweers authored Oct 22, 2017
1 parent 60d95bf commit 4a1d96d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 51 deletions.
4 changes: 2 additions & 2 deletions autodispose-rxlifecycle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ dependencies {
apt deps.build.nullAway
testApt deps.build.nullAway

compile project(':autodispose')
compile deps.misc.rxlifecycle
api project(':autodispose')
api deps.misc.rxlifecycle
compileOnly deps.misc.errorProneAnnotations
compileOnly deps.misc.javaxExtras

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.OutsideLifecycleException;
import com.uber.autodispose.AutoDispose.ScopeHandler;
import com.uber.autodispose.LifecycleEndedException;
import com.uber.autodispose.ScopeProvider;
import io.reactivex.Maybe;

/**
* Interop for RxLifecycle. It provides static utility methods to convert {@link
* LifecycleProvider} to {@link ScopeProvider}.
*
* <p>There are several static utility converter
* methods such as {@link #from(LifecycleProvider)} for {@link
* LifecycleProvider#bindToLifecycle()} and {@link #from(LifecycleProvider, Object)} for
* {@link LifecycleProvider#bindUntilEvent(Object)}.
* <p>
* Interop for RxLifecycle. This provides static factory methods to convert {@link
* LifecycleProvider}s into {@link ScopeProvider} representations.
*
* <em>Note:</em> RxLifecycle treats the {@link OutsideLifecycleException}
* as normal terminal event. There is no mapping to {@link LifecycleEndedException} and in such
* cases the stream is normally disposed.
* cases the stream is just disposed.
*/
public final class RxLifecycleInterop {

Expand All @@ -46,19 +39,18 @@ private RxLifecycleInterop() {
private static final Object DEFAULT_THROWAWAY_OBJECT = new Object();

/**
* Converter for transforming {@link LifecycleProvider} to {@link ScopeProvider}.
* It disposes the source when the next reasonable event occurs.
* Factory creating a {@link ScopeProvider} representation of a {@link LifecycleProvider}.
* <p>
* Example usage:
* <pre><code>
* Observable.just(1)
* .to(RxLifecycleInterop.from(lifecycleProvider))
* .to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider)).forObservable())
* .subscribe(...)
* </code></pre>
*
* @param <E> the lifecycle event.
* @param provider the {@link LifecycleProvider} for RxLifecycle.
* @return a {@link ScopeHandler} to create AutoDisposing transformation
* @param provider the {@link LifecycleProvider}.
* @return a {@link ScopeProvider}
*/
public static <E> ScopeProvider from(final LifecycleProvider<E> provider) {
return new ScopeProvider() {
Expand All @@ -73,20 +65,19 @@ public static <E> ScopeProvider from(final LifecycleProvider<E> provider) {
}

/**
* Converter for transforming {@link LifecycleProvider} to {@link ScopeProvider}.
* It disposes the source when a specific event occurs.
* Factory creating a {@link ScopeProvider} representation of a {@link LifecycleProvider}.
* <p>
* Example usage:
* <pre><code>
* Observable.just(1)
* .to(RxLifecycleInterop.from(lifecycleProvider, event))
* .to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider, event)).forObservable())
* .subscribe(...)
* </code></pre>
*
* @param <E> the lifecycle event.
* @param provider the {@link LifecycleProvider} for RxLifecycle.
* @param event the event at which the source is disposed.
* @return a {@link ScopeHandler} to create AutoDisposing transformation
* @param provider the {@link LifecycleProvider}.
* @param event a target event to dispose upon.
* @return a {@link ScopeProvider}
*/
public static <E> ScopeProvider from(final LifecycleProvider<E> provider, final E event) {
return new ScopeProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public class RxLifecycleInteropTest {

private TestLifecycleProvider lifecycleProvider = new TestLifecycleProvider();

@Test
public void bindLifecycle_normalTermination_completeTheStream() {
@Test public void bindLifecycle_normalTermination_completeTheStream() {
lifecycleProvider.emitCreate();
TestObserver<Integer> o = new TestObserver<>();
PublishSubject<Integer> source = PublishSubject.create();
Disposable d = source.to(AutoDispose.with(
RxLifecycleInterop.from(lifecycleProvider)).<Integer>forObservable())
.subscribeWith(o);
Disposable d =
source.to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider))
.<Integer>forObservable())
.subscribeWith(o);
o.assertSubscribed();

assertThat(source.hasObservers()).isTrue();
Expand All @@ -58,13 +58,11 @@ public void bindLifecycle_normalTermination_completeTheStream() {
assertThat(source.hasObservers()).isFalse();
}

@Test
public void bindLifecycle_normalTermination_unsubscribe() {
@Test public void bindLifecycle_normalTermination_unsubscribe() {
lifecycleProvider.emitCreate();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
PublishSubject<Integer> source = PublishSubject.create();
source.to(AutoDispose.with(
RxLifecycleInterop.from(lifecycleProvider)).<Integer>forObservable())
source.to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider)).<Integer>forObservable())
.subscribe(o);
o.takeSubscribe();

Expand All @@ -79,33 +77,27 @@ public void bindLifecycle_normalTermination_unsubscribe() {
assertThat(source.hasObservers()).isFalse();
}

@Test
public void bindLifecycle_outsideLifecycleBound_unsubscribe() {
@Test public void bindLifecycle_outsideLifecycleBound_unsubscribe() {
lifecycleProvider.emitCreate();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
PublishSubject<Integer> source = PublishSubject.create();
lifecycleProvider.emitDestroy();
source
.to(AutoDispose.with(
RxLifecycleInterop.from(lifecycleProvider)).<Integer>forObservable())
source.to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider)).<Integer>forObservable())
.subscribe(o);

o.takeSubscribe();

source.onNext(2);
o.assertNoMoreEvents();
assertThat(
source.hasObservers()).isFalse(); // Because RxLifecycle
assertThat(source.hasObservers()).isFalse(); // Because RxLifecycle
// treats OutsideLifecycleException as terminal event.
}

@Test
public void bindUntilEvent_normalTermination_completeTheStream() {
@Test public void bindUntilEvent_normalTermination_completeTheStream() {
lifecycleProvider.emitCreate();
TestObserver<Integer> o = new TestObserver<>();
PublishSubject<Integer> source = PublishSubject.create();
Disposable d = source.to(AutoDispose.with(RxLifecycleInterop.from(
lifecycleProvider,
Disposable d = source.to(AutoDispose.with(RxLifecycleInterop.from(lifecycleProvider,
TestLifecycleProvider.Event.DESTROY)).<Integer>forObservable())
.subscribeWith(o);
o.assertSubscribed();
Expand All @@ -123,8 +115,7 @@ public void bindUntilEvent_normalTermination_completeTheStream() {
assertThat(source.hasObservers()).isFalse();
}

@Test
public void bindUntilEvent_interruptedTermination_unsubscribe() {
@Test public void bindUntilEvent_interruptedTermination_unsubscribe() {
lifecycleProvider.emitCreate();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
PublishSubject<Integer> source = PublishSubject.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
final class TestLifecycleProvider implements LifecycleProvider<TestLifecycleProvider.Event> {

private static final Function<Event, Event> CORRESPONDING_EVENTS = new Function<Event, Event>() {
@Override public Event apply(Event event)
throws Exception {
@Override public Event apply(Event event) throws Exception {
switch (event) {
case CREATE:
return Event.DESTROY;
Expand All @@ -44,8 +43,7 @@ final class TestLifecycleProvider implements LifecycleProvider<TestLifecycleProv
return lifecycle.hide();
}

@Override
public <T> LifecycleTransformer<T> bindUntilEvent(Event event) {
@Override public <T> LifecycleTransformer<T> bindUntilEvent(Event event) {
return RxLifecycle.bindUntilEvent(lifecycle, event);
}

Expand All @@ -62,7 +60,6 @@ void emitDestroy() {
}

enum Event {
CREATE,
DESTROY
CREATE, DESTROY
}
}

0 comments on commit 4a1d96d

Please sign in to comment.