0

I have method that is called from template:

public submit(): void {
    const data = { ...this.parcel, ...this.form.value, appid: this.appid };
    const requestEgrnData = {};

    iif(
        () => this.mode === MODES.CREATE,
        this.applicationOrderParcelsRepository
            .create(data)
            .pipe(switchMap(() => (this.cadnum.value ? this.egrnService.SendRequestGetEGRP(requestEgrnData) : of(null)))),
        this.applicationOrderParcelsRepository.update(data),
    )
        .pipe(indicate(this.loading$), observableHandlerResponse(this.messageService))
        .subscribe(() => {
            this.dialogRef.close(true);
        });
}

Should I unsubscribe from this.applicationOrderParcelsRepository and from outer this.egrnService.SendRequestGetEGRP, this.applicationOrderParcelsRepository.update?

Or enough unsubscribe from let subscription = this.applicationOrderParcelsRepository()...?

  • Does this answer your question? [Angular/RxJs When should I unsubscribe from \`Subscription\`](https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription) – frido Oct 22 '20 at 16:24
  • @fridoo The referenced question covers only basic observables and their subscriptions. This question is asking about inner Observables and their subscriptions. So no, this is not a duplicate of that question. – DeborahK Oct 22 '20 at 20:56
  • When I dont need to unsubscribe? –  Oct 23 '20 at 09:26

2 Answers2

2

you should and only can unsubscribe only in places where you've called subscribe() (or in rare cases connect()) explicitly. the dependant "subscriptions" if any, will be auto unsubscribed when you unsubscribe from the main one

Andrei
  • 10,117
  • 13
  • 21
  • In my case it is only `let subscription = this.applicationOrderParcelsRepository()`. `subscription.unsubscribe()` –  Oct 22 '20 at 15:05
  • no. In your case it should be `let subscription = iif(.....).pipe().subscribe(...)` <--- see, here subscribe is explicitly called. this.`.subscribe()` call returns a `Subscription`. if you then `subscription.unsubscribe()`, all the logic in this code will stop – Andrei Oct 22 '20 at 15:10
  • Also form I need unsubscrive from `fromEvent(this.inputHouseSearch.nativeElement, 'keyup').subscribe()`. What if there are some subscription on the page? How to unsubscribe all in destructor? –  Oct 22 '20 at 15:25
  • there are several ways, but there is no elegant easy way to do it. you either save them and unsubscribe, or use the tricks that do almost the same – Andrei Oct 22 '20 at 15:33
2

In your case the entire subscription is const subscription = iif(...) .subscribe(() => { this.dialogRef.close(true);});

so this subscription can be unsubscribed.

Marcin Milewicz
  • 315
  • 1
  • 8