diff --git a/NvimView/NvimView/RxSwiftCommons.swift b/NvimView/NvimView/RxSwiftCommons.swift index 90060ca4..430b2258 100644 --- a/NvimView/NvimView/RxSwiftCommons.swift +++ b/NvimView/NvimView/RxSwiftCommons.swift @@ -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]) -> Single<[Element]> { - return Observable.merge(singles.map { $0.asObservable() }).toArray().asSingle() + static func fromSinglesToSingleOfArray( + _ singles: [Single] + ) -> 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() -} diff --git a/VimR/VimR/RxSwiftCommons.swift b/VimR/VimR/RxSwiftCommons.swift index 267bda04..92814ab2 100644 --- a/VimR/VimR/RxSwiftCommons.swift +++ b/VimR/VimR/RxSwiftCommons.swift @@ -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() -}