Merge pull request #37 from carlosypunto/more-playgrounds
More playgrounds
This commit is contained in:
commit
6628516fa8
|
|
@ -18,6 +18,8 @@ DerivedData
|
|||
*.ipa
|
||||
*.xcuserstate
|
||||
|
||||
timeline.xctimeline
|
||||
|
||||
# CocoaPods
|
||||
#
|
||||
# We recommend against adding the Pods directory to your .gitignore. However
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
/*:
|
||||
## Combination operators
|
||||
|
||||
Operators that work with multiple source Observables to create a single Observable.
|
||||
|
||||
|
||||
|
||||
### `startWith`
|
||||
|
||||
Return an observeble which emits a specified item before emitting the items from the source Observable.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/startwith.html )
|
||||
*/
|
||||
|
||||
example("startWith") {
|
||||
|
||||
let aggregateSubscriber = from([4, 5, 6, 7, 8, 9])
|
||||
>- startWith(3)
|
||||
>- startWith(2)
|
||||
>- startWith(1)
|
||||
>- startWith(0)
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*:
|
||||
### `combineLatest`
|
||||
|
||||
Takes several source Obserbables and a closure as parameters, returns an Observable which emits the latest items of each source Obsevable, procesed through the closure.
|
||||
Once each source Observables have each emitted an item, `combineLatest` emits an item every time either source Observable emits an item.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/combinelatest.html )
|
||||
|
||||
The next example shows how
|
||||
*/
|
||||
|
||||
example("combineLatest 1st") {
|
||||
let intOb1 = Subject<String>()
|
||||
let intOb2 = Subject<Int>()
|
||||
|
||||
combineLatest(intOb1, intOb2) {
|
||||
"\($0) \($1)"
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
|
||||
println("send A to first channel")
|
||||
sendNext(intOb1, "A")
|
||||
println("note that nothing outputs")
|
||||
|
||||
println("\nsend 1 to second channel")
|
||||
sendNext(intOb2, 1)
|
||||
println("now that there is something in both channels, there is output")
|
||||
|
||||
println("\nsend B to first channel")
|
||||
sendNext(intOb1, "B")
|
||||
println("now that both channels are full, whenever either channel emits a value, the combined channel also emits a value")
|
||||
|
||||
println("\nsend 2 to second channel")
|
||||
sendNext(intOb2, 2)
|
||||
println("note that the combined channel emits a value whenever either sub-channel emits a value, even if the value is the same")
|
||||
|
||||
|
||||
}
|
||||
|
||||
//: This example show once in each channel there are output for each new channel output the resulting observable also produces an output
|
||||
|
||||
example("combineLatest 2nd") {
|
||||
let intOb1 = just(2)
|
||||
let intOb2 = from([0, 1, 2, 3, 4])
|
||||
|
||||
combineLatest(intOb1, intOb2) {
|
||||
$0 * $1
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
There are a serie of functions `combineLatest`, they take from two to ten sources Obserbables and the closure
|
||||
The next sample shows combineLatest called with three sorce Observables
|
||||
*/
|
||||
|
||||
example("combineLatest 3th") {
|
||||
let intOb1 = just(2)
|
||||
let intOb2 = from([0, 1, 2, 3])
|
||||
let intOb3 = from([0, 1, 2, 3, 4])
|
||||
|
||||
combineLatest(intOb1, intOb2, intOb3) {
|
||||
($0 + $1) * $2
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `zip`
|
||||
|
||||
Takes several source Observables and a closure as parameters, returns an Observable which emit the items of the second Obsevable procesed, through the closure, with the last item of first Observable
|
||||
The Observable returned by `zip` emits an item only when all of the imputs Observables have emited an item
|
||||
[More info in reactive.io website](http://reactivex.io/documentation/operators/zip.html)
|
||||
*/
|
||||
|
||||
example("zip 1st") {
|
||||
let intOb1 = Subject<String>()
|
||||
let intOb2 = Subject<Int>()
|
||||
|
||||
zip(intOb1, intOb2) {
|
||||
"\($0) \($1)"
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
|
||||
println("send A to first channel")
|
||||
sendNext(intOb1, "A")
|
||||
println("note that nothing outputs")
|
||||
|
||||
println("\nsend 1 to second channel")
|
||||
sendNext(intOb2, 1)
|
||||
println("now that both source channels have output, there is output")
|
||||
|
||||
println("\nsend B to first channel")
|
||||
sendNext(intOb1, "B")
|
||||
println("note that nothing outputs, since channel 1 has two outputs but channel 2 only has one")
|
||||
|
||||
println("\nsend C to first channel")
|
||||
sendNext(intOb1, "C")
|
||||
println("note that nothing outputs, it is the same as in the previous step, since channel 1 has three outputs but channel 2 only has one")
|
||||
|
||||
println("\nsend 2 to second channel")
|
||||
sendNext(intOb2, 2)
|
||||
println("note that the combined channel emits a value with the second output of each channel")
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//: This example show once in each channel there are output for each new channel output the resulting observable also produces an output
|
||||
|
||||
example("zip 2nd") {
|
||||
let intOb1 = just(2)
|
||||
let intOb2 = from([0, 1, 2, 3, 4])
|
||||
|
||||
zip(intOb1, intOb2) {
|
||||
$0 * $1
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
There are a serie of functions `zip`, they take from two to ten sources Obserbables and the closure
|
||||
The next sample shows zip called with three sorce Observables
|
||||
*/
|
||||
|
||||
example("zip 3th") {
|
||||
let intOb1 = from([0, 1])
|
||||
let intOb2 = from([0, 1, 2, 3])
|
||||
let intOb3 = from([0, 1, 2, 3, 4])
|
||||
|
||||
zip(intOb1, intOb2, intOb3) {
|
||||
($0 + $1) * $2
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `merge`
|
||||
|
||||
Combine multiple Observables, of the same type, into one by merging their emissions
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/merge.html )
|
||||
*/
|
||||
|
||||
example("merge 1st") {
|
||||
let subject1 = Subject<Int>()
|
||||
let subject2 = Subject<Int>()
|
||||
|
||||
merge(returnElements(subject1, subject2))
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
sendNext(subject1, 20)
|
||||
sendNext(subject1, 40)
|
||||
sendNext(subject1, 60)
|
||||
sendNext(subject2, 1)
|
||||
sendNext(subject1, 80)
|
||||
sendNext(subject1, 100)
|
||||
sendNext(subject2, 1)
|
||||
}
|
||||
|
||||
|
||||
example("merge 2nd") {
|
||||
let subject1 = Subject<Int>()
|
||||
let subject2 = Subject<Int>()
|
||||
|
||||
returnElements(subject1, subject2)
|
||||
>- merge(maxConcurrent: 2)
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
sendNext(subject1, 20)
|
||||
sendNext(subject1, 40)
|
||||
sendNext(subject1, 60)
|
||||
sendNext(subject2, 1)
|
||||
sendNext(subject1, 80)
|
||||
sendNext(subject1, 100)
|
||||
sendNext(subject2, 1)
|
||||
}
|
||||
|
||||
|
||||
/*:
|
||||
### `switchLatest`
|
||||
|
||||
Convert an Observable that emits Observables into a single Observable that emits the items emitted by the most-recently-emitted of those Observables.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/switch.html )
|
||||
*/
|
||||
|
||||
|
||||
example("switchLatest") {
|
||||
let var1 = Variable(0)
|
||||
let var2 = Variable(200)
|
||||
|
||||
// var3 is like an Observable<Observable<Int>>
|
||||
let var3 = Variable(var1 as Observable<Int>)
|
||||
|
||||
let d = var3
|
||||
>- switchLatest
|
||||
>- subscribeNext { (e: Int) -> Void in
|
||||
println("\(e)")
|
||||
}
|
||||
|
||||
var1.next(1)
|
||||
var1.next(2)
|
||||
var1.next(3)
|
||||
var1.next(4)
|
||||
|
||||
var3.next(var2)
|
||||
|
||||
var2.next(201)
|
||||
|
||||
println("Note which no listen to var1")
|
||||
var1.next(5)
|
||||
var1.next(6)
|
||||
var1.next(7)
|
||||
sendCompleted(var1)
|
||||
|
||||
var2.next(202)
|
||||
var2.next(203)
|
||||
var2.next(204)
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Conditional and Boolean Operators
|
||||
|
||||
Operators that evaluate one or more Observables or items emitted by Observables.
|
||||
|
||||
|
||||
|
||||
### `takeUntil`
|
||||
Discard any items emitted by an Observable after a second Observable emits an item or terminates.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/takeuntil.html )
|
||||
*/
|
||||
|
||||
example("takeUntil") {
|
||||
|
||||
let observable1 = Subject<Int>()
|
||||
let observable2 = Subject<Int>()
|
||||
|
||||
observable1
|
||||
>- takeUntil(observable2)
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
sendNext(observable1, 1)
|
||||
sendNext(observable1, 2)
|
||||
sendNext(observable1, 3)
|
||||
sendNext(observable1, 4)
|
||||
|
||||
sendNext(observable2, 1)
|
||||
|
||||
sendNext(observable1, 5)
|
||||
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
|
||||
### `takeWhile`
|
||||
|
||||
Mirror items emitted by an Observable until a specified condition becomes false
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/takewhile.html )
|
||||
*/
|
||||
|
||||
example("takeWhile") {
|
||||
|
||||
let observable1 = Subject<Int>()
|
||||
|
||||
observable1
|
||||
>- takeWhile { int in
|
||||
int < 4
|
||||
}
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
sendNext(observable1, 1)
|
||||
sendNext(observable1, 2)
|
||||
sendNext(observable1, 3)
|
||||
sendNext(observable1, 4)
|
||||
sendNext(observable1, 5)
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Connectable Observable Operators
|
||||
|
||||
Specialty Observables that have more precisely-controlled subscription dynamics.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `multicast`
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/publish.html )
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `publish`
|
||||
Convert an ordinary Observable into a connectable Observable.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/publish.html )
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `refCount`
|
||||
Make a Connectable Observable behave like an ordinary Observable.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/refcount.html )
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `replay`
|
||||
Ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/replay.html )
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `Variable` / `sharedWithCachedLastResult`
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
|
||||
|
|
@ -0,0 +1,213 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Creating observables
|
||||
|
||||
Operators that originate new Observables.
|
||||
|
||||
|
||||
### `empty`
|
||||
|
||||
Creates an observable that contains no objects. The only message it sends is the `.Completed` message.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/empty-never-throw.html )
|
||||
*/
|
||||
example("Empty observable") {
|
||||
let emptyObservable: Observable<Int> = empty()
|
||||
|
||||
let emptySubscriber = emptyObservable >- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
As you can see, no values are ever sent to the subscriber of an empty observable. It just completes and is done.
|
||||
|
||||
|
||||
|
||||
### `never`
|
||||
|
||||
Creates an observable that contains no objects and never completes or errors out.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/empty-never-throw.html )
|
||||
*/
|
||||
example("Never observable") {
|
||||
let neverObservable: Observable<String> = never()
|
||||
|
||||
let neverSubscriber = neverObservable
|
||||
>- subscribe { _ in
|
||||
println("This block is never called.")
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
### `failWith`
|
||||
|
||||
Creates an observable that contains no objects and send only a error out.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/empty-never-throw.html )
|
||||
*/
|
||||
|
||||
example("failWith") {
|
||||
let error = NSError(domain: "Test", code: -1, userInfo: nil)
|
||||
|
||||
let errorObservable: Observable<Int> = failWith(error)
|
||||
|
||||
let errorSubscriber = errorObservable
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `returnElement` / `just`
|
||||
|
||||
These two functions behave identically. They send two messages to subscribers. The first message is the value and the second message is `.Complete`.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/just.html )
|
||||
*/
|
||||
|
||||
example("returnElement/just") {
|
||||
let oneObservable = just(32)
|
||||
|
||||
let oneObservableSubscriber = oneObservable
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
Here we see that the `.Next` event is sent just once, then the `.Completed` event is sent.
|
||||
|
||||
|
||||
|
||||
### `returnElements`
|
||||
|
||||
Now we are getting to some more interesting ways to create an Observable. This function creates an observable that produces a number of values before completing.
|
||||
*/
|
||||
|
||||
example("returnElements") {
|
||||
let multipleObservable/* : Observable<Int> */ = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
||||
let multipleObservableSubscriber = multipleObservable
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
With the above, you will see that the `.Next` event was sent ten times, once for each element. Then `.Complete` was sent.
|
||||
|
||||
|
||||
|
||||
### `from`
|
||||
We can also create an observable from any SequenceType, such as an array
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/from.html )
|
||||
*/
|
||||
|
||||
example("from") {
|
||||
let fromArrayObservable = from([1, 2, 3, 4, 5])
|
||||
|
||||
let fromArrayObservableSubscriber = fromArrayObservable
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `create`
|
||||
|
||||
Create an Observable from scratch by means of a function
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/create.html )
|
||||
*/
|
||||
|
||||
example("create") {
|
||||
|
||||
println("creating")
|
||||
let observable: Observable<Int> = create { observer in
|
||||
println("emmiting")
|
||||
sendNext(observer, 0)
|
||||
sendNext(observer, 1)
|
||||
sendNext(observer, 2)
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
observable
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
|
||||
observable
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `defer`
|
||||
|
||||
Create an Observable from a function which create an observable. But do not create the Observable until the observer subscribes, and create a fresh Observable for each observer
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/defer.html )
|
||||
*/
|
||||
|
||||
example("defer") {
|
||||
|
||||
let defered: Observable<Int> = defer {
|
||||
println("creating")
|
||||
return create { observer in
|
||||
println("emmiting")
|
||||
sendNext(observer, 0)
|
||||
sendNext(observer, 1)
|
||||
sendNext(observer, 2)
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
}
|
||||
|
||||
defered
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
|
||||
defered
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Error Handling Operators
|
||||
|
||||
Operators that help to recover from error notifications from an Observable.
|
||||
*/
|
||||
|
||||
/*:
|
||||
### `catch`
|
||||
|
||||
Recover from an onError notification by continuing the sequence without error
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/catch.html )
|
||||
*/
|
||||
|
||||
example("catch 1st") {
|
||||
|
||||
let observable1 = Subject<Int>()
|
||||
let observable2 = Subject<Int>()
|
||||
|
||||
observable1
|
||||
>- catch { error in
|
||||
return observable2
|
||||
}
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sendNext(observable1, 1)
|
||||
sendNext(observable1, 2)
|
||||
sendNext(observable1, 3)
|
||||
sendNext(observable1, 4)
|
||||
sendError(observable1, NSError(domain: "Test", code: 0, userInfo: nil))
|
||||
|
||||
sendNext(observable2, 100)
|
||||
sendNext(observable2, 200)
|
||||
sendNext(observable2, 300)
|
||||
sendNext(observable2, 400)
|
||||
sendCompleted(observable2)
|
||||
|
||||
|
||||
}
|
||||
|
||||
example("catch 2nd") {
|
||||
|
||||
let observable1 = Subject<Int>()
|
||||
|
||||
observable1
|
||||
>- catch(100)
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sendNext(observable1, 1)
|
||||
sendNext(observable1, 2)
|
||||
sendNext(observable1, 3)
|
||||
sendNext(observable1, 4)
|
||||
sendError(observable1, NSError(domain: "Test", code: 0, userInfo: nil))
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*:
|
||||
### `retry`
|
||||
|
||||
If a source Observable emits an error, resubscribe to it in the hopes that it will complete without error
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/retry.html )
|
||||
*/
|
||||
|
||||
example("retry") {
|
||||
|
||||
var count = 1 // bad practice, only for example purposes
|
||||
let observable: Observable<Int> = create { observer in
|
||||
let error = NSError(domain: "Test", code: 0, userInfo: nil)
|
||||
sendNext(observer, 0)
|
||||
sendNext(observer, 1)
|
||||
sendNext(observer, 2)
|
||||
if count < 2 {
|
||||
sendError(observer, error)
|
||||
count++
|
||||
}
|
||||
sendNext(observer, 3)
|
||||
sendNext(observer, 4)
|
||||
sendNext(observer, 5)
|
||||
sendCompleted(observer)
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
observable
|
||||
>- retry
|
||||
>- subscribe { event in
|
||||
switch event {
|
||||
case .Next(let box):
|
||||
println("\(box.value)")
|
||||
case .Completed:
|
||||
println("completed")
|
||||
case .Error(let error):
|
||||
println("\(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Filtering Observables
|
||||
|
||||
Operators that selectively emit items from a source Observable.
|
||||
|
||||
|
||||
|
||||
### `where` / `filter`
|
||||
|
||||
emit only those items from an Observable that pass a predicate test
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/filter.html )
|
||||
*/
|
||||
|
||||
example("filter") {
|
||||
let onlyEvensSubscriber = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
>- filter {
|
||||
$0 % 2 == 0
|
||||
}
|
||||
>- subscribeNext { value in
|
||||
println("\(value)")
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `distinctUntilChanged`
|
||||
|
||||
suppress duplicate items emitted by an Observable
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/distinct.html )
|
||||
*/
|
||||
|
||||
example("distinctUntilChanged") {
|
||||
let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 1, 1, 4)
|
||||
>- distinctUntilChanged
|
||||
>- subscribeNext { value in
|
||||
println("\(value)")
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `take`
|
||||
|
||||
Emit only the first n items emitted by an Observable
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/take.html )
|
||||
*/
|
||||
|
||||
example("take") {
|
||||
let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 4, 5, 6)
|
||||
>- take(3)
|
||||
>- subscribeNext { value in
|
||||
println("\(value)")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Mathematical and Aggregate Operators
|
||||
|
||||
Operators that operate on the entire sequence of items emitted by an Observable
|
||||
|
||||
|
||||
|
||||
### `concat`
|
||||
|
||||
Emit the emissions from two or more Observables without interleaving them.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/concat.html )
|
||||
*/
|
||||
|
||||
example("concat") {
|
||||
let var1 = Variable(0)
|
||||
let var2 = Variable(200)
|
||||
|
||||
// var3 is like an Observable<Observable<Int>>
|
||||
let var3 = Variable(var1 as Observable<Int>)
|
||||
|
||||
let d = var3
|
||||
>- concat
|
||||
>- subscribeNext { (e: Int) -> Void in
|
||||
println("\(e)")
|
||||
}
|
||||
|
||||
var1.next(1)
|
||||
var1.next(2)
|
||||
var1.next(3)
|
||||
var1.next(4)
|
||||
|
||||
var3.next(var2)
|
||||
|
||||
var2.next(201)
|
||||
|
||||
var1.next(5)
|
||||
var1.next(6)
|
||||
var1.next(7)
|
||||
sendCompleted(var1)
|
||||
|
||||
var2.next(202)
|
||||
var2.next(203)
|
||||
var2.next(204)
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
|
||||
### `reduce` / `aggregate`
|
||||
|
||||
Apply a function to each item emitted by an Observable, sequentially, and emit the final value.
|
||||
This function will perform a function on each element in the sequence until it is completed, then send a message with the aggregate value. It works much like the Swift `reduce` function works on sequences.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/reduce.html )
|
||||
|
||||
*/
|
||||
|
||||
example("aggregate") {
|
||||
let aggregateSubscriber = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
>- aggregate(0, +)
|
||||
>- subscribeNext { value in
|
||||
println("\(value)")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Transforming Observables
|
||||
|
||||
Operators that transform items that are emitted by an Observable.
|
||||
|
||||
|
||||
### `map` / `select`
|
||||
|
||||
Transform the items emitted by an Observable by applying a function to each item
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/map.html )
|
||||
*/
|
||||
|
||||
example("map") {
|
||||
|
||||
let observable1: Observable<Character> = create { observer in
|
||||
sendNext(observer, Character("A"))
|
||||
sendNext(observer, Character("B"))
|
||||
sendNext(observer, Character("C"))
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
observable1
|
||||
>- map { char in
|
||||
char.hashValue
|
||||
}
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `flatMap`
|
||||
|
||||
Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/flatmap.html )
|
||||
*/
|
||||
|
||||
example("flatMap") {
|
||||
|
||||
let observable1: Observable<Int> = create { observer in
|
||||
sendNext(observer, 1)
|
||||
sendNext(observer, 2)
|
||||
sendNext(observer, 3)
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
let observable2: Observable<String> = create { observer in
|
||||
sendNext(observer, "A")
|
||||
sendNext(observer, "B")
|
||||
sendNext(observer, "C")
|
||||
sendNext(observer, "D")
|
||||
sendNext(observer, "F")
|
||||
sendNext(observer, "--")
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
observable1
|
||||
>- flatMap { int in
|
||||
observable2
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
||||
/*:
|
||||
### `scan`
|
||||
|
||||
Apply a function to each item emitted by an Observable, sequentially, and emit each successive value
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/scan.html )
|
||||
*/
|
||||
|
||||
example("scan") {
|
||||
|
||||
let observable: Observable<Int> = create { observer in
|
||||
sendNext(observer, 0)
|
||||
sendNext(observer, 1)
|
||||
sendNext(observer, 2)
|
||||
sendNext(observer, 3)
|
||||
sendNext(observer, 4)
|
||||
sendNext(observer, 5)
|
||||
|
||||
return AnonymousDisposable {}
|
||||
}
|
||||
|
||||
observable
|
||||
>- scan(0) { acum, elem in
|
||||
acum + elem
|
||||
}
|
||||
>- subscribeNext {
|
||||
println($0)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
import Cocoa
|
||||
import RxSwift
|
||||
|
||||
|
||||
/*:
|
||||
## Observable Utility Operators
|
||||
|
||||
A toolbox of useful Operators for working with Observables.
|
||||
|
||||
|
||||
|
||||
### `subscribe`
|
||||
|
||||
Create an Disposable which listen events from source Observable, the given closure take the Even and is responsible for the actions to perform when the it is produced.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/subscribe.html )
|
||||
*/
|
||||
|
||||
example("subscribe") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
intOb1
|
||||
>- subscribe { event in
|
||||
println(event)
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
sendCompleted(intOb1)
|
||||
}
|
||||
|
||||
/*:
|
||||
There are several variants of the `subscribe` operator. They works over one posible event type:
|
||||
|
||||
|
||||
|
||||
### `subscribeNext`
|
||||
|
||||
Create an Disposable which listen only Next event from source Observable, the given closure take the Even's value and is responsible for the actions to perform only when the Next even is produced.
|
||||
*/
|
||||
|
||||
example("subscribeNext") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
intOb1
|
||||
>- subscribeNext { int in
|
||||
println(int)
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
sendCompleted(intOb1)
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
|
||||
### `subscribeCompleted`
|
||||
|
||||
Create an Disposable which listen only Completed event from source Observable, the given closure take the Even's value and is responsible for the actions to perform only when the Completed even is produced.
|
||||
*/
|
||||
|
||||
example("subscribeCompleted") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
intOb1
|
||||
>- subscribeCompleted {
|
||||
println("It's completed")
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
sendCompleted(intOb1)
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
### `subscribeError
|
||||
|
||||
Create an Disposable which listen only Error event from source Observable, the given closure take the Even's value and is responsible for the actions to perform only when the Error even is produced
|
||||
*/
|
||||
|
||||
example("subscribeError") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
intOb1
|
||||
>- subscribeError { error in
|
||||
println(error)
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
sendError(intOb1, NSError(domain: "Examples", code: -1, userInfo: nil))
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
|
||||
### `do`
|
||||
|
||||
Returns the same source Observable but the given closure responsible for the actions to perform when the even is produced. The gived closure obtain the event produced by the source observable
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html )
|
||||
*/
|
||||
|
||||
example("do") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
let intOb2 = intOb1
|
||||
>- `do` { event in
|
||||
println("first \(event)")
|
||||
}
|
||||
|
||||
intOb2
|
||||
>- subscribeNext { int in
|
||||
println("second \(int)")
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
|
||||
}
|
||||
|
||||
/*:
|
||||
|
||||
### `doOnNext`
|
||||
|
||||
It is a variant of the `do` operator. Returns the same source Observable but the given closure responsible for the actions to perform when the Next even is produced. The gived closure obtain the value of the Next event produced by the source observable.
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html )
|
||||
*/
|
||||
|
||||
example("doOnNext") {
|
||||
let intOb1 = Subject<Int>()
|
||||
|
||||
let intOb2 = intOb1
|
||||
>- doOnNext { int in
|
||||
println("first \(int)")
|
||||
}
|
||||
|
||||
intOb2
|
||||
>- subscribeNext { int in
|
||||
println("second \(int)")
|
||||
}
|
||||
|
||||
sendNext(intOb1, 1)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
public func example(description: String, action: () -> ()) {
|
||||
println("\n--- \(description) example ---")
|
||||
action()
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
|
||||
|
|
@ -88,6 +88,7 @@
|
|||
C811086F1AF5114D001C13E4 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0700;
|
||||
LastUpgradeCheck = 0630;
|
||||
ORGANIZATIONNAME = "Krunoslav Zaher";
|
||||
TargetAttributes = {
|
||||
|
|
@ -236,6 +237,7 @@
|
|||
C81108801AF5114D001C13E4 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ Now something a little more interesting:
|
|||
* bind results to label (resultLabel.rx_subscribeTextTo)
|
||||
|
||||
```swift
|
||||
let subscription/*: Disposable */ = primeTextField.rx_text() // type is Observable<String>
|
||||
let subscription/*: Disposable */ = primeTextField.rx_text // type is Observable<String>
|
||||
>- map { WolframAlphaIsPrime($0.toInt() ?? 0) } // type is Observable<Observable<Prime>>
|
||||
>- concat // type is Observable<Prime>
|
||||
>- map { "number \($0.n) is prime? \($0.isPrime)" } // type is Observable<String>
|
||||
|
|
@ -154,7 +154,7 @@ Let's give it a shot.
|
|||
```swift
|
||||
// bind UI control values directly
|
||||
// use username from `usernameOutlet` as username values source
|
||||
self.usernameOutlet.rx_text() >- map { username in
|
||||
self.usernameOutlet.rx_text >- map { username in
|
||||
|
||||
// synchronous validation, nothing special here
|
||||
if count(username) == 0 {
|
||||
|
|
|
|||
|
|
@ -7,9 +7,44 @@
|
|||
<FileRef
|
||||
location = "group:README.md">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:OSXPlayground.playground">
|
||||
</FileRef>
|
||||
<Group
|
||||
location = "group:Playgrounds"
|
||||
name = "Playgrounds">
|
||||
<FileRef
|
||||
location = "group:OSXPlayground.playground">
|
||||
</FileRef>
|
||||
<Group
|
||||
location = "group:ObservablesOperators"
|
||||
name = "ObservablesOperators">
|
||||
<FileRef
|
||||
location = "group:Observables+Creating.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Transforming.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Filtering.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Combining.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Error Handling.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Utility.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Conditional and Boolean.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Mathematical and Aggregate.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Observables+Connectable.playground">
|
||||
</FileRef>
|
||||
</Group>
|
||||
</Group>
|
||||
<FileRef
|
||||
location = "group:RxSwift/RxSwift.xcodeproj">
|
||||
</FileRef>
|
||||
|
|
|
|||
|
|
@ -253,6 +253,7 @@
|
|||
C81553D51A98AB4A00C63152 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0700;
|
||||
LastUpgradeCheck = 0610;
|
||||
ORGANIZATIONNAME = "Krunoslav Zaher";
|
||||
TargetAttributes = {
|
||||
|
|
|
|||
|
|
@ -323,6 +323,7 @@
|
|||
C81108161AF50DDA001C13E4 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0700;
|
||||
LastUpgradeCheck = 0630;
|
||||
TargetAttributes = {
|
||||
C811081F1AF50E11001C13E4 = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue