@@ -3,16 +3,14 @@ SwiftTask
3
3
4
4
[ Promise] ( http://www.html5rocks.com/en/tutorials/es6/promises/ ) + progress + pause + cancel, using [ SwiftState] ( https://github.com/inamiy/SwiftState ) (state machine).
5
5
6
- (2014/08/23 Currently testing in Xcode6-beta5)
7
-
8
6
![ SwiftTask] ( Screenshots/diagram.png )
9
7
10
8
11
9
## Example
12
10
13
11
### Basic
14
12
15
- ```
13
+ ``` swift
16
14
// define task
17
15
let task = Task< Float , String , NSError> { (progress, fulfill, reject, configure) in
18
16
@@ -58,7 +56,8 @@ One of the best example would be [Alamofire](https://github.com/Alamofire/Alamof
58
56
as seen below.
59
57
60
58
### Using [ Alamofire] ( https://github.com/Alamofire/Alamofire )
61
- ```
59
+
60
+ ``` swift
62
61
typealias Progress = (bytesWritten: Int64 , totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 )
63
62
64
63
// define task
@@ -104,7 +103,7 @@ You can even use custom operators for chaining! :dizzy:
104
103
- ` task *** {...} ` = ` task.then { value in ...} ` (fulfilled only)
105
104
- ` task !!! {...} ` = ` task.catch { errorInfo in ...} ` (rejected only)
106
105
107
- ```
106
+ ``` swift
108
107
task ~ { (progress : Float ) in
109
108
110
109
println (" progress = \( progress ) " )
@@ -148,7 +147,7 @@ For more examples, please see XCTest cases.
148
147
149
148
Define your ` task ` inside ` closure ` .
150
149
151
- ```
150
+ ``` swift
152
151
let task = Task< Float , NSString? , NSError> { (progress, fulfill, reject, configure) in
153
152
154
153
player.doSomethingWithCompletion { (value : NSString? , error : NSError? ) in
@@ -168,15 +167,15 @@ Optionally, you can call `progress(progressValue)` multiple times before calling
168
167
169
168
To add ` pause ` /` resume ` /` cancel ` functionality to your ` task ` , use ` configure ` to wrap up the original one.
170
169
171
- ```
170
+ ``` swift
172
171
configure.pause = { player.pause () }
173
172
configure.resume = { player.resume () }
174
173
configure.cancel = { player.cancel () }
175
174
```
176
175
177
176
### task.progress(_ progressClosure:) -> task
178
177
179
- ```
178
+ ``` swift
180
179
task.progress { (progressValue : Progress) in
181
180
println (progressValue)
182
181
return
@@ -201,7 +200,7 @@ This case is similar to JavaScript's `promise.then(onFulfilled)`.
201
200
202
201
- ` fulfilledClosure: Value -> Value2 ` (flow: * task => newTask* )
203
202
204
- ```
203
+ ``` swift
205
204
// task will be fulfilled with value "Hello"
206
205
207
206
task.then { (value : String ) -> String in
@@ -214,7 +213,7 @@ This case is similar to JavaScript's `promise.then(onFulfilled)`.
214
213
215
214
- `fulfilledClosure: Value -> Task` (flow: *task => task2 => newTask*)
216
215
217
- ```
216
+ ```swift
218
217
// task will be fulfilled with value " Hello"
219
218
// task2 will be fulfilled with value " \() Swift"
220
219
@@ -235,7 +234,7 @@ This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
235
234
236
235
- ` thenClosure: (Value?, ErrorInfo?) -> Value2 ` (flow: * task => newTask* )
237
236
238
- ```
237
+ ``` swift
239
238
// task will be fulfilled with value "Hello"
240
239
241
240
task.then { (value : String ? , errorInfo : ErrorInfo? ) -> String in
@@ -254,7 +253,7 @@ This case is similar to JavaScript's `promise.then(onFulfilled, onRejected)`.
254
253
255
254
- `thenClosure: (Value?, ErrorInfo?) -> Task` (flow: *task => task2 => newTask*)
256
255
257
- ```
256
+ ```swift
258
257
// task will be fulfilled with value " Hello"
259
258
// task2 will be fulfilled with value " \(value) Swift"
260
259
@@ -278,7 +277,7 @@ Similar to `task.then(fulfilledClosure)` for fulfilled only, `task.catch(catchCl
278
277
279
278
This case is similar to JavaScript's ` promise.then(undefined, onRejected) ` or ` promise.catch(onRejected) ` .
280
279
281
- ```
280
+ ``` swift
282
281
// task will be rejected with error "Oh My God"
283
282
284
283
task.then { (value : String ) -> Void in
0 commit comments