Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First update to happen on the dispatcher pool #42

Open
andreacalia opened this issue Mar 17, 2020 · 2 comments
Open

First update to happen on the dispatcher pool #42

andreacalia opened this issue Mar 17, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@andreacalia
Copy link
Member

At the moment if you subscribe to an Observable with first update option, the Observer#onValue method is called immediately before the #subscribe method finishes. This behaviour is correct but the listener code is run on the calling thread of the subscribe. This is a side effect that the user has to know and can be sometime problematic (maybe subscriptions are started in the constructor of the class in the GUI thread or, even worst, the Observer#onValue throws and, potentially, kills the caller thread).

I was just wondering if we could improve this by executing the first update call on the same dispatcher pool of the Observable so that it is consistent with the, future, updates of the stream of data 🙂 . If we do this, we have to, willingly, block the caller thread until this is done so the correct behaviour mentioned above is kept.

What do you think? @michi42 @kaifox It is not urgent I just wanted to document it on an issue so we don't forget about it hahaha

public Subscription subscribe(Observer<? super T> listener, SubscriptionOption... options) {
Set<SubscriptionOption> optionSet = new HashSet<>(Arrays.asList(options));
if (optionSet.contains(FIRST_UPDATE)) {
Optional.ofNullable(lastValue.get()).ifPresent(listener::onValue);
}
return super.subscribe(listener, options);

@michi42
Copy link
Member

michi42 commented Mar 17, 2020

At the moment, this is exactly the documented behavior:

/**
* On subscription, deliver the actual value (it it exists) as a "first update".
* This happens on the subscribing thread, before any other updates are delivered,
* and before subscribe() returns.
*/
FIRST_UPDATE,

But I'm fine with changing, as long as the guarantee that the update is delivered before subscribe() returns is kept.

Anyway, even if we keep the behavior of dispatching from the subscribing thread, we should at least catch and deflect the exception.

@andreacalia
Copy link
Member Author

Exactly 😄 I think we have to keep the functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants