Last active
August 20, 2021 09:36
-
-
Save kuotinyen/81f3c17401d4d86b32a5884498b7d09d to your computer and use it in GitHub Desktop.
Worker demo
This file contains 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 | |
class AccountInfoWorker { | |
struct AccountInfo { /* properties */ } | |
typealias ApiResult = Result<AccountInfo, Error> | |
typealias ApiCompletion = (ApiResult) -> Void | |
func fetchAccountInfo(id: String, completion: @escaping ApiCompletion) { /* IMP */ } | |
} | |
class LoginInfoWorker { | |
struct LoginInfo { | |
let id: String | |
} | |
typealias ApiResult = Result<LoginInfo, Error> | |
typealias ApiCompletion = (ApiResult) -> Void | |
func fetchLoginInfo(completion: @escaping ApiCompletion) { /* IMP */ } | |
} | |
class LoginAccountInfoWorker { | |
private let accountInfoWorker = AccountInfoWorker() | |
private let loginInfoWorker = LoginInfoWorker() | |
func fetchLoginAccountInfo(id: String, completion: @escaping AccountInfoWorker.ApiCompletion) { | |
loginInfoWorker.fetchLoginInfo { [weak self] result in | |
self?.handle(loginInfo: result, completion: completion) | |
} | |
} | |
private func handle(loginInfo result: LoginInfoWorker.ApiResult, completion: @escaping AccountInfoWorker.ApiCompletion) { | |
switch result { | |
case let .success(loginInfo): | |
accountInfoWorker.fetchAccountInfo(id: loginInfo.id) { result in | |
switch result { | |
case let .success(accountInfo): | |
completion(.success(accountInfo)) | |
case let .failure(error): | |
completion(.failure(error)) | |
} | |
} | |
case let .failure(error): | |
completion(.failure(error)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment