From d165d1efc27baed6cc98326ea98585bdfa0aa60d Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Thu, 2 Jun 2022 11:20:20 +0200 Subject: [PATCH] Add MessageSender --- Sources/ElixxirDAppsSDK/Connection.swift | 10 ++++-- Sources/ElixxirDAppsSDK/MessageSender.swift | 39 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 Sources/ElixxirDAppsSDK/MessageSender.swift diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift index 2633c603..a723764b 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 00000000..ffacd4e0 --- /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 -- GitLab