diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/LifecycleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/LifecycleTransformer.java index 10c0ba7..bb1115a 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/LifecycleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/LifecycleTransformer.java @@ -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 extends ObservableTransformer, SingleTransformer, + MaybeTransformer, CompletableTransformer { diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformer.java index a1da678..4d593f8 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformer.java @@ -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; @@ -41,6 +43,11 @@ public SingleSource apply(Single upstream) { return upstream.takeUntil(takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents).singleOrError()); } + @Override + public MaybeSource apply(Maybe upstream) { + return upstream.takeUntil(takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents).firstElement()); + } + @Override public CompletableSource apply(Completable upstream) { return Completable.ambArray( diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilEventTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilEventTransformer.java index c2d71d7..316e97a 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilEventTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilEventTransformer.java @@ -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; @@ -36,12 +38,16 @@ public SingleSource apply(Single upstream) { return upstream.takeUntil(takeUntilEvent(lifecycle, event).singleOrError()); } + @Override + public MaybeSource apply(Maybe 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) ); } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilLifecycleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilLifecycleTransformer.java index 4b0251a..9d34380 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilLifecycleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle2/UntilLifecycleTransformer.java @@ -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; @@ -32,12 +34,16 @@ public SingleSource apply(Single upstream) { return upstream.takeUntil(lifecycle.take(1).singleOrError()); } + @Override + public MaybeSource apply(Maybe 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) ); } diff --git a/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformerMaybeTest.java b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformerMaybeTest.java new file mode 100644 index 0000000..cc1f89a --- /dev/null +++ b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilCorrespondingEventTransformerMaybeTest.java @@ -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 subject; + Maybe maybe; + PublishSubject lifecycle; + + @Before + public void setup() { + subject = PublishSubject.create(); + maybe = subject.firstElement(); + lifecycle = PublishSubject.create(); + } + + @Test + public void noEvents() { + TestObserver testObserver = maybe + .compose(new UntilCorrespondingEventTransformer(lifecycle, CORRESPONDING_EVENTS)) + .test(); + + subject.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void oneStartEvent() { + TestObserver testObserver = maybe + .compose(new UntilCorrespondingEventTransformer(lifecycle, CORRESPONDING_EVENTS)) + .test(); + + lifecycle.onNext("create"); + subject.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void twoOpenEvents() { + TestObserver testObserver = maybe + .compose(new UntilCorrespondingEventTransformer(lifecycle, CORRESPONDING_EVENTS)) + .test(); + + lifecycle.onNext("create"); + lifecycle.onNext("start"); + subject.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void openAndCloseEvent() { + TestObserver testObserver = maybe + .compose(new UntilCorrespondingEventTransformer(lifecycle, CORRESPONDING_EVENTS)) + .test(); + + lifecycle.onNext("create"); + lifecycle.onNext("destroy"); + subject.onNext("1"); + testObserver.assertNoValues(); + testObserver.assertComplete(); + } + + private static final Function CORRESPONDING_EVENTS = new Function() { + @Override + public String apply(String s) throws Exception { + if (s.equals("create")) { + return "destroy"; + } + + throw new IllegalArgumentException("Cannot handle: " + s); + } + }; +} \ No newline at end of file diff --git a/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilEventTransformerMaybeTest.java b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilEventTransformerMaybeTest.java new file mode 100644 index 0000000..29a842a --- /dev/null +++ b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilEventTransformerMaybeTest.java @@ -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 subject; + Maybe maybe; + PublishSubject lifecycle; + + @Before + public void setup() { + subject = PublishSubject.create(); + maybe = subject.firstElement(); + lifecycle = PublishSubject.create(); + } + + @Test + public void noEvents() { + TestObserver testObserver = maybe + .compose(new UntilEventTransformer(lifecycle, "stop")) + .test(); + + subject.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void oneWrongEvent() { + TestObserver testObserver = maybe + .compose(new UntilEventTransformer(lifecycle, "stop")) + .test(); + + lifecycle.onNext("keep going"); + subject.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void twoEvents() { + TestObserver testObserver = maybe + .compose(new UntilEventTransformer(lifecycle, "stop")) + .test(); + + lifecycle.onNext("keep going"); + lifecycle.onNext("stop"); + + subject.onNext("1"); + testObserver.assertNoValues(); + testObserver.assertComplete(); + } + +} \ No newline at end of file diff --git a/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilLifecycleTransformerMaybeTest.java b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilLifecycleTransformerMaybeTest.java new file mode 100644 index 0000000..1420c90 --- /dev/null +++ b/rxlifecycle/src/test/java/com/trello/rxlifecycle2/UntilLifecycleTransformerMaybeTest.java @@ -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 stream; + Maybe maybe; + PublishSubject lifecycle; + + @Before + public void setup() { + stream = PublishSubject.create(); + maybe = stream.firstElement(); + lifecycle = PublishSubject.create(); + } + + @Test + public void noEvent() { + TestObserver testObserver = maybe + .compose(new UntilLifecycleTransformer(lifecycle)) + .test(); + + stream.onNext("1"); + testObserver.assertValue("1"); + testObserver.assertComplete(); + } + + @Test + public void oneEvent() { + TestObserver testObserver = maybe + .compose(new UntilLifecycleTransformer(lifecycle)) + .test(); + + lifecycle.onNext("stop"); + stream.onNext("1"); + + testObserver.assertNoValues(); + testObserver.assertComplete(); + } +} \ No newline at end of file