I'm reading Monix documentation about observers ad I faced with the following example:
Or you can quickly build an Observer that only logs the events that it receives. We’ll use this in other samples:
import monix.reactive.Observer val out = Observer.dump("O") // out: Observer.Sync[Any] out.onNext(1) //=> 0: O-->1 // res0: Ack = Continue out.onNext(2) //=> 1: O-->2 // res0: Ack = Continue out.onComplete() //=> 2: O completed
Yet the next ILLEGAL example:
Feeding two elements, then stopping. This is NOT legal:
// BAD SAMPLE observer.onNext(1) observer.onNext(2) observer.onComplete()
So we can see the same onNext -> onNext -> onComplete
chain. Is it not legal? Why?
In the documentation that you've linked, it's explained directly after the example
This is the legal way of doing it:
As you can see in the comments, the issue is back-pressure. So why is there an example, using
.dump
that seems to be illegal?Note the comments in that example:
These comments are showing what you would get if you ran this in the Scala REPL. When you enter an expression and hit return, the REPL prints something like
res0
and lets you know what the return value of the last command was.So this example is demonstrating:
.onNext
has completed withContinue
It wouldn't be correct to write a program that feeds an Observer in this way, but this is a correct transcription of a legal execution of feeding an observer.
You can see the rules relating to back-pressure under the Contract section:
This is a good point to dwell on as elegant back-pressure management is one of the big promises of reactive streams.