Skip to content

Commit 9a64566

Browse files
committed
Merge pull request #30 from ReactKit/playground
[Playground] Add examples.
2 parents 30014bf + f93ba36 commit 9a64566

File tree

12 files changed

+272
-0
lines changed

12 files changed

+272
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Cocoa
4+
import XCPlayground
5+
6+
//
7+
// NOTE: custom framework needs to be built first (and restart Xcode if needed)
8+
//
9+
// Importing Custom Frameworks Into a Playground
10+
// https://developer.apple.com/library/prerelease/ios/recipes/Playground_Help/Chapters/ImportingaFrameworkIntoaPlayground.html
11+
//
12+
import SwiftTask
13+
14+
typealias Progress = Float
15+
typealias Value = String
16+
typealias Error = NSError
17+
18+
typealias MyTask = Task<Progress, Value, Error>
19+
20+
let myError = NSError(domain: "MyErrorDomain", code: 0, userInfo: nil)
21+
22+
//--------------------------------------------------
23+
// Example 1: Sync fulfilled -> success
24+
//--------------------------------------------------
25+
26+
let task = MyTask(value: "Hello")
27+
.success { value -> String in
28+
return "\(value) World"
29+
}
30+
.success { value -> String in
31+
return "\(value)!!!"
32+
}
33+
34+
task.value
35+
36+
//--------------------------------------------------
37+
// Example 2: Sync rejected -> success -> failure
38+
//--------------------------------------------------
39+
40+
let task2a = MyTask(error: myError)
41+
.success { value -> String in
42+
return "Never reaches here..."
43+
}
44+
let task2b = task2a
45+
.failure { error, isCancelled -> String in
46+
return "ERROR: \(error!.domain)" // recovery from failure
47+
}
48+
49+
task2a.value
50+
task2a.errorInfo
51+
task2b.value
52+
task2b.errorInfo
53+
54+
//--------------------------------------------------
55+
// Example 3: Sync fulfilled or rejected -> then
56+
//--------------------------------------------------
57+
58+
// fulfills or rejects immediately
59+
let task3a = MyTask { progress, fulfill, reject, configure in
60+
if arc4random_uniform(2) == 0 {
61+
fulfill("Hello")
62+
}
63+
else {
64+
reject(myError)
65+
}
66+
}
67+
let task3b = task3a
68+
.then { value, errorInfo -> String in
69+
if let errorInfo = errorInfo {
70+
return "ERROR: \(errorInfo.error!.domain)"
71+
}
72+
73+
return "\(value!) World"
74+
}
75+
76+
task3a.value
77+
task3a.errorInfo
78+
task3b.value
79+
task3b.errorInfo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//
2+
// This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to 01-Sync.playground.
3+
//
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'/>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Cocoa
4+
import XCPlayground
5+
6+
//
7+
// NOTE: custom framework needs to be built first (and restart Xcode if needed)
8+
//
9+
// Importing Custom Frameworks Into a Playground
10+
// https://developer.apple.com/library/prerelease/ios/recipes/Playground_Help/Chapters/ImportingaFrameworkIntoaPlayground.html
11+
//
12+
import SwiftTask
13+
14+
typealias Progress = Float
15+
typealias Value = String
16+
typealias Error = NSError
17+
18+
typealias MyTask = Task<Progress, Value, Error>
19+
20+
let myError = NSError(domain: "MyErrorDomain", code: 0, userInfo: nil)
21+
22+
// for async test
23+
XCPSetExecutionShouldContinueIndefinitely()
24+
25+
// fulfills after 100ms
26+
func asyncTask(value: String) -> MyTask
27+
{
28+
return MyTask { progress, fulfill, reject, configure in
29+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100_000_000), dispatch_get_main_queue()) {
30+
fulfill(value)
31+
}
32+
}
33+
}
34+
35+
//--------------------------------------------------
36+
// Example 1: Async
37+
//--------------------------------------------------
38+
39+
let task1a = asyncTask("Hello")
40+
let task1b = task1a
41+
.success { value -> String in
42+
return "\(value) World"
43+
}
44+
45+
// NOTE: these values should be all nil because task is async
46+
task1a.value
47+
task1a.errorInfo
48+
task1b.value
49+
task1b.errorInfo
50+
51+
//--------------------------------------------------
52+
// Example 2: Async chaining
53+
//--------------------------------------------------
54+
55+
let task2a = asyncTask("Hello")
56+
let task2b = task2a
57+
.success { value -> MyTask in
58+
return asyncTask("\(value) Cruel") // next async
59+
}
60+
.success { value -> String in
61+
return "\(value) World"
62+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//
2+
// This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to 02-Async.playground.
3+
//
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Cocoa
4+
import XCPlayground
5+
6+
//
7+
// NOTE: custom framework needs to be built first (and restart Xcode if needed)
8+
//
9+
// Importing Custom Frameworks Into a Playground
10+
// https://developer.apple.com/library/prerelease/ios/recipes/Playground_Help/Chapters/ImportingaFrameworkIntoaPlayground.html
11+
//
12+
import SwiftTask
13+
import Alamofire
14+
15+
typealias Progress = (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
16+
typealias Value = String
17+
typealias Error = NSError
18+
19+
typealias AlamofireTask = Task<Progress, Value, Error>
20+
21+
let myError = NSError(domain: "MyErrorDomain", code: 0, userInfo: nil)
22+
23+
// for async test
24+
XCPSetExecutionShouldContinueIndefinitely()
25+
26+
//--------------------------------------------------
27+
// Example 1: Alamofire progress
28+
//--------------------------------------------------
29+
30+
// define task
31+
let task = AlamofireTask { progress, fulfill, reject, configure in
32+
33+
let request = Alamofire.download(.GET, "http://httpbin.org/stream/100", Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask))
34+
35+
request
36+
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
37+
progress((bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) as Progress)
38+
}
39+
.response { (request, response, data, error) in
40+
41+
// print
42+
data
43+
error?.localizedDescription
44+
45+
if let error = error {
46+
reject(error)
47+
return
48+
}
49+
50+
fulfill("OK")
51+
52+
}
53+
}
54+
55+
task
56+
.progress { oldProgress, newProgress in
57+
// print
58+
newProgress.bytesWritten
59+
newProgress.totalBytesWritten
60+
}
61+
.then { value, errorInfo -> String in
62+
if let errorInfo = errorInfo {
63+
return "ERROR: \(errorInfo.error!.domain)"
64+
}
65+
66+
return "\(value!) World"
67+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//
2+
// This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to 03-Alamofire.playground.
3+
//
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "#CharacterRangeLen=19&amp;CharacterRangeLoc=1785&amp;EndingColumnNumber=23&amp;EndingLineNumber=58&amp;StartingColumnNumber=9&amp;StartingLineNumber=58&amp;Timestamp=450426052.187599"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
<LoggerValueHistoryTimelineItem
11+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1804&amp;EndingColumnNumber=42&amp;EndingLineNumber=58&amp;StartingColumnNumber=9&amp;StartingLineNumber=58&amp;Timestamp=450426052.18781"
12+
selectedRepresentationIndex = "1"
13+
shouldTrackSuperviewWidth = "NO">
14+
</LoggerValueHistoryTimelineItem>
15+
<LoggerValueHistoryTimelineItem
16+
documentLocation = "#CharacterRangeLen=29&amp;CharacterRangeLoc=1776&amp;EndingColumnNumber=38&amp;EndingLineNumber=58&amp;StartingColumnNumber=9&amp;StartingLineNumber=58&amp;Timestamp=450426052.187982"
17+
selectedRepresentationIndex = "0"
18+
shouldTrackSuperviewWidth = "NO">
19+
</LoggerValueHistoryTimelineItem>
20+
<LoggerValueHistoryTimelineItem
21+
documentLocation = "#CharacterRangeLen=24&amp;CharacterRangeLoc=1743&amp;EndingColumnNumber=33&amp;EndingLineNumber=57&amp;StartingColumnNumber=9&amp;StartingLineNumber=57&amp;Timestamp=450426052.188093"
22+
selectedRepresentationIndex = "0"
23+
shouldTrackSuperviewWidth = "NO">
24+
</LoggerValueHistoryTimelineItem>
25+
</TimelineItems>
26+
</Timeline>

SwiftTask.xcworkspace/contents.xcworkspacedata

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)