Skip to content

Commit aad1fc8

Browse files
committed
Merge pull request #18 from ReactKit/no-cancelling-upstream
Don't propagate pause/resume/cancel to upstream.
2 parents 090a410 + 6d6fbcb commit aad1fc8

File tree

2 files changed

+79
-201
lines changed

2 files changed

+79
-201
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,7 @@ public class TaskConfiguration
6565
}
6666
}
6767

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>
8269
{
8370
public typealias ErrorInfo = (error: Error?, isCancelled: Bool)
8471

@@ -101,6 +88,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
10188
private var machine: Machine!
10289

10390
// store initial parameters for cloning task when using `try()`
91+
internal let _weakified: Bool
10492
internal var _initClosure: _InitClosure? // will be nil on fulfilled/rejected
10593

10694
/// wrapper closure for `_initClosure` to invoke only once when started `.Running`
@@ -144,7 +132,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
144132
///
145133
public init(weakified: Bool, paused: Bool, initClosure: InitClosure)
146134
{
147-
super.init(weakified: weakified, paused: paused)
135+
self._weakified = weakified
148136

149137
let _initClosure: _InitClosure = { machine, progress, fulfill, _reject, configure in
150138
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
@@ -214,7 +202,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
214202
/// (NOTE: _initClosure has _RejectHandler as argument)
215203
internal init(weakified: Bool = false, paused: Bool = false, _initClosure: _InitClosure)
216204
{
217-
super.init(weakified: weakified, paused: paused)
205+
self._weakified = weakified
206+
218207
self.setup(weakified, paused: paused, _initClosure)
219208
}
220209

@@ -335,46 +324,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
335324

336325
_initClosure(machine: self_.machine, progress: progressHandler, fulfill: fulfillHandler, _reject: rejectHandler, configure: configuration)
337326

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-
378327
}
379328

380329
}
@@ -407,6 +356,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
407356

408357
let newTask = Task { [weak self] machine, progress, fulfill, _reject, configure in
409358

359+
var chainedTasks = [self!]
410360
var nextTask: Task = self!
411361

412362
for i in 1...maxTryCount-1 {
@@ -415,6 +365,8 @@ public class Task<Progress, Value, Error>: _Task<Error>
415365
}.failure { _ -> Task in
416366
return Task(weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
417367
}
368+
369+
chainedTasks += [nextTask]
418370
}
419371

420372
nextTask.progress { _, progressValue in
@@ -425,9 +377,22 @@ public class Task<Progress, Value, Error>: _Task<Error>
425377
_reject(errorInfo)
426378
}
427379

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+
}
431396

432397
}
433398

@@ -540,8 +505,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
540505

541506
}
542507

543-
newTask._parentTask = self
544-
545508
return newTask
546509
}
547510

@@ -621,8 +584,6 @@ public class Task<Progress, Value, Error>: _Task<Error>
621584

622585
}
623586

624-
newTask._parentTask = self
625-
626587
return newTask
627588
}
628589

@@ -705,17 +666,15 @@ public class Task<Progress, Value, Error>: _Task<Error>
705666

706667
}
707668

708-
newTask._parentTask = self
709-
710669
return newTask
711670
}
712671

713-
public override func pause() -> Bool
672+
public func pause() -> Bool
714673
{
715674
return self.machine <-! .Pause
716675
}
717676

718-
public override func resume() -> Bool
677+
public func resume() -> Bool
719678
{
720679
// always try `_performInitClosure` only once on `resume()`
721680
// even when to-Resume-transition fails, e.g. already been fulfilled/rejected
@@ -725,7 +684,7 @@ public class Task<Progress, Value, Error>: _Task<Error>
725684
return self.machine <-! .Resume
726685
}
727686

728-
public override func cancel(error: Error? = nil) -> Bool
687+
public func cancel(error: Error? = nil) -> Bool
729688
{
730689
return self._cancel(error: error)
731690
}

0 commit comments

Comments
 (0)