Skip to content

Commit ec281c7

Browse files
committed
Merge pull request ReactiveX#76 from carlosypunto/subjects-playground
Subjects playground
2 parents f79ed3c + 5fa8d9e commit ec281c7

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Cocoa
2+
import RxSwift
3+
4+
/*:
5+
6+
To use playgrounds please open Rx.xcworkspace, build RxSwift-OSX scheme and then open playgrounds in Rx.xcworkspace tree view.
7+
8+
*/
9+
10+
func writeSequenceToConsole(name: String, sequence: Observable<String>) {
11+
sequence
12+
>- subscribeNext {
13+
println("Subscription: \(name), value: \($0)")
14+
}
15+
}
16+
17+
18+
/*:
19+
20+
## PublishSubject
21+
22+
PublishSubject can begin emitting items immediately upon creation, but there is a risk that one or more items may be lost between the time the Subject is created and the observer subscribes to it.
23+
24+
*/
25+
example("PublishSubject") {
26+
let subject = PublishSubject<String>()
27+
writeSequenceToConsole("1", subject)
28+
sendNext(subject, "a")
29+
sendNext(subject, "b")
30+
writeSequenceToConsole("2", subject)
31+
sendNext(subject, "c")
32+
sendNext(subject, "d")
33+
}
34+
35+
36+
/*:
37+
38+
## ReplaySubject
39+
40+
ReplaySubject emits to any observer all of the items, in the buffer, that were emitted by the source
41+
42+
*/
43+
example("ReplaySubject") {
44+
let subject = ReplaySubject<String>(bufferSize: 1)
45+
writeSequenceToConsole("1", subject)
46+
sendNext(subject, "a")
47+
sendNext(subject, "b")
48+
writeSequenceToConsole("2", subject)
49+
sendNext(subject, "c")
50+
sendNext(subject, "d")
51+
}
52+
53+
54+
/*:
55+
56+
## BehaviorSubject a.k.a. Variable
57+
58+
ReplaySubject emits to any observer all of the items, in the buffer, that were emitted by the source
59+
60+
*/
61+
example("ReplaySubject") {
62+
let subject = BehaviorSubject(value: "z")
63+
writeSequenceToConsole("1", subject)
64+
sendNext(subject, "a")
65+
sendNext(subject, "b")
66+
writeSequenceToConsole("2", subject)
67+
sendNext(subject, "c")
68+
sendNext(subject, "d")
69+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
public func example(description: String, action: () -> ()) {
4+
println("\n--- \(description) example ---")
5+
action()
6+
}
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' requires-full-environment='true' display-mode='rendered'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Playgrounds/Subjects.playground/playground.xcworkspace/contents.xcworkspacedata

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

Rx.xcworkspace/contents.xcworkspacedata

Lines changed: 3 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)