RxJava extension: favor Observable.defer + ReplaySubject over Observable.create
See original GitHub issueHi @kittinunf 😃
Creating an Observable
using its static built-in create
method could lead to unexpected results, and because of that its usage is generally disallowed.
This method requires advanced knowledge about building operators and data sources; please consider other standard methods first
Extracted from the official docs.
That’s why I would recommend to change the current implementation to use defer
and ReplaySubject
:
private fun <T : Any> Request.rx_response(deserializable: Deserializable<T>): Observable<Pair<Response, T>> =
Observable.defer {
val source = ReplaySubject.create<Pair<Response, T>>()
response(deserializable) { request, response, result ->
when (result) {
is Result.Success -> {
source.onNext(response to result.value)
source.onCompleted()
}
is Result.Failure -> {
source.onError(result.error)
}
}
}
source.doOnSubscribe { this.cancel() }
}
If you agree with this proposal I could make a PR 😃
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
RxJava extension: favor Observable.defer + ReplaySubject ...
It emits to any observer all of the items that were emitted by the source Observable(s). Extracted from the Subject official docs. On...
Read more >Difference between Observable.defer and Observable.create ...
So the distinction seems to be: defer is good when you have something that creates/returns an observable already, but you don't want that ......
Read more >4. Applying Reactive Programming to Existing Applications
The underlying Observable is eager, so we want to postpone its creation. defer() will wait until the last possible moment to actually create...
Read more >Reactive Extensions for the JVM: RxJava Part 2 - Medium
The just method creates an Observable that will emit a predifined sequence of values, supplied on creation, and then terminate. Observable<String> values = ......
Read more >Breaking Changes in Version 7 - RxJS
It was NEVER documented that end users of the library should be creating operators using lift . ... Until the end of v7,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This should be closed then! Thanks @VictorAlbertos so much for helping me out ❤️
Of course 😃 Already done at #138