-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[RFC] Add the "subscribeImmediately" parameter to the ObservableAsPropertyHelper class #969
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
[RFC] Add the "subscribeImmediately" parameter to the ObservableAsPropertyHelper class #969
Conversation
…elper class When this parameter is set to true, the ObservableAsPropertyHelper immediately subscribes to the source sequence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kentcb We can add this line, even if we don't merge this PR, so connect isn't called a second time in Value
|
I know it's a big, scary breaking change and all, but my inclination would be to default |
|
I would agree with that. |
|
The reason I think that delaying the subscription by default is good, is because I think that the constructor of an object shouldn't fire off any work on other threads. Take the following class for example: public class MyViewModel : ReactiveObject
{
private readonly ObservableAsPropertyHelper<int> myProperty;
public MyViewModel()
{
this.myProperty = CalculateAsync().ToProperty(this, x => x.MyProperty);
}
public int MyProperty => this.myProperty.Value;
public IObservable<int> CalculateAsync()
{
// Calculate something expensive here
}
}If |
|
Isn't this a case of catering the esoteric scenario at the expense of the common? I don't think it's a stretch to say that most OAPH usage will simply be massaging existing properties into new ones. And to have those scenarios constantly require that people remember to specify |
|
Yeah, you're probably right that my use case isn't very common. Maybe @paulcbetts also has some input on this? |
|
cc @niik for feels on this change |
|
Doesn't seem to be any further feedback on this and I would ❤️ for this to get into the 7 branch ASAP so people can start banging on it. @flagbug could you please change the argument name to |
|
@kentcb Sure! I'll open a new PR for that change, because I deleted my fork where this PR is based 🙈 |
|
Just did a quick scan of GitHub Desktop's code - we have 66 Not sure how flipping the default behaviour here from lazy to eager will impact us, but I suspect it will. I don't want to hold this up this PR from being merged (I like this being exposed directly, and it's more clearer than using |
|
Isn't scheduling a different issue entirely? The purpose of the |
|
@shiftkey also, allow me to flip the question around a little: what negative consequences could there be from making the default behavior a hot observable? To me, this aligns the feature far more with people's expectations. We've seen that time and again through support questions and (certainly for me anyway) our own experiences using OAPH. The only possible argument I see against making it hot is performance, and I would consider that a poor argument because:
|
|
@kentcb I shall check the history on these changes to understand why we've done the |
I'm probably missing some anecdotes on this, as aside from this
This is true - and I'm not afraid of writing our own extension methods over this to ensure we do it by default.
Perhaps this is the issue at hand. Something definitely necessitated us doing this, which made us feel bad at the time. I'll let you know what I find out.
For most of the usages as I see it we're not talking about expensive operations. So it's gotta be something else. |
|
One common example that just came to my mind is the Android With the new behavior of immediate subscription, all the properties are loaded at once, as soon as the ViewModels for the |
|
@flagbug: interesting. My approach to such a scenario would be to only load the image if the VM is activated. Here are some other examples:
|
|
I've created the new PR at #1061 @kentcb Don't get me wrong, I don't like the current behavior, especially the switch where in unit tests, OAPH immediately subscribes to the source, which makes unit tests behave different than production code. I just wanted to show a case where the current behavior is beneficial, if you have a VM with 10 properties times a list with 100 items, that's at least 1000 calls to |
|
Closing in favor of #1061 |
When this parameter is set to true, the ObservableAsPropertyHelper
immediately subscribes to the source sequence.