Skip to content

Commit 72aeca7

Browse files
committed
Reduce codes using Swift's type-inference.
1 parent 662d686 commit 72aeca7

File tree

5 files changed

+105
-154
lines changed

5 files changed

+105
-154
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ public class Task<Progress, Value, Error>
118118

119119
///
120120
/// Creates new task.
121-
/// e.g. Task<P, V, E>(weakified: false) { (progress, fulfill, reject, configure) in ... }
121+
/// e.g. Task<P, V, E>(weakified: false) { progress, fulfill, reject, configure in ... }
122122
///
123123
/// :param: weakified Weakifies progress/fulfill/reject handlers to let player (inner asynchronous implementation inside initClosure) NOT CAPTURE this created new task. Normally, weakified = false should be set to gain "player -> task" retaining, so that task will be automatically deinited when player is deinited. If weakified = true, task must be manually retained somewhere else, or it will be immediately deinited.
124124
///
125-
/// :param: initClosure e.g. { (progress, fulfill, reject, configure) in ... }. fulfill(value) and reject(error) handlers must be called inside this closure, where calling progress(progressValue) handler is optional. Also as options, configure.pause/resume/cancel closures can be set to gain control from outside e.g. task.pause()/resume()/cancel(). When using configure, make sure to use weak modifier when appropriate to avoid "task -> player" retaining which often causes retain cycle.
125+
/// :param: initClosure e.g. { progress, fulfill, reject, configure in ... }. fulfill(value) and reject(error) handlers must be called inside this closure, where calling progress(progressValue) handler is optional. Also as options, configure.pause/resume/cancel closures can be set to gain control from outside e.g. task.pause()/resume()/cancel(). When using configure, make sure to use weak modifier when appropriate to avoid "task -> player" retaining which often causes retain cycle.
126126
///
127127
/// :returns: New task.
128128
///
@@ -144,7 +144,7 @@ public class Task<Progress, Value, Error>
144144
/// creates fulfilled task
145145
public convenience init(value: Value)
146146
{
147-
self.init(initClosure: { (progress, fulfill, reject, configure) in
147+
self.init(initClosure: { progress, fulfill, reject, configure in
148148
fulfill(value)
149149
return
150150
})
@@ -153,7 +153,7 @@ public class Task<Progress, Value, Error>
153153
/// creates rejected task
154154
public convenience init(error: Error)
155155
{
156-
self.init(initClosure: { (progress, fulfill, reject, configure) in
156+
self.init(initClosure: { progress, fulfill, reject, configure in
157157
reject(error)
158158
return
159159
})
@@ -162,7 +162,7 @@ public class Task<Progress, Value, Error>
162162
/// creates promise-like task which only allows fulfill & reject (no progress & configure)
163163
public convenience init(promiseInitClosure: PromiseInitClosure)
164164
{
165-
self.init(initClosure: { (progress, fulfill, reject, configure) in
165+
self.init(initClosure: { progress, fulfill, reject, configure in
166166
promiseInitClosure(fulfill: fulfill, reject: { (error: Error) in reject(error) })
167167
return
168168
})
@@ -283,9 +283,7 @@ public class Task<Progress, Value, Error>
283283
{
284284
self.machine.addEventHandler(.Progress) { [weak self] context in
285285
if let progress = context.userInfo as? Progress {
286-
if let self_ = self {
287-
progressClosure(oldValue: self_.progress, newValue: progress)
288-
}
286+
progressClosure(oldValue: self?.progress, newValue: progress)
289287
}
290288
}
291289

SwiftTaskTests/AlamofireTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AlamofireTests: _TestCase
1717
{
1818
var expect = self.expectationWithDescription(__FUNCTION__)
1919

20-
let task = Task<Progress, String, NSError> { (progress, fulfill, reject, configure) in
20+
let task = Task<Progress, String, NSError> { progress, fulfill, reject, configure in
2121

2222
request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
2323
.response { (request, response, data, error) in
@@ -52,7 +52,7 @@ class AlamofireTests: _TestCase
5252
{
5353
var expect = self.expectationWithDescription(__FUNCTION__)
5454

55-
let task = Task<Progress, String?, NSError> { (progress, fulfill, reject, configure) in
55+
let task = Task<Progress, String?, NSError> { progress, fulfill, reject, configure in
5656

5757
let dummyURLString = "http://xxx-swift-task.org/get"
5858

@@ -96,7 +96,7 @@ class AlamofireTests: _TestCase
9696
var expect = self.expectationWithDescription(__FUNCTION__)
9797

9898
// define task
99-
let task = Task<Progress, String, NSError> { (progress, fulfill, reject, configure) in
99+
let task = Task<Progress, String, NSError> { progress, fulfill, reject, configure in
100100

101101
download(.GET, "http://httpbin.org/stream/100", Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask))
102102

@@ -146,7 +146,7 @@ class AlamofireTests: _TestCase
146146
var nsProgress = NSProgress(totalUnitCount: 100)
147147

148148
// define task
149-
let task = Task<Progress, String, NSError> { (progress, fulfill, reject, configure) in
149+
let task = Task<Progress, String, NSError> { progress, fulfill, reject, configure in
150150

151151
nsProgress.becomeCurrentWithPendingUnitCount(50)
152152

@@ -199,7 +199,7 @@ class AlamofireTests: _TestCase
199199
{
200200
var expect = self.expectationWithDescription(__FUNCTION__)
201201

202-
let task = Task<Progress, String?, NSError> { (progress, fulfill, reject, configure) in
202+
let task = Task<Progress, String?, NSError> { progress, fulfill, reject, configure in
203203

204204
let downloadRequst = download(.GET, "http://httpbin.org/stream/100", Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask))
205205

SwiftTaskTests/BasicTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BasicTests: _TestCase
1919
var progressCount = 0
2020

2121
// define task
22-
let task = Task { (progress, fulfill, reject, configure) in
22+
let task = Task { progress, fulfill, reject, configure in
2323

2424
Async.main(after: 0.1) {
2525
progress(0.0)
@@ -35,17 +35,17 @@ class BasicTests: _TestCase
3535
return
3636

3737
}
38-
38+
3939
task.onProgress { oldValue, newValue in
4040

4141
println("progress = \(newValue)")
4242

43-
}.onSuccess { (value: String) -> String in // `task.onSuccess {...}` = JavaScript's `promise.then(onFulfilled)`
43+
}.onSuccess { value -> String in // `task.onSuccess {...}` = JavaScript's `promise.then(onFulfilled)`
4444

4545
XCTAssertEqual(value, "OK")
4646
return "Now OK"
47-
48-
}.onFailure { (error: ErrorString?, isCancelled: Bool) -> String in // `task.onFailure {...}` = JavaScript's `promise.catch(onRejected)`
47+
48+
}.onFailure { error, isCancelled -> String in // `task.onFailure {...}` = JavaScript's `promise.catch(onRejected)`
4949

5050
XCTAssertEqual(error!, "ERROR")
5151
return "Now RECOVERED"

SwiftTaskTests/RetainCycleTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class RetainCycleTests: _TestCase
6161
// 2. dispatch_queue x-> player
6262
// dispatch_queue (via player impl) x-> player (via completion capturing)
6363
//
64-
self.task = Task { (progress, fulfill, reject, configure) in
64+
self.task = Task { progress, fulfill, reject, configure in
6565

6666
let player = Player()
6767
self.player = player
@@ -78,7 +78,7 @@ class RetainCycleTests: _TestCase
7878
XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
7979
XCTAssertNotNil(self.player, "self.player (weak) should NOT nil because player is not retained by dispatch_queue.")
8080

81-
self.task!.onSuccess { (value: String) -> Void in
81+
self.task!.onSuccess { value -> Void in
8282

8383
XCTAssertEqual(value, "OK")
8484
expect.fulfill()
@@ -108,7 +108,7 @@ class RetainCycleTests: _TestCase
108108
// 3. task -> player
109109
// task -> task.machine -> configure (via pause/resume addEventHandler) -> configure.cancel -> player
110110
//
111-
self.task = Task { (progress, fulfill, reject, configure) in
111+
self.task = Task { progress, fulfill, reject, configure in
112112

113113
let player = Player()
114114
self.player = player
@@ -124,7 +124,7 @@ class RetainCycleTests: _TestCase
124124
XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
125125
XCTAssertNotNil(self.player, "self.player (weak) should NOT be nil because of retain cycle: player <- configure <- task.")
126126

127-
self.task!.onSuccess { (value: String) -> Void in
127+
self.task!.onSuccess { value -> Void in
128128

129129
XCTAssertEqual(value, "OK")
130130
expect.fulfill()
@@ -148,7 +148,7 @@ class RetainCycleTests: _TestCase
148148
// 1. dispatch_queue x-> player -> task
149149
// dispatch_queue (via player impl) x-> player -> player.completionHandler -> fulfill -> task
150150
//
151-
self.task = Task { (progress, fulfill, reject, configure) in
151+
self.task = Task { progress, fulfill, reject, configure in
152152

153153
let player = Player()
154154
self.player = player
@@ -166,7 +166,7 @@ class RetainCycleTests: _TestCase
166166
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
167167
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
168168

169-
self.task!.onSuccess { (value: String) -> Void in
169+
self.task!.onSuccess { value -> Void in
170170

171171
XCTAssertEqual(value, "OK")
172172
expect.fulfill()
@@ -193,7 +193,7 @@ class RetainCycleTests: _TestCase
193193
// 2. task x-> player
194194
// task -> task.machine -> configure (via pause/resume addEventHandler) -> configure.pause/resume/cancel x-> player
195195
//
196-
self.task = Task { (progress, fulfill, reject, configure) in
196+
self.task = Task { progress, fulfill, reject, configure in
197197

198198
let player = Player()
199199
self.player = player
@@ -210,7 +210,7 @@ class RetainCycleTests: _TestCase
210210
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
211211
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
212212

213-
self.task!.onSuccess { (value: String) -> Void in
213+
self.task!.onSuccess { value -> Void in
214214

215215
XCTAssertEqual(value, "OK")
216216
expect.fulfill()

0 commit comments

Comments
 (0)