Skip to content

Commit

Permalink
Binding outside lifecycle causes instant completion
Browse files Browse the repository at this point in the history
Before, it would throw an exception, which doesn't make as much sense - if
the logic worked out such that you bound outside the lifecycle, that prboably
meant that you just don't want the subscription to occur outside the
lifecycle, so it should end immediately.
  • Loading branch information
dlew committed Sep 5, 2015
1 parent c12edf9 commit 60dfcbf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
14 changes: 12 additions & 2 deletions rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package com.trello.rxlifecycle;

import android.view.View;

import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.view.ViewAttachEvent;

import rx.Observable;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
import rx.functions.Func2;

Expand Down Expand Up @@ -214,6 +213,17 @@ public Boolean call(R bindUntilEvent, R lifecycleEvent) {
return lifecycleEvent == bindUntilEvent;
}
})
.onErrorReturn(new Func1<Throwable, Boolean>() {
@Override
public Boolean call(Throwable throwable) {
if (throwable instanceof IllegalStateException) {
return true;
}

Exceptions.propagate(throwable);
return false;
}
})
.takeFirst(new Func1<Boolean, Boolean>() {
@Override
public Boolean call(Boolean shouldComplete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

import android.app.Activity;
import android.view.View;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.concurrent.CopyOnWriteArrayList;

import rx.Observable;
import rx.Subscription;
import rx.observers.TestSubscriber;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;

import java.util.concurrent.CopyOnWriteArrayList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -137,13 +135,15 @@ public void testBindActivityLifecycle() {
stopTestSub.assertUnsubscribed();
}

public void testThrowsExceptionOutsideActivityLifecycle() {
@Test
public void testEndsImmediatelyOutsideActivityLifecycle() {
BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();
lifecycle.onNext(ActivityEvent.DESTROY);

TestSubscriber<Object> testSubscriber = new TestSubscriber<>();
observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(testSubscriber);
testSubscriber.assertError(IllegalStateException.class);
testSubscriber.assertCompleted();
testSubscriber.assertUnsubscribed();
}

@Test
Expand Down Expand Up @@ -227,13 +227,15 @@ public void testBindFragmentLifecycle() {
destroyTestSub.assertUnsubscribed();
}

public void testThrowsExceptionOutsideFragmentLifecycle() {
@Test
public void testEndsImmediatelyOutsideFragmentLifecycle() {
BehaviorSubject<FragmentEvent> lifecycle = BehaviorSubject.create();
lifecycle.onNext(FragmentEvent.DETACH);

TestSubscriber<Object> testSubscriber = new TestSubscriber<>();
observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(testSubscriber);
testSubscriber.assertError(IllegalStateException.class);
testSubscriber.assertCompleted();
testSubscriber.assertUnsubscribed();
}

@Test
Expand Down

0 comments on commit 60dfcbf

Please sign in to comment.