1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Why is it easy to check .closed on manual RxJS subscriptions, but not with...

Discussão em 'Angular' iniciado por Shaiju T, Maio 15, 2025.

  1. Shaiju T

    Shaiju T Guest

    I am using RxJS for subscriptions in Angular. I noticed that when I manually subscribe to an observable and store the Subscription, I can easily check if it was unsubscribed using .closed, especially in ngOnDestroy().

    But when I use the newer takeUntilDestroyed() utility (from @angular/core/rxjs-interop), I don’t get a Subscription object, so I can’t check if the unsubscription actually happened. This makes it harder to confirm at runtime whether the observable was cleaned up properly. Here’s current example of Old Subscription approach:

    Manual Subscription (with .closed check):

    import { Component, OnDestroy, OnInit } from '@angular/core';
    import { interval, Subscription } from 'rxjs';
    @Component({
    selector: 'app-manual-sub',
    template: `<p>Manual subscription</p>`
    })
    export class ManualSubComponent implements OnInit, OnDestroy {
    subscription!: Subscription;
    ngOnInit(): void {
    this.subscription = interval(1000).subscribe(val => {
    console.log('Manual:', val);
    });
    }
    ngOnDestroy(): void {
    this.subscription.unsubscribe();
    console.log('Unsubscribed?', this.subscription.closed); // Easy to check
    }
    }


    My Question:

    Is there a way to confirm at runtime that takeUntilDestroyed() actually unsubscribed — similar to how we can check .closed on a Subscription? Is there a recommended pattern or function to track this more explicitly when using takeUntilDestroyed() ?

    Thanks in advance :)

    Continue reading...

Compartilhe esta Página