Skip to content

Commit 0aee5b6

Browse files
committed
Improve optional-binding (strongifying self)
1 parent 800cd71 commit 0aee5b6

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,25 @@ public class Task<Progress, Value, Error>
177177
// TODO: how to nest these inside StateMachine's initClosure? (using `self` is not permitted)
178178
self.machine.addEventHandler(.Progress, order: 90) { [weak self] context in
179179
if let progress = context.userInfo as? Progress {
180-
self!.progress = progress
180+
if let self_ = self {
181+
self_.progress = progress
182+
}
181183
}
182184
}
183185
// NOTE: use order < 100 (default) to let fulfillHandler be invoked after setting value
184186
self.machine.addEventHandler(.Fulfill, order: 90) { [weak self] context in
185187
if let value = context.userInfo as? Value {
186-
self!.value = value
188+
if let self_ = self {
189+
self_.value = value
190+
}
187191
}
188192
configuration.clear()
189193
}
190194
self.machine.addEventHandler(.Reject, order: 90) { [weak self] context in
191195
if let errorInfo = context.userInfo as? ErrorInfo {
192-
self!.errorInfo = errorInfo
196+
if let self_ = self {
197+
self_.errorInfo = errorInfo
198+
}
193199
configuration.cancel?() // NOTE: call configured cancellation on reject as well
194200
}
195201
configuration.clear()
@@ -272,22 +278,24 @@ public class Task<Progress, Value, Error>
272278
configure.cancel = { innerTask.cancel(); return }
273279
}
274280

275-
switch self!.machine.state {
276-
case .Fulfilled:
277-
bind(self!.value!, nil)
278-
case .Rejected:
279-
bind(nil, self!.errorInfo!)
280-
default:
281-
self!.machine.addEventHandler(.Fulfill) { context in
282-
if let value = context.userInfo as? Value {
283-
bind(value, nil)
281+
if let self_ = self {
282+
switch self_.machine.state {
283+
case .Fulfilled:
284+
bind(self_.value!, nil)
285+
case .Rejected:
286+
bind(nil, self_.errorInfo!)
287+
default:
288+
self_.machine.addEventHandler(.Fulfill) { context in
289+
if let value = context.userInfo as? Value {
290+
bind(value, nil)
291+
}
284292
}
285-
}
286-
self!.machine.addEventHandler(.Reject) { context in
287-
if let errorInfo = context.userInfo as? ErrorInfo {
288-
bind(nil, errorInfo)
293+
self_.machine.addEventHandler(.Reject) { context in
294+
if let errorInfo = context.userInfo as? ErrorInfo {
295+
bind(nil, errorInfo)
296+
}
289297
}
290-
}
298+
}
291299
}
292300

293301
}
@@ -325,22 +333,24 @@ public class Task<Progress, Value, Error>
325333
configure.cancel = { innerTask.cancel(); return }
326334
}
327335

328-
switch self!.machine.state {
329-
case .Fulfilled:
330-
bind(self!.value!)
331-
case .Rejected:
332-
_reject(self!.errorInfo!)
333-
default:
334-
self!.machine.addEventHandler(.Fulfill) { context in
335-
if let value = context.userInfo as? Value {
336-
bind(value)
336+
if let self_ = self {
337+
switch self_.machine.state {
338+
case .Fulfilled:
339+
bind(self_.value!)
340+
case .Rejected:
341+
_reject(self_.errorInfo!)
342+
default:
343+
self_.machine.addEventHandler(.Fulfill) { context in
344+
if let value = context.userInfo as? Value {
345+
bind(value)
346+
}
337347
}
338-
}
339-
self!.machine.addEventHandler(.Reject) { context in
340-
if let errorInfo = context.userInfo as? ErrorInfo {
341-
_reject(errorInfo)
348+
self_.machine.addEventHandler(.Reject) { context in
349+
if let errorInfo = context.userInfo as? ErrorInfo {
350+
_reject(errorInfo)
351+
}
342352
}
343-
}
353+
}
344354
}
345355

346356
}
@@ -378,23 +388,25 @@ public class Task<Progress, Value, Error>
378388
configure.cancel = { innerTask.cancel(); return}
379389
}
380390

381-
switch self!.machine.state {
382-
case .Fulfilled:
383-
fulfill(self!.value!)
384-
case .Rejected:
385-
let errorInfo = self!.errorInfo!
386-
bind(errorInfo)
387-
default:
388-
self!.machine.addEventHandler(.Fulfill) { context in
389-
if let value = context.userInfo as? Value {
390-
fulfill(value)
391+
if let self_ = self {
392+
switch self_.machine.state {
393+
case .Fulfilled:
394+
fulfill(self_.value!)
395+
case .Rejected:
396+
let errorInfo = self_.errorInfo!
397+
bind(errorInfo)
398+
default:
399+
self_.machine.addEventHandler(.Fulfill) { context in
400+
if let value = context.userInfo as? Value {
401+
fulfill(value)
402+
}
391403
}
392-
}
393-
self!.machine.addEventHandler(.Reject) { context in
394-
if let errorInfo = context.userInfo as? ErrorInfo {
395-
bind(errorInfo)
404+
self_.machine.addEventHandler(.Reject) { context in
405+
if let errorInfo = context.userInfo as? ErrorInfo {
406+
bind(errorInfo)
407+
}
396408
}
397-
}
409+
}
398410
}
399411

400412
}

Vendor/SwiftState

0 commit comments

Comments
 (0)