|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.lang |
| 17 | + |
| 18 | +import java.util.concurrent.TimeUnit |
| 19 | +import java.util.Date |
| 20 | + |
| 21 | +/* |
| 22 | + * Note that: |
| 23 | + * - Scala users cannot use Java's types with variance without always using writing |
| 24 | + * e.g. rx.Notification[_ <: T], so we create aliases fixing the variance |
| 25 | + * - For consistency, we create aliases for all types which Scala users need |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * This package contains all classes that RxScala users need. |
| 30 | + * |
| 31 | + * It mirrors the structure of package `rx`, but implementation classes that RxScala users |
| 32 | + * will not need are left out. |
| 33 | + */ |
| 34 | +package object scala { |
| 35 | + |
| 36 | + /* |
| 37 | + * Here we're imitating C's preprocessor using Search & Replace. |
| 38 | + * |
| 39 | + * To activate the code needed to get nice Scaladoc, do the following replacements: |
| 40 | + * /*//#ifdef SCALADOC --> //#ifdef SCALADOC |
| 41 | + * *///#else --> /*//#else |
| 42 | + * //#endif --> *///#endif |
| 43 | + * |
| 44 | + * To get back to the actual code, undo the above replacements. |
| 45 | + * |
| 46 | + */ |
| 47 | + |
| 48 | + /*//#ifdef SCALADOC |
| 49 | +
|
| 50 | + /** |
| 51 | + * Provides a mechanism for receiving push-based notifications. |
| 52 | + * |
| 53 | + * After an Observer calls an [[Observable]]'s `subscribe` method, the Observable |
| 54 | + * calls the Observer's `onNext` method to provide notifications. A well-behaved Observable will |
| 55 | + * call an Observer's `onCompleted` method exactly once or the Observer's `onError` method exactly once. |
| 56 | + */ |
| 57 | + trait Observer[-T] { |
| 58 | +
|
| 59 | + /** |
| 60 | + * Notifies the Observer that the [[Observable]] has finished sending push-based notifications. |
| 61 | + * |
| 62 | + * The [[Observable]] will not call this method if it calls `onError`. |
| 63 | + */ |
| 64 | + def onCompleted(): Unit |
| 65 | +
|
| 66 | + /** |
| 67 | + * Notifies the Observer that the [[Observable]] has experienced an error condition. |
| 68 | + * |
| 69 | + * If the [[Observable]] calls this method, it will not thereafter call `onNext` or `onCompleted`. |
| 70 | + */ |
| 71 | + def onError(e: Throwable): Unit |
| 72 | +
|
| 73 | + /** |
| 74 | + * Provides the Observer with new data. |
| 75 | + * |
| 76 | + * The [[Observable]] calls this closure 0 or more times. |
| 77 | + * |
| 78 | + * The [[Observable]] will not call this method again after it calls either `onCompleted` or `onError`. |
| 79 | + */ |
| 80 | + def onNext(arg: T): Unit |
| 81 | + } |
| 82 | +
|
| 83 | + /** |
| 84 | + * Subscriptions are returned from all `Observable.subscribe` methods to allow unsubscribing. |
| 85 | + * |
| 86 | + * This interface is the equivalent of `IDisposable` in the .NET Rx implementation. |
| 87 | + */ |
| 88 | + trait Subscription { |
| 89 | + /** |
| 90 | + * Call this method to stop receiving notifications on the Observer that was registered when |
| 91 | + * this Subscription was received. |
| 92 | + */ |
| 93 | + def unsubscribe(): Unit |
| 94 | + } |
| 95 | + |
| 96 | + import language.implicitConversions |
| 97 | + |
| 98 | + private[scala] implicit def fakeSubscription2RxSubscription(s: Subscription): rx.Subscription = |
| 99 | + new rx.Subscription { |
| 100 | + def unsubscribe() = s.unsubscribe() |
| 101 | + } |
| 102 | + private[scala] implicit def rxSubscription2FakeSubscription(s: rx.Subscription): Subscription = |
| 103 | + new Subscription { |
| 104 | + def unsubscribe() = s.unsubscribe() |
| 105 | + } |
| 106 | +
|
| 107 | + private[scala] implicit def schedulerActionToFunc2[T](action: (Scheduler, T) => Subscription) = |
| 108 | + new rx.util.functions.Func2[rx.Scheduler, T, rx.Subscription] { |
| 109 | + def call(s: rx.Scheduler, t: T): rx.Subscription = { |
| 110 | + action(ImplicitFunctionConversions.javaSchedulerToScalaScheduler(s), t) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private[scala] implicit def fakeObserver2RxObserver[T](o: Observer[T]): rx.Observer[_ >: T] = ??? |
| 115 | + private[scala] implicit def rxObserver2fakeObserver[T](o: rx.Observer[_ >: T]): Observer[T] = ??? |
| 116 | + |
| 117 | + *///#else |
| 118 | + |
| 119 | + type Observer[-T] = rx.Observer[_ >: T] |
| 120 | + |
| 121 | + type Subscription = rx.Subscription |
| 122 | + |
| 123 | + //#endif |
| 124 | + |
| 125 | + /** |
| 126 | + * Allows to construct observables in a similar way as futures. |
| 127 | + * |
| 128 | + * Example: |
| 129 | + * |
| 130 | + * {{{ |
| 131 | + * implicit val scheduler = Schedulers.threadPoolForIO |
| 132 | + * val o: Observable[List[Friend]] = observable { |
| 133 | + * session.getFriends |
| 134 | + * } |
| 135 | + * o.subscribe( |
| 136 | + * friendList => println(friendList), |
| 137 | + * err => println(err.getMessage) |
| 138 | + * ) |
| 139 | + * }}} |
| 140 | + */ |
| 141 | + def observable[T](body: => T)(implicit scheduler: Scheduler): Observable[T] = { |
| 142 | + Observable(1).observeOn(scheduler).map(_ => body) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/* |
| 147 | +
|
| 148 | +These classes are considered unnecessary for Scala users, so we don't create aliases for them: |
| 149 | +
|
| 150 | +rx.plugins.RxJavaErrorHandler |
| 151 | +rx.plugins.RxJavaObservableExecutionHook |
| 152 | +rx.plugins.RxJavaPlugins |
| 153 | +
|
| 154 | +rx.subscriptions.BooleanSubscription |
| 155 | +rx.subscriptions.CompositeSubscription |
| 156 | +rx.subscriptions.Subscriptions |
| 157 | +
|
| 158 | +*/ |
0 commit comments