Skip to content

Commit f382409

Browse files
committed
Move machine.addEventHandler codes inside machine's initClosure.
1 parent 246be53 commit f382409

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public class Task<Progress, Value, Error>
171171
{
172172
let configuration = Configuration()
173173

174+
weak var weakSelf = self
175+
174176
// setup state machine
175177
self.machine = Machine(state: .Running) {
176178

@@ -190,33 +192,32 @@ public class Task<Progress, Value, Error>
190192
return
191193
}
192194

193-
}
194-
195-
// TODO: how to nest these inside StateMachine's initClosure? (using `self` is not permitted)
196-
self.machine.addEventHandler(.Progress, order: 90) { [weak self] context in
197-
if let progressTuple = context.userInfo as? ProgressTuple {
198-
if let self_ = self {
199-
self_.progress = progressTuple.newProgress
195+
$0.addEventHandler(.Progress, order: 90) { context in
196+
if let progressTuple = context.userInfo as? ProgressTuple {
197+
if let self_ = weakSelf {
198+
self_.progress = progressTuple.newProgress
199+
}
200200
}
201201
}
202-
}
203-
// NOTE: use order < 100 (default) to let fulfillHandler be invoked after setting value
204-
self.machine.addEventHandler(.Fulfill, order: 90) { [weak self] context in
205-
if let value = context.userInfo as? Value {
206-
if let self_ = self {
207-
self_.value = value
202+
203+
$0.addEventHandler(.Fulfill, order: 90) { context in
204+
if let value = context.userInfo as? Value {
205+
if let self_ = weakSelf {
206+
self_.value = value
207+
}
208208
}
209+
configuration.clear()
209210
}
210-
configuration.clear()
211-
}
212-
self.machine.addEventHandler(.Reject, order: 90) { [weak self] context in
213-
if let errorInfo = context.userInfo as? ErrorInfo {
214-
if let self_ = self {
215-
self_.errorInfo = errorInfo
211+
$0.addEventHandler(.Reject, order: 90) { context in
212+
if let errorInfo = context.userInfo as? ErrorInfo {
213+
if let self_ = weakSelf {
214+
self_.errorInfo = errorInfo
215+
}
216+
configuration.cancel?() // NOTE: call configured cancellation on reject as well
216217
}
217-
configuration.cancel?() // NOTE: call configured cancellation on reject as well
218+
configuration.clear()
218219
}
219-
configuration.clear()
220+
220221
}
221222

222223
var progressHandler: ProgressHandler

0 commit comments

Comments
 (0)