From 5642810882fa413c65b124e7f4b71b55e5eea4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Sat, 20 Jun 2015 08:39:37 +0200 Subject: [PATCH 1/8] small rx_text correction --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 586a61b5..e1cf09ca 100644 --- a/README.md +++ b/README.md @@ -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 +let subscription/*: Disposable */ = primeTextField.rx_text // type is Observable >- map { WolframAlphaIsPrime($0.toInt() ?? 0) } // type is Observable> >- concat // type is Observable >- map { "number \($0.n) is prime? \($0.isPrime)" } // type is Observable @@ -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 { From 395e1c3fef7c89511da8a99df35679fb8893d1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Sat, 20 Jun 2015 13:07:07 +0200 Subject: [PATCH 2/8] Xcode error fixed --- .../OSXPlayground.playground}/Contents.swift | 0 .../Sources/SupportCode.swift | 0 .../contents.xcplayground | 0 .../contents.xcworkspacedata | 0 .../timeline.xctimeline | 0 .../Contents.swift | 274 ++++++++++++++++++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + .../Contents.swift | 59 ++++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + .../Contents.swift | 5 + .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 2 + .../Contents.swift | 80 +++++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + .../Contents.swift | 46 +++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + .../Contents.swift | 5 + .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 2 + .../Contents.swift | 41 +++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + .../Contents.swift | 5 + .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 2 + .../Contents.swift | 144 +++++++++ .../Sources/SupportCode.swift | 6 + .../contents.xcplayground | 4 + .../timeline.xctimeline | 6 + Rx.xcworkspace/contents.xcworkspacedata | 41 ++- 39 files changed, 817 insertions(+), 3 deletions(-) rename {OSXPlayground.playground => Playgrounds/OSXPlayground.playground}/Contents.swift (100%) rename {OSXPlayground.playground => Playgrounds/OSXPlayground.playground}/Sources/SupportCode.swift (100%) rename {OSXPlayground.playground => Playgrounds/OSXPlayground.playground}/contents.xcplayground (100%) rename {OSXPlayground.playground => Playgrounds/OSXPlayground.playground}/playground.xcworkspace/contents.xcworkspacedata (100%) rename {OSXPlayground.playground => Playgrounds/OSXPlayground.playground}/timeline.xctimeline (100%) create mode 100644 Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Combining.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Combining.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Connectable.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Error Handling.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Filtering.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Transforming.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Utility.playground/Sources/SupportCode.swift create mode 100644 Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground create mode 100644 Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline diff --git a/OSXPlayground.playground/Contents.swift b/Playgrounds/OSXPlayground.playground/Contents.swift similarity index 100% rename from OSXPlayground.playground/Contents.swift rename to Playgrounds/OSXPlayground.playground/Contents.swift diff --git a/OSXPlayground.playground/Sources/SupportCode.swift b/Playgrounds/OSXPlayground.playground/Sources/SupportCode.swift similarity index 100% rename from OSXPlayground.playground/Sources/SupportCode.swift rename to Playgrounds/OSXPlayground.playground/Sources/SupportCode.swift diff --git a/OSXPlayground.playground/contents.xcplayground b/Playgrounds/OSXPlayground.playground/contents.xcplayground similarity index 100% rename from OSXPlayground.playground/contents.xcplayground rename to Playgrounds/OSXPlayground.playground/contents.xcplayground diff --git a/OSXPlayground.playground/playground.xcworkspace/contents.xcworkspacedata b/Playgrounds/OSXPlayground.playground/playground.xcworkspace/contents.xcworkspacedata similarity index 100% rename from OSXPlayground.playground/playground.xcworkspace/contents.xcworkspacedata rename to Playgrounds/OSXPlayground.playground/playground.xcworkspace/contents.xcworkspacedata diff --git a/OSXPlayground.playground/timeline.xctimeline b/Playgrounds/OSXPlayground.playground/timeline.xctimeline similarity index 100% rename from OSXPlayground.playground/timeline.xctimeline rename to Playgrounds/OSXPlayground.playground/timeline.xctimeline diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift new file mode 100644 index 00000000..feb04e81 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift @@ -0,0 +1,274 @@ + +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() + let intOb2 = Subject() + + 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() + let intOb2 = Subject() + + 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() + let subject2 = Subject() + + 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() + let subject2 = Subject() + + 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` +[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> + let var3 = Variable(var1 as Observable) + + 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) +} + + +/*: +To be continued... +*/ diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Combining.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift new file mode 100644 index 00000000..b69571c9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift @@ -0,0 +1,59 @@ +//: Playground - noun: a place where people can play + +import Cocoa +import RxSwift + +/*: +## takeUntil +Discard any items emitted by an Observable after a second Observable emits an item or terminates +*/ + +example("takeUntil") { + + let observable1 = Subject() + let observable2 = Subject() + + 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) + +} + + +/*: +## takeUntil +Mirror items emitted by an Observable until a specified condition becomes false + +*/ + +example("takeWhile") { + + let observable1 = Subject() + + 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) + +} + diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift new file mode 100644 index 00000000..07ee6d28 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift @@ -0,0 +1,5 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +var str = "Hello, playground" diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground new file mode 100644 index 00000000..7e5b407c --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift new file mode 100644 index 00000000..6866f2cb --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift @@ -0,0 +1,80 @@ +//: Playground - noun: a place where people can play + +import Cocoa +import RxSwift + +/*: + +# Creating observables + +Besides creation operators seen in the introduction, there are several more. + +## asObservable + +*/ + + + + +/*: +## create + +Create an Observable from scratch by means of a function +*/ + +example("create") { + + println("creating") + let observable: Observable = 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 +*/ + +example("defer") { + + let defered: Observable = 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) + } +} + diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift new file mode 100644 index 00000000..cfa4fbc6 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -0,0 +1,46 @@ +//: Playground - noun: a place where people can play + +import Cocoa +import RxSwift + +/*: +## catch + +Recover from an onError notification by continuing the sequence without error +*/ + +example("catch") { + + let observable1 = Subject() + let observable2 = Subject() + + 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, 5) + sendNext(observable2, 6) + sendNext(observable2, 7) + sendNext(observable2, 8) + sendCompleted(observable2) + + +} diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift new file mode 100644 index 00000000..07ee6d28 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift @@ -0,0 +1,5 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +var str = "Hello, playground" diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground new file mode 100644 index 00000000..7e5b407c --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift new file mode 100644 index 00000000..74d4c9ce --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift @@ -0,0 +1,41 @@ +//: Playground - noun: a place where people can play + +import Cocoa +import RxSwift + +/*: +## concat +Emit the emissions from two or more Observables without interleaving them +*/ + +example("concat") { + let var1 = Variable(0) + let var2 = Variable(200) + + // var3 is like an Observable> + let var3 = Variable(var1 as Observable) + + 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) +} diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift new file mode 100644 index 00000000..07ee6d28 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -0,0 +1,5 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +var str = "Hello, playground" diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground new file mode 100644 index 00000000..7e5b407c --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift new file mode 100644 index 00000000..97672958 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift @@ -0,0 +1,144 @@ + +import Cocoa +import RxSwift + + + +/*: +### `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() + + 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() + + intOb1 + >- subscribeNext { int in + println(int) + } + + sendNext(intOb1, 1) + sendCompleted(intOb1) +} + + +/*: +###subscribeNext +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() + + 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() + + 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() + + 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 +*/ + +example("doOnNext") { + let intOb1 = Subject() + + let intOb2 = intOb1 + >- doOnNext { int in + println("first \(int)") + } + + intOb2 + >- subscribeNext { int in + println("second \(int)") + } + + sendNext(intOb1, 1) + +} + + +/*: +### `observeSingleOn` +Specify the Scheduler on which an observer will observe this Observable +[More info in reactive.io website](http://reactivex.io/documentation/operators/observeon.html) +*/ + +//TODO: Do not work in playgrounds +example("observeSingleOn") { + let intOb1 = Subject() + + intOb1 + >- observeSingleOn(MainScheduler.sharedInstance) + >- subscribeNext { int in + println(int) + } + + sendNext(intOb1, 1) +} + diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Sources/SupportCode.swift new file mode 100644 index 00000000..6f18f735 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Sources/SupportCode.swift @@ -0,0 +1,6 @@ + + +public func example(description: String, action: () -> ()) { + println("\n--- \(description) example ---") + action() +} diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground new file mode 100644 index 00000000..06828af9 --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Rx.xcworkspace/contents.xcworkspacedata b/Rx.xcworkspace/contents.xcworkspacedata index cfdbd6b3..df8a8b74 100644 --- a/Rx.xcworkspace/contents.xcworkspacedata +++ b/Rx.xcworkspace/contents.xcworkspacedata @@ -7,9 +7,44 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + From c85e028e6a8d4b6eb790147c7c550955ae641024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Sat, 20 Jun 2015 15:03:58 +0200 Subject: [PATCH 3/8] more playgrounds and homogenization --- .../contents.xcplayground | 2 +- .../Contents.swift | 16 +- .../Contents.swift | 27 ++- .../Contents.swift | 54 +++++- .../Contents.swift | 172 ++++++++++++++++-- .../Contents.swift | 13 +- .../Contents.swift | 41 ++++- .../contents.xcplayground | 4 +- .../timeline.xctimeline | 6 + .../Contents.swift | 32 +++- .../Contents.swift | 103 ++++++++++- .../contents.xcplayground | 4 +- .../timeline.xctimeline | 6 + .../Contents.swift | 24 ++- 14 files changed, 449 insertions(+), 55 deletions(-) create mode 100644 Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline create mode 100644 Playgrounds/ObservablesOperators/Observables+Transforming.playground/timeline.xctimeline diff --git a/Playgrounds/OSXPlayground.playground/contents.xcplayground b/Playgrounds/OSXPlayground.playground/contents.xcplayground index 3de2b51b..06828af9 100644 --- a/Playgrounds/OSXPlayground.playground/contents.xcplayground +++ b/Playgrounds/OSXPlayground.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift index feb04e81..1d036449 100644 --- a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift @@ -1,14 +1,15 @@ - import Cocoa import RxSwift -/*: +/*: ## Combination operators -Operators that work with multiple source Observables to create a single Observable +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 ) @@ -186,7 +187,7 @@ example("zip 3th") { ### `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) +[More info in reactive.io website]( http://reactivex.io/documentation/operators/merge.html ) */ example("merge 1st") { @@ -231,7 +232,8 @@ example("merge 2nd") { /*: ### `switchLatest` -[More info in reactive.io website](http://reactivex.io/documentation/operators/switch.html) +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 ) */ @@ -268,7 +270,3 @@ example("switchLatest") { var2.next(204) } - -/*: -To be continued... -*/ diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift index b69571c9..91f7c6d1 100644 --- a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift @@ -1,11 +1,26 @@ -//: Playground - noun: a place where people can play - import Cocoa import RxSwift + /*: -## takeUntil -Discard any items emitted by an Observable after a second Observable emits an item or terminates +## Conditional and Boolean Operators + +Operators that evaluate one or more Observables or items emitted by Observables. +*/ + + +/*: +### `amb` +Given two or more source Observables, emit all of the items from only the first of these Observables to emit an item or notification +[More info in reactive.io website]( http://reactivex.io/documentation/operators/amb.html ) +*/ + + + +/*: +### `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") { @@ -32,9 +47,9 @@ example("takeUntil") { /*: -## takeUntil +### `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") { diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift index 07ee6d28..f84bbd4e 100644 --- a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift @@ -1,5 +1,53 @@ -//: Playground - noun: a place where people can play - 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` +*/ + + + + + + + -var str = "Hello, playground" diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift index 6866f2cb..1a34ed66 100644 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift @@ -1,25 +1,170 @@ -//: Playground - noun: a place where people can play - import Cocoa import RxSwift + /*: +## Creating observables -# Creating observables +Operators that originate new Observables. +*/ -Besides creation operators seen in the introduction, there are several more. -## asObservable + +/*: +### asObservable */ +/*: +### `empty` +`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 = 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)") + } + } +} /*: -## create +As you can see, no values are ever sent to the subscriber of an empty observable. It just completes and is done. +*/ + + + +/*: +### `never` +`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 = never() + + let neverSubscriber = neverObservable + >- subscribe { _ in + println("This block is never called.") + } +} + + + +/*: +### `failWith` +`never` 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 = 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 */ = 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") { @@ -38,19 +183,15 @@ example("create") { >- subscribeNext { println($0) } - - observable - >- subscribeNext { - println($0) - } } /*: -## defer +## `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") { @@ -78,3 +219,10 @@ example("defer") { } } + + +/*: +### `timer` + +*/ + diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift index cfa4fbc6..77e2fbf7 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -1,12 +1,19 @@ -//: Playground - noun: a place where people can play - import Cocoa import RxSwift + /*: -## catch +## 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") { diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift index 07ee6d28..547394ea 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift @@ -1,5 +1,40 @@ -//: Playground - noun: a place where people can play - import Cocoa +import RxSwift -var str = "Hello, playground" + +/*: +## 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)") + } +} \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground index 7e5b407c..06828af9 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground @@ -1,2 +1,4 @@ - \ No newline at end of file + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift index 74d4c9ce..d18c89e4 100644 --- a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift @@ -1,11 +1,18 @@ -//: Playground - noun: a place where people can play - import Cocoa import RxSwift + /*: -## concat -Emit the emissions from two or more Observables without interleaving them +## 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") { @@ -39,3 +46,20 @@ example("concat") { 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)") + } +} \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift index 07ee6d28..3209aa25 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -1,5 +1,102 @@ -//: Playground - noun: a place where people can play - import Cocoa +import RxSwift -var str = "Hello, playground" + +/*: +## Transforming Observables + +Operators that transform items that are emitted by an Observable. +*/ + + +/*: +### `map` / `select` + +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/map.html ) +*/ + +example("map") { + + let observable1: Observable = create { observer in + sendNext(observer, Character("A")) + sendNext(observer, Character("B")) + sendNext(observer, Character("C")) + + return AnonymousDisposable {} + } + + observable1 + >- map { char in + char.hashValue + } + >- subscribeNext { + println($0) + } +} + +/*: +### `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 = create { observer in + sendNext(observer, 1) + sendNext(observer, 2) + sendNext(observer, 3) + + return AnonymousDisposable {} + } + + let observable2: Observable = 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 = 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) + } +} \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground index 7e5b407c..06828af9 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground @@ -1,2 +1,4 @@ - \ No newline at end of file + + + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/timeline.xctimeline new file mode 100644 index 00000000..bf468afe --- /dev/null +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift index 97672958..a91ea8e2 100644 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift @@ -1,13 +1,18 @@ - 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) +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") { @@ -26,8 +31,8 @@ example("subscribe") { /*: 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 +### `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() @@ -43,8 +48,8 @@ example("subscribeNext") { /*: -###subscribeNext -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 +### `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() @@ -60,7 +65,7 @@ example("subscribeCompleted") { /*: -###subscribeError +### `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") { @@ -102,7 +107,8 @@ example("do") { /*: ### `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 +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") { From 95ba6123346c75af3a64aab7dd32e084b2e8209d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Sun, 21 Jun 2015 10:57:41 +0200 Subject: [PATCH 4/8] retry an take added --- .../Contents.swift | 45 +++++++++++++++++++ .../Contents.swift | 17 ++++++- .../Contents.swift | 2 +- .../Contents.swift | 25 +---------- .../Preprocessor.xcodeproj/project.pbxproj | 2 + RxCocoa/RxCocoa.xcodeproj/project.pbxproj | 1 + RxTests/RxTests.xcodeproj/project.pbxproj | 1 + 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift index 77e2fbf7..a5d2d3ce 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -51,3 +51,48 @@ example("catch") { } + + +/*: +## `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 = 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)") + } + } + + +} diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift index 547394ea..813b79d6 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift @@ -37,4 +37,19 @@ example("distinctUntilChanged") { >- subscribeNext { value in println("\(value)") } -} \ No newline at end of file +} + +/*: +### `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)") + } +} + diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift index 3209aa25..5bff78ed 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -99,4 +99,4 @@ example("scan") { >- subscribeNext { println($0) } -} \ No newline at end of file +} diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift index a91ea8e2..45745bfc 100644 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift @@ -85,7 +85,7 @@ example("subscribeError") { ### `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) +[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html ) */ example("do") { @@ -108,7 +108,7 @@ example("do") { /*: ### `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) +[More info in reactive.io website]( http://reactivex.io/documentation/operators/do.html ) */ example("doOnNext") { @@ -127,24 +127,3 @@ example("doOnNext") { sendNext(intOb1, 1) } - - -/*: -### `observeSingleOn` -Specify the Scheduler on which an observer will observe this Observable -[More info in reactive.io website](http://reactivex.io/documentation/operators/observeon.html) -*/ - -//TODO: Do not work in playgrounds -example("observeSingleOn") { - let intOb1 = Subject() - - intOb1 - >- observeSingleOn(MainScheduler.sharedInstance) - >- subscribeNext { int in - println(int) - } - - sendNext(intOb1, 1) -} - diff --git a/Preprocessor/Preprocessor.xcodeproj/project.pbxproj b/Preprocessor/Preprocessor.xcodeproj/project.pbxproj index 55fd1f4c..03b95c3e 100644 --- a/Preprocessor/Preprocessor.xcodeproj/project.pbxproj +++ b/Preprocessor/Preprocessor.xcodeproj/project.pbxproj @@ -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 */ }; diff --git a/RxCocoa/RxCocoa.xcodeproj/project.pbxproj b/RxCocoa/RxCocoa.xcodeproj/project.pbxproj index 39000a82..c6dd9843 100644 --- a/RxCocoa/RxCocoa.xcodeproj/project.pbxproj +++ b/RxCocoa/RxCocoa.xcodeproj/project.pbxproj @@ -253,6 +253,7 @@ C81553D51A98AB4A00C63152 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0610; ORGANIZATIONNAME = "Krunoslav Zaher"; TargetAttributes = { diff --git a/RxTests/RxTests.xcodeproj/project.pbxproj b/RxTests/RxTests.xcodeproj/project.pbxproj index bb2b5317..7b6ed762 100644 --- a/RxTests/RxTests.xcodeproj/project.pbxproj +++ b/RxTests/RxTests.xcodeproj/project.pbxproj @@ -323,6 +323,7 @@ C81108161AF50DDA001C13E4 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0630; TargetAttributes = { C811081F1AF50E11001C13E4 = { From e3a4bc2f4d170f3ff9b433ae36134593c876e1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Mon, 22 Jun 2015 01:55:38 +0200 Subject: [PATCH 5/8] little corrections --- .../Contents.swift | 21 ++++++++++++------- .../Contents.swift | 10 ++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift index 1a34ed66..e32ab420 100644 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift @@ -182,7 +182,12 @@ example("create") { observable >- subscribeNext { println($0) - } + } + + observable + >- subscribeNext { + println($0) + } } @@ -196,7 +201,7 @@ Create an Observable from a function which create an observable. But do not crea example("defer") { - let defered: Observable = defer({ + let defered: Observable = defer { println("creating") return create { observer in println("emmiting") @@ -206,17 +211,17 @@ example("defer") { return AnonymousDisposable {} } - }) - - defered - >- subscribeNext { - println($0) } defered >- subscribeNext { println($0) - } + } + + defered + >- subscribeNext { + println($0) + } } diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift index 5bff78ed..1536e779 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -12,7 +12,7 @@ Operators that transform items that are emitted by an Observable. /*: ### `map` / `select` -transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable +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 ) */ @@ -30,15 +30,15 @@ example("map") { >- map { char in char.hashValue } - >- subscribeNext { - println($0) + >- 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 +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 ) */ @@ -75,7 +75,7 @@ example("flatMap") { /*: ### `scan` -apply a function to each item emitted by an Observable, sequentially, and emit each successive value +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 ) */ From a283444048e8b5488f3ddcc44a92daf8b4095e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Tue, 23 Jun 2015 13:19:28 +0200 Subject: [PATCH 6/8] secon catch example added --- .../Sources/SupportCode.swift | 2 +- .../Contents.swift | 42 +++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift index 6f18f735..ba8c634d 100644 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Sources/SupportCode.swift @@ -3,4 +3,4 @@ public func example(description: String, action: () -> ()) { println("\n--- \(description) example ---") action() -} +} \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift index a5d2d3ce..f4901be9 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -8,7 +8,6 @@ import RxSwift Operators that help to recover from error notifications from an Observable. */ - /*: ## `catch` @@ -16,15 +15,15 @@ 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") { +example("catch 1st") { let observable1 = Subject() let observable2 = Subject() observable1 - >- catch({ error in + >- catch { error in return observable2 - }) + } >- subscribe { event in switch event { case .Next(let box): @@ -43,13 +42,40 @@ example("catch") { sendNext(observable1, 4) sendError(observable1, NSError(domain: "Test", code: 0, userInfo: nil)) - sendNext(observable2, 5) - sendNext(observable2, 6) - sendNext(observable2, 7) - sendNext(observable2, 8) + sendNext(observable2, 100) + sendNext(observable2, 200) + sendNext(observable2, 300) + sendNext(observable2, 400) sendCompleted(observable2) +} + +example("catch 2nd") { + + let observable1 = Subject() + + 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)) + + } From adc3a7d77fb6f98c6727a4c18ddf061a390be4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Wed, 24 Jun 2015 08:34:54 +0200 Subject: [PATCH 7/8] rendered markup setted --- .../Contents.swift | 19 +++----- .../contents.xcplayground | 2 +- .../Contents.swift | 13 ++---- .../contents.xcplayground | 2 +- .../Contents.swift | 6 +-- .../contents.xcplayground | 2 +- .../Contents.swift | 46 ++++++------------- .../contents.xcplayground | 4 +- .../timeline.xctimeline | 6 --- .../Contents.swift | 7 +-- .../contents.xcplayground | 2 +- .../Contents.swift | 6 ++- .../contents.xcplayground | 4 +- .../timeline.xctimeline | 6 --- .../Contents.swift | 10 ++-- .../contents.xcplayground | 2 +- .../Contents.swift | 2 - .../contents.xcplayground | 2 +- .../Contents.swift | 23 +++++++--- .../contents.xcplayground | 4 +- .../timeline.xctimeline | 6 --- 21 files changed, 61 insertions(+), 113 deletions(-) delete mode 100644 Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline delete mode 100644 Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline delete mode 100644 Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift index 1d036449..cb7916cb 100644 --- a/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/Contents.swift @@ -1,16 +1,15 @@ 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 ) */ @@ -28,11 +27,9 @@ example("startWith") { } - - /*: - ### `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 ) @@ -40,7 +37,6 @@ Once each source Observables have each emitted an item, `combineLatest` emits an The next example shows how */ - example("combineLatest 1st") { let intOb1 = Subject() let intOb2 = Subject() @@ -85,7 +81,6 @@ example("combineLatest 2nd") { } } - /*: 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 @@ -104,11 +99,9 @@ example("combineLatest 3th") { } } - - /*: - ### `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) @@ -184,8 +177,8 @@ example("zip 3th") { /*: - ### `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 ) */ @@ -230,8 +223,8 @@ example("merge 2nd") { /*: - ### `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 ) */ diff --git a/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground index 06828af9..3de2b51b 100644 --- a/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Combining.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift index 91f7c6d1..3b33b8da 100644 --- a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/Contents.swift @@ -6,18 +6,9 @@ import RxSwift ## Conditional and Boolean Operators Operators that evaluate one or more Observables or items emitted by Observables. -*/ - - -/*: -### `amb` -Given two or more source Observables, emit all of the items from only the first of these Observables to emit an item or notification -[More info in reactive.io website]( http://reactivex.io/documentation/operators/amb.html ) -*/ -/*: ### `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 ) @@ -45,9 +36,11 @@ example("takeUntil") { } - /*: + + ### `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 ) */ diff --git a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground index 06828af9..3de2b51b 100644 --- a/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Conditional and Boolean.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift index f84bbd4e..f9a4b24c 100644 --- a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/Contents.swift @@ -9,6 +9,7 @@ Specialty Observables that have more precisely-controlled subscription dynamics. */ + /*: ### `multicast` [More info in reactive.io website]( http://reactivex.io/documentation/operators/publish.html ) @@ -46,8 +47,3 @@ Ensure that all observers see the same sequence of emitted items, even if they s - - - - - diff --git a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground index 7e5b407c..b909959d 100644 --- a/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Connectable.playground/contents.xcplayground @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift index e32ab420..a601ded3 100644 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/Contents.swift @@ -6,23 +6,13 @@ import RxSwift ## Creating observables Operators that originate new Observables. -*/ - -/*: -### asObservable - -*/ - - - -/*: ### `empty` -`empty` creates an observable that contains no objects. The only message it sends is the `.Completed` message. + +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 = empty() @@ -40,16 +30,14 @@ example("Empty observable") { /*: As you can see, no values are ever sent to the subscriber of an empty observable. It just completes and is done. -*/ -/*: ### `never` -`never` creates an observable that contains no objects and never completes or errors out. + +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 = never() @@ -59,11 +47,11 @@ example("Never observable") { } } - - /*: + ### `failWith` -`never` creates an observable that contains no objects and send only a error out. + +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 ) */ @@ -87,6 +75,7 @@ example("failWith") { /*: ### `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 ) */ @@ -109,10 +98,11 @@ example("returnElement/just") { /*: 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. */ @@ -134,11 +124,9 @@ example("returnElements") { /*: 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 ) @@ -161,7 +149,7 @@ example("from") { } /*: -## `create` +### `create` Create an Observable from scratch by means of a function [More info in reactive.io website]( http://reactivex.io/documentation/operators/create.html ) @@ -190,10 +178,8 @@ example("create") { } } - - /*: -## `defer` +### `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 ) @@ -225,9 +211,3 @@ example("defer") { } - -/*: -### `timer` - -*/ - diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground index 06828af9..b909959d 100644 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Creating.playground/contents.xcplayground @@ -1,4 +1,2 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline deleted file mode 100644 index bf468afe..00000000 --- a/Playgrounds/ObservablesOperators/Observables+Creating.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift index f4901be9..a8dd5777 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/Contents.swift @@ -9,7 +9,7 @@ Operators that help to recover from error notifications from an Observable. */ /*: -## `catch` +### `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 ) @@ -80,7 +80,7 @@ example("catch 2nd") { /*: -## `retry` +### `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 ) @@ -106,7 +106,6 @@ example("retry") { return AnonymousDisposable {} } - observable >- retry >- subscribe { event in @@ -119,6 +118,4 @@ example("retry") { println("\(error)") } } - - } diff --git a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground index 06828af9..3de2b51b 100644 --- a/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Error Handling.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift index 813b79d6..09708245 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/Contents.swift @@ -6,11 +6,11 @@ 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 ) */ @@ -27,6 +27,7 @@ example("filter") { /*: ### `distinctUntilChanged` + suppress duplicate items emitted by an Observable [More info in reactive.io website]( http://reactivex.io/documentation/operators/distinct.html ) */ @@ -41,6 +42,7 @@ example("distinctUntilChanged") { /*: ### `take` + Emit only the first n items emitted by an Observable [More info in reactive.io website]( http://reactivex.io/documentation/operators/take.html ) */ diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground index 06828af9..b909959d 100644 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/contents.xcplayground @@ -1,4 +1,2 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline deleted file mode 100644 index bf468afe..00000000 --- a/Playgrounds/ObservablesOperators/Observables+Filtering.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift index d18c89e4..e04feba4 100644 --- a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/Contents.swift @@ -6,11 +6,11 @@ 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 ) */ @@ -47,9 +47,11 @@ example("concat") { 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 ) @@ -62,4 +64,4 @@ example("aggregate") { >- subscribeNext { value in println("\(value)") } -} \ No newline at end of file +} diff --git a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground index 06828af9..3de2b51b 100644 --- a/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Mathematical and Aggregate.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift index 1536e779..f86b6af4 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/Contents.swift @@ -6,10 +6,8 @@ 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 diff --git a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground index 06828af9..3de2b51b 100644 --- a/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Transforming.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift index 45745bfc..754e7455 100644 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/Contents.swift @@ -6,11 +6,11 @@ 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 ) */ @@ -27,13 +27,16 @@ example("subscribe") { 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() @@ -46,11 +49,14 @@ example("subscribeNext") { 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() @@ -63,11 +69,13 @@ example("subscribeCompleted") { 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() @@ -80,10 +88,11 @@ example("subscribeError") { 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 ) */ @@ -106,7 +115,9 @@ example("do") { } /*: + ### `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 ) */ diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground b/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground index 06828af9..b909959d 100644 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground +++ b/Playgrounds/ObservablesOperators/Observables+Utility.playground/contents.xcplayground @@ -1,4 +1,2 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline b/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline deleted file mode 100644 index bf468afe..00000000 --- a/Playgrounds/ObservablesOperators/Observables+Utility.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - From f5dd473d05c4b227bfcf55a837b65812806f23da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Garci=CC=81a?= Date: Wed, 24 Jun 2015 08:44:12 +0200 Subject: [PATCH 8/8] timeline.xctimeline added to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index e03643f6..c90a0fd4 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,8 @@ DerivedData *.ipa *.xcuserstate +timeline.xctimeline + # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However