io.reactivex.Observable.concatMapMaybe()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(174)

本文整理了Java中io.reactivex.Observable.concatMapMaybe()方法的一些代码示例,展示了Observable.concatMapMaybe()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.concatMapMaybe()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:concatMapMaybe

Observable.concatMapMaybe介绍

[英]Maps the upstream items into MaybeSources and subscribes to them one after the other succeeds or completes, emits their success value if available or terminates immediately if either this Observable or the current inner MaybeSource fail.

Scheduler: concatMapMaybe does not operate by default on a particular Scheduler.
[中]将上游项目映射到MaybeSource中,并一个接一个地订阅它们,如果可用,则发送它们的成功值,或者如果此可观察或当前内部MaybeSource失败,则立即终止。
调度器:默认情况下,ConcatMap可能不会在特定的调度器上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

/**
 * Maps the upstream items into {@link MaybeSource}s and subscribes to them one after the
 * other succeeds or completes, emits their success value if available or terminates immediately if
 * either this {@code Observable} or the current inner {@code MaybeSource} fail.
 * <p>
 * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concatMap.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code concatMapMaybe} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * <p>History: 2.1.11 - experimental
 * @param <R> the result type of the inner {@code MaybeSource}s
 * @param mapper the function called with the upstream item and should return
 *               a {@code MaybeSource} to become the next source to
 *               be subscribed to
 * @return a new Observable instance
 * @see #concatMapMaybeDelayError(Function)
 * @see #concatMapMaybe(Function, int)
 * @since 2.2
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Observable<R> concatMapMaybe(Function<? super T, ? extends MaybeSource<? extends R>> mapper) {
  return concatMapMaybe(mapper, 2);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scalarMapperCrash() {
  TestObserver<Object> to = Observable.just(1)
  .concatMapMaybe(new Function<Integer, MaybeSource<? extends Object>>() {
    @Override
    public MaybeSource<? extends Object> apply(Integer v)
        throws Exception {
          throw new TestException();
        }
  })
  .test();
  to.assertFailure(TestException.class);
}

代码示例来源:origin: redisson/redisson

/**
 * Maps the upstream items into {@link MaybeSource}s and subscribes to them one after the
 * other succeeds or completes, emits their success value if available or terminates immediately if
 * either this {@code Observable} or the current inner {@code MaybeSource} fail.
 * <p>
 * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concatMap.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code concatMapMaybe} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * @param <R> the result type of the inner {@code MaybeSource}s
 * @param mapper the function called with the upstream item and should return
 *               a {@code MaybeSource} to become the next source to
 *               be subscribed to
 * @return a new Observable instance
 * @since 2.1.11 - experimental
 * @see #concatMapMaybeDelayError(Function)
 * @see #concatMapMaybe(Function, int)
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
@Experimental
public final <R> Observable<R> concatMapMaybe(Function<? super T, ? extends MaybeSource<? extends R>> mapper) {
  return concatMapMaybe(mapper, 2);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void disposed() {
  TestHelper.checkDisposed(Observable.just(1).hide()
      .concatMapMaybe(Functions.justFunction(Maybe.never()))
  );
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void simple() {
  Observable.range(1, 5)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      return Maybe.just(v);
    }
  })
  .test()
  .assertResult(1, 2, 3, 4, 5);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void empty() {
  Observable.range(1, 10)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      return Maybe.empty();
    }
  })
  .test()
  .assertResult();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void innerError() {
  Observable.just(1)
  .concatMapMaybe(Functions.justFunction(Maybe.error(new TestException())))
  .test()
  .assertFailure(TestException.class);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void mixed() {
  Observable.range(1, 10)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      if (v % 2 == 0) {
        return Maybe.just(v);
      }
      return Maybe.empty();
    }
  })
  .test()
  .assertResult(2, 4, 6, 8, 10);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void take() {
  Observable.range(1, 5)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      return Maybe.just(v);
    }
  })
  .take(3)
  .test()
  .assertResult(1, 2, 3);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void simpleLong() {
  Observable.range(1, 1024)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      return Maybe.just(v);
    }
  }, 32)
  .test()
  .assertValueCount(1024)
  .assertNoErrors()
  .assertComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void mainError() {
  Observable.error(new TestException())
  .concatMapMaybe(Functions.justFunction(Maybe.just(1)))
  .test()
  .assertFailure(TestException.class);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void innerSuccessDisposeRace() {
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {

      final MaybeSubject<Integer> ms = MaybeSubject.create();

      final TestObserver<Integer> to = Observable.just(1)
          .hide()
          .concatMapMaybe(Functions.justFunction(ms))
          .test();

      Runnable r1 = new Runnable() {
        @Override
        public void run() {
          ms.onSuccess(1);
        }
      };
      Runnable r2 = new Runnable() {
        @Override
        public void run() {
          to.dispose();
        }
      };

      TestHelper.race(r1, r2);

      to.assertNoErrors();
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scalarEmptySource() {
  MaybeSubject<Integer> ms = MaybeSubject.create();
  Observable.empty()
  .concatMapMaybe(Functions.justFunction(ms))
  .test()
  .assertResult();
  assertFalse(ms.hasObservers());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void checkUnboundedInnerQueue() {
  MaybeSubject<Integer> ms = MaybeSubject.create();
  @SuppressWarnings("unchecked")
  TestObserver<Integer> to = Observable
      .fromArray(ms, Maybe.just(2), Maybe.just(3), Maybe.just(4))
      .concatMapMaybe(Functions.<Maybe<Integer>>identity(), 2)
      .test();
  to.assertEmpty();
  ms.onSuccess(1);
  to.assertResult(1, 2, 3, 4);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void cancel() {
  Observable.range(1, 5).concatWith(Observable.<Integer>never())
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      return Maybe.just(v);
    }
  })
  .test()
  .assertValues(1, 2, 3, 4, 5)
  .assertNoErrors()
  .assertNotComplete()
  .cancel();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void mixedLong() {
  Observable.range(1, 1024)
  .concatMapMaybe(new Function<Integer, MaybeSource<Integer>>() {
    @Override
    public MaybeSource<Integer> apply(Integer v)
        throws Exception {
      if (v % 2 == 0) {
        return Maybe.just(v).subscribeOn(Schedulers.computation());
      }
      return Maybe.<Integer>empty().subscribeOn(Schedulers.computation());
    }
  })
  .test()
  .awaitDone(5, TimeUnit.SECONDS)
  .assertValueCount(512)
  .assertNoErrors()
  .assertComplete()
  .assertOf(new Consumer<TestObserver<Integer>>() {
    @Override
    public void accept(TestObserver<Integer> to) throws Exception {
      for (int i = 0; i < 512; i ++) {
        to.assertValueAt(i, (i + 1) * 2);
      }
    }
  });
}

相关文章

Observable类方法