根据条件对 rx 可观察值进行反跳 - RxSwift

问题描述 投票:0回答:2

我在 Kotlin 中使用它来根据条件进行反跳:

// variables
private val subject_isUpdating = PublishSubject.create<Int>()
var lastClickedItem = -1


// inside onCreate
adapter_cartProducts.setOnItemClickedListener { position ->
     subject_isUpdating.onNext(position)
}


// subscribing 
        subject_isUpdating
            .debounce
            { position ->
                // here if lastClickedItem changed, no debounce
                if(position != lastClickedItem) {
                    lastClickedItem = position
                    Observable.empty()
                }
                // else if same item clicked -> debounce
                else Observable.timer(300, TimeUnit.MILLISECONDS) }
            .subscribe({ position ->
                updateOnWS(position, adapter_cartProducts.items[position])
            }, { error ->
                Timber.e(error) // printing the error
            })

这是 RxJava 使用的去抖动选择器函数:

/**
 * Returns an Observable that mirrors the source ObservableSource, except that it drops items emitted by the
 * source ObservableSource that are followed by another item within a computed debounce duration.
 * <p>
 * <img width="640" height="425" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/debounce.f.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>This version of {@code debounce} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param <U>
 *            the debounce value type (ignored)
 * @param debounceSelector
 *            function to retrieve a sequence that indicates the throttle duration for each item
 * @return an Observable that omits items emitted by the source ObservableSource that are followed by another item
 *         within a computed debounce duration
 * @see <a href="http://reactivex.io/documentation/operators/debounce.html">ReactiveX operators documentation: Debounce</a>
 */
public final <U> Observable<T> debounce(Function<? super T, ? extends ObservableSource<U>> debounceSelector) {
    ObjectHelper.requireNonNull(debounceSelector, "debounceSelector is null");
    return RxJavaPlugins.onAssembly(new ObservableDebounce<T, U>(this, debounceSelector));
}

此代码的想法,用户将单击列表中的项目,当用户停止单击 400 毫秒或单击另一个项目时,该项目将在 Web 服务上更新

这可以在 RxSwift 中完成吗?

swift rx-swift
2个回答
2
投票

我不能说我特别喜欢您提供的代码,因为它依赖于外部变量。

这是一个可以完成您想要的操作的运算符:

extension ObservableType where E: Equatable {

    func throttleUnlessChanged(_ dueTime: TimeInterval, scheduler: SchedulerType) -> Observable<E> {
        return Observable.create { observer in
            let lock = NSRecursiveLock()
            var last: E?
            var lastTime: RxTime?
            return self.subscribe { event in
                lock.lock(); defer { lock.unlock() }
                switch event {
                case .next(let element):
                    let now = scheduler.now
                    let timeIntervalSinceLast = lastTime != nil ? now.timeIntervalSince(lastTime!) : dueTime
                    if element != last {
                        observer.onNext(element)
                        last = element
                        lastTime = now
                    }
                    else if timeIntervalSinceLast >= dueTime {
                        observer.onNext(element)
                        last = element
                        lastTime = now
                    }
                case .error(let error):
                    observer.onError(error)
                case .completed:
                    observer.onCompleted()
                }
            }
        }
    }
}

这是包含测试的要点:https://gist.github.com/dtartaglia/f5b041facfdcdd64630e0cb8cfc2cc5b


0
投票

我觉得flatMapLatest可以用来解决这里需要的条件去抖。

subject
     .flatMapLatest { position in
           if position != lastClickedPosition {
               lastClickedItem = position
               return Observable.just(position)
           } else {
                return Observable.just(position).delay(.milliseconds(300), scheduler: scheduler)
           }
     }
// Subscribe 

我有一篇关于使用组合和 RxSwift 创建变量去抖动的文章。请检查一下。 https://medium.com/dev-genius/part-1-variable-time-debounce-in-swift-30a987511c6c

© www.soinside.com 2019 - 2024. All rights reserved.