diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift index 2633c603df46a405d1684af9e41f27f4d4144489..a723764bbfd3cfef97d5dbda159bf64ad019c58c 100644 --- a/Sources/ElixxirDAppsSDK/Connection.swift +++ b/Sources/ElixxirDAppsSDK/Connection.swift @@ -2,6 +2,7 @@ import Bindings public struct Connection { public var isAuthenticated: () -> Bool + public var send: MessageSender } extension Connection { @@ -9,7 +10,8 @@ extension Connection { bindingsConnection: BindingsConnection ) -> Connection { Connection( - isAuthenticated: { false } + isAuthenticated: { false }, + send: .live(bindingsConnection: bindingsConnection) ) } @@ -17,7 +19,8 @@ extension Connection { bindingsAuthenticatedConnection: BindingsAuthenticatedConnection ) -> Connection { Connection( - isAuthenticated: bindingsAuthenticatedConnection.isAuthenticated + isAuthenticated: bindingsAuthenticatedConnection.isAuthenticated, + send: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection) ) } } @@ -25,7 +28,8 @@ extension Connection { #if DEBUG extension Connection { public static let failing = Connection( - isAuthenticated: { false } + isAuthenticated: { false }, + send: .failing ) } #endif diff --git a/Sources/ElixxirDAppsSDK/MessageSender.swift b/Sources/ElixxirDAppsSDK/MessageSender.swift new file mode 100644 index 0000000000000000000000000000000000000000..ffacd4e0bcab2edefabcf40b7c663312f475d942 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/MessageSender.swift @@ -0,0 +1,39 @@ +import Bindings + +public struct MessageSender { + public var send: (Int, Data) throws -> Data + + public func callAsFunction( + messageType: Int, + payload: Data + ) throws -> Data { + try send(messageType, payload) + } +} + +extension MessageSender { + public static func live( + bindingsConnection: BindingsConnection + ) -> MessageSender { + MessageSender { messageType, payload in + try bindingsConnection.sendE2E(messageType, payload: payload) + } + } + + public static func live( + bindingsAuthenticatedConnection: BindingsAuthenticatedConnection + ) -> MessageSender { + MessageSender { messageType, payload in + try bindingsAuthenticatedConnection.sendE2E(messageType, payload: payload) + } + } +} + +#if DEBUG +extension MessageSender { + public static let failing = MessageSender { _, _ in + struct NotImplemented: Error {} + throw NotImplemented() + } +} +#endif