io.reactivex.Flowable.throttleFirst()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(158)

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

Flowable.throttleFirst介绍

[英]Returns a Flowable that emits only the first item emitted by the source Publisher during sequential time windows of a specified duration.

This differs from #throttleLast in that this only tracks the passage of time whereas #throttleLast ticks at scheduled intervals.

Backpressure: This operator does not support backpressure as it uses time to control data flow. Scheduler: throttleFirst operates by default on the computation Scheduler.
[中]返回在指定持续时间的连续时间窗口内仅发出源发布服务器发出的第一项的可流动项。
这与#throttleLast的不同之处在于,它只跟踪时间的流逝,而#throttleLast以预定的间隔滴答作响。
背压:此运算符不支持背压,因为它使用时间来控制数据流。调度程序:默认情况下,throttleFirst在计算调度程序上运行。

代码示例

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

  1. @Test(expected = NullPointerException.class)
  2. public void throttleFirstSchedulerNull() {
  3. just1.throttleFirst(1, TimeUnit.SECONDS, null);
  4. }

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

  1. @Test(expected = NullPointerException.class)
  2. public void throttleFirstUnitNull() {
  3. just1.throttleFirst(1, null, Schedulers.single());
  4. }

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

  1. /**
  2. * Returns a Flowable that emits only the first item emitted by the source Publisher during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleLast} in that this only tracks the passage of time whereas
  6. * {@link #throttleLast} ticks at scheduled intervals.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleFirst.png" alt="">
  9. * <dl>
  10. * <dt><b>Backpressure:</b></dt>
  11. * <dd>This operator does not support backpressure as it uses time to control data flow.</dd>
  12. * <dt><b>Scheduler:</b></dt>
  13. * <dd>{@code throttleFirst} operates by default on the {@code computation} {@link Scheduler}.</dd>
  14. * </dl>
  15. *
  16. * @param windowDuration
  17. * time to wait before emitting another item after emitting the last item
  18. * @param unit
  19. * the unit of time of {@code windowDuration}
  20. * @return a Flowable that performs the throttle operation
  21. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  22. * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
  23. */
  24. @CheckReturnValue
  25. @BackpressureSupport(BackpressureKind.ERROR)
  26. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  27. public final Flowable<T> throttleFirst(long windowDuration, TimeUnit unit) {
  28. return throttleFirst(windowDuration, unit, Schedulers.computation());
  29. }

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

  1. @Test
  2. public void dispose() {
  3. TestHelper.checkDisposed(Flowable.just(1).throttleFirst(1, TimeUnit.DAYS));
  4. }

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

  1. @Test
  2. public void testThrottlingWithCompleted() {
  3. Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {
  4. @Override
  5. public void subscribe(Subscriber<? super String> subscriber) {
  6. subscriber.onSubscribe(new BooleanSubscription());
  7. publishNext(subscriber, 100, "one"); // publish as it's first
  8. publishNext(subscriber, 300, "two"); // skip as it's last within the first 400
  9. publishNext(subscriber, 900, "three"); // publish
  10. publishNext(subscriber, 905, "four"); // skip
  11. publishCompleted(subscriber, 1000); // Should be published as soon as the timeout expires.
  12. }
  13. });
  14. Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
  15. sampled.subscribe(subscriber);
  16. InOrder inOrder = inOrder(subscriber);
  17. scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
  18. inOrder.verify(subscriber, times(1)).onNext("one");
  19. inOrder.verify(subscriber, times(0)).onNext("two");
  20. inOrder.verify(subscriber, times(1)).onNext("three");
  21. inOrder.verify(subscriber, times(0)).onNext("four");
  22. inOrder.verify(subscriber, times(1)).onComplete();
  23. inOrder.verifyNoMoreInteractions();
  24. }

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

  1. /**
  2. * Returns a Flowable that emits only the first item emitted by the source Publisher during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleLast} in that this only tracks the passage of time whereas
  6. * {@link #throttleLast} ticks at scheduled intervals.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleFirst.png" alt="">
  9. * <dl>
  10. * <dt><b>Backpressure:</b></dt>
  11. * <dd>This operator does not support backpressure as it uses time to control data flow.</dd>
  12. * <dt><b>Scheduler:</b></dt>
  13. * <dd>{@code throttleFirst} operates by default on the {@code computation} {@link Scheduler}.</dd>
  14. * </dl>
  15. *
  16. * @param windowDuration
  17. * time to wait before emitting another item after emitting the last item
  18. * @param unit
  19. * the unit of time of {@code windowDuration}
  20. * @return a Flowable that performs the throttle operation
  21. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  22. * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
  23. */
  24. @CheckReturnValue
  25. @BackpressureSupport(BackpressureKind.ERROR)
  26. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  27. public final Flowable<T> throttleFirst(long windowDuration, TimeUnit unit) {
  28. return throttleFirst(windowDuration, unit, Schedulers.computation());
  29. }

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

  1. @Test
  2. public void backpressureNoRequest() {
  3. Flowable.range(1, 3)
  4. .throttleFirst(1, TimeUnit.MINUTES)
  5. .test(0L)
  6. .assertFailure(MissingBackpressureException.class);
  7. }
  8. }

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

  1. @Test
  2. public void testThrottlingWithError() {
  3. Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {
  4. @Override
  5. public void subscribe(Subscriber<? super String> subscriber) {
  6. subscriber.onSubscribe(new BooleanSubscription());
  7. Exception error = new TestException();
  8. publishNext(subscriber, 100, "one"); // Should be published since it is first
  9. publishNext(subscriber, 200, "two"); // Should be skipped since onError will arrive before the timeout expires
  10. publishError(subscriber, 300, error); // Should be published as soon as the timeout expires.
  11. }
  12. });
  13. Flowable<String> sampled = source.throttleFirst(400, TimeUnit.MILLISECONDS, scheduler);
  14. sampled.subscribe(subscriber);
  15. InOrder inOrder = inOrder(subscriber);
  16. scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS);
  17. inOrder.verify(subscriber).onNext("one");
  18. inOrder.verify(subscriber).onError(any(TestException.class));
  19. inOrder.verifyNoMoreInteractions();
  20. }

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

  1. @Test
  2. public void throttleFirstDefaultScheduler() {
  3. Flowable.just(1).throttleFirst(100, TimeUnit.MILLISECONDS)
  4. .test()
  5. .awaitDone(5, TimeUnit.SECONDS)
  6. .assertResult(1);
  7. }

相关文章

Flowable类方法