-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: Scala
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.
- duplicates
-
JAVA-4303 Improve map/flatmap of Publisher[Void]
- Closed