async await

This commit is contained in:
Scott Pfeil 2024-01-18 16:23:10 -05:00
parent f5e8c0b2e6
commit 0316828728

View File

@ -6,6 +6,22 @@
// Copyright © 2021 myverizon. All rights reserved.
//
/// Sample clientParameters
///{
/// "timeout": 30,
/// "list": [
/// {
/// "type": "currentLocation",
/// "inputParameters": {
/// "accuracy": 10,
/// "timeThreshold": 600
/// }
/// }
/// ]
///}
/// completionHandler can return flat dictinary or a map. It depends on the paramters handler
@objcMembers open class ClientParameterHandler: NSObject {
public static let DefaultTimeout = 30.0
@ -36,30 +52,19 @@
return try JSONDecoder.create().decode(ClientParameterModel.self, from: data)
}
/// Sample clientParameters
///{
/// "timeout": 30,
/// "list": [
/// {
/// "type": "currentLocation",
/// "inputParameters": {
/// "accuracy": 10,
/// "timeThreshold": 600
/// }
/// }
/// ]
///}
/// completionHandler can return flat dictinary or a map. It depends on the paramters handler
open func getParameters(with clientParameters: [String: Any], requestParameters: [String: Any], actionId: String, completionHandler:@escaping ([String: Any]?) -> ()) throws {
guard let clientParameterModel = try getClientParameterModel(clientParameters) else {
completionHandler(nil)
return
}
getParameters(with: clientParameterModel, requestParameters: requestParameters, actionId: actionId, completionHandler: completionHandler)
Task {
let parameters = await getParameters(with: clientParameterModel, requestParameters: requestParameters, actionId: actionId)
completionHandler(parameters)
}
}
open func getParameters(with model: ClientParameterModel, requestParameters: [String: Any], actionId: String) async -> [String: AnyHashable] {
let timeout = model.timeout ?? Self.DefaultTimeout
let timeout = TimeInterval(model.timeout ?? Self.DefaultTimeout)
let parameterHandlerList = model.list.compactMap { createParametersHandler($0) }
return await withTaskGroup(of: [String: AnyHashable]?.self, returning: [String: AnyHashable].self) { group in
for handler in parameterHandlerList {
@ -70,7 +75,7 @@
name: handler.clientParameterModel.type,
uuid: handler.clientParameterModel.id,
actionId: actionId))
let parameter: [String: AnyHashable]? = try await MVMCoreActionUtility.perform(withTimeout: .now() + .seconds(Int(timeout))) {
let parameter: [String: AnyHashable]? = try await MVMCoreActionUtility.perform(withTimeout: timeout) {
let parameter = await handler.fetchClientParameters(requestParameters: requestParameters, timingOutIn: timeout)
MVMCoreLoggingHandler.shared()?.logCoreEvent(.clientParameterFetchComplete(
@ -83,11 +88,12 @@
} catch MVMCoreActionUtilityError.timeOut {
// The client parameter timed out.
MVMCoreLoggingHandler.shared()?.logCoreEvent(.clientParameterTimeout(
name: model.list[index].type,
name: handler.clientParameterModel.type,
uuid: handler.clientParameterModel.id,
actionId: actionId))
return handler.valueOnTimeout()
} catch {
// Should be impossible.
return nil
}
}