mirror of
https://github.com/qvacua/vimr.git
synced 2024-11-24 11:37:32 +03:00
Refactor slightly
This commit is contained in:
parent
f503516cba
commit
b9922ad859
@ -7,13 +7,13 @@ import XCTest
|
||||
import RxSwift
|
||||
|
||||
class RxMsgpackRpcTests: XCTestCase {
|
||||
|
||||
|
||||
private var connection: MsgpackRpc!
|
||||
private let disposeBag = DisposeBag()
|
||||
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
|
||||
// $ NVIM_LISTEN_ADDRESS=/tmp/nvim.sock nvim --headless $SOMEFILE
|
||||
connection = MsgpackRpc()
|
||||
connection.stream
|
||||
@ -28,139 +28,57 @@ class RxMsgpackRpcTests: XCTestCase {
|
||||
}
|
||||
}, onError: { print("ERROR: \($0)") })
|
||||
.disposed(by: self.disposeBag)
|
||||
|
||||
|
||||
_ = connection.run(at: "/tmp/nvim.sock")
|
||||
.andThen(connection.request(
|
||||
method: "nvim_ui_attach", params: [.int(40), .int(40), .map([:])], expectsReturnValue: true)
|
||||
)
|
||||
method: "nvim_ui_attach",
|
||||
params: [.int(40), .int(40), .map([:])],
|
||||
expectsReturnValue: true
|
||||
))
|
||||
.syncValue()
|
||||
}
|
||||
|
||||
|
||||
override func tearDown() {
|
||||
super.tearDown()
|
||||
_ = self.connection.request(method: "nvim_command", params: [.string("q!")], expectsReturnValue: true).syncValue()
|
||||
try? self.connection
|
||||
.request(
|
||||
method: "nvim_command", params: [.string("q!")],
|
||||
expectsReturnValue: false
|
||||
)
|
||||
.asCompletable()
|
||||
.wait()
|
||||
try? self.connection.stop().wait()
|
||||
}
|
||||
|
||||
|
||||
func testExample() {
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
|
||||
let lineCount = connection
|
||||
.request(method: "nvim_buf_line_count", params: [.int(0)], expectsReturnValue: true)
|
||||
.request(
|
||||
method: "nvim_buf_line_count",
|
||||
params: [.int(0)],
|
||||
expectsReturnValue: true
|
||||
)
|
||||
.syncValue()
|
||||
print(lineCount ?? "???")
|
||||
|
||||
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "mm:ss.SSS"
|
||||
for i in 0...100 {
|
||||
let date = Date()
|
||||
connection
|
||||
.request(method: "nvim_command_output",
|
||||
params: [.string("echo '\(i) \(formatter.string(from: date))'")],
|
||||
expectsReturnValue: true)
|
||||
.subscribe(onSuccess: { response in
|
||||
print(response)
|
||||
}, onError: { error in
|
||||
print(error)
|
||||
})
|
||||
.request(
|
||||
method: "nvim_command_output",
|
||||
params: [.string("echo '\(i) \(formatter.string(from: date))'")],
|
||||
expectsReturnValue: true
|
||||
)
|
||||
.subscribe(
|
||||
onSuccess: { response in print(response) },
|
||||
onError: { error in print(error) }
|
||||
)
|
||||
.disposed(by: disposeBag)
|
||||
}
|
||||
|
||||
sleep(2)
|
||||
}
|
||||
}
|
||||
|
||||
extension PrimitiveSequence
|
||||
where Element == Never, TraitType == CompletableTrait {
|
||||
|
||||
func wait(
|
||||
onCompleted: (() -> Void)? = nil,
|
||||
onError: ((Swift.Error) -> Void)? = nil
|
||||
) throws {
|
||||
var trigger = false
|
||||
var err: Swift.Error? = nil
|
||||
|
||||
let condition = NSCondition()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
|
||||
let disposable = self.subscribe(onCompleted: {
|
||||
onCompleted?()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
onError?(error)
|
||||
err = error
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
disposable.dispose()
|
||||
|
||||
if let e = err {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
|
||||
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? {
|
||||
var trigger = false
|
||||
var value: Element?
|
||||
|
||||
let condition = NSCondition()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
|
||||
let disposable = self.subscribe(onSuccess: { result in
|
||||
value = result
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
disposable.dispose()
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func asCompletable() -> Completable {
|
||||
return self.asObservable().ignoreElements()
|
||||
|
||||
sleep(1)
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
4B0226B3224B5A180052362B /* RxSwift.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4B02266D224ACCE10052362B /* RxSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
|
||||
4B0226B4224B5A310052362B /* ReadersWriterLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B022683224ACFCB0052362B /* ReadersWriterLock.swift */; };
|
||||
4B0226B5224B5A310052362B /* RxMsgpackRpc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BDD5380FFB00320A20C9 /* RxMsgpackRpc.swift */; };
|
||||
4BB1EF8C224B62BB00A5CD5A /* RxSwiftCommons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB1EF8B224B625700A5CD5A /* RxSwiftCommons.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
@ -66,6 +67,7 @@
|
||||
4B0226A5224B58DB0052362B /* RxMsgpackRpcTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RxMsgpackRpcTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
4B0226A7224B58DB0052362B /* RxMsgpackRpcTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxMsgpackRpcTests.swift; sourceTree = "<group>"; };
|
||||
4B0226A9224B58DB0052362B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
4BB1EF8B224B625700A5CD5A /* RxSwiftCommons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxSwiftCommons.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@ -93,6 +95,7 @@
|
||||
4B02263A224AB11A0052362B = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
4BB1EF8B224B625700A5CD5A /* RxSwiftCommons.swift */,
|
||||
4B022683224ACFCB0052362B /* ReadersWriterLock.swift */,
|
||||
4B022684224ACFCB0052362B /* RxMessagePort.swift */,
|
||||
4B02265D224AB1490052362B /* RxMessagePortDemo */,
|
||||
@ -252,6 +255,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
4BB1EF8C224B62BB00A5CD5A /* RxSwiftCommons.swift in Sources */,
|
||||
4B0226B4224B5A310052362B /* ReadersWriterLock.swift in Sources */,
|
||||
4B0226B5224B5A310052362B /* RxMsgpackRpc.swift in Sources */,
|
||||
4B0226A8224B58DB0052362B /* RxMsgpackRpcTests.swift in Sources */,
|
||||
|
102
RxPack/RxSwiftCommons.swift
Normal file
102
RxPack/RxSwiftCommons.swift
Normal file
@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Tae Won Ha - http://taewon.de - @hataewon
|
||||
* See LICENSE
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import RxSwift
|
||||
|
||||
extension PrimitiveSequence
|
||||
where Element == Never, TraitType == CompletableTrait {
|
||||
|
||||
func wait(
|
||||
onCompleted: (() -> Void)? = nil,
|
||||
onError: ((Error) -> Void)? = nil
|
||||
) throws {
|
||||
var trigger = false
|
||||
var err: Error? = nil
|
||||
|
||||
let condition = NSCondition()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
|
||||
let disposable = self.subscribe(onCompleted: {
|
||||
onCompleted?()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
onError?(error)
|
||||
err = error
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
disposable.dispose()
|
||||
|
||||
if let e = err {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PrimitiveSequence where TraitType == SingleTrait {
|
||||
|
||||
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? {
|
||||
var trigger = false
|
||||
var value: Element?
|
||||
|
||||
let condition = NSCondition()
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
|
||||
let disposable = self.subscribe(onSuccess: { result in
|
||||
value = result
|
||||
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
}, onError: { error in
|
||||
condition.lock()
|
||||
defer { condition.unlock() }
|
||||
trigger = true
|
||||
condition.broadcast()
|
||||
})
|
||||
|
||||
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
|
||||
disposable.dispose()
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func asCompletable() -> Completable {
|
||||
return self.asObservable().ignoreElements()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user