Managing Subscriptions

in Angular

@edbzn

  1. πŸ‘¨πŸ»β€πŸ« Theory
  2. πŸ‘¨πŸ»β€πŸ’» Examples
  3. πŸ’‘ Best practices

@edbzn

Subscription ? 🀨

@edbzn

It’s an object that holds the Observable execution.

@edbzn

@edbzn

MEMORY LEAKS

 

The component get destroyed

but the subscription lives on.

DEMO

πŸ’‘ Best practices 

@edbzn

βœ–οΈ Avoid nested .subscribe().

@edbzn

βœ”οΈ Consider flattening operators instead.

@edbzn

βœ–οΈ Avoid .subscribe() in constructors.

@edbzn

βœ”οΈ Use lifecycle hooks instead.

@edbzn

βœ–οΈ Avoid logic in .subscribe().

 Having logic in subscribe is usually an indicator that you're not using RxJS to its best potential.

βœ–οΈ Avoid subscription in services.

@edbzn

βœ”οΈ Reduce view subscriptions.

@edbzn

βœ–οΈ Don’t pass streams to components directly.

@edbzn

@edbzn

@edbzn

Resources

@edbzn

Edouard Bozon

Angular, RxJS, NgRx, Node

πŸš΄β€β™‚οΈ β›· 🍻

@edbzn

Managing Subscriptions in Angular

By Edouard Bozon

Managing Subscriptions in Angular

  • 165