Created
June 25, 2016 18:04
-
-
Save cqfd/aceedd02223c0f0c15ec291a410dfefe to your computer and use it in GitHub Desktop.
Some extensions for RxSwift Variables, motivated by applicatives.
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
extension Variable { | |
func debug() -> Self { | |
_ = self.asObservable().debug().subscribeNext { _ in } | |
return self | |
} | |
static func combineLatest<A, B>(a: Variable<A>, _ b: Variable<B>, resultSelector: (A, B) -> E) -> Variable<E> { | |
let v = Variable(resultSelector(a.value, b.value)) | |
_ = Observable.combineLatest(a.asObservable().skip(1), b.asObservable().skip(1), resultSelector: resultSelector).subscribeNext { e in | |
v.value = e | |
} | |
return v | |
} | |
static func combineLatest<A,B,C>(a: Variable<A>, _ b: Variable<B>, _ c: Variable<C>, resultSelector: (A, B, C) -> E) -> Variable<E> { | |
let v = Variable(resultSelector(a.value, b.value, c.value)) | |
_ = Observable.combineLatest(a.asObservable().skip(1), b.asObservable().skip(1), c.asObservable().skip(1), resultSelector: resultSelector).subscribeNext { e in | |
v.value = e | |
} | |
return v | |
} | |
func map<R>(selector: E -> R) -> Variable<R> { | |
let v = Variable<R>(selector(self.value)) | |
_ = self.asObservable().skip(1).subscribeNext { e in | |
v.value = selector(e) | |
} | |
return v | |
} | |
func flatMapLatest<R>(selector: E -> Variable<R>) -> Variable<R> { | |
let currentVariable = Variable<Variable<R>>(selector(self.value)) | |
_ = self.asObservable().skip(1).subscribeNext { e in | |
currentVariable.value = selector(e) | |
} | |
let currentValue = Variable<R>(currentVariable.value.value) | |
_ = currentVariable.value.asObservable().skip(1).subscribeNext { r in | |
currentValue.value = r | |
} | |
return currentValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment