Skip to content

Commit

Permalink
Renamed package, fixed javadoc and addressed other review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bangarharshit committed Oct 21, 2017
1 parent d7a237c commit ad33a87
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 93 deletions.

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=AutoDispose (RXLifeCycle Interop)
POM_ARTIFACT_ID=autodispose-rxlifecycle-interop
POM_ARTIFACT_ID=autodispose-rxlifecycle
POM_PACKAGING=jar
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.ubercab.autodispose.rxlifecycleinterop;
package com.ubercab.autodispose.rxlifecycle;

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

/**
* Interop class for {@link RxLifecycle}. It provides static utility methods to convert {@link
* LifecycleProvider} to {@link ScopeProvider}
* <p> There are several static utility converter
* methods such as {@link #bindLifecycle(LifecycleProvider)} for {@link
* LifecycleProvider#bindToLifecycle()} and {@link #bindUntilEvent(LifecycleProvider, Object)} for
* {@link LifecycleProvider#bindUntilEvent(Object)} </p>
* Interop class for RxLifecycle. It provides static utility methods to convert {@link
* LifecycleProvider} to {@link ScopeProvider}.
*
* <em>Unlike {@link AutoDispose}, {@link RxLifecycle} treats the {@link OutsideLifecycleException}
* <p>There are several static utility converter
* methods such as {@link #fromBindLifecycle(LifecycleProvider)} for {@link
* LifecycleProvider#bindToLifecycle()} and {@link #fromBindUntilEvent(LifecycleProvider, Object)} for
* {@link LifecycleProvider#bindUntilEvent(Object)}.
* </p>
*
* <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 </em>
* cases the stream is normally disposed.
*/
public final class RXLifeCycleInterop {
public final class RXLifecycleInterop {

private RXLifeCycleInterop() {
private RXLifecycleInterop() {
throw new AssertionError("No Instances");
}

private static final Object DEFAULT_THROWAWAY_OBJECT = new Object();

static <E> ScopeProvider bindLifecycle(final LifecycleProvider<E> provider) {
public static <E> ScopeProvider fromBindLifecycle(final LifecycleProvider<E> provider) {
return new ScopeProvider() {
@Override public Maybe<?> requestScope() {
return provider.lifecycle()
Expand All @@ -40,7 +40,7 @@ static <E> ScopeProvider bindLifecycle(final LifecycleProvider<E> provider) {
};
}

static <E> ScopeProvider bindUntilEvent(final LifecycleProvider<E> provider, final E event) {
public static <E> ScopeProvider fromBindUntilEvent(final LifecycleProvider<E> provider, final E event) {
return new ScopeProvider() {
@Override public Maybe<?> requestScope() {
return provider.lifecycle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/

/**
* These are Observers AutoDispose uses when scoping an observable. They are exposed as a public API
* to allow for consumers to watch for them if they want, such as in RxJava plugins.
* Interop for RxLifecycle.
*/
@com.uber.javaxextras.FieldsMethodsAndParametersAreNonNullByDefault
package com.ubercab.autodispose.rxlifecycleinterop;
package com.ubercab.autodispose.rxlifecycle;

Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
package com.ubercab.autodispose.rxlifecycleinterop;
package com.ubercab.autodispose.rxlifecycle;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.test.RecordingObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.PublishSubject;
import org.junit.Before;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public class RXLifeCycleInteropTest {
public class RXLifecycleInteropTest {

private static final RecordingObserver.Logger LOGGER = new RecordingObserver.Logger() {
@Override public void log(String message) {
System.out.println(RXLifeCycleInteropTest.class.getSimpleName() + ": " + message);
System.out.println(RXLifecycleInteropTest.class.getSimpleName() + ": " + message);
}
};

private LifeCycleProviderImpl lifeCycleProvider = new LifeCycleProviderImpl();

@Before
public void setup() {
lifeCycleProvider.onCreate();
}
private TestLifecycleProvider lifecycleProvider = new TestLifecycleProvider();

@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.bindLifecycle(lifeCycleProvider)).<Integer>forObservable())
RXLifecycleInterop.fromBindLifecycle(lifecycleProvider)).<Integer>forObservable())
.subscribeWith(o);
o.assertSubscribed();

Expand All @@ -49,10 +44,11 @@ public void bindLifecycle_normalTermination_completeTheStream() {

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

Expand All @@ -61,20 +57,21 @@ public void bindLifecycle_normalTermination_unsubscribe() {
source.onNext(1);
assertThat(o.takeNext()).isEqualTo(1);

lifeCycleProvider.onDestroy();
lifecycleProvider.emitDestroy();
source.onNext(2);
o.assertNoMoreEvents();
assertThat(source.hasObservers()).isFalse();
}

@Test
public void bindLifecycle_errorTermination_unsubscribe() {
public void bindLifecycle_outsideLifecycleBound_unsubscribe() {
lifecycleProvider.emitCreate();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
PublishSubject<Integer> source = PublishSubject.create();
lifeCycleProvider.onDestroy();
lifecycleProvider.emitDestroy();
source
.to(AutoDispose.with(
RXLifeCycleInterop.bindLifecycle(lifeCycleProvider)).<Integer>forObservable())
RXLifecycleInterop.fromBindLifecycle(lifecycleProvider)).<Integer>forObservable())
.subscribe(o);

o.takeSubscribe();
Expand All @@ -87,10 +84,12 @@ public void bindLifecycle_errorTermination_unsubscribe() {

@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.bindUntilEvent(lifeCycleProvider,
LifeCycleProviderImpl.Event.DESTROY)).<Integer>forObservable())
Disposable d = source.to(AutoDispose.with(RXLifecycleInterop.fromBindUntilEvent(
lifecycleProvider,
TestLifecycleProvider.Event.DESTROY)).<Integer>forObservable())
.subscribeWith(o);
o.assertSubscribed();

Expand All @@ -109,10 +108,11 @@ public void bindUntilEvent_normalTermination_completeTheStream() {

@Test
public void bindUntilEvent_interruptedTermination_unsubscribe() {
lifecycleProvider.emitCreate();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
PublishSubject<Integer> source = PublishSubject.create();
source.to(AutoDispose.with(RXLifeCycleInterop.bindUntilEvent(lifeCycleProvider,
LifeCycleProviderImpl.Event.DESTROY)).<Integer>forObservable())
source.to(AutoDispose.with(RXLifecycleInterop.fromBindUntilEvent(lifecycleProvider,
TestLifecycleProvider.Event.DESTROY)).<Integer>forObservable())
.subscribe(o);
o.takeSubscribe();

Expand All @@ -121,7 +121,7 @@ public void bindUntilEvent_interruptedTermination_unsubscribe() {
source.onNext(1);
assertThat(o.takeNext()).isEqualTo(1);

lifeCycleProvider.onDestroy();
lifecycleProvider.emitDestroy();
source.onNext(2);
o.assertNoMoreEvents();
assertThat(source.hasObservers()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ubercab.autodispose.rxlifecycle;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.OutsideLifecycleException;
import com.trello.rxlifecycle2.RxLifecycle;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import io.reactivex.subjects.BehaviorSubject;

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 {
switch (event) {
case CREATE:
return Event.DESTROY;
default:
throw new OutsideLifecycleException("Lifecycle ended");
}
}
};

private final BehaviorSubject<Event> lifecycle = BehaviorSubject.create();

@Override public Observable<Event> lifecycle() {
return lifecycle.hide();
}

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

@Override public <T> LifecycleTransformer<T> bindToLifecycle() {
return RxLifecycle.bind(lifecycle, CORRESPONDING_EVENTS);
}

void emitCreate() {
lifecycle.onNext(Event.CREATE);
}

void emitDestroy() {
lifecycle.onNext(Event.DESTROY);
}

enum Event {
CREATE,
DESTROY
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include ':android:autodispose-android-archcomponents-test'
include ':android:autodispose-android-archcomponents-test-kotlin'
include ':autodispose'
include ':autodispose-kotlin'
include 'autodispose-rxlifecycle'
include ':sample'
include ':test-utils'
include 'autodispose-rxlifecycle-interop'

0 comments on commit ad33a87

Please sign in to comment.