Я использую это в 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
})
Это функция селектора debounce, используемая в 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 мс или нажимает другой элемент
Возможно ли это сделать в RxSwift?