Skip to main content
deleted 35 characters in body
Source Link
Naren Murali
  • 41.7k
  • 5
  • 37
  • 66

Your code works great, but we need to unsubscribe the subscription, when the component is destroyed. Else it will keep listening for events and gradually pile up during application use, finally causing a memory leak. If you do this, there is no need for first operator.

As a best practice always add your subscribes to a subscription and unsubscribe on component destroy.

private sub = new Subscription();

this.sub.add(
 this.eventService.get(ProductDetailsPageEvent)
  .pipe(
    takeUntil(this.destroy$),
    switchMap((event) =>
      combineLatest([
        of(event),
        this.getCurrentProduct(),
        this.getCurrentPage(),
      ])
    ),
  ).subscribe(([event, page, product]) =>{
      track(product,page,event);
    }
  )
);
...

...
ngOnDestroy() { 
    this.sub.unsubscribe();
}

Your code works great, but we need to unsubscribe the subscription, when the component is destroyed. Else it will keep listening for events and gradually pile up during application use, finally causing a memory leak. If you do this, there is no need for first operator.

As a best practice always add your subscribes to a subscription and unsubscribe on component destroy.

private sub = new Subscription();

this.sub.add(
 this.eventService.get(ProductDetailsPageEvent)
  .pipe(
    takeUntil(this.destroy$),
    switchMap((event) =>
      combineLatest([
        of(event),
        this.getCurrentProduct(),
        this.getCurrentPage(),
      ])
    ),
  ).subscribe(([event, page, product]) =>{
      track(product,page,event);
    }
  )
);
...

...
ngOnDestroy() { 
    this.sub.unsubscribe();
}

Your code works great, but we need to unsubscribe the subscription, when the component is destroyed. Else it will keep listening for events and gradually pile up during application use, finally causing a memory leak. If you do this, there is no need for first operator.

As a best practice always add your subscribes to a subscription and unsubscribe on component destroy.

private sub = new Subscription();

this.sub.add(
 this.eventService.get(ProductDetailsPageEvent)
  .pipe(
    switchMap((event) =>
      combineLatest([
        of(event),
        this.getCurrentProduct(),
        this.getCurrentPage(),
      ])
    ),
  ).subscribe(([event, page, product]) =>{
      track(product,page,event);
    }
  )
);
...

...
ngOnDestroy() { 
    this.sub.unsubscribe();
}
Source Link
Naren Murali
  • 41.7k
  • 5
  • 37
  • 66

Your code works great, but we need to unsubscribe the subscription, when the component is destroyed. Else it will keep listening for events and gradually pile up during application use, finally causing a memory leak. If you do this, there is no need for first operator.

As a best practice always add your subscribes to a subscription and unsubscribe on component destroy.

private sub = new Subscription();

this.sub.add(
 this.eventService.get(ProductDetailsPageEvent)
  .pipe(
    takeUntil(this.destroy$),
    switchMap((event) =>
      combineLatest([
        of(event),
        this.getCurrentProduct(),
        this.getCurrentPage(),
      ])
    ),
  ).subscribe(([event, page, product]) =>{
      track(product,page,event);
    }
  )
);
...

...
ngOnDestroy() { 
    this.sub.unsubscribe();
}