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

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

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

Observable.throttleFirst介绍

[英]Returns an Observable that emits only the first item emitted by the source Observable 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.

Backpressure Support: 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: HotBitmapGG/bilibili-android-client

private void search() {
  RxView.clicks(mSearchBtn)
      .throttleFirst(2, TimeUnit.SECONDS)
      .map(aVoid -> mSearchEdit.getText().toString().trim())
      .filter(s -> !TextUtils.isEmpty(s))
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(s -> {
        KeyBoardUtil.closeKeybord(mSearchEdit, TotalStationSearchActivity.this);
        showSearchAnim();
        clearData();
        content = s;
        getSearchData();
      });
}

代码示例来源:origin: cn-ljb/rxjava_for_android

/**
   * 3秒内不允许按钮多次点击
   * */
  private void notMoreClick() {
    RxView.clicks(btn_click)
        .throttleFirst(3, TimeUnit.SECONDS)
        .subscribe(new Action1<Void>() {
          @Override
          public void call(Void aVoid) {
            Toast.makeText(getActivity(), R.string.des_demo_not_more_click, Toast.LENGTH_SHORT).show();
          }
        });
  }
}

代码示例来源:origin: THEONE10211024/RxJavaSamples

@OnClick(R.id.btn_click)
public void btnClick(){
  RxView.clicks(btnClick)
     .throttleFirst(1, TimeUnit.SECONDS)
     .subscribe(new Observer<Object>() {
       @Override
       public void onCompleted() {
       }
       @Override
       public void onError(Throwable e) {
       }
       @Override
       public void onNext(Object o) {
         Toast.makeText(getContext(),"Click",Toast.LENGTH_SHORT).show();
       }
     });
}
@Override

代码示例来源:origin: jhusain/learnrxjava

public static void main(String args[]) {
  // first item emitted in each time window
  hotStream().throttleFirst(500, TimeUnit.MILLISECONDS).take(10).toBlocking().forEach(System.out::println);
  
  // last item emitted in each time window
  hotStream().throttleLast(500, TimeUnit.MILLISECONDS).take(10).toBlocking().forEach(System.out::println);
}

代码示例来源:origin: henrymorgen/android-advanced-light

private void throttleFirst() {
  Observable.create(new Observable.OnSubscribe<Integer>() {
    @Override
    public void call(Subscriber<? super Integer> subscriber) {
       for(int i=0;i<10;i++){
         subscriber.onNext(i);
         try {
           Thread.sleep(100);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }
      subscriber.onCompleted();
    }
  }).throttleFirst(200, TimeUnit.MILLISECONDS).subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
      Log.d(TAG, "throttleFirst:"+integer);
    }
  });
}

代码示例来源:origin: mickyliu945/CommonProj

public ItemViewHolder(final View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    RxView.clicks(itemView).throttleFirst(1000, TimeUnit.MILLISECONDS).subscribe(new Action1<Void>() {
      @Override
      public void call(Void aVoid) {
        mOnPlaceClickListener.onClick(itemView, place);
      }
    });
  }
}

代码示例来源:origin: androidmalin/RengwuxianRxjava

/**
 * RxBinding
 */
private void method17() {
  RxView.longClicks(mImageView)
      .throttleFirst(500, TimeUnit.MILLISECONDS)
      .subscribe(new Action1<Void>() {
        @Override
        public void call(Void aVoid) {
          Toast.makeText(MainActivity.this, "long click", Toast.LENGTH_SHORT).show();
        }
      });
}

代码示例来源:origin: leeowenowen/rxjava-examples

@Override
 public void run() {
  final Subscription subscription = Observable.create(new Observable.OnSubscribe<Integer>() {
   @Override
   public void call(Subscriber<? super Integer> subscriber) {
    for (int i = 0; i < 5; i++) {
     subscriber.onNext(i);
     sleep(300);
    }
   }
  }).throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Integer>() {
   @Override
   public void call(Integer integer) {
    log(integer);
   }
  });
  AsyncExecutor.SINGLETON.schedule(new Runnable() {
   @Override
   public void run() {
    if (!subscription.isUnsubscribed()) {
     subscription.unsubscribe();
    }
   }
  }, 3, TimeUnit.SECONDS);
 }
});

代码示例来源:origin: mingjunli/GithubApp

@CheckResult
  @NonNull
  public static Observable<Void> clicks(@NonNull View view) {
    return RxView.clicks(view)
        .throttleFirst(500, TimeUnit.MILLISECONDS);
  }
}

代码示例来源:origin: androidmalin/RengwuxianRxjava

/**
 * RxBinding
 * RxBinding 是 Jake Wharton 的一个开源库,它提供了一套在 Android 平台上的基于 RxJava 的 Binding API。
 * 所谓 Binding,就是类似设置 OnClickListener 、设置 TextWatcher 这样的注册绑定对象的 API。
 * 举个设置点击监听的例子。使用 RxBinding ,可以把事件监听用这样的方法来设置:
 * throttleFirst() ,用于去抖动,也就是消除手抖导致的快速连环点击:
 */
private void method16() {
  RxView.clicks(mImageView)
      .throttleFirst(500, TimeUnit.MILLISECONDS)//500ms,第一次点击后,500ms内点击无效,500ms后点击才会响应
      .subscribeOn(AndroidSchedulers.mainThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Action1<Void>() {
        @Override
        public void call(Void aVoid) {
          Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
        }
      });
}

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

public static void main(String[] args) {
    Observable.just(100, 2000, 3000, 6000, 7000, 10000)
    .flatMap(time -> Observable.timer(time, TimeUnit.MILLISECONDS).map(v -> time))
    .throttleFirst(4000, TimeUnit.MILLISECONDS)
    .toBlocking()
    .forEach(System.out::println);
  }
}

代码示例来源:origin: vihuela/RAD

/**
 * 防止重复点击
 */
public static void singleClick(final View v, final View.OnClickListener onClickListener) {
  RxView.clicks(v)
      .throttleFirst(1, TimeUnit.SECONDS)
      .subscribe(new Observer<Object>() {
        @Override
        public void onCompleted() {
        }
        @Override
        public void onError(Throwable e) {
          e.printStackTrace();
        }
        @Override
        public void onNext(Object o) {
          onClickListener.onClick(v);
        }
      });
}

代码示例来源:origin: com.netflix.rxjava/rxjava-core

/**
 * Returns an Observable that emits only the first item emitted by the source Observable during sequential
 * time windows of a specified duration.
 * <p>
 * This differs from {@link #throttleLast} in that this only tracks passage of time whereas
 * {@link #throttleLast} ticks at scheduled intervals.
 * <p>
 * <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/throttleFirst.png" alt="">
 * <dl>
 *  <dt><b>Backpressure Support:</b></dt>
 *  <dd>This operator does not support backpressure as it uses time to control data flow.</dd>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code throttleFirst} operates by default on the {@code computation} {@link Scheduler}.</dd>
 * </dl>
 * 
 * @param windowDuration
 *            time to wait before emitting another item after emitting the last item
 * @param unit
 *            the unit of time of {@code windowDuration}
 * @return an Observable that performs the throttle operation
 * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#throttlefirst">RxJava wiki: throttleFirst</a>
 * @see <a href="https://github.com/Netflix/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
 */
public final Observable<T> throttleFirst(long windowDuration, TimeUnit unit) {
  return throttleFirst(windowDuration, unit, Schedulers.computation());
}

代码示例来源:origin: xiaolongonly/Ticket-Analysis

public Observable ClickView(View view) {
  return RxView.clicks(view).throttleFirst(BuildConfig.TIME_CLICK_IGNORE, TimeUnit.MILLISECONDS).compose(bindUntilEvent(FragmentEvent.DESTROY));
}

代码示例来源:origin: xiaolongonly/Ticket-Analysis

public Observable ClickView(View view) {
  return RxView.clicks(view).throttleFirst(BuildConfig.TIME_CLICK_IGNORE, TimeUnit.MILLISECONDS).compose(bindUntilEvent(ActivityEvent.DESTROY));
}

代码示例来源:origin: fanturbo/Kanzhibo

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_search);
  mPlatForms = getResources().getStringArray(R.array.platform);
  viewPager.setAdapter(new SearchFragmentStatePagerAdapter(getSupportFragmentManager(), Arrays.asList(mPlatForms)));
  viewPager.setOffscreenPageLimit(5);
  tabLayout.setupWithViewPager(viewPager);
  //给editText内部的drawable添加点击事件
  searchKeyEditText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_UP) {
        if (event.getRawX() >= searchKeyEditText.getRight() - searchKeyEditText.getTotalPaddingRight()) {
          searchKeyEditText.setText("");
          return true;
        }
      }
      return false;
    }
  });
  RxView.clicks(mIbSearch).throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
    @Override
    public void call(Void aVoid) {
      RxBus.get().post(new SearchEvent(searchKeyEditText.getText().toString().trim()));
    }
  });
}

代码示例来源:origin: nurkiewicz/rxjava-book-examples

@Test
public void sample_73() throws Exception {
  Observable<String> delayedNames = delayedNames();
  delayedNames
      .throttleFirst(1, SECONDS)
      .subscribe(System.out::println);
}

代码示例来源:origin: oVirt/moVirt

private rx.Observable<List<E>> asObservableInternal(int throttle) {
  return briteResolver.createQuery(baseUri,
      projection,
      selection.toString(),
      getSelectionArgs(),
      sortOrderWithLimit(), true)
      .mapToList(cursor -> EntityMapper.forEntity(clazz).fromCursor(cursor))
      .throttleFirst(throttle, TimeUnit.MILLISECONDS);
}

代码示例来源:origin: oVirt/moVirt

public Observable<E> singleAsObservable() {
  rx.Observable<E> o = briteResolver.createQuery(baseUri,
      projection,
      selection.toString(),
      getSelectionArgs(),
      sortOrderWithLimit(" LIMIT 1 "), true)
      .mapToOne(cursor -> EntityMapper.forEntity(clazz).fromCursor(cursor))
      .throttleFirst(THROTTLE_BATCH, TimeUnit.MILLISECONDS);
  return RxJavaInterop.toV2Observable(o);
}

代码示例来源:origin: mickyliu945/CommonProj

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main, true);

    final ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.mipmap.menu);
    ab.setDisplayHomeAsUpEnabled(true);

    initView();

    mMainPresenter = new MainPresenterImpl(this);
    mMainPresenter.onCreate();
//        mMainPresenter.getPlaceData();
//        mMainPresenter.getWeatherData("北京");

    mMainPresenter.getPlaceAndWeatherData("北京");
    RxView.clicks(mFloatingActionBar).throttleFirst(1000, TimeUnit.MILLISECONDS).subscribe(new Action1<Void>() {
      @Override
      public void call(Void aVoid) {
        mMainPresenter.onRefresh();
      }
    });

  }

相关文章

Observable类方法