Skip to content

Commit

Permalink
Migrate RxJava to RxJava2 for droidux core modules
Browse files Browse the repository at this point in the history
  • Loading branch information
wakwak3125 committed Jan 2, 2018
1 parent 142a4fa commit a1e9a62
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import info.izumin.android.droidux.processor.model.StoreImplModel;
import info.izumin.android.droidux.processor.model.StoreMethodModel;
import info.izumin.android.droidux.processor.model.StoreModel;
import rx.Observable;
import io.reactivex.Single;

import static info.izumin.android.droidux.processor.util.PoetUtils.getOverrideAnnotation;
import static info.izumin.android.droidux.processor.util.PoetUtils.getParameterSpec;
Expand Down Expand Up @@ -157,7 +157,7 @@ private MethodSpec createDispatchMethodSpec() {
return MethodSpec.methodBuilder(StoreModel.DISPATCH_METHOD_NAME)
.addAnnotation(getOverrideAnnotation())
.addModifiers(Modifier.PUBLIC)
.returns(ParameterizedTypeName.get(ClassName.get(Observable.class), ClassName.get(Action.class)))
.returns(ParameterizedTypeName.get(ClassName.get(Single.class), ClassName.get(Action.class)))
.addParameter(getParameterSpec(Action.class))
.addStatement("return $N.$N($N)",
DispatcherModel.VARIABLE_NAME, DispatcherModel.DISPATCH_METHOD_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;

import rx.Observable;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/28/15.
Expand All @@ -32,7 +32,7 @@ enum Kind {
UNKNOWN
}

private static final Class OBSERVE_METHOD_CLASS = Observable.class;
private static final Class OBSERVE_METHOD_CLASS = Flowable.class;

private final ExecutableElement element;
private final StoreModel storeModel;
Expand Down
5 changes: 2 additions & 3 deletions droidux/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'com.novoda.bintray-release'

targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

dependencies {
compile "io.reactivex:rxjava:${project.rxJavaVersion}"
compile "io.reactivex.rxjava2:rxjava:${project.rxJava2Version}"

testCompile "org.spockframework:spock-core:${project.spockCoreVersion}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package info.izumin.android.droidux;

import io.reactivex.Single;

/**
* Created by izumin on 12/6/15.
*/
public interface BaseStore {
rx.Observable<Action> dispatch(Action action);
Single<Action> dispatch(Action action);
}
40 changes: 20 additions & 20 deletions droidux/src/main/java/info/izumin/android/droidux/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import java.util.List;
import java.util.ListIterator;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
* Created by izumin on 11/28/15.
Expand All @@ -22,52 +23,51 @@ public Dispatcher(List<Middleware> middlewares, StoreImpl... storeImpls) {
this.storeImpls = Arrays.asList(storeImpls);
}

public Observable<Action> dispatch(Action action) {
return Observable.just(action)
.flatMap(new Func1<Action, Observable<Action>>() {
public Single<Action> dispatch(Action action) {
return Single.just(action)
.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action action) {
public SingleSource<? extends Action> apply(Action action) throws Exception {
return applyMiddlewaresBeforeDispatch(action);
}
})
.doOnNext(new Action1<Action>() {
}).doOnSuccess(new Consumer<Action>() {
@Override
public void call(Action action) {
public void accept(Action action) throws Exception {
for (StoreImpl store : storeImpls) {
store.dispatch(action);
}
}
})
.flatMap(new Func1<Action, Observable<Action>>() {
.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action action) {
public SingleSource<? extends Action> apply(Action action) throws Exception {
return applyMiddlewaresAfterDispatch(action);
}
});
}

private Observable<Action> applyMiddlewaresBeforeDispatch(Action action) {
Observable<Action> o = Observable.just(action);
private Single<Action> applyMiddlewaresBeforeDispatch(Action action) {
Single<Action> o = Single.just(action);

for (final Middleware<?> mw : middlewares) {
o = o.flatMap(new Func1<Action, Observable<Action>>() {
o = o.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action a) {
public Single<Action> apply(Action a) throws Exception {
return mw.beforeDispatch(a);
}
});
}
return o;
}

private Observable<Action> applyMiddlewaresAfterDispatch(Action action) {
Observable<Action> o = Observable.just(action);
private Single<Action> applyMiddlewaresAfterDispatch(Action action) {
Single<Action> o = Single.just(action);
ListIterator<Middleware> iterator = middlewares.listIterator(middlewares.size());
while(iterator.hasPrevious()) {
final Middleware<?> mw = iterator.previous();
o = o.flatMap(new Func1<Action, Observable<Action>>() {
o = o.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action a) {
public Single<Action> apply(Action a) throws Exception {
return mw.afterDispatch(a);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package info.izumin.android.droidux;

import rx.Observable;
import io.reactivex.Single;

/**
* Created by izumin on 11/2/15.
Expand All @@ -24,6 +24,6 @@ protected Dispatcher getDispatcher() {
return dispatcher;
}

public abstract Observable<Action> beforeDispatch(Action action);
public abstract Observable<Action> afterDispatch(Action action);
public abstract Single<Action> beforeDispatch(Action action);
public abstract Single<Action> afterDispatch(Action action);
}
13 changes: 9 additions & 4 deletions droidux/src/main/java/info/izumin/android/droidux/StoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.util.HashSet;
import java.util.Set;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.subjects.BehaviorSubject;

/**
* Created by izumin on 11/2/15.
Expand All @@ -24,8 +25,12 @@ protected StoreImpl(T state, R reducer) {
listeners = new HashSet<>();
}

public Observable<T> observe() {
return subject;
public Flowable<T> observe() {
return observe(BackpressureStrategy.DROP);
}

public Flowable<T> observe(BackpressureStrategy strategy) {
return subject.toFlowable(strategy);
}

public T getState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package info.izumin.android.droidux

import rx.Observable
import io.reactivex.Single
import spock.lang.Specification;

/**
Expand Down Expand Up @@ -34,10 +34,10 @@ class DispatcherTest extends Specification {
dispatcher.dispatch(action).subscribe()

then:
1 * middleware1.beforeDispatch(action) >> Observable.just(action)
1 * middleware1.beforeDispatch(action) >> Single.just(action)

then:
1 * middleware2.beforeDispatch(action) >> Observable.just(action)
1 * middleware2.beforeDispatch(action) >> Single.just(action)

then:
1 * store1.dispatch(action)
Expand All @@ -46,9 +46,9 @@ class DispatcherTest extends Specification {
1 * store2.dispatch(action)

then:
1 * middleware2.afterDispatch(action) >> Observable.just(action)
1 * middleware2.afterDispatch(action) >> Single.just(action)

then:
1 * middleware1.afterDispatch(action) >> Observable.just(action)
1 * middleware1.afterDispatch(action) >> Single.just(action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import info.izumin.android.droidux.Action;
import info.izumin.android.droidux.Dispatcher;
import rx.Observable;
import io.reactivex.Single;

/**
* Created by izumin on 11/29/15.
*/
public interface AsyncAction extends Action {
Observable<Action> call(Dispatcher dispatcher);
Single<Action> call(Dispatcher dispatcher);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import info.izumin.android.droidux.Action;
import info.izumin.android.droidux.Middleware;
import rx.Observable;
import rx.functions.Func1;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;

/**
* Created by izumin on 11/29/15.
Expand All @@ -12,27 +13,27 @@ public class ThunkMiddleware extends Middleware {
public static final String TAG = ThunkMiddleware.class.getSimpleName();

@Override
public Observable<Action> beforeDispatch(final Action action) {
public Single<Action> beforeDispatch(final Action action) {
if (action instanceof AsyncAction) {
return ((AsyncAction) action).call(getDispatcher())
.flatMap(new Func1<Action, Observable<Action>>() {
.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action next) {
public Single<Action> apply(Action next) throws Exception {
return getDispatcher().dispatch(next);
}
})
.flatMap(new Func1<Action, Observable<Action>>() {
.flatMap(new Function<Action, SingleSource<? extends Action>>() {
@Override
public Observable<Action> call(Action _next) {
return Observable.just(action);
public Single<Action> apply(Action _next) throws Exception {
return Single.just(action);
}
});
}
return Observable.just(action);
return Single.just(action);
}

@Override
public Observable<Action> afterDispatch(Action action) {
return Observable.just(action);
public Single<Action> afterDispatch(Action action) {
return Single.just(action);
}
}

0 comments on commit a1e9a62

Please sign in to comment.