3.x: ConnectableFlowable/ConnetableObservabe redesign #6519
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the connectable API to have a specific
reset
method to reset a terminated connectable source as part of the official API.In 2.x, when
publish()
terminated, it reset itself to a fresh state which could lead to late consumers not receiving events as there might be no one to callconnect()
again (see #6501). However,replay()
did not reset itself, thus late consumers got the cached events, however, a reconnect started the sequence and new consumers may have missed items.In 3.x, this two corner cases have been fixed by the introduction of
reset()
. Bothpublish
andreplay
now remain in their terminated state untilreset
is called. If the connection is disposed, it will automatically reset their state just like before. The state transitions are as follows:connect()
-> running ->onComplete()
/onError()
-> terminated ->reset()
-> freshconnect()
-> running ->dispose()
-> freshconnect()
-> running ->onComplete()
/onError()
-> terminated ->dispose()
-> freshconnect()
-> running ->onComplete()
/onError()
-> terminated ->connect()
-> runningThis does resolve the race condition with
publish().refCount()
described in #6501.In addition, there are some changes to
Flowable.publish()
's behavior:It no longer keeps consuming the upstream if there are no subscribers. This implies if the source terminates while there are unconsumed items in the internal buffer, those will be available for observation.
I have no strong preference on this property and in comparison,
Observable.publish
drops items because there is no backpressure buffer in its implementation.Upstream errors are not reported to the
RxJavaPlugins.onError
handler when if there are no subscribers but have to be observed via a subscriber.Because terminal events are available until
reset
now, we can't know really if there is going to be a subscriber or not. However, it might be possible to detect the no-consumer case upon an error and still report it whenreset
ordispose
is called.Resolves #5628
Resolves #5899