How to get rid of the internal observable when using the Switch statement

I have a nested IObservable, and I use the switch statement from Rx, which helps me handle previous sequences. But what if I want to dispose of manually? Eliminating the outer sequence is not an option.

_performSearchSubject
.Select(_ => return PerformQuery())
            .Switch()
            .Subscribe(HandleResponseStream, HandleError);

PerformQuery returns IObservable<Result>;

+4
source share
2 answers

After a while I found this myself ... So, the answer is:

TakeUntil(IObservable<TOther>), , , . , , Switch() .

:

Subject<Unit> _cancellationObservable = new Subject<Unit>();

_performSearchSubject
.Select(_ => {
                return PerformQuery().TakeUntil(_cancellationObservable);
              })
            .Switch()
            .Subscribe(HandleResponseStream, HandleError);

, , :

_cancellationObservable.OnNext(Unit.Default);
+4

, "PerformQuery", , :

  • 'Switch' - '_performSearchSubject'. .. "Switch".
  • .

, .Subscribe, . .

? Linqpad :

var inner = Observable.Create<string>((o) =>
{
    o.OnNext("First item of new inner");
    return Disposable.Create(() => "Inner Disposed".Dump());
});

var outer = Observable.Timer(TimeSpan.MinValue, TimeSpan.FromSeconds(10))
        .Select(_ => inner)
        .Switch()
        .Subscribe(output => output.Dump());


Console.ReadLine();

outer.Dispose();

, , ( Console.ReadLine), . , Switch , .

"TakeUntil", , , rerturned, .

0

Source: https://habr.com/ru/post/1625985/


All Articles