Skip to content

Commit

Permalink
Use Flowable instead of Single
Browse files Browse the repository at this point in the history
  • Loading branch information
wakwak3125 committed Jan 2, 2018
1 parent 62c3e88 commit 23061cd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
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 io.reactivex.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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package info.izumin.android.droidux;

import io.reactivex.Flowable;
import io.reactivex.Single;

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

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.Flowable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

Expand All @@ -23,51 +22,51 @@ public Dispatcher(List<Middleware> middlewares, StoreImpl... storeImpls) {
this.storeImpls = Arrays.asList(storeImpls);
}

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

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

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

private Single<Action> applyMiddlewaresAfterDispatch(Action action) {
Single<Action> o = Single.just(action);
private Flowable<Action> applyMiddlewaresAfterDispatch(Action action) {
Flowable<Action> o = Flowable.just(action);
ListIterator<Middleware> iterator = middlewares.listIterator(middlewares.size());
while(iterator.hasPrevious()) {
final Middleware<?> mw = iterator.previous();
o = o.flatMap(new Function<Action, SingleSource<? extends Action>>() {
o = o.flatMap(new Function<Action, Flowable<? extends Action>>() {
@Override
public Single<Action> apply(Action a) throws Exception {
public Flowable<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 io.reactivex.Single;
import io.reactivex.Flowable;

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

public abstract Single<Action> beforeDispatch(Action action);
public abstract Single<Action> afterDispatch(Action action);
public abstract Flowable<Action> beforeDispatch(Action action);
public abstract Flowable<Action> afterDispatch(Action action);
}

0 comments on commit 23061cd

Please sign in to comment.