diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift index 7ae0df36c1aa42a1b70dbd8242b53f308e6632d6..cd6e0fa8b023f0554c06dfa2612a4f901b0525a2 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 0000000000000000000000000000000000000000..8d2bcbce76c9693f3b4a615d43a5f41193551c99 --- /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