Skip to content
Snippets Groups Projects
Select Git revision
  • d6f65882812e2e48432512a27cc17e28fc54efe0
  • main default protected
  • development
  • integration
  • v1.1.5
  • v1.1.4
  • v1.1.3
  • v1.1.2
  • v1.1.1
  • v1.1.0
  • v1.0.0
11 results

LoadCmix.swift

  • MessageDeliveryWaiter.swift 1.64 KiB
    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