From 92febf27b879ccff6b89cdeab53f1235a34cb6be Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Thu, 2 Jun 2022 14:12:00 +0200 Subject: [PATCH] Add ConnectionPartnerProvider --- Sources/ElixxirDAppsSDK/Connection.swift | 18 ++------ .../ConnectionPartnerProvider.swift | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 Sources/ElixxirDAppsSDK/ConnectionPartnerProvider.swift diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift index 7ae0df36..cd6e0fa8 100644 --- a/Sources/ElixxirDAppsSDK/Connection.swift +++ b/Sources/ElixxirDAppsSDK/Connection.swift @@ -2,7 +2,7 @@ import Bindings public struct Connection { public var isAuthenticated: () -> Bool - public var getPartner: () -> Data + public var getPartner: ConnectionPartnerProvider public var send: MessageSender public var listen: MessageListener public var close: ConnectionCloser @@ -14,12 +14,7 @@ extension Connection { ) -> Connection { Connection( isAuthenticated: { false }, - getPartner: { - guard let data = bindingsConnection.getPartner() else { - fatalError("BindingsConnection.getPartner returned `nil`") - } - return data - }, + getPartner: .live(bindingsConnection: bindingsConnection), send: .live(bindingsConnection: bindingsConnection), listen: .live(bindingsConnection: bindingsConnection), close: .live(bindingsConnection: bindingsConnection) @@ -31,12 +26,7 @@ extension Connection { ) -> Connection { Connection( isAuthenticated: bindingsAuthenticatedConnection.isAuthenticated, - getPartner: { - guard let data = bindingsAuthenticatedConnection.getPartner() else { - fatalError("BindingsAuthenticatedConnection.getPartner returned `nil`") - } - return data - }, + getPartner: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection), send: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection), listen: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection), close: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection) @@ -48,7 +38,7 @@ extension Connection { extension Connection { public static let failing = Connection( isAuthenticated: { fatalError("Not implemented") }, - getPartner: { fatalError("Not implemented") }, + getPartner: .failing, send: .failing, listen: .failing, close: .failing diff --git a/Sources/ElixxirDAppsSDK/ConnectionPartnerProvider.swift b/Sources/ElixxirDAppsSDK/ConnectionPartnerProvider.swift new file mode 100644 index 00000000..8d2bcbce --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ConnectionPartnerProvider.swift @@ -0,0 +1,41 @@ +import Bindings + +public struct ConnectionPartnerProvider { + public var get: () -> Data + + public func callAsFunction() -> Data { + get() + } +} + +extension ConnectionPartnerProvider { + public static func live( + bindingsConnection: BindingsConnection + ) -> ConnectionPartnerProvider { + ConnectionPartnerProvider { + guard let data = bindingsConnection.getPartner() else { + fatalError("BindingsConnection.getPartner returned `nil`") + } + return data + } + } + + public static func live( + bindingsAuthenticatedConnection: BindingsAuthenticatedConnection + ) -> ConnectionPartnerProvider { + ConnectionPartnerProvider { + guard let data = bindingsAuthenticatedConnection.getPartner() else { + fatalError("BindingsAuthenticatedConnection.getPartner returned `nil`") + } + return data + } + } +} + +#if DEBUG +extension ConnectionPartnerProvider { + public static let failing = ConnectionPartnerProvider { + fatalError("Not implemented") + } +} +#endif -- GitLab