Last active
July 29, 2016 14:33
-
-
Save devxoul/e25851fda719d5201c79d64ad8e78ea3 to your computer and use it in GitHub Desktop.
RxSwift: Filters the elements of an observable sequence based on an ActivityIndicator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment