@@ -13,26 +13,18 @@ open class AsyncOperation: Operation, Pausable {
13
13
public enum State : String , Hashable {
14
14
/// The operation is waiting to start.
15
15
case waiting = " isWaiting "
16
-
17
16
/// The operation is ready to start.
18
17
case ready = " isReady "
19
-
20
18
/// The operation is executing.
21
19
case executing = " isExecuting "
22
-
23
20
/// The operation is finished.
24
21
case finished = " isFinished "
25
-
26
22
/// The operation is cancelled.
27
23
case cancelled = " isCancelled "
28
-
29
24
/// The operation is paused.
30
25
case paused = " isPaused "
31
26
}
32
27
33
- /// The handler that gets called when the state changes.
34
- open var onStateChange : ( ( State ) -> Void ) ?
35
-
36
28
/// The error, if the operation failed.
37
29
open var error : Error ?
38
30
@@ -45,96 +37,69 @@ open class AsyncOperation: Operation, Pausable {
45
37
willChangeValue ( forKey: State . cancelled. rawValue)
46
38
}
47
39
didSet {
48
- switch self . state {
40
+ switch state {
49
41
case . waiting:
50
- assert ( oldValue == . waiting, " Invalid change from \( oldValue) to \( self . state) " )
42
+ assert ( oldValue == . waiting, " Invalid change from \( oldValue) to \( state) " )
51
43
case . ready:
52
- assert ( oldValue == . waiting, " Invalid change from \( oldValue) to \( self . state) " )
44
+ assert ( oldValue == . waiting, " Invalid change from \( oldValue) to \( state) " )
53
45
case . executing:
54
- assert (
55
- oldValue == . ready || oldValue == . waiting || oldValue == . paused,
56
- " Invalid change from \( oldValue) to \( self . state) "
57
- )
46
+ assert ( oldValue == . ready || oldValue == . waiting || oldValue == . paused, " Invalid change from \( oldValue) to \( state) " )
58
47
case . finished:
59
- assert ( oldValue != . cancelled, " Invalid change from \( oldValue) to \( self . state) " )
48
+ assert ( oldValue != . cancelled, " Invalid change from \( oldValue) to \( state) " )
60
49
case . cancelled:
61
50
break
62
51
case . paused:
63
- assert ( oldValue == . executing, " Invalid change from \( oldValue) to \( self . state) " )
52
+ assert ( oldValue == . executing, " Invalid change from \( oldValue) to \( state) " )
64
53
}
65
-
66
54
didChangeValue ( forKey: State . cancelled. rawValue)
67
55
didChangeValue ( forKey: State . finished. rawValue)
68
56
didChangeValue ( forKey: State . executing. rawValue)
69
57
didChangeValue ( forKey: State . ready. rawValue)
70
- if oldValue != self . state {
71
- self . onStateChange ? ( self . state)
72
- }
73
58
}
74
59
}
75
60
76
61
override open var isReady : Bool {
77
- if self . state == . waiting {
78
- return super. isReady
79
- } else {
80
- return self . state == . ready
81
- }
62
+ state == . waiting ? super. isReady : state == . ready
82
63
}
83
64
84
65
override open var isExecuting : Bool {
85
- if self . state == . waiting {
86
- return super. isExecuting
87
- } else {
88
- return self . state == . executing || self . state == . paused
89
- }
66
+ state == . waiting ? super. isExecuting : state == . executing || state == . paused
90
67
}
91
68
92
69
override open var isFinished : Bool {
93
- if self . state == . waiting {
94
- return super. isFinished
95
- } else {
96
- return self . state == . finished
97
- }
70
+ state == . waiting ? super. isFinished : state == . finished
98
71
}
99
72
100
73
override open var isCancelled : Bool {
101
- if self . state == . waiting {
102
- return super. isCancelled
103
- } else {
104
- return self . state == . cancelled
105
- }
74
+ state == . waiting ? super. isCancelled : state == . cancelled
106
75
}
107
76
108
77
/// A Boolean value indicating whether the operation has been paused.
109
78
open var isPaused : Bool {
110
- self . state == . paused
79
+ state == . paused
111
80
}
112
81
113
82
/// Resumes the operation, if it's paused.
114
83
open func resume( ) {
115
- if isExecuting, state == . paused {
116
- state = . executing
117
- }
84
+ guard isExecuting, state == . paused else { return }
85
+ state = . executing
118
86
}
119
87
120
88
/// Pauses the operation.
121
89
open func pause( ) {
122
- if isExecuting, state != . paused {
123
- state = . paused
124
- }
90
+ guard isExecuting, state != . paused else { return }
91
+ state = . paused
125
92
}
126
93
127
94
/// Finishes executing the operation.
128
95
open func finish( ) {
129
- if isExecuting {
130
- state = . finished
131
- }
96
+ guard isExecuting else { return }
97
+ state = . finished
132
98
}
133
99
134
100
override open func cancel( ) {
135
- if isExecuting {
136
- state = . cancelled
137
- }
101
+ guard isExecuting else { return }
102
+ state = . cancelled
138
103
}
139
104
140
105
override open var isAsynchronous : Bool {
@@ -159,7 +124,14 @@ open class AsyncBlockOperation: AsyncOperation {
159
124
160
125
override open func start( ) {
161
126
super. start ( )
162
- guard isExecuting else { return }
127
+ guard isExecuting, !isPaused else { return }
163
128
closure ( self )
129
+ state = . finished
130
+ }
131
+
132
+ open override func resume( ) {
133
+ super. resume ( )
134
+ guard isExecuting, !isPaused else { return }
135
+ start ( )
164
136
}
165
137
}
0 commit comments