refactored hasActionHandler to include a guard for HandlerType and refactored out old method to hard cast acation MVMCoreActionHandlerProtocol

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2021-10-27 14:54:52 -05:00
parent c0958b58e4
commit b546fcbb8b

View File

@ -13,17 +13,6 @@ public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol {
func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?)
}
extension ModelRegistry {
public static func getActionHandler(_ model: ActionModelProtocol) throws -> MVMCoreActionHandlerProtocol {
do {
let type = try ModelRegistry.getHandler(model) as! MVMCoreActionHandlerProtocol.Type
return type.init()
} catch {
throw ModelRegistry.Error.other(message: error.localizedDescription)
}
}
}
public extension MVMCoreActionHandler {
/// Converts the action to json for old action handler to handle.
@ -46,30 +35,40 @@ public extension MVMCoreActionHandler {
//ensure there is a Serialized version of the Action
guard let actionType = actionType,
let actionInformation = actionInformation,
let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self)
let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self),
let actionHandlerType = try? ModelRegistry.getHandlerType(for: actionModelType)
else { return false }
do {
//Cast the actionHandlerType to the new Protocol
guard let actionHandlerProtocolType = actionHandlerType as? MVMCoreActionHandlerProtocol.Type else {
throw ModelRegistry.Error.decoderOther(message: "ModelHandlerProtocol found not of MVMCoreActionHandlerProtocol Type")
}
//deserialize the actionModel for the actionType found
guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else {
throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol")
}
//get the action Handler for the actionModel created
let actionHandler = try ModelRegistry.getActionHandler(actionModel)
//create the handler since we know it can initialize
let actionHandler = actionHandlerProtocolType.init()
//call the handleAction of the handler
actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject)
//found the handler and executed the action
return true
} catch {
//log the error
if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "") {
MVMCoreActionHandler.shared()?.defaultHandleActionError(errorObject, additionalData: additionalData)
}
//there was a handler however something failed
return false
}
//complete
return true
}
/// Start action on current thread.