Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - what is the preferred way to terminate Observables?

From my understanding of Angular and RxJs there are two ways to terminate Observables. You can unsubscribe() from them or use takeUntil() and complete(). Below are examples of each approach (in pseudocode).

The unsubscribe() approach

private _id: number;
private _subscriptions: Subscription[] = [];

constructor(private _route: ActivatedRoute) {
    this._getId();
}

public ngOnDestroy(): void {
    this._subscriptions.forEach(
        subscription => subscription.unsubscribe()
    );
}

private _getId(): void {
    this._subscriptions.push(
        this._route.params.subscribe(params => this._id = +params['id'])
    );
}

The takeUntil() and complete() approach

private _id: number;
private _ngUnsubscribe: Subject<void> = new Subject<void>();

constructor(private _route: ActivatedRoute) {
    this._getId();
}

public ngOnDestroy(): void {
    this._ngUnsubscribe.next();
    this._ngUnsubscribe.complete();
}

private _getId(): void {
    this._route.params.takeUntil(this._ngUnsubscribe).subscribe(
        params => this._id = +params['id']
    );
}

In Angular, is there a preferred way to terminate Observables?

like image 933
ebakunin Avatar asked Jul 26 '17 17:07

ebakunin


People also ask

What are observables in Angular 7?

Angular 7 | Observables Last Updated : 25 Sep, 2020 Observables provide support for data sharing between publishers and subscribers in an angular application. It is referred to as a better technique for event handling, asynchronous programming, and handling multiple values as compared to techniques like promises.

Is there a preferred way to terminate observables?

In Angular, is there a preferred way to terminate Observables? The preferred way is in ngOnDestroy, which you are doing. The only thing I might add is to check that the values are "truthy", meaning not null or not undefined, otherwise you will get an error trying to call unsubscribe ().

How to unsubscribe from an observable in angular?

Just call unsubscribe () to cancel the execution. So you are free to that Observable. You can find more about Angular Observables here. That’s it for this tutorial.

How to terminate observables in RxJS?

From my understanding of Angular and RxJs there are two ways to terminate Observables. You can unsubscribe () from them or use takeUntil () and complete (). Below are examples of each approach (in pseudocode).


1 Answers

Both approaches are correct even though they aren't equivalent.

In my opinion (and experience) using unsubscribe() make usually more sense and is more obvious to other developers that don't have extensive experience with RxJS.

Using takeUntil() is recommended by the lead developer of RxJS 5 (https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87) and is sometimes easier to use than handling multiple subscription objects. For example if you use the partition() operator to split one stream into two it's easier to use just takeUntil(...).partition(...).

However there are two important things:

  1. These two aren't the same. If you use takeUntil() you're completing the Observable chain which means that all complete handles are called followed by tear down functions. On the other hand when you call unsubscribe() only tear down functions are called (including operators such as finally()).

    This is why I think it makes more sense to use unsubscribe(). With takeUntil() you might have a complete handler that is invoked even though you just wanted to unsubscribe (not mentioning that this triggers operators that work with the complete signal such as repeat() that might resubscribe again). The fact you want to unsubscribe doesn't mean that the source Observable completed. You just don't care any more about its values so it's probably better to use unsubscribe() in this case.

    However, in practise it usually doesn't matter whether you complete the chain or just unsubscribe.

  2. You can compose Subscriptions into a single one and unsubscribe all of them at once:

    const subscription = new Subscription();
    
    const sub1 = Observable...subscribe(...);
    const sub2 = Observable...subscribe(...);
    const sub3 = Observable...subscribe(...);
    
    subscription.add(sub1).add(sub2).add(sub3);
    
    ...
    
    subscription.unsubscribe(); // unsubscribes all of them
    
like image 95
martin Avatar answered Oct 25 '22 09:10

martin