[JAVA-4311] Observable[Void] cannot be composed using monadic operations Created: 24/Sep/21  Updated: 04/May/22  Resolved: 28/Sep/21

Status: Closed
Project: Java Driver
Component/s: Scala
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Scott Rice Assignee: Ross Lawley
Resolution: Duplicate Votes: 0
Labels: external-user
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates JAVA-4303 Improve map/flatmap of Publisher[Void] Closed

 Description   

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.



 Comments   
Comment by Ross Lawley [ 28/Sep/21 ]

Hi scott.rice@rallyhealth.com,

Thanks for the ticket - we also have JAVA-4303 which essentially is the same request.

I'm in the process of reviewing and will take a look at your PR and that ticket and see what the best course of action is.

Unfortunately, changing signatures to Observable[Unit] would be an API break and would require a major release.

Please follow JAVA-4303 - as I'll follow up there.

Ross

Comment by Scott Rice [ 26/Sep/21 ]

This should have Component/s: Scala - noticed that after creating this ticket. Cannot edit after the fact.

Comment by Scott Rice [ 24/Sep/21 ]

Possible solution PR - https://github.com/mongodb/mongo-java-driver/pull/788

Generated at Thu Feb 08 09:01:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.