1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 16:56:40 +03:00

Refactor slightly

This commit is contained in:
Tae Won Ha 2017-12-15 17:02:43 +01:00
parent 8023152461
commit 11bb387784
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -115,9 +115,44 @@ public class NvimApi {
}
}
class Session {
public init?(at path: String) {
guard let session = Session(at: path) else {
return nil
}
var notificationCallback: NotificationCallback? {
self.session = session
}
public func connect() throws {
try self.session.run()
}
public func disconnect() {
self.session.stop()
}
@discardableResult
public func checkBlocked<T>(_ fn: () -> NvimApi.Response<T>) -> NvimApi.Response<T> {
if self.getMode().value?["blocking"] == .bool(true) {
return NvimApi.Response.failure(NvimApi.Error(type: .blocked, message: "Nvim is currently blocked."))
}
return fn()
}
public func rpc(method: String,
params: [NvimApi.Value],
expectsReturnValue: Bool = true) -> NvimApi.Response<NvimApi.Value> {
return self.session.rpc(method: method, params: params, expectsReturnValue: expectsReturnValue)
}
private let session: Session
}
class Session {
var notificationCallback: NvimApi.NotificationCallback? {
get {
return self.connection.notificationCallback
}
@ -127,7 +162,7 @@ public class NvimApi {
}
}
var unknownMessageCallback: UnknownCallback? {
var unknownMessageCallback: NvimApi.UnknownCallback? {
get {
return self.connection.unknownMessageCallback
}
@ -137,7 +172,7 @@ public class NvimApi {
}
}
var errorCallback: ErrorCallback? {
var errorCallback: NvimApi.ErrorCallback? {
get {
return self.connection.errorCallback
}
@ -147,7 +182,7 @@ public class NvimApi {
}
}
public init?(at path: String) {
init?(at path: String) {
guard let connection = MsgPackRpc.Connection(unixDomainSocketPath: path) else {
return nil
}
@ -155,15 +190,15 @@ public class NvimApi {
self.connection = connection
}
public func run() throws {
func run() throws {
try self.connection.run()
}
public func stop() {
func stop() {
self.connection.stop()
}
public func rpc(method: String,
func rpc(method: String,
params: [MsgPackRpc.Value],
expectsReturnValue: Bool) -> Result<MsgPackRpc.Value, NvimApi.Error> {
@ -200,39 +235,4 @@ public class NvimApi {
defer { lock.unlock() }
return fn()
}
}
public init?(at path: String) {
guard let session = Session(at: path) else {
return nil
}
self.session = session
}
public func connect() throws {
try self.session.run()
}
public func disconnect() {
self.session.stop()
}
@discardableResult
public func checkBlocked<T>(_ fn: () -> NvimApi.Response<T>) -> NvimApi.Response<T> {
if self.getMode().value?["blocking"] == .bool(true) {
return NvimApi.Response.failure(NvimApi.Error(type: .blocked, message: "Nvim is currently blocked."))
}
return fn()
}
public func rpc(method: String,
params: [NvimApi.Value],
expectsReturnValue: Bool = true) -> NvimApi.Response<NvimApi.Value> {
return self.session.rpc(method: method, params: params, expectsReturnValue: expectsReturnValue)
}
private let session: Session
}