1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

Refactor slightly

This commit is contained in:
Tae Won Ha 2019-03-23 10:55:08 +01:00
parent bd37492b81
commit fe6dc0381c
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 43 additions and 32 deletions

View File

@ -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()
}

View File

@ -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()
}