From 4cc1c04ad3255ac29a674e4d14f4ee8e80041fc8 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 2 Jun 2022 14:09:39 +0200
Subject: [PATCH] Add ConnectionCloser

---
 Sources/ElixxirDAppsSDK/Connection.swift      | 10 ++++--
 .../ElixxirDAppsSDK/ConnectionCloser.swift    | 31 +++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)
 create mode 100644 Sources/ElixxirDAppsSDK/ConnectionCloser.swift

diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift
index 9f269119..7ae0df36 100644
--- a/Sources/ElixxirDAppsSDK/Connection.swift
+++ b/Sources/ElixxirDAppsSDK/Connection.swift
@@ -5,6 +5,7 @@ public struct Connection {
   public var getPartner: () -> Data
   public var send: MessageSender
   public var listen: MessageListener
+  public var close: ConnectionCloser
 }
 
 extension Connection {
@@ -20,7 +21,8 @@ extension Connection {
         return data
       },
       send: .live(bindingsConnection: bindingsConnection),
-      listen: .live(bindingsConnection: bindingsConnection)
+      listen: .live(bindingsConnection: bindingsConnection),
+      close: .live(bindingsConnection: bindingsConnection)
     )
   }
 
@@ -36,7 +38,8 @@ extension Connection {
         return data
       },
       send: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection),
-      listen: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection)
+      listen: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection),
+      close: .live(bindingsAuthenticatedConnection: bindingsAuthenticatedConnection)
     )
   }
 }
@@ -47,7 +50,8 @@ extension Connection {
     isAuthenticated: { fatalError("Not implemented") },
     getPartner: { fatalError("Not implemented") },
     send: .failing,
-    listen: .failing
+    listen: .failing,
+    close: .failing
   )
 }
 #endif
diff --git a/Sources/ElixxirDAppsSDK/ConnectionCloser.swift b/Sources/ElixxirDAppsSDK/ConnectionCloser.swift
new file mode 100644
index 00000000..c74c62fa
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/ConnectionCloser.swift
@@ -0,0 +1,31 @@
+import Bindings
+
+public struct ConnectionCloser {
+  public var close: () -> Void
+
+  public func callAsFunction() {
+    close()
+  }
+}
+
+extension ConnectionCloser {
+  public static func live(
+    bindingsConnection: BindingsConnection
+  ) -> ConnectionCloser {
+    ConnectionCloser(close: bindingsConnection.close)
+  }
+
+  public static func live(
+    bindingsAuthenticatedConnection: BindingsAuthenticatedConnection
+  ) -> ConnectionCloser {
+    ConnectionCloser(close: bindingsAuthenticatedConnection.close)
+  }
+}
+
+#if DEBUG
+extension ConnectionCloser {
+  public static let failing = ConnectionCloser {
+    fatalError("Not implemented")
+  }
+}
+#endif
-- 
GitLab