Skip to content

Commit 7e93d19

Browse files
committed
Update README.md
1 parent 72aeca7 commit 7e93d19

File tree

1 file changed

+76
-90
lines changed

1 file changed

+76
-90
lines changed

README.md

Lines changed: 76 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@ SwiftTask
55

66
![SwiftTask](Screenshots/diagram.png)
77

8+
### Ver 2.0.0 Changelog
9+
10+
- `task.progress()` is renamed to `task.onProgress()`
11+
- `progressClosure`'s closure type changed from `Progress -> Void` to `(oldProgress: Progress?, newProgress: Progress) -> Void`
12+
- `task.then(thenClosure)` is renamed to `task.onComplete()`
13+
- `task.then(fulfilledClosure)` is renamed to `task.onSuccess()`
14+
- `task.catch(catchClosure)` is renamed to `task.onFailure()`
15+
816

917
## Example
1018

1119
### Basic
1220

1321
```swift
1422
// define task
15-
let task = Task<Float, String, NSError> { (progress, fulfill, reject, configure) in
23+
let task = Task<Float, String, NSError> { progress, fulfill, reject, configure in
1624

1725
player.doSomethingWithProgress({ (progressValue: Float) in
1826
progress(progressValue) // optional
@@ -38,10 +46,10 @@ let task = Task<Float, String, NSError> { (progress, fulfill, reject, configure)
3846

3947
}
4048

41-
// set then & catch
42-
task.then { (value: String) -> Void in
49+
// set onSuccess & onFailure
50+
task.onSuccess { (value: String) -> Void in
4351
// do something with fulfilled value
44-
}.catch { (error: NSError?, isCancelled: Bool) -> Void in
52+
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
4553
// do something with rejected error
4654
}
4755

@@ -65,16 +73,17 @@ One of the best example would be [Alamofire](https://github.com/Alamofire/Alamof
6573

6674
```swift
6775
typealias Progress = (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
76+
typealias AlamoFireTask = Task<Progress, String, NSError>
6877

6978
// define task
70-
let task = Task<Progress, String, NSError> { (progress, fulfill, reject, configure) in
79+
let task = AlamoFireTask { progress, fulfill, reject, configure in
7180

7281
Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: somewhere)
73-
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
82+
.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
7483

7584
progress((bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) as Progress)
7685

77-
}.response { (request, response, data, error) in
86+
}.response { request, response, data, error in
7887

7988
if let error = error {
8089
reject(error)
@@ -88,15 +97,15 @@ let task = Task<Progress, String, NSError> { (progress, fulfill, reject, configu
8897
return
8998
}
9099

91-
// set progress & then
92-
task.progress { progress in
100+
// set onProgress & onComplete
101+
task.onProgress { (oldProgress: Progress?, newProgress: Progress) in
93102

94-
println("\(progress.bytesWritten)")
95-
println("\(progress.totalBytesWritten)")
96-
println("\(progress.totalBytesExpectedToWrite)")
103+
println("\(newProgress.bytesWritten)")
104+
println("\(newProgress.totalBytesWritten)")
105+
println("\(newProgress.totalBytesExpectedToWrite)")
97106

98-
}.then { (value: String) -> Void in
99-
// do something with fulfilled value
107+
}.onComplete { (value: String?, errorInfo: AlamoFireTask.ErrorInfo?) -> Void in
108+
// do something with fulfilled value or rejected errorInfo
100109
}
101110
```
102111

@@ -107,10 +116,10 @@ For more examples, please see XCTest cases.
107116

108117
### Task.init(closure:)
109118

110-
Define your `task` inside `closure`.
119+
Define your `task` inside `initClosure`.
111120

112121
```swift
113-
let task = Task<Float, NSString?, NSError> { (progress, fulfill, reject, configure) in
122+
let task = Task<Float, NSString?, NSError> { progress, fulfill, reject, configure in
114123

115124
player.doSomethingWithCompletion { (value: NSString?, error: NSError?) in
116125
if error == nil {
@@ -123,9 +132,9 @@ let task = Task<Float, NSString?, NSError> { (progress, fulfill, reject, configu
123132
}
124133
```
125134

126-
In order to pipeline future `task.value` or `task.errorInfo` (tuple of `(error: Error?, isCancelled: Bool)`) via `then` and `catch` methods, you have to call `fulfill(value)` and `reject(error)` inside closure.
135+
In order to pipeline future `task.value` or `task.errorInfo` (tuple of `(error: Error?, isCancelled: Bool)`) via `onComplete()`/`onSuccess()`/`onFailure()`, you have to call `fulfill(value)` and/or `reject(error)` inside `initClosure`.
127136

128-
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the closure, notifying it to `task` itself.
137+
Optionally, you can call `progress(progressValue)` multiple times before calling `fulfill`/`reject` to transfer `progressValue` outside of the `initClosure`, notifying it to `task` itself.
129138

130139
To add `pause`/`resume`/`cancel` functionality to your `task`, use `configure` to wrap up the original one.
131140

@@ -142,117 +151,94 @@ configure.cancel = { [weak player] in
142151
}
143152
```
144153

145-
### task.progress(_ progressClosure:) -> task
154+
### task.onProgress(_ progressClosure:) -> task
146155

147156
```swift
148-
task.progress { (progressValue: Progress) in
149-
println(progressValue)
157+
task.onProgress { (oldValue: Progress?, newValue: Progress) in
158+
println(newValue)
150159
return
151-
}.then { ... }
160+
}.onSuccess { ... }
152161
```
153162

154-
`task.progress(progressClosure)` will add `progressClosure` to observe `progressValue` which is notified from inside previous init-closure. This method will return same task, so it is useful to chain with forthcoming `then` and `catch`.
155-
156-
157-
### task.then(_ closure:) -> newTask
163+
`task.onProgress(progressClosure)` will add `progressClosure` to observe `progressValue` which is notified from inside previous `initClosure`. This method will return **same task**, so it is useful to chain with forthcoming `onComplete`/`onSuccess`/`onFailure`.
158164

159-
`task.then(closure)` will return a new task which behaves differently depending on what kind of `closure` is passed in.
160-
161-
1. `closure` used for **fulfilled only**
162-
2. `closure` used for both **fulfilled & rejected**
163-
164-
#### 1. closure used for fulfilled only = `fulfilledClosure`
165-
166-
`fulfilledClosure` will be invoked only when `task` is only *fulfilled*.
167-
168-
This case is similar to JavaScript's `promise.then(onFulfilled)`.
165+
### task.onComplete(_ completeClosure:) -> newTask
169166

170-
- `fulfilledClosure: Value -> Value2` (flow: *task => newTask*)
171-
172-
```swift
173-
// task will be fulfilled with value "Hello"
174-
175-
task.then { (value: String) -> String in
176-
return "\(value) World" // string value returns new string
177-
}.then { (value: String) -> Void in
178-
println("\(value)") // Hello World
179-
return"
180-
}
181-
```
182-
183-
- `fulfilledClosure: Value -> Task` (flow: *task => task2 => newTask*)
184-
185-
```swift
186-
// task will be fulfilled with value "Hello"
187-
// task2 will be fulfilled with value "\(value) Swift"
188-
189-
task.then { (value: String) -> Task<Float, String, NSError> in
190-
let task2 = ... // fulfilling "\(value) Swift"
191-
return task2
192-
}.then { (value: String) -> Void in
193-
println("\(value)") // Hello Swift
194-
return"
195-
}
196-
```
197-
198-
#### 2. closure for both fulfilled & rejected = `thenClosure`
199-
200-
In this case, `thenClosure` will be invoked when `task` is either *fulfilled* or *rejected*. This means, `thenClosure` is mostly called in future compared to `fulfilledClosure`, which is invoked only when *fulfilled*.
167+
`task.onComplete(completeClosure)` will return a new task where `completeClosure` will be invoked when `task` is either **fulfilled** or **rejected**.
201168

202169
This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
203170

204-
- `thenClosure: (Value?, ErrorInfo?) -> Value2` (flow: *task => newTask*)
171+
`completeClosure` can be two types of closure form:
172+
173+
1. `completeClosure: (Value?, ErrorInfo?) -> Value2` (flow: *task => newTask*)
205174

206175
```swift
207-
// task will be fulfilled with value "Hello"
176+
// let task will be fulfilled with value "Hello"
208177

209-
task.then { (value: String?, errorInfo: ErrorInfo?) -> String in
178+
task.onComplete { (value: String?, errorInfo: ErrorInfo?) -> String in
210179
// nil-check to find out whether task is fulfilled or rejected
211180
if errorInfo == nil {
212-
return "\(value) World" // string value returns new string
181+
return "\(value!) World"
213182
}
214183
else {
215-
return "\(value) Error"
184+
return "\(value!) Error"
216185
}
217-
}.then { (value: String) -> Void in
186+
}.onSuccess { (value: String) -> Void in
218187
println("\(value)") // Hello World
219188
return"
220189
}
221190
```
222191
223-
- `thenClosure: (Value?, ErrorInfo?) -> Task` (flow: *task => task2 => newTask*)
192+
2. `completeClosure: (Value?, ErrorInfo?) -> Task` (flow: *task => task2 => newTask*)
224193
225194
```swift
226-
// task will be fulfilled with value "Hello"
227-
// task2 will be fulfilled with value "\(value) Swift"
195+
// let task will be fulfilled with value "Hello"
228196
229-
task.then { (value: String) -> Task<Float, String, NSError> in
197+
task.onComplete { (value: String?, errorInfo: ErrorInfo?) -> Task<Float, String, NSError> in
230198
if errorInfo == nil {
231-
let task2 = ... // fulfilling "\(value) Swift"
199+
// let task2 will be fulfilled with value "\(value!) Swift"
200+
let task2 = ...
232201
return task2
233202
}
234203
else {
235204
return someOtherTask
236205
}
237-
}.then { (value: String) -> Void in
206+
}.onSuccess { (value: String) -> Void in
238207
println("\(value)") // Hello Swift
239208
return"
240209
}
241210
```
242211

243-
### task.catch(_ catchClosure:) -> newTask
212+
### task.onSuccess(_ successClosure:) -> newTask
213+
214+
Similar to `onComplete()` method, `task.onSuccess(successClosure)` will return a new task, but this time, `successClosure` will be invoked when task is **only fulfilled**.
215+
216+
This case is similar to JavaScript's `promise.then(onFulfilled)`.
217+
218+
```swift
219+
// let task will be fulfilled with value "Hello"
220+
221+
task.onSuccess { (value: String) -> String in
222+
return "\(value) World"
223+
}.onSuccess { (value: String) -> Void in
224+
println("\(value)") // Hello World
225+
return"
226+
}
227+
```
228+
229+
### task.onFailure(_ failureClosure:) -> newTask
244230
245-
Similar to `task.then(fulfilledClosure)` for fulfilled only, `task.catch(catchClosure)` will invoke `catchClosure` only when `task` is either *rejected* or *cancelled*.
231+
Just the opposite of `onSuccess()`, `task.onFailure(failureClosure)` will return a new task where `failureClosure` will be invoked when task is **only rejected/cancelled**.
246232
247233
This case is similar to JavaScript's `promise.then(undefined, onRejected)` or `promise.catch(onRejected)`.
248234
249235
```swift
250-
// task will be rejected with error "Oh My God"
236+
// let task will be rejected with error "Oh My God"
251237
252-
task.then { (value: String) -> Void in
238+
task.onSuccess { (value: String) -> Void in
253239
println("\(value)") // never reaches here
254240
return
255-
}.catch { (error: NSError?, isCancelled: Bool) -> Void in
241+
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
256242
println("\(error!)") // Oh My God
257243
return
258244
}
@@ -262,24 +248,24 @@ task.then { (value: String) -> Void in
262248
263249
`Task.all(tasks)` is a new task that performs all `tasks` simultaneously and will be:
264250
265-
- fulfilled when **all tasks will be fulfilled**
266-
- rejected when **any of the task will be rejected**
251+
- fulfilled when **all tasks are fulfilled**
252+
- rejected when **any of the task is rejected**
267253
268254
### Task.any(_ tasks:) -> newTask
269255
270256
`Task.any(tasks)` is an opposite of `Task.all(tasks)` which will be:
271257
272-
- fulfilled when **any of the task will be fulfilled**
273-
- rejected when **all tasks will be rejected**
258+
- fulfilled when **any of the task is fulfilled**
259+
- rejected when **all tasks are rejected**
274260
275261
### Task.some(_ tasks:) -> newTask
276262
277-
`Task.some(tasks)` is a new task that performs all `tasks` without internal rejection, and is fulfilled with given `tasks`'s fulfilled values. Note that this new task will also become *fulfilled* with empty value-array, even though all `tasks` are rejected.
263+
`Task.some(tasks)` is a new task that performs all `tasks` without internal rejection, and is fulfilled with given `tasks`'s fulfilled values. Note that this new task **will be fulfilled with empty value-array, even though all `tasks` are rejected.**
278264
279265
280266
## Related Articles
281267
282-
- [SwiftTask(Promise拡張)を使う - Qiita](http://qiita.com/inamiy/items/0756339aee35849384c3) (Japanese)
268+
- [SwiftTask(Promise拡張)を使う - Qiita](http://qiita.com/inamiy/items/0756339aee35849384c3) (Japanese, ver 1.0.0)
283269
284270
285271
## Licence

0 commit comments

Comments
 (0)