diff --git a/Sources/ElixxirDAppsSDK/Client.swift b/Sources/ElixxirDAppsSDK/Client.swift index 38e236ace9ac689fdf9def0b0fac6324b6723e1e..1ceef52cf61f1cf34ce52278c1fc1d924f9969f3 100644 --- a/Sources/ElixxirDAppsSDK/Client.swift +++ b/Sources/ElixxirDAppsSDK/Client.swift @@ -5,6 +5,7 @@ public struct Client { public var waitForNetwork: NetworkWaiter public var makeIdentity: IdentityMaker public var connect: ConnectionMaker + public var waitForDelivery: MessageDeliveryWaiter } extension Client { @@ -13,7 +14,8 @@ extension Client { networkFollower: .live(bindingsClient: bindingsClient), waitForNetwork: .live(bindingsClient: bindingsClient), makeIdentity: .live(bindingsClient: bindingsClient), - connect: .live(bindingsClient: bindingsClient) + connect: .live(bindingsClient: bindingsClient), + waitForDelivery: .live(bindingsClient: bindingsClient) ) } } @@ -24,7 +26,8 @@ extension Client { networkFollower: .failing, waitForNetwork: .failing, makeIdentity: .failing, - connect: .failing + connect: .failing, + waitForDelivery: .failing ) } #endif diff --git a/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift b/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift new file mode 100644 index 0000000000000000000000000000000000000000..75d0bec26d4f1dba159a4367b6d4afe0c1f1e59e --- /dev/null +++ b/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift @@ -0,0 +1,61 @@ +import Bindings + +public struct MessageDeliveryWaiter { + public struct Result: Equatable { + public init(delivered: Bool, timedOut: Bool, roundResults: Data?) { + self.delivered = delivered + self.timedOut = timedOut + self.roundResults = roundResults + } + + public var delivered: Bool + public var timedOut: Bool + public var roundResults: Data? + } + + public var wait: (Data, Int, @escaping (Result) -> Void) throws -> Void + + public func callAsFunction( + roundList: Data, + timeoutMS: Int, + callback: @escaping (Result) -> Void + ) throws -> Void { + try wait(roundList, timeoutMS, callback) + } +} + +extension MessageDeliveryWaiter { + public static func live(bindingsClient: BindingsClient) -> MessageDeliveryWaiter { + MessageDeliveryWaiter { roundList, timeoutMS, callback in + try bindingsClient.wait( + forMessageDelivery: roundList, + mdc: Callback(onCallback: { delivered, timedOut, roundResults in + callback(Result(delivered: delivered, timedOut: timedOut, roundResults: roundResults)) + }), + timeoutMS: timeoutMS + ) + } + } +} + +private final class Callback: NSObject, BindingsMessageDeliveryCallbackProtocol { + init(onCallback: @escaping (Bool, Bool, Data?) -> Void) { + self.onCallback = onCallback + super.init() + } + + let onCallback: (Bool, Bool, Data?) -> Void + + func eventCallback(_ delivered: Bool, timedOut: Bool, roundResults: Data?) { + onCallback(delivered, timedOut, roundResults) + } +} + +#if DEBUG +extension MessageDeliveryWaiter { + public static let failing = MessageDeliveryWaiter { _, _, _ in + struct NotImplemented: Error {} + throw NotImplemented() + } +} +#endif