Created
November 16, 2018 12:32
-
-
Save celian-m/543783556bac33de4a7072bd22458441 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Either<T> { | |
case success(T) | |
case error(Swift.Error) | |
var successResult: T? { | |
switch self { | |
case .success(let result): | |
return result | |
case .error(_): | |
return nil | |
} | |
} | |
} | |
protocol UseCaseProtocol { | |
associatedtype ReturnType | |
associatedtype Param | |
func execute(_ params: Param, completion: @escaping ((Either<ReturnType>) -> Void)) | |
} | |
struct UseCase<P, R> { | |
typealias ReturnType = R | |
typealias Param = P | |
init<U>(_ useCase: U) where U: UseCaseProtocol, U.ReturnType == ReturnType, U.Param == Param { | |
_execute = useCase.execute | |
} | |
func execute(_ params: P, completion: @escaping ((Either<R>) -> Void)) { | |
_execute(params, completion) | |
} | |
let _execute: (P, @escaping (Either<R>) -> Void) -> Void | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment