@@ -68,10 +68,6 @@ public class Task<Progress, Value, Error>: Printable
68
68
internal let _paused : Bool
69
69
internal var _initClosure : _InitClosure ! // retained throughout task's lifetime
70
70
71
- /// wrapper closure for `_initClosure` to invoke only once when started `.Running`,
72
- /// and will be set to `nil` afterward
73
- internal var _performInitClosure : ( Void -> Void ) ?
74
-
75
71
public var state : TaskState { return self . _machine. state }
76
72
77
73
/// progress value (NOTE: always nil when `weakified = true`)
@@ -122,10 +118,10 @@ public class Task<Progress, Value, Error>: Printable
122
118
123
119
let _initClosure : _InitClosure = { _, progress, fulfill, _reject, configure in
124
120
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
125
- initClosure ( progress: progress, fulfill: fulfill, reject: { ( error: Error ? ) in _reject ( ErrorInfo ( error: error, isCancelled: false ) ) } , configure: configure)
121
+ initClosure ( progress: progress, fulfill: fulfill, reject: { ( error: Error ) in _reject ( ErrorInfo ( error: error, isCancelled: false ) ) } , configure: configure)
126
122
}
127
123
128
- self . setup ( weakified, paused: paused, _initClosure)
124
+ self . setup ( weakified: weakified , paused: paused, _initClosure : _initClosure)
129
125
}
130
126
131
127
///
@@ -194,19 +190,20 @@ public class Task<Progress, Value, Error>: Printable
194
190
self . _paused = paused
195
191
self . _machine = _Machine ( weakified: weakified, paused: paused)
196
192
197
- self . setup ( weakified, paused: paused, _initClosure)
193
+ self . setup ( weakified: weakified , paused: paused, _initClosure : _initClosure)
198
194
}
199
195
200
- internal func setup( weakified: Bool , paused: Bool , _initClosure: _InitClosure )
201
- {
196
+ // NOTE: don't use `internal init` for this setup method, or this will be a designated initalizer
197
+ internal func setup( #weakified: Bool, paused: Bool, _initClosure: _InitClosure)
198
+ {
202
199
// #if DEBUG
203
200
// println("[init] \(self.name)")
204
201
// #endif
205
202
206
203
self . _initClosure = _initClosure
207
204
208
205
// will be invoked on 1st resume (only once)
209
- self . _performInitClosure = { [ weak self] in
206
+ self . _machine . initResumeClosure = { [ weak self] in
210
207
211
208
// strongify `self` on 1st resume
212
209
if let self_ = self {
@@ -259,7 +256,7 @@ public class Task<Progress, Value, Error>: Printable
259
256
self_. _machine. handleRejectInfo ( errorInfo)
260
257
}
261
258
}
262
-
259
+
263
260
_initClosure ( machine: self_. _machine, progress: progressHandler, fulfill: fulfillHandler, _reject: rejectInfoHandler, configure: self_. _machine. configuration)
264
261
265
262
}
@@ -342,7 +339,7 @@ public class Task<Progress, Value, Error>: Printable
342
339
///
343
340
public func progress( progressClosure: ProgressTuple -> Void ) -> Task
344
341
{
345
- self . _machine. progressTupleHandlers . append ( progressClosure)
342
+ self . _machine. addProgressTupleHandler ( progressClosure)
346
343
347
344
return self
348
345
}
@@ -392,7 +389,7 @@ public class Task<Progress, Value, Error>: Printable
392
389
case . Fulfilled, . Rejected, . Cancelled:
393
390
completionHandler ( )
394
391
default :
395
- self . _machine. completionHandlers . append ( completionHandler)
392
+ self . _machine. addCompletionHandler ( completionHandler)
396
393
}
397
394
}
398
395
@@ -478,40 +475,6 @@ public class Task<Progress, Value, Error>: Printable
478
475
479
476
public func resume( ) -> Bool
480
477
{
481
- //
482
- // Always try `_performInitClosure` only once on `resume()`
483
- // even when `.Pause => .Resume` transition fails, e.g. already been fulfilled/rejected.
484
- //
485
- // NOTE:
486
- // **`downstream._performInitClosure` should be invoked first before `downstream.machine <-! .Resume`**
487
- // to add upstream's progress/fulfill/reject handlers inside `downstream.initClosure()`
488
- // before their actual calls, which often happens
489
- // when downstream's `resume()` is configured to call upstream's `resume()`
490
- // which eventually calls `upstream._performInitClosure` and thus actual event handlers.
491
- //
492
- if ( self . _performInitClosure != nil ) {
493
-
494
- let isPaused = ( self . state == . Paused)
495
-
496
- //
497
- // Temporarily switch to `.Running` without invoking `configure.resume()`.
498
- // This allows paused-inited-task to safely call progress/fulfill/reject handlers
499
- // inside its `initClosure` *immediately*.
500
- //
501
- if isPaused {
502
- self . _machine. state = . Running // switch temporarily
503
- }
504
-
505
- self . _performInitClosure ? ( )
506
- self . _performInitClosure = nil
507
-
508
- // switch back to `.Paused` only if temporary `.Running` has not changed
509
- // (NOTE: `_performInitClosure` sometimes invokes `initClosure`'s `fulfill()`/`reject()` immediately)
510
- if isPaused && self . state == . Running {
511
- self . _machine. state = . Paused // switch back
512
- }
513
- }
514
-
515
478
return self . _machine. handleResume ( )
516
479
}
517
480
0 commit comments