Skip to content
Snippets Groups Projects
Commit 95d29a63 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add MessageDeliveryWaiter

parent d811b991
No related branches found
No related tags found
1 merge request!2Bindings API wrapper
......@@ -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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment