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

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

本文整理了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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@Test
public void emitLastTimed() {
  Observable.just(1)
  .sample(1, TimeUnit.DAYS, true)
  .test()
  .assertResult(1);
}

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

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

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

@Test
public void emitLastTimedCustomScheduler() {
  Observable.just(1)
  .sample(1, TimeUnit.DAYS, Schedulers.single(), true)
  .test()
  .assertResult(1);
}

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

@Test
public void emitLastOther() {
  Observable.just(1)
  .sample(Observable.timer(1, TimeUnit.DAYS), true)
  .test()
  .assertResult(1);
}

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

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

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

@Test
public void emitLastTimedEmpty() {
  Observable.empty()
  .sample(1, TimeUnit.DAYS, true)
  .test()
  .assertResult();
}

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

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

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

@Test
public void error() {
  Observable.error(new TestException())
  .sample(1, TimeUnit.SECONDS)
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void emitLastOtherEmpty() {
  Observable.empty()
  .sample(Observable.timer(1, TimeUnit.DAYS), true)
  .test()
  .assertResult();
}

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

@Test
  public void test() throws Exception {

    Observable.interval(50, TimeUnit.MILLISECONDS)
    .doOnNext(v -> {
      if (v % 20 == 0) {
        System.out.println("--------- second: " + v / 20);
      }
    })
    .sample(200, TimeUnit.MILLISECONDS)
    .subscribe(System.out::println);

    Thread.sleep(5000);
  }
}

相关文章

Observable类方法