Skip to content

Commit 2a6566b

Browse files
committed
Add configure.isFinished & always call configure.cancel() on finished.
1 parent 231973d commit 2a6566b

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,29 @@ public class TaskConfiguration
2828
public var resume: (Void -> Void)?
2929
public var cancel: (Void -> Void)?
3030

31+
/// useful to terminate immediate-infinite-sequence while performing `initClosure`
32+
public private(set) var isFinished: Bool = false
33+
3134
public init()
3235
{
3336

3437
}
3538

36-
internal func clear()
39+
internal func finish()
3740
{
41+
//
42+
// Cancel anyway on task finished (fulfilled/rejected/cancelled).
43+
//
44+
// NOTE:
45+
// ReactKit uses this closure to call `upstreamSignal.cancel()`
46+
// and let it know `configure.isFinished = true` while performing its `initClosure`.
47+
//
48+
self.cancel?()
49+
3850
self.pause = nil
3951
self.resume = nil
4052
self.cancel = nil
53+
self.isFinished = true
4154
}
4255
}
4356

SwiftTask/_StateMachine.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal class _StateMachine<Progress, Value, Error>
7373
if self.state == .Running {
7474
self.state = .Fulfilled
7575
self.value = value
76-
self.complete()
76+
self.finish()
7777
}
7878
}
7979

@@ -82,7 +82,7 @@ internal class _StateMachine<Progress, Value, Error>
8282
if self.state == .Running || self.state == .Paused {
8383
self.state = errorInfo.isCancelled ? .Cancelled : .Rejected
8484
self.errorInfo = errorInfo
85-
self.complete()
85+
self.finish()
8686
}
8787
}
8888

@@ -157,33 +157,26 @@ internal class _StateMachine<Progress, Value, Error>
157157
internal func handleCancel(error: Error? = nil) -> Bool
158158
{
159159
if self.state == .Running || self.state == .Paused {
160-
161160
self.state = .Cancelled
162161
self.errorInfo = ErrorInfo(error: error, isCancelled: true)
163-
self.complete {
164-
// NOTE: call `configuration.cancel()` after all `completionHandlers` are invoked
165-
self.configuration.cancel?()
166-
return
167-
}
168-
162+
self.finish()
169163
return true
170164
}
171165
else {
172166
return false
173167
}
174168
}
175169

176-
internal func complete(closure: (Void -> Void)? = nil)
170+
internal func finish()
177171
{
178172
for handler in self.completionHandlers {
179173
handler()
180174
}
181175

182-
closure?()
183-
184176
self.progressTupleHandlers.removeAll()
185177
self.completionHandlers.removeAll()
186-
self.configuration.clear()
178+
179+
self.configuration.finish()
187180

188181
self.initResumeClosure = nil
189182
self.progress = nil

0 commit comments

Comments
 (0)