1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-28 08:13:17 +03:00
vimr/RxPack/RxNeovimApi.swift

94 lines
2.2 KiB
Swift
Raw Normal View History

2019-03-27 11:16:29 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import RxSwift
public final class RxNeovimApi {
public enum Event {
case error(msg: String)
}
public struct Buffer: Equatable {
2020-01-25 22:56:24 +03:00
public static func ==(lhs: Buffer, rhs: Buffer) -> Bool { lhs.handle == rhs.handle }
2019-03-27 11:16:29 +03:00
public let handle: Int
2020-01-25 22:56:24 +03:00
public init(_ handle: Int) { self.handle = handle }
2019-03-27 11:16:29 +03:00
}
public struct Window: Equatable {
2020-01-25 22:56:24 +03:00
public static func ==(lhs: Window, rhs: Window) -> Bool { lhs.handle == rhs.handle }
2019-03-27 11:16:29 +03:00
public let handle: Int
2020-01-25 22:56:24 +03:00
public init(_ handle: Int) { self.handle = handle }
2019-03-27 11:16:29 +03:00
}
public struct Tabpage: Equatable {
2020-01-25 22:56:24 +03:00
public static func ==(lhs: Tabpage, rhs: Tabpage) -> Bool { lhs.handle == rhs.handle }
2019-03-27 11:16:29 +03:00
public let handle: Int
2020-01-25 22:56:24 +03:00
public init(_ handle: Int) { self.handle = handle }
2019-03-27 11:16:29 +03:00
}
public typealias Value = RxMsgpackRpc.Value
public var streamResponses: Bool {
2020-01-25 22:56:24 +03:00
get { self.msgpackRpc.streamResponses }
set { self.msgpackRpc.streamResponses = newValue }
2019-03-27 11:16:29 +03:00
}
public var streamRawResponses: Bool {
2020-01-25 22:56:24 +03:00
get { self.msgpackRpc.streamResponses }
set { self.msgpackRpc.streamResponses = newValue }
2019-03-27 11:16:29 +03:00
}
2020-01-25 22:56:24 +03:00
public var msgpackRawStream: Observable<RxMsgpackRpc.Message> { self.msgpackRpc.stream }
2019-03-27 11:16:29 +03:00
2020-01-25 22:56:24 +03:00
public func run(at path: String) -> Completable { self.msgpackRpc.run(at: path) }
2019-03-27 11:16:29 +03:00
2020-01-25 22:56:24 +03:00
public func stop() -> Completable { self.msgpackRpc.stop() }
2019-03-27 11:16:29 +03:00
public func checkBlocked<T>(_ single: Single<T>) -> Single<T> {
2020-01-25 22:56:24 +03:00
self
2019-03-27 11:16:29 +03:00
.getMode()
.flatMap { dict -> Single<T> in
guard (dict["blocking"]?.boolValue ?? false) == false else {
throw RxNeovimApi.Error.blocked
}
return single
}
}
2020-01-25 22:56:24 +03:00
public func rpc(
method: String,
params: [RxNeovimApi.Value],
expectsReturnValue: Bool = true
) -> Single<RxNeovimApi.Value> {
self.msgpackRpc
2019-03-27 11:16:29 +03:00
.request(method: method, params: params, expectsReturnValue: expectsReturnValue)
.map { response -> RxMsgpackRpc.Value in
2020-01-25 22:56:24 +03:00
guard response.error.isNil else { throw RxNeovimApi.Error(response.error) }
2019-03-27 11:16:29 +03:00
return response.result
}
}
private let msgpackRpc = RxMsgpackRpc()
2020-01-26 22:31:10 +03:00
private let queue = DispatchQueue(
label: String(reflecting: RxNeovimApi.self),
qos: .userInitiated
)
2019-03-27 11:16:29 +03:00
}