Java Code Examples for io.reactivex.Observable.merge()

The following are Jave code examples for showing how to use merge() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: cleanarchitecture-unidirectional   File: LandingPresenter.java   Source Code and License Vote up 6 votes
@Override
protected ObservableTransformer<Action, Result> actionsToResults() {
    return upstream -> Observable.merge(
            upstream.ofType(LandingUiActions.LogoutAction.class)
                .flatMap(action ->
                    logoutUseCase.performAction(action)
                        .onErrorReturn(throwable -> Result.error(action, throwable))
                        .startWith(Result.loading(action))),
            upstream.ofType(LandingUiActions.GetProfileAction.class)
                .flatMap(action ->
                    getUserUseCase.performAction(action)
                        .onErrorReturn(throwable -> Result.error(action, throwable))
                        .startWith(Result.loading(action))
                )
    );
}
 
Example 2
Project: cleanarchitecture-unidirectional   File: TimezoneEditPresenter.java   Source Code and License Vote up 6 votes
@Override
protected ObservableTransformer<UiEvent, Action> eventsToActions() {
    return upstream -> Observable.merge(
            upstream.ofType(TimezoneEditUiEvents.GetTimezoneEvent.class)
                .map(event -> TimezoneEditUiActions.get(event.getUserId(),event.getTimezoneId())),
            upstream.ofType(TimezoneEditUiEvents.SaveTimezoneEvent.class)
                .map(event -> {
                    final TimezoneEditUiActions.SaveTimezoneAction.Builder builder = new TimezoneEditUiActions.SaveTimezoneAction.Builder();
                    final TimezoneModel tz = event.getTimezone();
                    builder.setCity(tz.getCity());
                    builder.setDifference(tryParseDifference(tz.getDifference()));
                    builder.setId(tz.getId());
                    builder.setName(tz.getName());
                    builder.setUserId(tz.getOwnerId());
                    return TimezoneEditUiActions.save(builder);
                }),
            upstream.ofType(DeleteTimezoneEvent.class)
                .map(event -> TimezoneEditUiActions.delete(event.getUserId(), event.getTimezoneId()))
    );
}
 
Example 3
Project: RIBs   File: TicTacToeView.java   Source Code and License Vote up 6 votes
@Override
public Observable<BoardCoordinate> squareClicks() {
  ArrayList<Observable<BoardCoordinate>> observables = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      final int finalI = i;
      final int finalJ = j;
      observables.add(
          RxView.clicks(imageButtons[i][j])
              .map(
                  new Function<Object, BoardCoordinate>() {
                    @Override
                    public BoardCoordinate apply(Object irrelevant) throws Exception {
                      return new BoardCoordinate(finalI, finalJ);
                    }
                  }));
    }
  }
  return Observable.merge(observables);
}
 
Example 4
Project: cleanarchitecture-unidirectional   File: TimezonePresenter.java   Source Code and License Vote up 6 votes
@Override
protected ObservableTransformer<Action, Result> actionsToResults() {
    return upstream -> Observable.merge(
            upstream.ofType(TimezonesUiActions.RefreshAction.class)
                    .flatMap(refreshAction ->
                            listUseCase.performAction(refreshAction)
                                    .onErrorReturn(t -> Result.error(refreshAction, t))
                                    .startWith(Result.loading(refreshAction))),
            upstream.ofType(TimezonesUiActions.LoadMoreAction.class)
                    .flatMap(loadMoreAction ->
                            listUseCase.performAction(loadMoreAction)
                                    .onErrorReturn(t -> Result.error(loadMoreAction, t))
                                    .startWith(Result.loading(loadMoreAction))),
            upstream.ofType(DeleteTimezoneAction.class)
                    .flatMap(action ->
                            deleteUseCase.performAction(action)
                                    .onErrorReturn(t -> Result.error(action, t))
                                    .startWith(Result.loading(action)))
    );
}
 
Example 5
Project: RIBs   File: TicTacToeView.java   Source Code and License Vote up 6 votes
@Override
public Observable<BoardCoordinate> squareClicks() {
  ArrayList<Observable<BoardCoordinate>> observables = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      final int finalI = i;
      final int finalJ = j;
      observables.add(
          RxView.clicks(imageButtons[i][j])
              .map(
                  new Function<Object, BoardCoordinate>() {
                    @Override
                    public BoardCoordinate apply(Object irrelevant) throws Exception {
                      return new BoardCoordinate(finalI, finalJ);
                    }
                  }));
    }
  }
  return Observable.merge(observables);
}
 
Example 6
Project: RIBs   File: TicTacToeView.java   Source Code and License Vote up 6 votes
@Override
public Observable<BoardCoordinate> squareClicks() {
  ArrayList<Observable<BoardCoordinate>> observables = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      final int finalI = i;
      final int finalJ = j;
      observables.add(
          RxView.clicks(imageButtons[i][j])
              .map(
                  new Function<Object, BoardCoordinate>() {
                    @Override
                    public BoardCoordinate apply(Object irrelevant) throws Exception {
                      return new BoardCoordinate(finalI, finalJ);
                    }
                  }));
    }
  }
  return Observable.merge(observables);
}
 
Example 7
Project: RIBs   File: TicTacToeView.java   Source Code and License Vote up 6 votes
@Override
public Observable<BoardCoordinate> squareClicks() {
  ArrayList<Observable<BoardCoordinate>> observables = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      final int finalI = i;
      final int finalJ = j;
      observables.add(
          RxView.clicks(imageButtons[i][j])
              .map(
                  new Function<Object, BoardCoordinate>() {
                    @Override
                    public BoardCoordinate apply(Object irrelevant) throws Exception {
                      return new BoardCoordinate(finalI, finalJ);
                    }
                  }));
    }
  }
  return Observable.merge(observables);
}
 
Example 8
Project: RIBs   File: OffGameView.java   Source Code and License Vote up 6 votes
@Override
public Observable<GameKey> startGameRequest(List<? extends GameKey> gameKeys) {
  List<Observable<GameKey>> observables = new ArrayList<>();
  for (final GameKey gameKey : gameKeys) {
    Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.game_button, this, false);
    button.setText(gameKey.gameName());
    Observable<GameKey> observable = RxView
        .clicks(button)
        .map(new Function<Object, GameKey>() {
          @Override
          public GameKey apply(Object o) throws Exception {
            return gameKey;
          }
        });
    observables.add(observable);
    addView(button);
  }
  return Observable.merge(observables);
}
 
Example 9
Project: reactive-playing   File: TickTack.java   Source Code and License Vote up 5 votes
private static Observable<Long> clock() {
    Observable<Long> fast = Observable.interval(1, TimeUnit.SECONDS);
    Observable<Long> slow = Observable.interval(3, TimeUnit.SECONDS);

    return Observable.merge(
            slow.filter(t -> isSlowTickTime()).doOnEach(w->System.out.println("<SLOW>")),
            fast.filter(t -> !isSlowTickTime()).doOnEach(w->System.out.println("<FAST>"))
    );
}
 
Example 10
Project: Watermark   File: RxBus.java   Source Code and License Vote up 5 votes
/**
 * 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者
 */
public <T> Observable<T> toObservableSticky(final Class<T> eventType) {
    synchronized (mStickyEventMap) {
        Observable<T> observable = mBus.ofType(eventType);
        final Object event = mStickyEventMap.get(eventType);

        if (event != null) {
            return Observable.merge(observable, Observable.create(subscriber -> subscriber.onNext(eventType.cast(event))));
        } else {
            return observable;
        }
    }
}
 
Example 11
Project: cleanarchitecture-unidirectional   File: LandingPresenter.java   Source Code and License Vote up 5 votes
@Override
protected ObservableTransformer<UiEvent, Action> eventsToActions() {
    return upstream -> Observable.merge(
            upstream.ofType(LandingUiEvents.LogoutUiEvent.class)
                .map(event -> LandingUiActions.logout(event.getMenuId())),
            upstream.ofType(LandingUiEvents.MenuSelectedUiEvent.class)
                .startWith(LandingUiEvents.menuSelected(getLastState().getData().getSelectedMenuId()))
                .map(event -> LandingUiActions.getProfile(event.getMenuId(), userManager.getUserId()))
    );
}
 
Example 12
Project: cleanarchitecture-unidirectional   File: TimezoneEditPresenter.java   Source Code and License Vote up 5 votes
@Override
protected ObservableTransformer<Action, Result> actionsToResults() {
    return upstream -> Observable.merge(
            upstream.ofType(TimezoneEditUiActions.GetTimezoneAction.class)
                .flatMap(action -> getUseCase.performAction(action)
                    .onErrorReturn(t -> Result.error(action, t))
                    .startWith(Result.loading(action))
                ),
            upstream.ofType(TimezoneEditUiActions.SaveTimezoneAction.class)
                .flatMap(action -> {
                    if (action.getName() == null || action.getName().isEmpty()) {
                        return Observable.just(Result.error(action, new FormValidationException("Name cannot be empty!")));
                    } else if (action.getCity() == null || action.getCity().isEmpty()) {
                        return Observable.just(Result.error(action, new FormValidationException("City cannot be empty!")));
                    } else if (action.getDifference() < -12 || action.getDifference() > 12) {
                        return Observable.just(Result.error(action, new FormValidationException("Difference must be an integer between -12 and +12!")));
                    }
                    final Observable<Result> result;
                    if (action.getId() == null) {
                        result = saveUseCase.performAction(action);
                    } else {
                        result = updateUseCase.performAction(action);
                    }
                    return result
                            .onErrorReturn(t -> Result.error(action, t))
                            .startWith(Result.loading(action));
                }),
            upstream.ofType(DeleteTimezoneAction.class)
                .flatMap(action -> deleteUseCase.performAction(action)
                        .onErrorReturn(t -> Result.error(action, t))
                        .startWith(Result.loading(action))
                )
    );
}
 
Example 13
Project: cleanarchitecture-unidirectional   File: CheckUserPresenter.java   Source Code and License Vote up 5 votes
@Override
protected ObservableTransformer<Action, Result> actionsToResults() {
    return upstream -> Observable.merge(
            upstream.ofType(CheckUserActions.CheckUserExists.class)
                    .flatMap(checkUserExists -> checkUserUseCase.performAction(checkUserExists)
                            .onErrorReturn(throwable -> Result.error(checkUserExists, throwable))
                            .startWith(Result.<Boolean, CheckUserActions.CheckUserExists>loading(checkUserExists))),
            upstream.ofType(CheckUserActions.RegisterUserRecord.class)
                    .flatMap(action -> registerUserUseCase.performAction(action)
                            .onErrorReturn(throwable -> Result.error(action, throwable)))
    );
}
 
Example 14
Project: buckaroo   File: CommonTasks.java   Source Code and License Vote up 5 votes
public static Observable<Event> maybeInitCookbooks(final FileSystem fs, final BuckarooConfig config) {

        final Path configFolder = fs.getPath(
            System.getProperty("user.home"),
            ".buckaroo");

        return Observable.merge(config.cookbooks
            .stream()
            .filter(cookbook -> !Files.exists(configFolder.resolve(cookbook.name.toString())))
            .map(cookbook ->
                UpdateTasks.updateCookbook(configFolder, cookbook))
            .collect(toImmutableList())
        );
    }
 
Example 15
Project: XPermission   File: XPermission.java   Source Code and License Vote up 4 votes
private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
    if (trigger == null) {
        return Observable.just(TRIGGER);
    }
    return Observable.merge(trigger, pending);
}
 
Example 16
Project: AesExoPlayer   File: RxPermissions.java   Source Code and License Vote up 4 votes
private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
    if (trigger == null) {
        return Observable.just(new Object());
    }
    return Observable.merge(trigger, pending);
}
 
Example 17
Project: XSnow   File: RxPermissions.java   Source Code and License Vote up 4 votes
private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
    if (trigger == null) {
        return Observable.just(new Object());
    }
    return Observable.merge(trigger, pending);
}
 
Example 18
Project: KTools   File: RxPermissions.java   Source Code and License Vote up 4 votes
private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
    if (trigger == null) {
        return Observable.just(new Object());
    }
    return Observable.merge(trigger, pending);
}
 
Example 19
Project: RxPermissions   File: RxPermissions.java   Source Code and License Vote up 4 votes
private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
    if (trigger == null) {
        return Observable.just(1);
    }
    return Observable.merge(trigger, pending);
}
 
Example 20
Project: ratpack-rx2   File: RxRatpack.java   Source Code and License Vote up 2 votes
/**
 * Converts a {@link Promise} for an iterable into an {@link Observable}.
 * <p>
 * The promised iterable will be emitted to the observer one element at a time, like {@link Observable#fromIterable(Iterable)}.
 * <p>
 * <pre class="java">{@code
 * import ratpack.rx.RxRatpack;
 * import ratpack.exec.Promise;
 * import ratpack.test.exec.ExecHarness;
 * <p>
 * import java.util.Arrays;
 * import java.util.LinkedList;
 * import java.util.List;
 * <p>
 * import public static org.junit.Assert.assertEquals;
 * <p>
 * public class Example {
 * public static void main(String... args) throws Exception {
 * final List<String> items = new LinkedList<>();
 * ExecHarness.runSingle(e ->
 * Promise.value(Arrays.asList("foo", "bar"))
 * .to(RxRatpack::observeEach)
 * .subscribe(items::add)
 * );
 * <p>
 * assertEquals(Arrays.asList("foo", "bar"), items);
 * }
 * }
 * }</pre>
 *
 * @param promise the promise
 * @param <T>     the element type of the promised iterable
 * @param <I>     the type of iterable
 * @return an observable for each element of the promised iterable
 * @see #observe(ratpack.exec.Promise)
 */
public static <T, I extends Iterable<T>> Observable<T> observeEach(Promise<I> promise) {
  return Observable.merge(observe(promise).map(Observable::fromIterable));
}