Skip to content
Snippets Groups Projects
Commit 28f7f025 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add ConnectionSend functor

parent 3f4d0415
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!18Update Bindings
......@@ -4,6 +4,7 @@ public struct Connection {
public var isAuthenticated: ConnectionIsAuthenticated
public var getId: ConnectionGetId
public var getPartner: ConnectionGetPartner
public var send: ConnectionSend
public var close: ConnectionClose
}
......@@ -13,6 +14,7 @@ extension Connection {
isAuthenticated: .live(bindingsConnection),
getId: .live(bindingsConnection),
getPartner: .live(bindingsConnection),
send: .live(bindingsConnection),
close: .live(bindingsConnection)
)
}
......@@ -22,6 +24,7 @@ extension Connection {
isAuthenticated: .live(bindingsConnection),
getId: .live(bindingsConnection),
getPartner: .live(bindingsConnection),
send: .live(bindingsConnection),
close: .live(bindingsConnection)
)
}
......@@ -32,6 +35,7 @@ extension Connection {
isAuthenticated: .unimplemented,
getId: .unimplemented,
getPartner: .unimplemented,
send: .unimplemented,
close: .unimplemented
)
}
import Bindings
import XCTestDynamicOverlay
public struct ConnectionSend {
public var run: (Int, Data) throws -> MessageSendReport
public func callAsFunction(
messageType: Int,
payload: Data
) throws -> MessageSendReport {
try run(messageType, payload)
}
}
extension ConnectionSend {
public static func live(_ bindingsConnection: BindingsConnection) -> ConnectionSend {
ConnectionSend { messageType, payload in
try MessageSendReport.decode(
bindingsConnection.sendE2E(messageType, payload: payload)
)
}
}
public static func live(_ bindingsConnection: BindingsAuthenticatedConnection) -> ConnectionSend {
ConnectionSend { messageType, payload in
try MessageSendReport.decode(
bindingsConnection.sendE2E(messageType, payload: payload)
)
}
}
}
extension ConnectionSend {
public static let unimplemented = ConnectionSend(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
public struct MessageSender {
public var send: (Int, Data) throws -> MessageSendReport
public func callAsFunction(
messageType: Int,
payload: Data
) throws -> MessageSendReport {
try send(messageType, payload)
}
}
extension MessageSender {
public static func live(
bindingsConnection: BindingsConnection
) -> MessageSender {
MessageSender.live(sendE2E: bindingsConnection.sendE2E(_:payload:))
}
public static func live(
bindingsAuthenticatedConnection: BindingsAuthenticatedConnection
) -> MessageSender {
MessageSender.live(sendE2E: bindingsAuthenticatedConnection.sendE2E(_:payload:))
}
private static func live(
sendE2E: @escaping (Int, Data) throws -> Data
) -> MessageSender {
MessageSender { messageType, payload in
let reportData = try sendE2E(messageType, payload)
let decoder = JSONDecoder()
let report = try decoder.decode(MessageSendReport.self, from: reportData)
return report
}
}
}
#if DEBUG
extension MessageSender {
public static let failing = MessageSender { _, _ in
struct NotImplemented: Error {}
throw NotImplemented()
}
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment