-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncOperation.swift
37 lines (31 loc) · 989 Bytes
/
AsyncOperation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// AsyncOperation.swift
// Created by Alex on 2017-09-03.
// Copyright © 2017 Alex Kozachenko. All rights reserved.
import Foundation
class AsynchronousOperation : Operation {
enum State: String {
case ready = "Ready"
case executing = "Executing"
case finished = "Finished"
fileprivate var keyPath: String { return "is" + self.rawValue }
}
override var isAsynchronous: Bool { return true }
override var isExecuting: Bool { return state == .executing }
override var isFinished: Bool { return state == .finished }
var state = State.ready {
willSet {
willChangeValue(forKey: state.keyPath)
willChangeValue(forKey: newValue.keyPath)
}
didSet {
didChangeValue(forKey: state.keyPath)
didChangeValue(forKey: oldValue.keyPath)
}
}
func finish() {
state = .finished
}
func execute() {
state = .executing
}
}