-2

SelectManyLatest is necessary operator. But All Internet don't answer how you can write it. So how?

SelectManyLatest is a FlatMapLatest in other Rx Frameworks. This operator like SelectMany but it complete previous subscription if new emit happens.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • Your question should be detailed enough for someone to know what you are talking about based on the question alone without reading your answer. – Ben Voigt Sep 22 '21 at 15:28
  • Could you include in the question the signature of the `SelectManyLatest` operator, and describe its desirable behavior? – Theodor Zoulias Sep 23 '21 at 09:48
  • Are you searching for the [`Switch`](http://reactivex.io/documentation/operators/switch.html) operator? – Theodor Zoulias Sep 24 '21 at 10:16
  • @TheodorZoulias Partially! Switch is a combining operator but SelectManyLatest is transforming operator. I wrote an Answer bellow. It works as it should) – Dmitry iLinikh Sep 24 '21 at 14:20
  • Well, saying that the `SelectManyLatest` operator is transforming, doesn't help me to the slightest to understand what this operator is supposed to do. Ηonestly I think that your question in its current state, is unlikely to be helpful to anyone. – Theodor Zoulias Sep 24 '21 at 15:37
  • @TheodorZoulias The only difference between SelectMany and SelectManyLatest is it disposes previous subscription when new subscription happens. – Dmitry iLinikh Sep 25 '21 at 06:58
  • That's what the `Switch` does. It unsubscribes from the previous inner sequence and subscribes to the next inner sequence. You said that `Switch` is a combining operator, which says nothing to me about why the `Switch` has not the functionality that you want. – Theodor Zoulias Sep 25 '21 at 07:09

1 Answers1

0

Add as an Extension

public static IObservable<V> SelectManyLatest<T, V>(this IObservable<T> source, Func<T, IObservable<V>> selector)
{
    return source
        .Select(selector)
        .Switch();
}
  • This answer subscribes multiple times to the `value` sequence, with is a no-no for general purpose operators, because it can cause inconsistent behavior, depending on whether the sequence is cold or hot. This problem is usually solved easily with the `Publish` operator ([example](https://stackoverflow.com/questions/66710789/observable-getting-latest-value-in-intervals-until-source-finishes/66713533#66713533)). – Theodor Zoulias Sep 24 '21 at 15:29
  • Value will be completed when a source will emit. If you are sure that this is wrong, please write correctly. – Dmitry iLinikh Sep 25 '21 at 07:02
  • Sorry, I can't write anything correctly when the specifications are unclear. – Theodor Zoulias Sep 25 '21 at 07:10
  • @TheodorZoulias Ok, I understood! I used Switch incorrectly and I didn't get desired effect. I will write correctly in an Answer bellow. Thanks! – Dmitry iLinikh Sep 25 '21 at 09:42
  • Oh! I can't add another Answer. I fixed current =) – Dmitry iLinikh Sep 25 '21 at 09:44