Should I manually call unsubscribe() on NgbModal @Output() EventEmitter subscription?

263 Views Asked by At

I'm opening ng-bootstrap's NgbModal programmatically and subscribing to some @Output() EventEmitter. I'm wondering if I should manually call unsubscribe() on each subscription or if that's handled somehow by Angular / NgBootstrap already?

What I'm currently doing is this:

const modalRef = this.modalService.open(SomeModalComponent);
let opened = true;

modalRef.componentInstance.someOutputEvent.pipe(
    takeWhile(() => opened),
)
    .subscribe(_ => {
        // do something
    });

modalRef.result.then(_ => {
    opened = false;
}, _ => {
    opened = false;
});

I'm now wondering if that actually causes a leaking subscription. Does the takeWhile() actually do something here since the EventEmitter would probably never fire once the modal is closed... ?

Should I do this instead?

const modalRef = this.modalService.open(SomeModalComponent, { size: 'lg' });
let opened = true;

const mySubscription = modalRef.componentInstance.someOutputEvent
    .subscribe(_ => {
        // do something
    });

modalRef.result.then(_ => {
    opened = false;
    mySubscription.unsubscribe();
}, _ => {
    opened = false;
    mySubscription.unsubscribe();
});
0

There are 0 best solutions below