Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ReplaySubject in Playgrounds #39

Merged
merged 1 commit into from
Jun 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ The next example shows how
*/

example("combineLatest 1st") {
let intOb1 = Subject<String>()
let intOb2 = Subject<Int>()
let intOb1 = PublishSubject<String>()
let intOb2 = PublishSubject<Int>()

combineLatest(intOb1, intOb2) {
"\($0) \($1)"
Expand Down Expand Up @@ -108,8 +108,8 @@ The Observable returned by `zip` emits an item only when all of the imputs Obser
*/

example("zip 1st") {
let intOb1 = Subject<String>()
let intOb2 = Subject<Int>()
let intOb1 = PublishSubject<String>()
let intOb2 = PublishSubject<Int>()

zip(intOb1, intOb2) {
"\($0) \($1)"
Expand Down Expand Up @@ -184,8 +184,8 @@ Combine multiple Observables, of the same type, into one by merging their emissi
*/

example("merge 1st") {
let subject1 = Subject<Int>()
let subject2 = Subject<Int>()
let subject1 = PublishSubject<Int>()
let subject2 = PublishSubject<Int>()

merge(returnElements(subject1, subject2))
>- subscribeNext { int in
Expand All @@ -203,8 +203,8 @@ example("merge 1st") {


example("merge 2nd") {
let subject1 = Subject<Int>()
let subject2 = Subject<Int>()
let subject1 = PublishSubject<Int>()
let subject2 = PublishSubject<Int>()

returnElements(subject1, subject2)
>- merge(maxConcurrent: 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Discard any items emitted by an Observable after a second Observable emits an it

example("takeUntil") {

let observable1 = Subject<Int>()
let observable2 = Subject<Int>()
let observable1 = PublishSubject<Int>()
let observable2 = PublishSubject<Int>()

observable1
>- takeUntil(observable2)
Expand Down Expand Up @@ -47,7 +47,7 @@ Mirror items emitted by an Observable until a specified condition becomes false

example("takeWhile") {

let observable1 = Subject<Int>()
let observable1 = PublishSubject<Int>()

observable1
>- takeWhile { int in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,210 @@
import Cocoa
import RxSwift

import XCPlayground

/*:
## Connectable Observable Operators

A Connectable Observable resembles an ordinary Observable, except that it does not begin emitting items when it is subscribed to, but only when its connect() method is called. In this way you can wait for all intended Subscribers to subscribe to the Observable before the Observable begins emitting items.

Specialty Observables that have more precisely-controlled subscription dynamics.
*/


func sampleWithoutConnectableOperators() {

let int1 = interval(1, MainScheduler.sharedInstance)

int1
>- subscribeNext {
println("first subscription \($0)")
}

delay(5) {
int1
>- subscribeNext {
println("second subscription \($0)")
}
}

}

sampleWithoutConnectableOperators()

/*:
### `multicast`
[More info in reactive.io website]( http://reactivex.io/documentation/operators/publish.html )
*/


func sampleWithMulticast() {

let subject1 = PublishSubject<Int64>()

subject1
>- subscribeNext {
println("Subject \($0)")
}

let int1 = interval(1, MainScheduler.sharedInstance)
>- multicast(subject1)

int1
>- subscribeNext {
println("first subscription \($0)")
}

delay(2) {
int1.connect()
}

delay(4) {
int1
>- subscribeNext {
println("second subscription \($0)")
println("---")
}
}

delay(6) {
int1
>- subscribeNext {
println("thirth subscription \($0)")
}
}

}

//sampleWithMulticast()



/*:
### `replay`
Ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items.

publish = multicast + replay subject


[More info in reactive.io website]( http://reactivex.io/documentation/operators/replay.html )
*/


func sampleWithReplayBuffer0() {

let int1 = interval(1, MainScheduler.sharedInstance)
>- replay(0)

int1
>- subscribeNext {
println("first subscription \($0)")
}

delay(2) {
int1.connect()
}

delay(4) {
int1
>- subscribeNext {
println("second subscription \($0)")
println("---")
}
}

delay(6) {
int1
>- subscribeNext {
println("thirth subscription \($0)")
}
}

}

//sampleWithReplayBuffer0()


func sampleWithReplayBuffer2() {

println("--- sampleWithReplayBuffer2 ---\n")

let int1 = interval(1, MainScheduler.sharedInstance)
>- replay(2)

int1
>- subscribeNext {
println("first subscription \($0)")
}

delay(2) {
int1.connect()
}

delay(4) {
int1
>- subscribeNext {
println("second subscription \($0)")
println("---")
}
}

delay(6) {
int1
>- subscribeNext {
println("thirth subscription \($0)")
}
}

}

//sampleWithReplayBuffer2()



/*:
### `publish`
Convert an ordinary Observable into a connectable Observable.

publish = multicast + publish subject

so publish is basically replay(0)

[More info in reactive.io website]( http://reactivex.io/documentation/operators/publish.html )
*/


func sampleWithPublish() {

let int1 = interval(1, MainScheduler.sharedInstance)
>- publish

int1
>- subscribeNext {
println("first subscription \($0)")
}

delay(2) {
int1.connect()
}

delay(4) {
int1
>- subscribeNext {
println("second subscription \($0)")
println("---")
}
}

delay(6) {
int1
>- subscribeNext {
println("thirth subscription \($0)")
}
}

}

//sampleWithPublish()


/*:
### `refCount`
Expand All @@ -33,17 +214,10 @@ Make a Connectable Observable behave like an ordinary Observable.



/*:
### `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`
*/


XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ public func example(description: String, action: () -> ()) {
println("\n--- \(description) example ---")
action()
}

import Foundation

public func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
<playground version='5.0' target-platform='osx' auto-termination-delay='11'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
<playground version='5.0' target-platform='osx' display-mode='rendered'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Recover from an onError notification by continuing the sequence without error

example("catch 1st") {

let observable1 = Subject<Int>()
let observable2 = Subject<Int>()
let observable1 = PublishSubject<Int>()
let observable2 = PublishSubject<Int>()

observable1
>- catch { error in
Expand Down Expand Up @@ -53,7 +53,7 @@ example("catch 1st") {

example("catch 2nd") {

let observable1 = Subject<Int>()
let observable1 = PublishSubject<Int>()

observable1
>- catch(100)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx' display-mode='rendered'/>
<playground version='5.0' target-platform='osx' display-mode='rendered'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Loading