From d66921938c672493996f0da1232a74686121a71e Mon Sep 17 00:00:00 2001 From: Aroooba Date: Tue, 7 Feb 2023 05:13:19 +0900 Subject: [PATCH] Unsubscribe subscription in component destructor to avoid memory leak --- frontend/src/app/docs/api-docs/api-docs.component.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/docs/api-docs/api-docs.component.ts b/frontend/src/app/docs/api-docs/api-docs.component.ts index 7b78d187b..0bffd5b2a 100644 --- a/frontend/src/app/docs/api-docs/api-docs.component.ts +++ b/frontend/src/app/docs/api-docs/api-docs.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit, Input, QueryList, AfterViewInit, ViewChildren } from '@angular/core'; import { Env, StateService } from '../../services/state.service'; -import { Observable, merge, of } from 'rxjs'; -import { tap } from 'rxjs/operators'; +import { Observable, merge, of, Subject } from 'rxjs'; +import { tap, takeUntil } from 'rxjs/operators'; import { ActivatedRoute } from "@angular/router"; import { faqData, restApiDocsData, wsApiDocsData } from './api-docs-data'; 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'] }) export class ApiDocsComponent implements OnInit, AfterViewInit { + private destroy$: Subject = new Subject(); plainHostname = document.location.hostname; electrsPort = 0; hostname = document.location.hostname; @@ -82,7 +83,7 @@ export class ApiDocsComponent implements OnInit, AfterViewInit { this.restDocs = restApiDocsData; this.wsDocs = wsApiDocsData; - this.network$.subscribe((network) => { + this.network$.pipe(takeUntil(this.destroy$)).subscribe((network) => { this.active = (network === 'liquid' || network === 'liquidtestnet') ? 2 : 0; switch( network ) { case "": @@ -102,6 +103,8 @@ export class ApiDocsComponent implements OnInit, AfterViewInit { } ngOnDestroy(): void { + this.destroy$.next(true); + this.destroy$.complete(); window.removeEventListener('scroll', this.onDocScroll); }