Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4311

Observable[Void] cannot be composed using monadic operations

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Scala
    • Labels:

      mongo-scala-driver: 4.3.1

      Operations returning Observable[Void] cannot used map, flatMap, filter, etc., since they rely on onNext being called and Observable[Void] does not call onNext.

      Two use cases where this is needed:

      1. Returning a custom return type for operations that return Observable[Void]. For example:

      rawDriverCollection.dropIndex(name).map(_ => CustomVoidMongoResult(request, someMetaData))
      

      The .map(_ => CustomVoidMongoResult(request, someMetaData)) does not get executed, since MapObservable relies on onNext get called, but it never is called.

      2. Chaining operations returning observables together using flatMap, where one of the (non-last) operations returns Observable[Void]. Fo example:

      val resultObservable: Observable[String] = for {
          createdIndex <- createIndex(index)
           _ <- dropIndex(createdIndex)
           createdAgain <- createIndex(index)
         } yield createdAgain
      

      This example uses for-comprehension syntactic sugar, where the arrows are maps and the yield is a flatMap. The last line createIndex(index) line never gets reached, since again MapObservable depends on onNext getting called.

      One solution would be to change ToSingleObservableVoid to ToSingleObservableUnit, like:

        implicit class ToSingleObservableUnit(pub: => Publisher[Void]) extends SingleObservable[Unit] {
          val publisher = pub
          override def subscribe(observer: Observer[_ >: Unit]): Unit = SingleItemObservable(()).subscribe(observer)
        }
      

      Unit is the Scala equivalent of Java Void, except that it can be instantiated.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            scott.rice@rallyhealth.com Scott Rice
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: