Files

61 lines
1.5 KiB
Swift

//
// FetchingServersOperation.swift
// PrivadoVPN
//
// Created by Juraldinio on 3/20/21.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Foundation
protocol FetchingServersOperationOutput {
var result: Result<MainInteractorResult, Error>? { get }
}
final class FetchingServersOperation: AsyncOperation {
enum OperationError: Error {
case cancelled
}
private(set) var result: Result<MainInteractorResult, Error>? // FetchingServersOperationOutput
private let interactor: MainInteractorInput
init(interactor: MainInteractorInput) {
self.interactor = interactor
}
// MARK: - AsyncOperation
override func main() {
guard !self.isCancelled else {
self.result = Result.failure(OperationError.cancelled)
return
}
self.interactor.fetchServers { [weak self] result in
guard let self = self else { return }
guard !self.isCancelled else {
self.result = Result.failure(OperationError.cancelled)
self.state = .finished
return
}
self.result = result
self.state = .finished
}
}
override func cancel() {
self.result = Result.failure(OperationError.cancelled)
super.cancel()
}
}
extension FetchingServersOperation: FetchingServersOperationOutput { }