mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-25 23:02:35 +03:00
Refactor slightly
This commit is contained in:
parent
bd37492b81
commit
fe6dc0381c
@ -6,7 +6,8 @@
|
||||
import Foundation
|
||||
import RxSwift
|
||||
|
||||
extension PrimitiveSequence where Element == Never, TraitType == CompletableTrait {
|
||||
extension PrimitiveSequence
|
||||
where Element == Never, TraitType == CompletableTrait {
|
||||
|
||||
func wait(
|
||||
onCompleted: (() -> Void)? = nil,
|
||||
@ -22,15 +23,19 @@ extension PrimitiveSequence where Element == Never, TraitType == CompletableTrai
|
||||
|
||||
let disposable = self.subscribe(onCompleted: {
|
||||
onCompleted?()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
onError?(error)
|
||||
condition.lock()
|
||||
trigger = true
|
||||
err = error
|
||||
broadcast(condition)
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
@ -44,8 +49,22 @@ extension PrimitiveSequence where Element == Never, TraitType == CompletableTrai
|
||||
|
||||
extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
|
||||
static func fromSinglesToSingleOfArray(_ singles: [Single<Element>]) -> Single<[Element]> {
|
||||
return Observable.merge(singles.map { $0.asObservable() }).toArray().asSingle()
|
||||
static func fromSinglesToSingleOfArray(
|
||||
_ singles: [Single<Element>]
|
||||
) -> Single<[Element]> {
|
||||
return Observable
|
||||
.merge(singles.map { $0.asObservable() })
|
||||
.toArray()
|
||||
.asSingle()
|
||||
}
|
||||
|
||||
func flatMapCompletable(
|
||||
_ selector: @escaping (Element) throws -> Completable
|
||||
) -> Completable {
|
||||
return self
|
||||
.asObservable()
|
||||
.flatMap { try selector($0).asObservable() }
|
||||
.ignoreElements()
|
||||
}
|
||||
|
||||
func syncValue() -> Element? {
|
||||
@ -59,13 +78,16 @@ extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
|
||||
let disposable = self.subscribe(onSuccess: { result in
|
||||
value = result
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
@ -74,19 +96,7 @@ extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
return value
|
||||
}
|
||||
|
||||
func flatMapCompletable(_ selector: @escaping (Element) throws -> Completable) -> Completable {
|
||||
return self
|
||||
.asObservable()
|
||||
.flatMap { try selector($0).asObservable() }
|
||||
.ignoreElements()
|
||||
}
|
||||
|
||||
func asCompletable() -> Completable {
|
||||
return self.asObservable().ignoreElements()
|
||||
}
|
||||
}
|
||||
|
||||
private func broadcast(_ condition: NSCondition) {
|
||||
condition.broadcast()
|
||||
condition.unlock()
|
||||
}
|
||||
|
@ -38,13 +38,16 @@ extension PrimitiveSequence
|
||||
|
||||
let disposable = self.subscribe(onCompleted: {
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
condition.lock()
|
||||
trigger = true
|
||||
err = error
|
||||
broadcast(condition)
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
@ -87,13 +90,16 @@ extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
|
||||
let disposable = self.subscribe(onSuccess: { result in
|
||||
value = result
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
broadcast(condition)
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
@ -102,8 +108,3 @@ extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
private func broadcast(_ condition: NSCondition) {
|
||||
defer { condition.unlock() }
|
||||
condition.broadcast()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user