Unsubscribe subscription in component destructor to avoid memory leak

This commit is contained in:
Aroooba 2023-02-07 05:13:19 +09:00
parent a67a074eaf
commit d66921938c
No known key found for this signature in database
GPG Key ID: BEF1A97C02FAC9ED

View File

@ -1,7 +1,7 @@
import { Component, OnInit, Input, QueryList, AfterViewInit, ViewChildren } from '@angular/core'; import { Component, OnInit, Input, QueryList, AfterViewInit, ViewChildren } from '@angular/core';
import { Env, StateService } from '../../services/state.service'; import { Env, StateService } from '../../services/state.service';
import { Observable, merge, of } from 'rxjs'; import { Observable, merge, of, Subject } from 'rxjs';
import { tap } from 'rxjs/operators'; import { tap, takeUntil } from 'rxjs/operators';
import { ActivatedRoute } from "@angular/router"; import { ActivatedRoute } from "@angular/router";
import { faqData, restApiDocsData, wsApiDocsData } from './api-docs-data'; import { faqData, restApiDocsData, wsApiDocsData } from './api-docs-data';
import { FaqTemplateDirective } from '../faq-template/faq-template.component'; import { FaqTemplateDirective } from '../faq-template/faq-template.component';
@ -12,6 +12,7 @@ import { FaqTemplateDirective } from '../faq-template/faq-template.component';
styleUrls: ['./api-docs.component.scss'] styleUrls: ['./api-docs.component.scss']
}) })
export class ApiDocsComponent implements OnInit, AfterViewInit { export class ApiDocsComponent implements OnInit, AfterViewInit {
private destroy$: Subject<any> = new Subject<any>();
plainHostname = document.location.hostname; plainHostname = document.location.hostname;
electrsPort = 0; electrsPort = 0;
hostname = document.location.hostname; hostname = document.location.hostname;
@ -82,7 +83,7 @@ export class ApiDocsComponent implements OnInit, AfterViewInit {
this.restDocs = restApiDocsData; this.restDocs = restApiDocsData;
this.wsDocs = wsApiDocsData; this.wsDocs = wsApiDocsData;
this.network$.subscribe((network) => { this.network$.pipe(takeUntil(this.destroy$)).subscribe((network) => {
this.active = (network === 'liquid' || network === 'liquidtestnet') ? 2 : 0; this.active = (network === 'liquid' || network === 'liquidtestnet') ? 2 : 0;
switch( network ) { switch( network ) {
case "": case "":
@ -102,6 +103,8 @@ export class ApiDocsComponent implements OnInit, AfterViewInit {
} }
ngOnDestroy(): void { ngOnDestroy(): void {
this.destroy$.next(true);
this.destroy$.complete();
window.removeEventListener('scroll', this.onDocScroll); window.removeEventListener('scroll', this.onDocScroll);
} }