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

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

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

Observable.throttleFirst介绍

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

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

Scheduler: throttleFirst operates by default on the computation Scheduler.
[中]返回在指定持续时间的连续时间窗口内仅发射源ObservableSource发射的第一项的Observable。
这与#throttleLast的不同之处在于,它只跟踪时间的流逝,而#throttleLast按预定的间隔滴答作响。
调度程序:throttleFirst默认在计算调度程序上运行。

代码示例

代码示例来源:origin: amitshekhariitbhu/RxJava2-Android-Samples

  1. private void doSomeWork() {
  2. getObservable()
  3. .throttleFirst(500, TimeUnit.MILLISECONDS)
  4. // Run on a background thread
  5. .subscribeOn(Schedulers.io())
  6. // Be notified on the main thread
  7. .observeOn(AndroidSchedulers.mainThread())
  8. .subscribe(getObserver());
  9. }

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

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

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

  1. /**
  2. * Returns an Observable that emits only the first item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleLast} in that this only tracks 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>Scheduler:</b></dt>
  11. * <dd>{@code throttleFirst} operates by default on the {@code computation} {@link Scheduler}.</dd>
  12. * </dl>
  13. *
  14. * @param windowDuration
  15. * time to wait before emitting another item after emitting the last item
  16. * @param unit
  17. * the unit of time of {@code windowDuration}
  18. * @return an Observable that performs the throttle operation
  19. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  20. */
  21. @CheckReturnValue
  22. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  23. public final Observable<T> throttleFirst(long windowDuration, TimeUnit unit) {
  24. return throttleFirst(windowDuration, unit, Schedulers.computation());
  25. }

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

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

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

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

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

  1. /**
  2. * Returns an Observable that emits only the first item emitted by the source ObservableSource during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleLast} in that this only tracks 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>Scheduler:</b></dt>
  11. * <dd>{@code throttleFirst} operates by default on the {@code computation} {@link Scheduler}.</dd>
  12. * </dl>
  13. *
  14. * @param windowDuration
  15. * time to wait before emitting another item after emitting the last item
  16. * @param unit
  17. * the unit of time of {@code windowDuration}
  18. * @return an Observable that performs the throttle operation
  19. * @see <a href="http://reactivex.io/documentation/operators/sample.html">ReactiveX operators documentation: Sample</a>
  20. */
  21. @CheckReturnValue
  22. @SchedulerSupport(SchedulerSupport.COMPUTATION)
  23. public final Observable<T> throttleFirst(long windowDuration, TimeUnit unit) {
  24. return throttleFirst(windowDuration, unit, Schedulers.computation());
  25. }

代码示例来源:origin: iMeiji/Toutiao

  1. @Override
  2. protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final MediaChannelBean item) {
  3. try {
  4. final Context context = holder.itemView.getContext();
  5. String url = item.getAvatar();
  6. ImageLoader.loadCenterCrop(context, url, holder.cv_avatar, R.color.viewBackground);
  7. holder.tv_mediaName.setText(item.getName());
  8. holder.tv_descText.setText(item.getDescText());
  9. RxView.clicks(holder.itemView)
  10. .throttleFirst(1, TimeUnit.SECONDS)
  11. .subscribe(o -> MediaHomeActivity.launch(item.getId()));
  12. } catch (Exception e) {
  13. ErrorAction.print(e);
  14. }
  15. }

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

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

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

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

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

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

代码示例来源:origin: iMeiji/Toutiao

  1. @Override
  2. protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final WendaContentBean.AnsListBean item) {
  3. try {
  4. String iv_user_avatar = item.getUser().getAvatar_url();
  5. ImageLoader.loadCenterCrop(holder.itemView.getContext(), iv_user_avatar, holder.iv_user_avatar, R.color.viewBackground);
  6. String tv_user_name = item.getUser().getUname();
  7. String tv_like_count = item.getDigg_count() + "";
  8. String tv_abstract = item.getContent_abstract().getText();
  9. holder.tv_user_name.setText(tv_user_name);
  10. holder.tv_like_count.setText(tv_like_count);
  11. holder.tv_abstract.setText(tv_abstract);
  12. RxView.clicks(holder.itemView)
  13. .throttleFirst(1, TimeUnit.SECONDS)
  14. .subscribe(o -> WendaDetailActivity.launch(item));
  15. } catch (Exception e) {
  16. ErrorAction.print(e);
  17. }
  18. }

代码示例来源:origin: iMeiji/Toutiao

  1. @Override
  2. protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final WendaArticleDataBean item) {
  3. try {
  4. String tv_title = item.getQuestionBean().getTitle();
  5. String tv_answer_count = item.getQuestionBean().getNormal_ans_count() + item.getQuestionBean().getNice_ans_count() + "回答";
  6. String tv_datetime = item.getQuestionBean().getCreate_time() + "";
  7. if (!TextUtils.isEmpty(tv_datetime)) {
  8. tv_datetime = TimeUtil.getTimeStampAgo(tv_datetime);
  9. }
  10. String tv_content = item.getAnswerBean().getAbstractX();
  11. holder.tv_title.setText(tv_title);
  12. holder.tv_title.setTextSize(SettingUtil.getInstance().getTextSize());
  13. holder.tv_answer_count.setText(tv_answer_count);
  14. holder.tv_time.setText(tv_datetime);
  15. holder.tv_content.setText(tv_content);
  16. RxView.clicks(holder.itemView)
  17. .throttleFirst(1, TimeUnit.SECONDS)
  18. .subscribe(o -> WendaContentActivity.launch(item.getQuestionBean().getQid() + ""));
  19. } catch (Exception e) {
  20. ErrorAction.print(e);
  21. }
  22. }

代码示例来源:origin: iMeiji/Toutiao

  1. @Override
  2. protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final WendaArticleDataBean item) {
  3. final Context context = holder.itemView.getContext();
  4. try {
  5. String url = item.getExtraBean().getWenda_image().getLarge_image_list().get(0).getUrl();
  6. ImageLoader.loadCenterCrop(context, url, holder.iv_image_big, R.color.viewBackground);
  7. final String tv_title = item.getQuestionBean().getTitle();
  8. String tv_answer_count = item.getQuestionBean().getNormal_ans_count() + item.getQuestionBean().getNice_ans_count() + "回答";
  9. String tv_datetime = item.getQuestionBean().getCreate_time() + "";
  10. if (!TextUtils.isEmpty(tv_datetime)) {
  11. tv_datetime = TimeUtil.getTimeStampAgo(tv_datetime);
  12. }
  13. holder.tv_title.setText(tv_title);
  14. holder.tv_title.setTextSize(SettingUtil.getInstance().getTextSize());
  15. holder.tv_answer_count.setText(tv_answer_count);
  16. holder.tv_time.setText(tv_datetime);
  17. RxView.clicks(holder.itemView)
  18. .throttleFirst(1, TimeUnit.SECONDS)
  19. .subscribe(o -> WendaContentActivity.launch(item.getQuestionBean().getQid() + ""));
  20. } catch (Exception e) {
  21. ErrorAction.print(e);
  22. }
  23. }

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribeOn(Schedulers.io())
  3. .switchMap((Function<String, ObservableSource<NewsContentBean>>) s -> RetrofitFactory.getRetrofit().create(INewsApi.class).getNewsContent(s))

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribe(o -> NewsContentActivity.launch(item, finalImgUrl));
  3. } catch (Exception e) {

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribe(o -> NewsContentActivity.launch(item));
  3. } catch (Exception e) {

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribe(o -> WendaContentActivity.launch(item.getQuestionBean().getQid() + ""));
  3. } catch (Exception e) {

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribe(o -> {
  3. MultiNewsArticleDataBean bean = new MultiNewsArticleDataBean();

代码示例来源:origin: iMeiji/Toutiao

  1. .throttleFirst(1, TimeUnit.SECONDS)
  2. .subscribe(o -> {
  3. WendaContentBean.AnsListBean ansBean = new WendaContentBean.AnsListBean();

代码示例来源:origin: iMeiji/Toutiao

  1. RxView.clicks(tv_refresh)
  2. .throttleFirst(1, TimeUnit.SECONDS)
  3. .as(this.bindAutoDispose())
  4. .subscribe(o -> {

相关文章

Observable类方法