ReactiveX Observable return constant until other observable condition met [on hold]
I have two observables called ConnectionProgress and Status. I'm implementing a new observable based on these two with the following logic: while Status value is Connecting, the observable should pass values from ConnectionProgress. When Status changes to anything else, the observable should emit single 0 value and then ignore ConnectionProgress values until Status becomes Connecting again.
I wrote two pieces of code, both of them seems to work. Is there any reason to prefer one of them? Performance-wise maybe? Is there a simpler way? Something to simplify in these solutions?
Approach 1:
Status
.Select(x => x == Status.Connecting)
.DistinctUntilChanged()
.Select(x => x ? ConnectionProgress : Observable.Return(0).Concat(Observable.Never<int>()))
.Switch()
Approach 2:
Observable.CombineLatest(ConnectionProgress, Status,
(progress, status) => new { progress, status })
.Select(x => x.status == Status.Connecting ? x.progress : 0)
.DistinctUntilChanged();
c# system.reactive reactive-programming
put on hold as off-topic by t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have two observables called ConnectionProgress and Status. I'm implementing a new observable based on these two with the following logic: while Status value is Connecting, the observable should pass values from ConnectionProgress. When Status changes to anything else, the observable should emit single 0 value and then ignore ConnectionProgress values until Status becomes Connecting again.
I wrote two pieces of code, both of them seems to work. Is there any reason to prefer one of them? Performance-wise maybe? Is there a simpler way? Something to simplify in these solutions?
Approach 1:
Status
.Select(x => x == Status.Connecting)
.DistinctUntilChanged()
.Select(x => x ? ConnectionProgress : Observable.Return(0).Concat(Observable.Never<int>()))
.Switch()
Approach 2:
Observable.CombineLatest(ConnectionProgress, Status,
(progress, status) => new { progress, status })
.Select(x => x.status == Status.Connecting ? x.progress : 0)
.DistinctUntilChanged();
c# system.reactive reactive-programming
put on hold as off-topic by t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
We cannot review it without knowing whatStatusandConnectionProgressare and your description doesn't clarify that.
– t3chb0t
yesterday
add a comment |
I have two observables called ConnectionProgress and Status. I'm implementing a new observable based on these two with the following logic: while Status value is Connecting, the observable should pass values from ConnectionProgress. When Status changes to anything else, the observable should emit single 0 value and then ignore ConnectionProgress values until Status becomes Connecting again.
I wrote two pieces of code, both of them seems to work. Is there any reason to prefer one of them? Performance-wise maybe? Is there a simpler way? Something to simplify in these solutions?
Approach 1:
Status
.Select(x => x == Status.Connecting)
.DistinctUntilChanged()
.Select(x => x ? ConnectionProgress : Observable.Return(0).Concat(Observable.Never<int>()))
.Switch()
Approach 2:
Observable.CombineLatest(ConnectionProgress, Status,
(progress, status) => new { progress, status })
.Select(x => x.status == Status.Connecting ? x.progress : 0)
.DistinctUntilChanged();
c# system.reactive reactive-programming
I have two observables called ConnectionProgress and Status. I'm implementing a new observable based on these two with the following logic: while Status value is Connecting, the observable should pass values from ConnectionProgress. When Status changes to anything else, the observable should emit single 0 value and then ignore ConnectionProgress values until Status becomes Connecting again.
I wrote two pieces of code, both of them seems to work. Is there any reason to prefer one of them? Performance-wise maybe? Is there a simpler way? Something to simplify in these solutions?
Approach 1:
Status
.Select(x => x == Status.Connecting)
.DistinctUntilChanged()
.Select(x => x ? ConnectionProgress : Observable.Return(0).Concat(Observable.Never<int>()))
.Switch()
Approach 2:
Observable.CombineLatest(ConnectionProgress, Status,
(progress, status) => new { progress, status })
.Select(x => x.status == Status.Connecting ? x.progress : 0)
.DistinctUntilChanged();
c# system.reactive reactive-programming
c# system.reactive reactive-programming
edited 2 days ago
Aleksey Shubin
asked 2 days ago
Aleksey ShubinAleksey Shubin
1244
1244
put on hold as off-topic by t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250 yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Mathias Ettinger, Ludisposed, Toby Speight, Hosch250
If this question can be reworded to fit the rules in the help center, please edit the question.
We cannot review it without knowing whatStatusandConnectionProgressare and your description doesn't clarify that.
– t3chb0t
yesterday
add a comment |
We cannot review it without knowing whatStatusandConnectionProgressare and your description doesn't clarify that.
– t3chb0t
yesterday
We cannot review it without knowing what
Status and ConnectionProgress are and your description doesn't clarify that.– t3chb0t
yesterday
We cannot review it without knowing what
Status and ConnectionProgress are and your description doesn't clarify that.– t3chb0t
yesterday
add a comment |
1 Answer
1
active
oldest
votes
Approach 1: is going to unsubscribe and subscribe ConnectionProgress each time. Depending if it's a hot or cold observable that might be ok but generally not a great idea. Plus if I was looking at this code the Observable.Return(0).Concat(Observable.Never() would cause me to look at it to figure it out for a couple minutes.
Approach 2: CombineLatest will always output whenever either Observable produces a value. So if Connection was disconnected and Progress pushed out a value when Connection gets a connection value it will publish out the last value of Progress at the disconnected state.
If you don't want the CombineLastest feature you can use the MostRecent and Zip
connectionProgress.Zip(status.MostRecent(default), (c, s) => s == Status.Connecting ? c : 0).DistinctUntilChanged();
Or you can see this question about making a Pausable Observable and tweak it to output a value when paused.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Approach 1: is going to unsubscribe and subscribe ConnectionProgress each time. Depending if it's a hot or cold observable that might be ok but generally not a great idea. Plus if I was looking at this code the Observable.Return(0).Concat(Observable.Never() would cause me to look at it to figure it out for a couple minutes.
Approach 2: CombineLatest will always output whenever either Observable produces a value. So if Connection was disconnected and Progress pushed out a value when Connection gets a connection value it will publish out the last value of Progress at the disconnected state.
If you don't want the CombineLastest feature you can use the MostRecent and Zip
connectionProgress.Zip(status.MostRecent(default), (c, s) => s == Status.Connecting ? c : 0).DistinctUntilChanged();
Or you can see this question about making a Pausable Observable and tweak it to output a value when paused.
add a comment |
Approach 1: is going to unsubscribe and subscribe ConnectionProgress each time. Depending if it's a hot or cold observable that might be ok but generally not a great idea. Plus if I was looking at this code the Observable.Return(0).Concat(Observable.Never() would cause me to look at it to figure it out for a couple minutes.
Approach 2: CombineLatest will always output whenever either Observable produces a value. So if Connection was disconnected and Progress pushed out a value when Connection gets a connection value it will publish out the last value of Progress at the disconnected state.
If you don't want the CombineLastest feature you can use the MostRecent and Zip
connectionProgress.Zip(status.MostRecent(default), (c, s) => s == Status.Connecting ? c : 0).DistinctUntilChanged();
Or you can see this question about making a Pausable Observable and tweak it to output a value when paused.
add a comment |
Approach 1: is going to unsubscribe and subscribe ConnectionProgress each time. Depending if it's a hot or cold observable that might be ok but generally not a great idea. Plus if I was looking at this code the Observable.Return(0).Concat(Observable.Never() would cause me to look at it to figure it out for a couple minutes.
Approach 2: CombineLatest will always output whenever either Observable produces a value. So if Connection was disconnected and Progress pushed out a value when Connection gets a connection value it will publish out the last value of Progress at the disconnected state.
If you don't want the CombineLastest feature you can use the MostRecent and Zip
connectionProgress.Zip(status.MostRecent(default), (c, s) => s == Status.Connecting ? c : 0).DistinctUntilChanged();
Or you can see this question about making a Pausable Observable and tweak it to output a value when paused.
Approach 1: is going to unsubscribe and subscribe ConnectionProgress each time. Depending if it's a hot or cold observable that might be ok but generally not a great idea. Plus if I was looking at this code the Observable.Return(0).Concat(Observable.Never() would cause me to look at it to figure it out for a couple minutes.
Approach 2: CombineLatest will always output whenever either Observable produces a value. So if Connection was disconnected and Progress pushed out a value when Connection gets a connection value it will publish out the last value of Progress at the disconnected state.
If you don't want the CombineLastest feature you can use the MostRecent and Zip
connectionProgress.Zip(status.MostRecent(default), (c, s) => s == Status.Connecting ? c : 0).DistinctUntilChanged();
Or you can see this question about making a Pausable Observable and tweak it to output a value when paused.
answered 2 days ago
CharlesNRiceCharlesNRice
1,872512
1,872512
add a comment |
add a comment |
We cannot review it without knowing what
StatusandConnectionProgressare and your description doesn't clarify that.– t3chb0t
yesterday