@@ -93,8 +93,13 @@ public class Task<Progress, Value, Error>
93
93
94
94
internal var machine : Machine !
95
95
96
+ /// progress value
96
97
public internal( set) var progress : Progress ?
98
+
99
+ /// fulfilled value
97
100
public internal( set) var value : Value ?
101
+
102
+ /// rejected/cancelled tuple info
98
103
public internal( set) var errorInfo : ErrorInfo ?
99
104
100
105
public var state : TaskState
@@ -194,7 +199,8 @@ public class Task<Progress, Value, Error>
194
199
}
195
200
196
201
// TODO: how to nest these inside StateMachine's initClosure? (using `self` is not permitted)
197
- self . machine. addEventHandler ( . Progress, order: 90 ) { [ weak self] context in
202
+ // NOTE: use order > 100 (default) to let `progressTupleClosure(self.progress, newValue)` be invoked first before updating old `self.progress`
203
+ self . machine. addEventHandler ( . Progress, order: 110 ) { [ weak self] context in
198
204
if let progress = context. userInfo as? Progress {
199
205
if let self_ = self {
200
206
self_. progress = progress
@@ -272,6 +278,7 @@ public class Task<Progress, Value, Error>
272
278
self . _cancel ( error: nil )
273
279
}
274
280
281
+ /// progress + newValue only
275
282
public func progress( progressClosure: Progress -> Void ) -> Task
276
283
{
277
284
self . machine. addEventHandler ( . Progress) { [ weak self] context in
@@ -283,6 +290,20 @@ public class Task<Progress, Value, Error>
283
290
return self
284
291
}
285
292
293
+ /// progress + (oldValue, newValue)
294
+ public func progress( progressTupleClosure: ( oldValue: Progress ? , newValue: Progress ) -> Void ) -> Task
295
+ {
296
+ self . machine. addEventHandler ( . Progress) { [ weak self] context in
297
+ if let progress = context. userInfo as? Progress {
298
+ if let self_ = self {
299
+ progressTupleClosure ( oldValue: self_. progress, newValue: progress)
300
+ }
301
+ }
302
+ }
303
+
304
+ return self
305
+ }
306
+
286
307
/// then (fulfilled & rejected) + returning value
287
308
public func then< Value2> ( thenClosure: ( Value ? , ErrorInfo ? ) -> Value2 ) -> Task < Progress , Value2 , Error >
288
309
{
0 commit comments