swift RxSwift:基于ActivityIndicator过滤可观察序列的元素。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift RxSwift:基于ActivityIndicator过滤可观察序列的元素。相关的知识,希望对你有一定的参考价值。
// The MIT License (MIT)
//
// Copyright (c) 2016 Suyeol Jeon (xoul.kr)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import RxSwift
public typealias ActivityIndicatorFilter = (activityIndicator: ActivityIndicator, condition: Bool)
public extension ObservableConvertibleType {
/// Filters the elements of an observable sequence based on an ActivityIndicator.
///
/// let loading = ActivityIndicator()
/// source
/// .filter(!loading)
/// .flatMap { ... }
/// .subscribe { ... }
///
@warn_unused_result(message="http://git.io/rxs.uo")
public func filter(activityIndicator: ActivityIndicator) -> Observable<Self.E> {
return self.filter(activityIndicator == true)
}
@warn_unused_result(message="http://git.io/rxs.uo")
public func filter(activityIndicatorFilter: ActivityIndicatorFilter) -> Observable<Self.E> {
let (activityIndicator, condition) = activityIndicatorFilter
let count = self.asObservable().scan(UInt(0)) { $0.0 + 1 }
let source = Observable.combineLatest(self.asObservable(), count) { ($0, $1) }
let loading = activityIndicator.asObservable()
return Observable
.combineLatest(source, loading) { (source: (element: E, count: UInt), loading: Bool) in
return (source.element, source.count, loading)
}
.distinctUntilChanged { (old: (element: E, count: UInt, loading: Bool),
new: (element: E, count: UInt, loading: Bool)) in
return old.count == new.count
}
.filter { (_, _, loading) -> Bool in
return loading == condition
}
.map { (element, _, _) -> E in
return element
}
}
}
prefix operator ! {}
public prefix func ! (activityIndicator: ActivityIndicator) -> ActivityIndicatorFilter {
return activityIndicator == false
}
public prefix func ! (activityIndicatorFilter: ActivityIndicatorFilter) -> ActivityIndicatorFilter {
return (activityIndicatorFilter.activityIndicator, !activityIndicatorFilter.condition)
}
func == (activityIndicator: ActivityIndicator, condition: Bool) -> ActivityIndicatorFilter {
return (activityIndicator, condition)
}
以上是关于swift RxSwift:基于ActivityIndicator过滤可观察序列的元素。的主要内容,如果未能解决你的问题,请参考以下文章
RXSwift的一些基本交互(OC,Swift,RXSwift对比)