From 5fa8d9e4dd4ff539a58d7c5f74bdff4552fb63f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Thu, 30 Jul 2015 14:43:57 +0200 Subject: [PATCH] subjects playground --- .../Subjects.playground/Contents.swift | 69 +++++++++++++++++++ .../Sources/SupportCode.swift | 6 ++ .../Subjects.playground/contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 ++ Rx.xcworkspace/contents.xcworkspacedata | 3 + 5 files changed, 89 insertions(+) create mode 100644 Playgrounds/Subjects.playground/Contents.swift create mode 100644 Playgrounds/Subjects.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/Subjects.playground/contents.xcplayground create mode 100644 Playgrounds/Subjects.playground/playground.xcworkspace/contents.xcworkspacedata diff --git a/Playgrounds/Subjects.playground/Contents.swift b/Playgrounds/Subjects.playground/Contents.swift new file mode 100644 index 00000000..c543e9d9 --- /dev/null +++ b/Playgrounds/Subjects.playground/Contents.swift @@ -0,0 +1,69 @@ +import Cocoa +import RxSwift + +/*: + +To use playgrounds please open Rx.xcworkspace, build RxSwift-OSX scheme and then open playgrounds in Rx.xcworkspace tree view. + +*/ + +func writeSequenceToConsole(name: String, sequence: Observable) { + sequence + >- subscribeNext { + println("Subscription: \(name), value: \($0)") + } +} + + +/*: + +## PublishSubject + +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. + +*/ +example("PublishSubject") { + let subject = PublishSubject() + writeSequenceToConsole("1", subject) + sendNext(subject, "a") + sendNext(subject, "b") + writeSequenceToConsole("2", subject) + sendNext(subject, "c") + sendNext(subject, "d") +} + + +/*: + +## ReplaySubject + +ReplaySubject emits to any observer all of the items, in the buffer, that were emitted by the source + +*/ +example("ReplaySubject") { + let subject = ReplaySubject(bufferSize: 1) + writeSequenceToConsole("1", subject) + sendNext(subject, "a") + sendNext(subject, "b") + writeSequenceToConsole("2", subject) + sendNext(subject, "c") + sendNext(subject, "d") +} + + +/*: + +## BehaviorSubject a.k.a. Variable + +ReplaySubject emits to any observer all of the items, in the buffer, that were emitted by the source + +*/ +example("ReplaySubject") { + let subject = BehaviorSubject(value: "z") + writeSequenceToConsole("1", subject) + sendNext(subject, "a") + sendNext(subject, "b") + writeSequenceToConsole("2", subject) + sendNext(subject, "c") + sendNext(subject, "d") +} diff --git a/Playgrounds/Subjects.playground/Sources/SupportCode.swift b/Playgrounds/Subjects.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..ba8c634d --- /dev/null +++ b/Playgrounds/Subjects.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} \ No newline at end of file diff --git a/Playgrounds/Subjects.playground/contents.xcplayground b/Playgrounds/Subjects.playground/contents.xcplayground new file mode 100644 index 00000000..0b962190 --- /dev/null +++ b/Playgrounds/Subjects.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/Subjects.playground/playground.xcworkspace/contents.xcworkspacedata b/Playgrounds/Subjects.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/Playgrounds/Subjects.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Rx.xcworkspace/contents.xcworkspacedata b/Rx.xcworkspace/contents.xcworkspacedata index b4da0bce..92bb55e5 100644 --- a/Rx.xcworkspace/contents.xcworkspacedata +++ b/Rx.xcworkspace/contents.xcworkspacedata @@ -13,6 +13,9 @@ + +