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

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

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

Observable.sample介绍

[英]Returns an Observable that emits the most recently emitted item (if any) emitted by the source ObservableSource within periodic time intervals.

Scheduler: sample operates by default on the computation Scheduler.
[中]返回在周期性时间间隔内发射源ObservableSource最近发射的项(如果有)的Observable。
调度程序:默认情况下,示例在计算调度程序上运行。

代码示例

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

  1. @Override
  2. public Observable<Object> apply(Observable<Object> o)
  3. throws Exception {
  4. return o.sample(1, TimeUnit.SECONDS);
  5. }
  6. });

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

  1. @Test(expected = NullPointerException.class)
  2. public void sampleUnitNull() {
  3. just1.sample(1, null);
  4. }

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

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

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

  1. @Test(expected = NullPointerException.class)
  2. public void sampleObservableNull() {
  3. just1.sample(null);
  4. }

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

  1. /**
  2. * Returns an Observable that emits only the last item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration, where the duration is governed by a specified Scheduler.
  4. * <p>
  5. * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas
  6. * {@link #throttleFirst} does not tick, it just tracks passage of time.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleLast.s.png" alt="">
  9. * <dl>
  10. * <dt><b>Scheduler:</b></dt>
  11. * <dd>You specify which {@link Scheduler} this operator will use.</dd>
  12. * </dl>
  13. *
  14. * @param intervalDuration
  15. * duration of windows within which the last item emitted by the source ObservableSource will be
  16. * emitted
  17. * @param unit
  18. * the unit of time of {@code intervalDuration}
  19. * @param scheduler
  20. * the {@link Scheduler} to use internally to manage the timers that handle timeout for each
  21. * event
  22. * @return an Observable that performs the throttle operation
  23. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  24. * @see #sample(long, TimeUnit, Scheduler)
  25. */
  26. @CheckReturnValue
  27. @SchedulerSupport(SchedulerSupport.CUSTOM)
  28. public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler) {
  29. return sample(intervalDuration, unit, scheduler);
  30. }

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

  1. /**
  2. * Returns an Observable that emits only the last item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas
  6. * {@link #throttleFirst} does not tick, it just tracks passage of time.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleLast.png" alt="">
  9. * <dl>
  10. * <dt><b>Scheduler:</b></dt>
  11. * <dd>{@code throttleLast} operates by default on the {@code computation} {@link Scheduler}.</dd>
  12. * </dl>
  13. *
  14. * @param intervalDuration
  15. * duration of windows within which the last item emitted by the source ObservableSource will be
  16. * emitted
  17. * @param unit
  18. * the unit of time of {@code intervalDuration}
  19. * @return an Observable that performs the throttle operation
  20. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  21. * @see #sample(long, TimeUnit)
  22. */
  23. @CheckReturnValue
  24. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  25. public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
  26. return sample(intervalDuration, unit);
  27. }

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

  1. /**
  2. * Returns an Observable that emits the most recently emitted item (if any) emitted by the source ObservableSource
  3. * within periodic time intervals.
  4. * <p>
  5. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/sample.png" alt="">
  6. * <dl>
  7. * <dt><b>Scheduler:</b></dt>
  8. * <dd>{@code sample} operates by default on the {@code computation} {@link Scheduler}.</dd>
  9. * </dl>
  10. *
  11. * @param period
  12. * the sampling rate
  13. * @param unit
  14. * the {@link TimeUnit} in which {@code period} is defined
  15. * @return an Observable that emits the results of sampling the items emitted by the source ObservableSource at
  16. * the specified time interval
  17. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  18. * @see #throttleLast(long, TimeUnit)
  19. */
  20. @CheckReturnValue
  21. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  22. public final Observable<T> sample(long period, TimeUnit unit) {
  23. return sample(period, unit, Schedulers.computation());
  24. }

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

  1. /**
  2. * Returns an Observable that emits the most recently emitted item (if any) emitted by the source ObservableSource
  3. * within periodic time intervals and optionally emit the very last upstream item when the upstream completes.
  4. * <p>
  5. * <img width="640" height="276" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/sample.emitlast.png" alt="">
  6. * <dl>
  7. * <dt><b>Scheduler:</b></dt>
  8. * <dd>{@code sample} operates by default on the {@code computation} {@link Scheduler}.</dd>
  9. * </dl>
  10. *
  11. * <p>History: 2.0.5 - experimental
  12. * @param period
  13. * the sampling rate
  14. * @param unit
  15. * the {@link TimeUnit} in which {@code period} is defined
  16. * @return an Observable that emits the results of sampling the items emitted by the source ObservableSource at
  17. * the specified time interval
  18. * @param emitLast
  19. * if true and the upstream completes while there is still an unsampled item available,
  20. * that item is emitted to downstream before completion
  21. * if false, an unsampled last item is ignored.
  22. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  23. * @see #throttleLast(long, TimeUnit)
  24. * @since 2.1
  25. */
  26. @CheckReturnValue
  27. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  28. public final Observable<T> sample(long period, TimeUnit unit, boolean emitLast) {
  29. return sample(period, unit, Schedulers.computation(), emitLast);
  30. }

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

  1. /**
  2. * Returns an Observable that emits only the last item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas
  6. * {@link #throttleFirst} does not tick, it just tracks passage of time.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleLast.png" alt="">
  9. * <dl>
  10. * <dt><b>Scheduler:</b></dt>
  11. * <dd>{@code throttleLast} operates by default on the {@code computation} {@link Scheduler}.</dd>
  12. * </dl>
  13. *
  14. * @param intervalDuration
  15. * duration of windows within which the last item emitted by the source ObservableSource will be
  16. * emitted
  17. * @param unit
  18. * the unit of time of {@code intervalDuration}
  19. * @return an Observable that performs the throttle operation
  20. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  21. * @see #sample(long, TimeUnit)
  22. */
  23. @CheckReturnValue
  24. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  25. public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
  26. return sample(intervalDuration, unit);
  27. }

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

  1. /**
  2. * Returns an Observable that emits only the last item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration, where the duration is governed by a specified Scheduler.
  4. * <p>
  5. * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas
  6. * {@link #throttleFirst} does not tick, it just tracks passage of time.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleLast.s.png" alt="">
  9. * <dl>
  10. * <dt><b>Scheduler:</b></dt>
  11. * <dd>You specify which {@link Scheduler} this operator will use.</dd>
  12. * </dl>
  13. *
  14. * @param intervalDuration
  15. * duration of windows within which the last item emitted by the source ObservableSource will be
  16. * emitted
  17. * @param unit
  18. * the unit of time of {@code intervalDuration}
  19. * @param scheduler
  20. * the {@link Scheduler} to use internally to manage the timers that handle timeout for each
  21. * event
  22. * @return an Observable that performs the throttle operation
  23. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  24. * @see #sample(long, TimeUnit, Scheduler)
  25. */
  26. @CheckReturnValue
  27. @SchedulerSupport(SchedulerSupport.CUSTOM)
  28. public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler) {
  29. return sample(intervalDuration, unit, scheduler);
  30. }

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

  1. @Test
  2. public void emitLastTimed() {
  3. Observable.just(1)
  4. .sample(1, TimeUnit.DAYS, true)
  5. .test()
  6. .assertResult(1);
  7. }

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

  1. /**
  2. * Returns an Observable that emits the most recently emitted item (if any) emitted by the source ObservableSource
  3. * within periodic time intervals.
  4. * <p>
  5. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/sample.png" alt="">
  6. * <dl>
  7. * <dt><b>Scheduler:</b></dt>
  8. * <dd>{@code sample} operates by default on the {@code computation} {@link Scheduler}.</dd>
  9. * </dl>
  10. *
  11. * @param period
  12. * the sampling rate
  13. * @param unit
  14. * the {@link TimeUnit} in which {@code period} is defined
  15. * @return an Observable that emits the results of sampling the items emitted by the source ObservableSource at
  16. * the specified time interval
  17. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  18. * @see #throttleLast(long, TimeUnit)
  19. */
  20. @CheckReturnValue
  21. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  22. public final Observable<T> sample(long period, TimeUnit unit) {
  23. return sample(period, unit, Schedulers.computation());
  24. }

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

  1. @Test
  2. public void emitLastTimedCustomScheduler() {
  3. Observable.just(1)
  4. .sample(1, TimeUnit.DAYS, Schedulers.single(), true)
  5. .test()
  6. .assertResult(1);
  7. }

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

  1. @Test
  2. public void emitLastOther() {
  3. Observable.just(1)
  4. .sample(Observable.timer(1, TimeUnit.DAYS), true)
  5. .test()
  6. .assertResult(1);
  7. }

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

  1. /**
  2. * Returns an Observable that emits the most recently emitted item (if any) emitted by the source ObservableSource
  3. * within periodic time intervals and optionally emit the very last upstream item when the upstream completes.
  4. * <p>
  5. * <img width="640" height="276" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/sample.emitlast.png" alt="">
  6. * <dl>
  7. * <dt><b>Scheduler:</b></dt>
  8. * <dd>{@code sample} operates by default on the {@code computation} {@link Scheduler}.</dd>
  9. * </dl>
  10. *
  11. * <p>History: 2.0.5 - experimental
  12. * @param period
  13. * the sampling rate
  14. * @param unit
  15. * the {@link TimeUnit} in which {@code period} is defined
  16. * @return an Observable that emits the results of sampling the items emitted by the source ObservableSource at
  17. * the specified time interval
  18. * @param emitLast
  19. * if true and the upstream completes while there is still an unsampled item available,
  20. * that item is emitted to downstream before completion
  21. * if false, an unsampled last item is ignored.
  22. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  23. * @see #throttleLast(long, TimeUnit)
  24. * @since 2.1
  25. */
  26. @CheckReturnValue
  27. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  28. public final Observable<T> sample(long period, TimeUnit unit, boolean emitLast) {
  29. return sample(period, unit, Schedulers.computation(), emitLast);
  30. }

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

  1. @Test
  2. public void emitLastTimedEmpty() {
  3. Observable.empty()
  4. .sample(1, TimeUnit.DAYS, true)
  5. .test()
  6. .assertResult();
  7. }

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

  1. Observable<Long> sampled = source.sample(400L, TimeUnit.MILLISECONDS, scheduler);
  2. sampled.subscribe(observer);

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

  1. @Test
  2. public void error() {
  3. Observable.error(new TestException())
  4. .sample(1, TimeUnit.SECONDS)
  5. .test()
  6. .assertFailure(TestException.class);
  7. }

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

  1. @Test
  2. public void emitLastOtherEmpty() {
  3. Observable.empty()
  4. .sample(Observable.timer(1, TimeUnit.DAYS), true)
  5. .test()
  6. .assertResult();
  7. }

代码示例来源:origin: akarnokd/akarnokd-misc

  1. @Test
  2. public void test() throws Exception {
  3. Observable.interval(50, TimeUnit.MILLISECONDS)
  4. .doOnNext(v -> {
  5. if (v % 20 == 0) {
  6. System.out.println("--------- second: " + v / 20);
  7. }
  8. })
  9. .sample(200, TimeUnit.MILLISECONDS)
  10. .subscribe(System.out::println);
  11. Thread.sleep(5000);
  12. }
  13. }

相关文章

Observable类方法