@@ -65,20 +65,7 @@ public class TaskConfiguration
65
65
}
66
66
}
67
67
68
- // abstract class for `weak _parentTask` with arbitrary `Progress` & `Value` types
69
- public class _Task < Error>
70
- {
71
- internal weak var _parentTask : _Task ?
72
-
73
- internal let _weakified : Bool
74
-
75
- public init ( weakified: Bool , paused: Bool ) { self . _weakified = weakified }
76
- public func pause( ) -> Bool { return true }
77
- public func resume( ) -> Bool { return true }
78
- public func cancel( error: Error ? = nil ) -> Bool { return true }
79
- }
80
-
81
- public class Task < Progress, Value, Error> : _Task < Error >
68
+ public class Task < Progress, Value, Error>
82
69
{
83
70
public typealias ErrorInfo = ( error: Error ? , isCancelled: Bool )
84
71
@@ -101,6 +88,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
101
88
private var machine : Machine !
102
89
103
90
// store initial parameters for cloning task when using `try()`
91
+ internal let _weakified : Bool
104
92
internal var _initClosure : _InitClosure ? // will be nil on fulfilled/rejected
105
93
106
94
/// wrapper closure for `_initClosure` to invoke only once when started `.Running`
@@ -144,7 +132,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
144
132
///
145
133
public init ( weakified: Bool , paused: Bool , initClosure: InitClosure )
146
134
{
147
- super . init ( weakified : weakified , paused : paused )
135
+ self . _weakified = weakified
148
136
149
137
let _initClosure : _InitClosure = { machine, progress, fulfill, _reject, configure in
150
138
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
@@ -214,7 +202,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
214
202
/// (NOTE: _initClosure has _RejectHandler as argument)
215
203
internal init ( weakified: Bool = false , paused: Bool = false , _initClosure: _InitClosure )
216
204
{
217
- super. init ( weakified: weakified, paused: paused)
205
+ self . _weakified = weakified
206
+
218
207
self . setup ( weakified, paused: paused, _initClosure)
219
208
}
220
209
@@ -335,46 +324,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
335
324
336
325
_initClosure ( machine: self_. machine, progress: progressHandler, fulfill: fulfillHandler, _reject: rejectHandler, configure: configuration)
337
326
338
- let userPauseClosure = configuration. pause
339
- let userResumeClosure = configuration. resume
340
- let userCancelClosure = configuration. cancel
341
-
342
- // add parentTask-pause/resume/cancel functionalities after retrieving user-defined configuration
343
- configuration. pause = { [ weak self_] in
344
- userPauseClosure ? ( )
345
-
346
- var task : _Task ? = self_
347
- while let parentTask = task? . _parentTask {
348
- if parentTask. _weakified { break }
349
-
350
- parentTask. pause ( )
351
- task = parentTask
352
- }
353
-
354
- }
355
- configuration. resume = { [ weak self_] in
356
- userResumeClosure ? ( )
357
-
358
- var task : _Task ? = self_
359
- while let parentTask = task? . _parentTask {
360
- if parentTask. _weakified { break }
361
-
362
- parentTask. resume ( )
363
- task = parentTask
364
- }
365
- }
366
- configuration. cancel = { [ weak self_] in
367
- userCancelClosure ? ( )
368
-
369
- var task : _Task ? = self_
370
- while let parentTask = task? . _parentTask {
371
- if parentTask. _weakified { break }
372
-
373
- parentTask. cancel ( )
374
- task = parentTask
375
- }
376
- }
377
-
378
327
}
379
328
380
329
}
@@ -407,6 +356,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
407
356
408
357
let newTask = Task { [ weak self] machine, progress, fulfill, _reject, configure in
409
358
359
+ var chainedTasks = [ self !]
410
360
var nextTask : Task = self !
411
361
412
362
for i in 1 ... maxTryCount- 1 {
@@ -415,6 +365,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
415
365
} . failure { _ -> Task in
416
366
return Task ( weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
417
367
}
368
+
369
+ chainedTasks += [ nextTask]
418
370
}
419
371
420
372
nextTask. progress { _, progressValue in
@@ -425,9 +377,22 @@ public class Task<Progress, Value, Error>: _Task<Error>
425
377
_reject ( errorInfo)
426
378
}
427
379
428
- configure. pause = { nextTask. pause ( ) ; return }
429
- configure. resume = { nextTask. resume ( ) ; return }
430
- configure. cancel = { nextTask. cancel ( ) ; return }
380
+ configure. pause = {
381
+ for task in chainedTasks {
382
+ task. pause ( ) ;
383
+ }
384
+ }
385
+ configure. resume = {
386
+ for task in chainedTasks {
387
+ task. resume ( ) ;
388
+ }
389
+ }
390
+ configure. cancel = {
391
+ // NOTE: use `reverse()` to cancel from downstream to upstream
392
+ for task in chainedTasks. reverse ( ) {
393
+ task. cancel ( ) ;
394
+ }
395
+ }
431
396
432
397
}
433
398
@@ -540,8 +505,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
540
505
541
506
}
542
507
543
- newTask. _parentTask = self
544
-
545
508
return newTask
546
509
}
547
510
@@ -621,8 +584,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
621
584
622
585
}
623
586
624
- newTask. _parentTask = self
625
-
626
587
return newTask
627
588
}
628
589
@@ -705,17 +666,15 @@ public class Task<Progress, Value, Error>: _Task<Error>
705
666
706
667
}
707
668
708
- newTask. _parentTask = self
709
-
710
669
return newTask
711
670
}
712
671
713
- public override func pause( ) -> Bool
672
+ public func pause( ) -> Bool
714
673
{
715
674
return self . machine <-! . Pause
716
675
}
717
676
718
- public override func resume( ) -> Bool
677
+ public func resume( ) -> Bool
719
678
{
720
679
// always try `_performInitClosure` only once on `resume()`
721
680
// even when to-Resume-transition fails, e.g. already been fulfilled/rejected
@@ -725,7 +684,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
725
684
return self . machine <-! . Resume
726
685
}
727
686
728
- public override func cancel( error: Error ? = nil ) -> Bool
687
+ public func cancel( error: Error ? = nil ) -> Bool
729
688
{
730
689
return self . _cancel ( error: error)
731
690
}
0 commit comments