Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Maybe support #170

Merged
merged 1 commit into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.trello.rxlifecycle2;

import io.reactivex.Completable;
import io.reactivex.CompletableTransformer;
import io.reactivex.Observable;
import io.reactivex.MaybeTransformer;
import io.reactivex.ObservableTransformer;
import io.reactivex.Single;
import io.reactivex.SingleTransformer;

/**
* A Transformer that works for all RxJava types ({@link Observable}, {@link Single} and {@link Completable}).
* A Transformer that works for all RxJava types.
*/
public interface LifecycleTransformer<T> extends ObservableTransformer<T, T>,
SingleTransformer<T, T>,
MaybeTransformer<T, T>,
CompletableTransformer
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
Expand Down Expand Up @@ -41,6 +43,11 @@ public SingleSource<T> apply(Single<T> upstream) {
return upstream.takeUntil(takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents).singleOrError());
}

@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
return upstream.takeUntil(takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents).firstElement());
}

@Override
public CompletableSource apply(Completable upstream) {
return Completable.ambArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
Expand Down Expand Up @@ -36,12 +38,16 @@ public SingleSource<T> apply(Single<T> upstream) {
return upstream.takeUntil(takeUntilEvent(lifecycle, event).singleOrError());
}

@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
return upstream.takeUntil(takeUntilEvent(lifecycle, event).firstElement());
}

@Override
public CompletableSource apply(Completable upstream) {
return Completable.ambArray(
upstream,
takeUntilEvent(lifecycle, event)
.flatMapCompletable(Functions.CANCEL_COMPLETABLE)
takeUntilEvent(lifecycle, event).flatMapCompletable(Functions.CANCEL_COMPLETABLE)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
Expand Down Expand Up @@ -32,12 +34,16 @@ public SingleSource<T> apply(Single<T> upstream) {
return upstream.takeUntil(lifecycle.take(1).singleOrError());
}

@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
return upstream.takeUntil(lifecycle.firstElement());
}

@Override
public CompletableSource apply(Completable upstream) {
return Completable.ambArray(
upstream,
lifecycle
.flatMapCompletable(Functions.CANCEL_COMPLETABLE)
lifecycle.flatMapCompletable(Functions.CANCEL_COMPLETABLE)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.trello.rxlifecycle2;

import io.reactivex.Maybe;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.PublishSubject;
import org.junit.Before;
import org.junit.Test;

public class UntilCorrespondingEventTransformerMaybeTest {

PublishSubject<String> subject;
Maybe<String> maybe;
PublishSubject<String> lifecycle;

@Before
public void setup() {
subject = PublishSubject.create();
maybe = subject.firstElement();
lifecycle = PublishSubject.create();
}

@Test
public void noEvents() {
TestObserver<String> testObserver = maybe
.compose(new UntilCorrespondingEventTransformer<String, String>(lifecycle, CORRESPONDING_EVENTS))
.test();

subject.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void oneStartEvent() {
TestObserver<String> testObserver = maybe
.compose(new UntilCorrespondingEventTransformer<String, String>(lifecycle, CORRESPONDING_EVENTS))
.test();

lifecycle.onNext("create");
subject.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void twoOpenEvents() {
TestObserver<String> testObserver = maybe
.compose(new UntilCorrespondingEventTransformer<String, String>(lifecycle, CORRESPONDING_EVENTS))
.test();

lifecycle.onNext("create");
lifecycle.onNext("start");
subject.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void openAndCloseEvent() {
TestObserver<String> testObserver = maybe
.compose(new UntilCorrespondingEventTransformer<String, String>(lifecycle, CORRESPONDING_EVENTS))
.test();

lifecycle.onNext("create");
lifecycle.onNext("destroy");
subject.onNext("1");
testObserver.assertNoValues();
testObserver.assertComplete();
}

private static final Function<String, String> CORRESPONDING_EVENTS = new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
if (s.equals("create")) {
return "destroy";
}

throw new IllegalArgumentException("Cannot handle: " + s);
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.trello.rxlifecycle2;

import io.reactivex.Maybe;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.PublishSubject;
import org.junit.Before;
import org.junit.Test;

public class UntilEventTransformerMaybeTest {

PublishSubject<String> subject;
Maybe<String> maybe;
PublishSubject<String> lifecycle;

@Before
public void setup() {
subject = PublishSubject.create();
maybe = subject.firstElement();
lifecycle = PublishSubject.create();
}

@Test
public void noEvents() {
TestObserver<String> testObserver = maybe
.compose(new UntilEventTransformer<String, String>(lifecycle, "stop"))
.test();

subject.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void oneWrongEvent() {
TestObserver<String> testObserver = maybe
.compose(new UntilEventTransformer<String, String>(lifecycle, "stop"))
.test();

lifecycle.onNext("keep going");
subject.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void twoEvents() {
TestObserver<String> testObserver = maybe
.compose(new UntilEventTransformer<String, String>(lifecycle, "stop"))
.test();

lifecycle.onNext("keep going");
lifecycle.onNext("stop");

subject.onNext("1");
testObserver.assertNoValues();
testObserver.assertComplete();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.trello.rxlifecycle2;

import io.reactivex.Maybe;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.PublishSubject;
import org.junit.Before;
import org.junit.Test;

public class UntilLifecycleTransformerMaybeTest {

PublishSubject<String> stream;
Maybe maybe;
PublishSubject<String> lifecycle;

@Before
public void setup() {
stream = PublishSubject.create();
maybe = stream.firstElement();
lifecycle = PublishSubject.create();
}

@Test
public void noEvent() {
TestObserver<String> testObserver = maybe
.compose(new UntilLifecycleTransformer<String, String>(lifecycle))
.test();

stream.onNext("1");
testObserver.assertValue("1");
testObserver.assertComplete();
}

@Test
public void oneEvent() {
TestObserver<String> testObserver = maybe
.compose(new UntilLifecycleTransformer<String, String>(lifecycle))
.test();

lifecycle.onNext("stop");
stream.onNext("1");

testObserver.assertNoValues();
testObserver.assertComplete();
}
}