From 8a8eb525273798828f9e5d3ce1ece5f7a8953340 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 28 Jul 2022 19:40:59 +0100
Subject: [PATCH] Add Listen functor

---
 Sources/ElixxirDAppsSDK/Functors/Listen.swift | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/Functors/Listen.swift

diff --git a/Sources/ElixxirDAppsSDK/Functors/Listen.swift b/Sources/ElixxirDAppsSDK/Functors/Listen.swift
new file mode 100644
index 00000000..56bb47ad
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/Functors/Listen.swift
@@ -0,0 +1,41 @@
+import Bindings
+import XCTestDynamicOverlay
+
+public struct Listen {
+  public var run: (Int, String, SingleUseCallback) throws -> Cancellable
+
+  public func callAsFunction(
+    e2eId: Int,
+    tag: String,
+    callback: SingleUseCallback
+  ) throws -> Cancellable {
+    try run(e2eId, tag, callback)
+  }
+}
+
+extension Listen {
+  public static let live = Listen { e2eId, tag, callback in
+    var error: NSError?
+    let stopper = BindingsListen(
+      e2eId,
+      tag,
+      callback.makeBindingsSingleUseCallback(),
+      &error
+    )
+    if let error = error {
+      throw error
+    }
+    guard let stopper = stopper else {
+      fatalError("BindingsListen returned `nil` without providing error")
+    }
+    return Cancellable {
+      stopper.stop()
+    }
+  }
+}
+
+extension Listen {
+  public static let unimplemented = Listen(
+    run: XCTUnimplemented("\(Self.self)")
+  )
+}
-- 
GitLab