Asynchronous Stream Publisher - Example#377
Asynchronous Stream Publisher - Example#377ttulka wants to merge 7 commits intoreactive-streams:masterfrom
Conversation
Based on Supplier, for the case you don't know the count of elements (for example streaming from a file, etc). Publisher is controlled by the null value - enf of stream.
| // we execute it asynchronously, this is to avoid executing the user code (`Iterable.iterator`) on the calling thread. | ||
| // It also makes it easier to follow rule 1.9 | ||
| private void doSubscribe() { | ||
| if (!cancelled) { |
There was a problem hiding this comment.
cancelled being true here is impossible because the only way to get that true is by submitting a Subscription to the Subscriber that happens once and under this condition.
| try { | ||
| subscriber.onSubscribe(this); | ||
| } catch (final Throwable t) { // Due diligence to obey 2.13 | ||
| terminateDueTo(new IllegalStateException(subscriber + " violated the Reactive Streams rule 2.13 by throwing an exception from onSubscribe.", t)); |
There was a problem hiding this comment.
The problem with crashing onXXX is that you may end up in an undefined state where even calling onError fails.
cancelled being true here is impossible because the only way to get that true is by submitting a Subscription to the Subscriber that happens once and under this condition.
The problem with crashing onXXX is that you may end up in an undefined state where even calling onError fails.
|
Hi @ttulka! For my understanding, does this solve a use-case that the Iterable-based example doesn't? |
|
Hi @viktorklang |
|
@ttulka Ah, ok, couldn't that be solved by doing a read-ahead of 1 element? (So when the def supplier2Iterable[T](s: Supplier[T]): Iterable[T] = new Iterable[T] {
override def iterator(): Iterator[T] = new Iterator[T] {
private[this] var elem = s.get()
override def hasNext(): Boolean = elem != null
override def next(): T =
if (elem == null) Iterator.empty.next()
else {
val prev = elem
elem = s.get()
prev
}
}} |
|
@viktorklang This makes definitely sense, actually it is the same logic I put into the iterable publisher. |
Based on Supplier, for the case you don't know the count of elements
(for example streaming from a file, etc).
Publisher is controlled by the null value - enf of stream.