Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 wait for all subscriptions

I have a component that is currently set up like this:

export class CompareComponent implements OnInit, OnDestroy {
  criteria: Criteria[]
  products: Product[] = [];

  private organisationId: string

  private criteriaSubscription: Subscription
  private routeSubscription: Subscription
  private productSubscription: Subscription

  constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    private route: ActivatedRoute,

    private attributeMatchService: AttributeMatchService,
    private criteriaService: CriteriaService,
    private optionsService: OptionsService,
    private productService: ProductService,
  ) { }

  ngOnInit() {
    this.organisationId = this.optionsService.get().organisation;
    this.routeSubscription = this.route.paramMap.subscribe(paramMap => {
      let category = paramMap.get('category');
      let gtins = paramMap.get('gtins').split('.');
      this.listCriteria(category);
      this.listProducts(category, gtins);
    });
  }

  ngOnDestroy() {
    if (this.criteriaSubscription) this.criteriaSubscription.unsubscribe();
    if (this.productSubscription) this.productSubscription.unsubscribe();
    if (this.routeSubscription) this.routeSubscription.unsubscribe();
  }

  close() {
    if (isPlatformServer(this.platformId)) return;
    window.history.back();
  }

  private listCriteria(category: string): void {
    this.criteriaSubscription = this.criteriaService.list(category, 'Attributes').subscribe(criteria => this.criteria = criteria);
  }

  private listProducts(category: string, gtins: string[]) {
    gtins.forEach(gtin => {
      this.productSubscription = this.productService.get(category, this.organisationId, parseInt(gtin)).subscribe(products => {
        this.products.push(products[0]);
      });
    });
  }
}

As you can see from the listProducts method, I get a list of products based on the gtins that have been passed as a parameter. What I would like to do is wait for all the subscriptions to finish in listProducts and after they have done, to run some code.

How can I do this?

like image 657
r3plica Avatar asked Mar 05 '23 03:03

r3plica


1 Answers

If you want to wait for all observables, use the forkJoin operator and then flatten the result to push it to your products array.

I'd also suggest to use the takeUntil operator as a cleanup helper for your subscriptions. See how it can be used in my example.

forkJoin(
      gtins.forEach(gtin => this.productService.get(category, this.organisationId, parseInt(gtin)))
    ).pipe(
      takeUntil(this.destroyed$)
    ).subscribe(products => {
      this.products.push(...products.flat())
    });

And in your onDestroy hook

  onDestroy() {
    this.destroyed$.next();
    this.destroyed$.complete();
  }

And in your component, define destroyed$ as

destroyed$: Subject<any> = new Subject<any>();

like image 118
Daniel B Avatar answered Mar 12 '23 01:03

Daniel B