2

As I'm starting to learn Angular I keep coming across sentences like 'don't forget to destroy the X in ngOnDestroy', where X might be something like a Subscription

In C++ there are smart pointers (probably, I'm 15+ years out of date). Are there any equivalents in Angular/Javascript?

So here

this.numberStream = obNumberStream.subscribe(
....

You would instead have something like numberStreamPtr, you would reference the Subscription functions like numberStreamPtr().XXX, and most importantly, you wouldn't need to call unsubscribe because it would be called automatically when the component destroy was called

tony
  • 2,178
  • 2
  • 23
  • 40

2 Answers2

1

There are a number of approaches mentioned here: https://blog.angularindepth.com/why-you-have-to-unsubscribe-from-observable-92502d5639d0

but if you use the package below then you can just place this decorator on the component for it to happen automagically

@AutoUnsubscribe

https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88

// There is also a simple/standard pattern for Observables here Angular/RxJs When should I unsubscribe from `Subscription`

tony
  • 2,178
  • 2
  • 23
  • 40
0

No, unfortunately there's no equivalent to smart pointers in Javascript, mostly due to it being an interpreted language. Observables are different than using a plain old object as well because they are internally responsible for destroying themselves; calling unsubscribe on an Observable does not immediately destroy it, it just ends the Subscription. Anything else that is still subscribed to it will continue to receive emissions.

However, there are a couple of usage patterns that make unsubscribing a bit easier to manage:

  • Per the Rx contract spec, a cold, finite Observable does not need to be unsubscribed. Cold means that the Observable has only one Subscriber (and is not 'hot'). Finite means the Observable emits one value and then completes. These kinds of Observables handle unsubscribing and destroy themselves one complete. The most common example of this in an Angular app is a call with the HttpClient.
  • Any Observable that issues an OnError or OnComplete notification to its subscribers will also handle the unsubscribe notification, and don't need to be unsubscribed explicitly.
  • Hot Observables with multiple observers and a long lifetime (like Subjects) should be unsubscribed to prevent memory leaks. However, using Angular's async pipe to unwrap emission values automatically internally handles the unsubscribe notification. It's a good idea to try and use it where possible.
  • Any Observables that need manual unsubscription can be handled elegantly with either a base class or a mixin (i.e. a base class that has an array of Subscribers and automatically unsubscribes them in ngOnDestroy, with derived classes adding the subscriptions to that array).

In my experience with Angular, Observables that need manual unsubscriptions are definitely in the minority. I got kind of off topic from your original question, but hopefully this helps you with something.

joh04667
  • 7,159
  • 27
  • 34
  • Observables were just one kind of entity, there's others and I'm on the first day so I'm expecting more – tony Feb 14 '19 at 21:16