Skip to content

Instantly share code, notes, and snippets.

@inekipelov
Created March 27, 2021 10:29
Show Gist options
  • Save inekipelov/f3c286b2525bde16197ec4f995309e76 to your computer and use it in GitHub Desktop.
Save inekipelov/f3c286b2525bde16197ec4f995309e76 to your computer and use it in GitHub Desktop.
import Foundation
class AsyncOperation: Operation {
public enum State: String {
case ready, executing, finished
fileprivate var keyPath: String {
return "is" + rawValue.capitalized
}
}
public var state = State.ready {
willSet {
willChangeValue(forKey: newValue.keyPath)
willChangeValue(forKey: state.keyPath)
}
didSet {
didChangeValue(forKey: oldValue.keyPath)
didChangeValue(forKey: state.keyPath)
}
}
override var isReady: Bool {
return super.isReady && state == .ready
}
override var isFinished: Bool { return state == .finished }
override var isExecuting: Bool { return state == .executing }
override var isAsynchronous: Bool { return true }
override func start() {
if isCancelled {
state = .finished
return
}
main()
state = .executing
}
override func cancel() {
super.cancel()
state = .finished
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment