1

I've read this answer which was very detailed but one had thing caught me eyes:

import { MyThingService } from '../my-thing.service';

@Component({
    selector: 'my-thing',
    templateUrl: './my-thing.component.html'
})
export class MyThingComponent implements OnDestroy, OnInit {
    private ngUnsubscribe: Subject = new Subject();

    constructor(
        private myThingService: MyThingService,
    ) { }

    ngOnInit() {
        this.myThingService.getThings()
            .takeUntil(this.ngUnsubscribe)
            .subscribe(things => console.log(things));

        /* if using lettable operators in rxjs ^5.5.0
        this.myThingService.getThings()
            .pipe(takeUntil(this.ngUnsubscribe))
            .subscribe(things => console.log(things));
        */

        this.myThingService.getOtherThings()
            .takeUntil(this.ngUnsubscribe)
            .subscribe(things => console.log(things));

    }

    ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }
}

A subject is created to handle all signals to complete. But looking at the subject itself in ngOnDestroy : why did they also complete the subject ? Won't he be collected when garbage collect ? I can agree about the idea that it is right to complete it but it is not necessary since gc will look at the component and see that subject is not used and not reference any other object

Question

Am I missing something here? Must we still call complete ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 2
    https://stackoverflow.com/questions/44289859/do-i-need-to-complete-a-subject-for-it-to-be-garbage-collected – yurzui Mar 30 '18 at 05:51
  • Thank you. Searched for that kind of answer and didn't find . Good to know that I wasn't mistaking . Complete() is not necessary – Royi Namir Mar 30 '18 at 05:57
  • Possible duplicate of [Do I need to complete a Subject for it to be garbage collected?](https://stackoverflow.com/questions/44289859/do-i-need-to-complete-a-subject-for-it-to-be-garbage-collected) – Pac0 Jul 17 '18 at 08:05
  • I took the liberty of flagging your question as duplicate of the one mentionned in the first comment. I'm also struggling to get a clear answer for this question. – Pac0 Jul 17 '18 at 08:07

0 Answers0