Unsubscribing Strategies with takeUntil
Explain the takeUntil + Subject pattern for reliably unsubscribing from multiple Observables tied to a component's lifecycle.
Manually tracking and unsubscribing every individual Subscription a component creates — storing each one, then calling .unsubscribe() on every single one inside ngOnDestroy — becomes tedious and error-prone once a component subscribes to more than one or two streams, since it's easy to forget one and introduce a memory leak. The takeUntil pattern solves this elegantly: create a single Subject (conventionally named destroy$) that the component calls .next() and .complete() on inside ngOnDestroy, and pipe every subscription in the component through takeUntil(this.destroy$), which automatically unsubscribes each one the moment that signal fires.
It's like a building-wide fire alarm that automatically shuts every individual door and vent the moment it's triggered, rather than needing a security guard to run around manually closing each door in the building one by one.