简体   繁体   中英

Angular 6 Prevent memory leaks multiple Observable in a component using takeUntil

I am following 2nd example from this site: https://www.agiratech.com/how-to-prevent-memory-leaks-in-angular-observables/

I would like to know if I have multiple Observables in a component, do I need to create that many references to the Subject object. I have used unsubscribe and unsubscribe1 variables. Should I be re-using unsubscribe in different methods, or create new Subject instance for each time I subscribe? Code did not throw error in either case.

Here is my code:

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { ProjDetailsService } from '../../services/proj-details.service';


@Component({
    selector: 'app-proj-details',
    templateUrl: './proj-details.component.html',
    styleUrls: ['./proj-details.component.scss']
})
export class ProjDetailsComponent implements OnInit {
    private unsubscribe = new Subject();
    private unsubscribe1 = new Subject();//is this required?

    constructor(public _projDetailsService: ProjDetailsService
    ) {

    }
    ngOnInit() {
        this.LoadApprovalManager();
        this.LoadActiveProjectSubmissions();

    }
    public LoadApprovalManager() {


        this._projDetailsService.GetDefaultMgrGeidData()
            .pipe(takeUntil(this.unsubscribe))
            .subscribe(result => {

            }, error => {
                //this.ErrorMessage('Unable to load search data ' + error.toString());
                //this.SelectedApproverManager = '';
            });

    }

    LoadActiveProjectSubmissions() {
        this._projDetailsService.GetActiveProjectSubmissions()
            .pipe(takeUntil(this.unsubscribe1))
            .subscribe(x => {
                //processing
            }, error => {
               // this.ErrorMessage(error.toString());
            });

    }

    ngOnDestroy() {
        this.unsubscribe.next();
        this.unsubscribe.complete();

        this.unsubscribe1.next();
        this.unsubscribe1.complete();

    }
}

following that example, the answer is no. You would use the same unsubscribe signal for all observables like this:

export class ProjDetailsComponent implements OnInit {
    private unsubscribe = new Subject();

    constructor(public _projDetailsService: ProjDetailsService
    ) {

    }
    ngOnInit() {
        this.LoadApprovalManager();
        this.LoadActiveProjectSubmissions();

    }
    public LoadApprovalManager() {


        this._projDetailsService.GetDefaultMgrGeidData()
            .pipe(takeUntil(this.unsubscribe))
            .subscribe(result => {

            }, error => {
                //this.ErrorMessage('Unable to load search data ' + error.toString());
                //this.SelectedApproverManager = '';
            });

    }

    LoadActiveProjectSubmissions() {
        this._projDetailsService.GetActiveProjectSubmissions()
            .pipe(takeUntil(this.unsubscribe))
            .subscribe(x => {
                //processing
            }, error => {
               // this.ErrorMessage(error.toString());
            });

    }

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

However, all due respect to the folks at that site, I think this method makes no sense whatsoever and increases boiler plate code to absurd levels when you could just do this:

export class ProjDetailsComponent implements OnInit {
    private subs: Subscription[] = [];

    constructor(public _projDetailsService: ProjDetailsService
    ) {

    }
    ngOnInit() {
        this.LoadApprovalManager();
        this.LoadActiveProjectSubmissions();

    }
    public LoadApprovalManager() {


        this.subs.push(this._projDetailsService.GetDefaultMgrGeidData()
            .subscribe(result => {

            }, error => {
                //this.ErrorMessage('Unable to load search data ' + error.toString());
                //this.SelectedApproverManager = '';
            }));

    }

    LoadActiveProjectSubmissions() {
        this.subs.push(this._projDetailsService.GetActiveProjectSubmissions()
            .subscribe(x => {
                //processing
            }, error => {
               // this.ErrorMessage(error.toString());
            }));

    }

    ngOnDestroy() {
        this.subs.forEach(s => s.unsubscribe());

    }
}

simple uniform approach to subscription management that doesn't pollute your observables with extra steps in the pipe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM