From cdeb2d24fec13e27011ee68c0ee8fbcb55e397cc Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Mon, 25 Jul 2022 18:26:56 +0100
Subject: [PATCH] Add RegisterLogWriter functor

---
 .../ElixxirDAppsSDK/Legacy/LogsListener.swift | 40 -------------------
 .../ElixxirDAppsSDK/RegisterLogWriter.swift   | 22 ++++++++++
 2 files changed, 22 insertions(+), 40 deletions(-)
 delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/LogsListener.swift
 create mode 100644 Sources/ElixxirDAppsSDK/RegisterLogWriter.swift

diff --git a/Sources/ElixxirDAppsSDK/Legacy/LogsListener.swift b/Sources/ElixxirDAppsSDK/Legacy/LogsListener.swift
deleted file mode 100644
index 2b070877..00000000
--- a/Sources/ElixxirDAppsSDK/Legacy/LogsListener.swift
+++ /dev/null
@@ -1,40 +0,0 @@
-import Bindings
-
-public struct LogsListener {
-  public var listen: (@escaping (String) -> Void) -> Void
-
-  public func callAsFunction(callback: @escaping (String) -> Void) {
-    listen(callback)
-  }
-}
-
-extension LogsListener {
-  public static let live = LogsListener { callback in
-    let listener = Listener(onLog: callback)
-    BindingsRegisterLogWriter(listener)
-  }
-}
-
-private final class Listener: NSObject, BindingsLogWriterProtocol {
-  init(onLog: @escaping (String) -> Void) {
-    self.onLog = onLog
-    super.init()
-  }
-
-  let onLog: (String) -> Void
-
-  func log(_ p0: String?) {
-    guard let p0 = p0 else {
-      fatalError("BindingsLogWriter.log received `nil`")
-    }
-    onLog(p0)
-  }
-}
-
-#if DEBUG
-extension LogsListener {
-  public static let failing = LogsListener { _ in
-    fatalError("Not implemented")
-  }
-}
-#endif
diff --git a/Sources/ElixxirDAppsSDK/RegisterLogWriter.swift b/Sources/ElixxirDAppsSDK/RegisterLogWriter.swift
new file mode 100644
index 00000000..f025bb59
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/RegisterLogWriter.swift
@@ -0,0 +1,22 @@
+import Bindings
+import XCTestDynamicOverlay
+
+public struct RegisterLogWriter {
+  public var run: (LogWriter) -> Void
+
+  public func callAsFunction(_ writer: LogWriter) {
+    run(writer)
+  }
+}
+
+extension RegisterLogWriter {
+  public static let live = RegisterLogWriter { writer in
+    BindingsRegisterLogWriter(writer.makeBindingsLogWriter())
+  }
+}
+
+extension RegisterLogWriter {
+  public static let unimplemented = RegisterLogWriter(
+    run: XCTUnimplemented("\(Self.self)")
+  )
+}
-- 
GitLab