Files
2021-09-20 02:48:00 +06:00

62 lines
1.4 KiB
Swift

//
// AsyncOperation.swift
// PrivadoVPN
//
// Created by Juraldinio on 3/8/21.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Foundation
open class AsyncOperation: Operation {
enum State: String {
case ready
case executing
case finished
var keyPath: String { return "is\(self.rawValue.capitalized)" }
}
var state: State = .ready {
willSet {
self.willChangeValue(forKey: newValue.keyPath)
self.willChangeValue(forKey: self.state.keyPath)
}
didSet {
self.didChangeValue(forKey: oldValue.keyPath)
self.didChangeValue(forKey: self.state.keyPath)
}
}
// MARK: - Operation
override open var isAsynchronous: Bool { true }
override open var isReady: Bool { super.isReady && self.state == .ready }
override open var isExecuting: Bool { self.state == .executing }
override open var isFinished: Bool { self.state == .finished }
override open func start() {
guard !self.isCancelled else {
self.state = .finished
return
}
self.state = .executing
self.main()
}
override open func cancel() {
super.cancel()
self.state = .finished
}
}