Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ReactiveX/RxScala
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fd936e73c69f835cd42393b3ea8dcb6ce7891ac3
Choose a base ref
..
head repository: ReactiveX/RxScala
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e4acac49393c34299caf4286fa137f0473b5d121
Choose a head ref
Showing with 54 additions and 7 deletions.
  1. +4 −2 src/main/scala/rx/lang/scala/Notification.scala
  2. +6 −5 src/main/scala/rx/lang/scala/package.scala
  3. +44 −0 src/test/scala/rx/lang/scala/NotificationTests.scala
6 changes: 4 additions & 2 deletions src/main/scala/rx/lang/scala/Notification.scala
Original file line number Diff line number Diff line change
@@ -64,9 +64,11 @@ sealed trait Notification[+T] {

def apply(observer: Observer[T]): Unit = accept(observer)

def map[U](f: T => U): Notification[U] = {
def map[U](f: T => U): Notification[U] = flatMap(t => Notification.OnNext(f(t)))

def flatMap[U](f: T => Notification[U]): Notification[U] = {
this match {
case Notification.OnNext(value) => Notification.OnNext(f(value))
case Notification.OnNext(value) => f(value)
case Notification.OnError(error) => Notification.OnError(error)
case Notification.OnCompleted => Notification.OnCompleted
}
11 changes: 6 additions & 5 deletions src/main/scala/rx/lang/scala/package.scala
Original file line number Diff line number Diff line change
@@ -25,21 +25,22 @@ import _root_.scala.concurrent.{ExecutionContext, Future}
*/
package object scala {

@deprecated("Use IterableToObservable", "0.26.5")
type ObservableExtensions[T] = IterableToObservable[T]

implicit class IterableToObservable[T](val iterable: Iterable[T]) extends AnyVal {
def toObservable: Observable[T] = Observable.from(iterable)
/** Adds a [[toObservable]] extension method to [[_root_.scala.collection.Iterable Iterable]] */
implicit class ObservableExtensions[T](val source: Iterable[T]) extends AnyVal {
def toObservable: Observable[T] = Observable.from(source)
}

/** Adds a [[toObservable]] extension method to [[_root_.scala.util.Try Try]] */
implicit class TryToObservable[T](val tryT: Try[T]) extends AnyVal {
def toObservable: Observable[T] = Observable.from(tryT)
}

/** Adds a [[toObservable]] extension method to [[_root_.scala.Option Option]] */
implicit class OptionToObservable[T](val opt: Option[T]) extends AnyVal {
def toObservable: Observable[T] = Observable.from(opt)
}

/** Adds a [[toObservable]] extension method to [[_root_.scala.concurrent.Future Future]] */
implicit class FutureToObservable[T](val future: Future[T]) extends AnyVal {
def toObservable(implicit ec: ExecutionContext): Observable[T] = Observable.from(future)
}
44 changes: 44 additions & 0 deletions src/test/scala/rx/lang/scala/NotificationTests.scala
Original file line number Diff line number Diff line change
@@ -55,4 +55,48 @@ class NotificationTests extends JUnitSuite {
assertEquals(13, onCompleted(x=>42, e=>4711,()=>13))

}

@Test
def TestFlatMapNextToNext() {
val notification = OnNext(41).flatMap(i => Notification.OnNext(i+1))
assertEquals(42, notification(i=>i, _ => -1, () => -1))
}

@Test
def TestFlatMapNextToError() {
val oops = new Exception("Oops")
val notification = OnNext(()).flatMap(_ => Notification.OnError(oops))
assertEquals(42, notification(_ => -1, {
case `oops` => 42
case _ => -1
}, () => -1))
}

@Test
def TestFlatMapNextToCompletion() {
val notification = OnNext(()).flatMap(_ => Notification.OnCompleted)
assertEquals(42, notification(_ => -1, e => -1, () => 42))
}

@Test
def TestFlatMapCompleted() {
val notification = OnCompleted.flatMap(_ => Notification.OnNext(1))
assertEquals(42, notification(_ => -1, e => -1, () => 42))
}

@Test
def TestFlatMapError() {
val oops = new Exception("Oops")
val notification = OnError(oops).flatMap(_ => Notification.OnNext(1))
assertEquals(42, notification(_ => -1, {
case `oops` => 42
case _ => -1
}, () => -1))
}

@Test
def map() {
val notification = OnNext(41).map(_+1)
assertEquals(42, notification(i=>i, _ => -1, () => -1))
}
}