LoginSignup
7
6

More than 5 years have passed since last update.

RxSwiftでthrottleFirst

Posted at

RxSwiftにはthrottleFirstオペレータがなかったのでつくってみました。一発イベントが来たらその後一定時間はイベントを無視します。チャタリングの防止によく使われる仕組みです。


extension ObservableType {
    func throttleFirst(time: RxTimeInterval, scheduler: SchedulerType) -> Observable<E> {
        let s = self.share()

        return s
            .throttle(time, scheduler: scheduler)
            .map { _ in () }
            .startWith()
            .flatMapLatest{ _ in s.take(1) }
    }
}

こちらにC#の実装を書いてくれていますが、RxSwiftにはwindowオペレータもなかったので考えてみました。頭の体操みたいなものですね。
http://qiita.com/Temarin_PITA/items/0625ac12912388ea73a4

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6