@@ -70,7 +70,9 @@ public class _Task<Error>
70
70
{
71
71
internal weak var _parentTask : _Task ?
72
72
73
- public init ( ) { }
73
+ internal let _weakified : Bool
74
+
75
+ public init ( weakified: Bool ) { self . _weakified = weakified }
74
76
public func pause( ) -> Bool { return true }
75
77
public func resume( ) -> Bool { return true }
76
78
public func cancel( error: Error ? = nil ) -> Bool { return true }
@@ -99,7 +101,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
99
101
internal var machine : Machine !
100
102
101
103
// store initial parameters for cloning task when using `try()`
102
- internal let _weakified : Bool
103
104
internal var _initClosure : _InitClosure ? // will be nil on fulfilled/rejected
104
105
105
106
/// progress value
@@ -138,15 +139,15 @@ public class Task<Progress, Value, Error>: _Task<Error>
138
139
///
139
140
public init ( weakified: Bool , initClosure: InitClosure )
140
141
{
141
- self . _weakified = weakified
142
- self . _initClosure = { machine, progress, fulfill, _reject, configure in
142
+ super. init ( weakified: weakified)
143
+
144
+ let _initClosure : _InitClosure = { machine, progress, fulfill, _reject, configure in
143
145
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
144
146
initClosure ( progress: progress, fulfill: fulfill, reject: { ( error: Error ? ) in _reject ( ErrorInfo ( error: error, isCancelled: false ) ) } , configure: configure)
145
147
return
146
148
}
147
149
148
- super. init ( )
149
- self . setup ( weakified, self . _initClosure!)
150
+ self . setup ( weakified, _initClosure)
150
151
}
151
152
152
153
/// creates task without weakifying progress/fulfill/reject handlers
@@ -185,10 +186,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
185
186
/// NOTE: _initClosure has _RejectHandler as argument
186
187
internal init ( weakified: Bool = false , _initClosure: _InitClosure )
187
188
{
188
- self . _weakified = weakified
189
- self . _initClosure = _initClosure
190
-
191
- super. init ( )
189
+ super. init ( weakified: weakified)
192
190
self . setup ( weakified, _initClosure)
193
191
}
194
192
@@ -198,6 +196,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
198
196
// println("[init] \(self)")
199
197
// #endif
200
198
199
+ self . _initClosure = _initClosure
200
+
201
201
let configuration = Configuration ( )
202
202
203
203
weak var weakSelf = self
@@ -307,6 +307,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
307
307
308
308
var task : _Task ? = self
309
309
while let parentTask = task? . _parentTask {
310
+ if parentTask. _weakified { break }
311
+
310
312
parentTask. pause ( )
311
313
task = parentTask
312
314
}
@@ -317,6 +319,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
317
319
318
320
var task : _Task ? = self
319
321
while let parentTask = task? . _parentTask {
322
+ if parentTask. _weakified { break }
323
+
320
324
parentTask. resume ( )
321
325
task = parentTask
322
326
}
@@ -326,11 +330,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
326
330
327
331
var task : _Task ? = self
328
332
while let parentTask = task? . _parentTask {
333
+ if parentTask. _weakified { break }
334
+
329
335
parentTask. cancel ( )
330
336
task = parentTask
331
337
}
332
338
}
333
-
334
339
}
335
340
336
341
deinit
@@ -354,15 +359,15 @@ public class Task<Progress, Value, Error>: _Task<Error>
354
359
355
360
if initClosure == nil { return self }
356
361
357
- var nextTask : Task ? = self
362
+ var nextTask : Task = self
358
363
359
364
for i in 1 ... tryCount- 1 {
360
- nextTask = nextTask! . failure { _ -> Task in
365
+ nextTask = nextTask. failure { _ -> Task in
361
366
return Task ( weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
362
367
}
363
368
}
364
369
365
- return nextTask!
370
+ return nextTask
366
371
}
367
372
368
373
///
@@ -400,7 +405,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
400
405
///
401
406
public func then< Progress2, Value2> ( thenClosure: ( Value ? , ErrorInfo ? ) -> Task < Progress2 , Value2 , Error > ) -> Task < Progress2 , Value2 , Error >
402
407
{
403
- let newTask = Task < Progress2 , Value2 , Error > { machine, progress, fulfill, _reject, configure in
408
+ let newTask = Task < Progress2 , Value2 , Error > { [ weak self ] machine, progress, fulfill, _reject, configure in
404
409
405
410
let bind = { [ weak machine] ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
406
411
let innerTask = thenClosure ( value, errorInfo)
@@ -443,27 +448,29 @@ public class Task<Progress, Value, Error>: _Task<Error>
443
448
}
444
449
}
445
450
446
- switch self . machine. state {
447
- case . Fulfilled:
448
- bind ( self . value!, nil )
449
- case . Rejected:
450
- bind ( nil , self . errorInfo!)
451
- default :
452
- self . machine. addEventHandler ( . Progress) { context in
453
- if let ( _, progressValue) = context. userInfo as? Task < Progress2 , Value2 , Error > . ProgressTuple {
454
- progress ( progressValue)
451
+ if let self_ = self {
452
+ switch self_. machine. state {
453
+ case . Fulfilled:
454
+ bind ( self_. value!, nil )
455
+ case . Rejected:
456
+ bind ( nil , self_. errorInfo!)
457
+ default :
458
+ self_. machine. addEventHandler ( . Progress) { context in
459
+ if let ( _, progressValue) = context. userInfo as? Task < Progress2 , Value2 , Error > . ProgressTuple {
460
+ progress ( progressValue)
461
+ }
455
462
}
456
- }
457
- self . machine . addEventHandler ( . Fulfill ) { context in
458
- if let value = context . userInfo as? Value {
459
- bind ( value , nil )
463
+ self_ . machine . addEventHandler ( . Fulfill ) { context in
464
+ if let value = context. userInfo as? Value {
465
+ bind ( value , nil )
466
+ }
460
467
}
461
- }
462
- self . machine . addEventHandler ( . Reject ) { context in
463
- if let errorInfo = context . userInfo as? ErrorInfo {
464
- bind ( nil , errorInfo )
468
+ self_ . machine . addEventHandler ( . Reject ) { context in
469
+ if let errorInfo = context. userInfo as? ErrorInfo {
470
+ bind ( nil , errorInfo )
471
+ }
465
472
}
466
- }
473
+ }
467
474
}
468
475
469
476
}
@@ -492,7 +499,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
492
499
///
493
500
public func success< Progress2, Value2> ( successClosure: Value -> Task < Progress2 , Value2 , Error > ) -> Task < Progress2 , Value2 , Error >
494
501
{
495
- let newTask = Task < Progress2 , Value2 , Error > { machine, progress, fulfill, _reject, configure in
502
+ let newTask = Task < Progress2 , Value2 , Error > { [ weak self ] machine, progress, fulfill, _reject, configure in
496
503
497
504
let bind = { [ weak machine] ( value: Value ) -> Void in
498
505
let innerTask = successClosure ( value)
@@ -521,27 +528,29 @@ public class Task<Progress, Value, Error>: _Task<Error>
521
528
}
522
529
}
523
530
524
- switch self . machine. state {
525
- case . Fulfilled:
526
- bind ( self . value!)
527
- case . Rejected:
528
- _reject ( self . errorInfo!)
529
- default :
530
- self . machine. addEventHandler ( . Progress) { context in
531
- if let ( _, progressValue) = context. userInfo as? Task < Progress2 , Value2 , Error > . ProgressTuple {
532
- progress ( progressValue)
531
+ if let self_ = self {
532
+ switch self_. machine. state {
533
+ case . Fulfilled:
534
+ bind ( self_. value!)
535
+ case . Rejected:
536
+ _reject ( self_. errorInfo!)
537
+ default :
538
+ self_. machine. addEventHandler ( . Progress) { context in
539
+ if let ( _, progressValue) = context. userInfo as? Task < Progress2 , Value2 , Error > . ProgressTuple {
540
+ progress ( progressValue)
541
+ }
533
542
}
534
- }
535
- self . machine . addEventHandler ( . Fulfill ) { context in
536
- if let value = context . userInfo as? Value {
537
- bind ( value )
543
+ self_ . machine . addEventHandler ( . Fulfill ) { context in
544
+ if let value = context. userInfo as? Value {
545
+ bind ( value )
546
+ }
538
547
}
539
- }
540
- self . machine . addEventHandler ( . Reject ) { context in
541
- if let errorInfo = context . userInfo as? ErrorInfo {
542
- _reject ( errorInfo )
548
+ self_ . machine . addEventHandler ( . Reject ) { context in
549
+ if let errorInfo = context. userInfo as? ErrorInfo {
550
+ _reject ( errorInfo )
551
+ }
543
552
}
544
- }
553
+ }
545
554
}
546
555
547
556
}
@@ -572,7 +581,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
572
581
///
573
582
public func failure( failureClosure: ErrorInfo -> Task ) -> Task
574
583
{
575
- let newTask = Task { machine, progress, fulfill, _reject, configure in
584
+ let newTask = Task { [ weak self ] machine, progress, fulfill, _reject, configure in
576
585
577
586
let bind = { [ weak machine] ( errorInfo: ErrorInfo ) -> Void in
578
587
let innerTask = failureClosure ( errorInfo)
@@ -601,28 +610,30 @@ public class Task<Progress, Value, Error>: _Task<Error>
601
610
}
602
611
}
603
612
604
- switch self . machine. state {
605
- case . Fulfilled:
606
- fulfill ( self . value!)
607
- case . Rejected:
608
- let errorInfo = self . errorInfo!
609
- bind ( errorInfo)
610
- default :
611
- self . machine. addEventHandler ( . Progress) { context in
612
- if let ( _, progressValue) = context. userInfo as? Task . ProgressTuple {
613
- progress ( progressValue)
613
+ if let self_ = self {
614
+ switch self_. machine. state {
615
+ case . Fulfilled:
616
+ fulfill ( self_. value!)
617
+ case . Rejected:
618
+ let errorInfo = self_. errorInfo!
619
+ bind ( errorInfo)
620
+ default :
621
+ self_. machine. addEventHandler ( . Progress) { context in
622
+ if let ( _, progressValue) = context. userInfo as? Task . ProgressTuple {
623
+ progress ( progressValue)
624
+ }
614
625
}
615
- }
616
- self . machine . addEventHandler ( . Fulfill ) { context in
617
- if let value = context . userInfo as? Value {
618
- fulfill ( value )
626
+ self_ . machine . addEventHandler ( . Fulfill ) { context in
627
+ if let value = context. userInfo as? Value {
628
+ fulfill ( value )
629
+ }
619
630
}
620
- }
621
- self . machine . addEventHandler ( . Reject ) { context in
622
- if let errorInfo = context . userInfo as? ErrorInfo {
623
- bind ( errorInfo )
631
+ self_ . machine . addEventHandler ( . Reject ) { context in
632
+ if let errorInfo = context. userInfo as? ErrorInfo {
633
+ bind ( errorInfo )
634
+ }
624
635
}
625
- }
636
+ }
626
637
}
627
638
628
639
}
0 commit comments