How do I get rid of a subscription inside a subscription?

There is a component that uses the service to transfer data. After that, depending on his response, another method is called or a modal window is shown and then this method is called. Because of this, there is a double subscription. Here is an example of the code:

    import { Component, OnDestroy, VERSION } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Subject, takeUntil } from 'rxjs';
import { DialogExampleComponent } from './dialog-example/dialog-example.component';
import { SomeServiceService } from './serivce/some-service.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnDestroy {
  destroy: Subject<boolean> = new Subject<boolean>();
  source: string = 'wd';
  food: string;

  constructor(
    private someService: SomeServiceService,
    public dialog: MatDialog
  ) {}

  onExampleMethod() {
    this.someService
      .getData()
      .pipe(takeUntil(this.destroy))
      .subscribe((response: string) => {
        if (this.source === 'aa') {
          this.create();
        } else {
          const dialog = this.dialog.open(DialogExampleComponent, {
            width: '250px',
          });
          dialog
            .afterClosed()
            .pipe(takeUntil(this.destroy))
            .subscribe((dialogData) => {
              this.food = dialogData;
              this.create();
            });
        }
      });
  }

  create(): void {
    console.log('create');
  }

  ngOnDestroy(): void {
    this.destroy.next(true);
    this.destroy.unsubscribe();
  }
}

Ответы (0 шт):