diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 6f0ee5f..875968b 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, ViewChild } from '@angular/core' +import { Component, OnInit, ViewChild, OnDestroy, HostListener } from '@angular/core'; import { Dex } from '@services/dex.service' import { Location } from '@angular/common' import { @@ -226,6 +226,9 @@ export class AppComponent implements OnInit { isIncomeChartOn = true; isIncomeChartOnKey = 'isIncomeChartOnKey'; + static timeoutIDs = []; + static intervalIDs = []; + private setFromPoolPair(id: string): Pool { const pool = new Pool(); @@ -380,10 +383,10 @@ export class AppComponent implements OnInit { if (this.allAddresses().length > 1) { await this.spinner.show(); - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** spinner ends after 15 seconds */ this.spinner.hide(); - }, 30000); + }, 30000)); } this.updateDescription('meta-data.description'); @@ -1029,16 +1032,16 @@ export class AppComponent implements OnInit { this.loggedIn = true; localStorage.setItem(this.loggedInKey, this.loggedInAuth); this.successBackend = 'OK'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.successBackend = null; - }, 3000); + }, 3000)); } }, (error) => { console.log('there was an error sending mutation register', error); this.errorBackend = error.message; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 3000); + }, 3000)); }); } @@ -1333,16 +1336,16 @@ export class AppComponent implements OnInit { }).subscribe((result: any) => { if (result?.data?.updateUser) { this.successBackend = 'User updated'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.successBackend = null; - }, 5000); + }, 5000)); } }, (error) => { console.log('there was an error sending mutation update', error); this.errorBackend = error.message; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 5000); + }, 5000)); }); localStorage.removeItem(this.adressesKey); @@ -1381,9 +1384,9 @@ export class AppComponent implements OnInit { } this.successBackend = 'Data Loaded!'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.successBackend = null; - }, 5000); + }, 5000)); } @@ -1402,22 +1405,22 @@ export class AppComponent implements OnInit { this.newsletter = result?.data?.userByKey.newsletter; this.successBackend = 'Data Loaded!'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.successBackend = null; - }, 5000); + }, 5000)); } else { this.errorBackend = 'No users found'; this.logout(); - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 5000); + }, 5000)); } }, (error) => { this.errorBackend = error.message; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 5000); + }, 5000)); }); } @@ -1493,21 +1496,21 @@ export class AppComponent implements OnInit { this.newsletter = result?.data?.userByKey.newsletter; this.successBackend = 'Data Loaded!'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.successBackend = null; - }, 5000); + }, 5000)); } else { this.errorBackend = 'No users found'; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 5000); + }, 5000)); } }, (error) => { this.errorBackend = error.message; - setInterval(() => { + AppComponent.intervalIDs.push(setInterval(() => { this.errorBackend = null; - }, 5000); + }, 5000)); }); } } @@ -1954,9 +1957,9 @@ export class AppComponent implements OnInit { this.clearWallet(); this.loadAddressesAndDexData(); - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { this.update(); - }, 10000); + }, 10000)); } @@ -1993,10 +1996,10 @@ export class AppComponent implements OnInit { this.mamonKey = ''; - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** dialog ends after 5 seconds */ this.showDialogAddressesAdded = false; - }, 5000); + }, 5000)); } } @@ -2012,26 +2015,26 @@ export class AppComponent implements OnInit { if (!this.adress) { this.showDialogAddressesNotAdded = true; - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** spinner ends after 5 seconds */ this.showDialogAddressesNotAdded = false; - }, 5000); + }, 5000)); return; } if (this.masternodeFreezer5 && this.masternodeFreezer10) { this.showDialogAddressesNotAdded = true; - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** spinner ends after 5 seconds */ this.showDialogAddressesNotAdded = false; - }, 5000); + }, 5000)); return; } else if ((this.masternodeFreezer5 && !this.masternodeAdress) || (this.masternodeFreezer10 && !this.masternodeAdress)) { this.showDialogAddressesNotAdded = true; - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** spinner ends after 5 seconds */ this.showDialogAddressesNotAdded = false; - }, 5000); + }, 5000)); return; } @@ -2118,11 +2121,11 @@ export class AppComponent implements OnInit { return; } - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** spinner ends after 10 seconds and update */ this.showDialogAddressesAdded = false; this.update(); - }, 10000); + }, 10000)); this.adress = ''; this.masternodeAdress = false; @@ -2158,10 +2161,10 @@ export class AppComponent implements OnInit { this.loadAddressesAndDexData(); } - setTimeout(() => { + AppComponent.timeoutIDs.push(setTimeout(() => { /** after 5 seconds update */ this.update(); - }, 5000); + }, 5000)); } @@ -2541,4 +2544,10 @@ export class AppComponent implements OnInit { closeButton: true, }); } + + @HostListener('unloaded') + public ngOnDestroy(): void { + AppComponent.timeoutIDs.forEach(timeOutID => clearTimeout(timeOutID)); + AppComponent.intervalIDs.forEach(intervalID => clearInterval(intervalID)); + } } diff --git a/src/app/pages/value/value.component.ts b/src/app/pages/value/value.component.ts index bd84ee2..5198f46 100644 --- a/src/app/pages/value/value.component.ts +++ b/src/app/pages/value/value.component.ts @@ -1,4 +1,13 @@ -import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core'; +import { + Component, + Input, + OnChanges, + OnInit, + SimpleChanges, + ViewChild, + OnDestroy, + HostListener, +} from '@angular/core'; import { AddressBalance, Pool, Stats } from '@interfaces/Dex'; import { ChartOptions, @@ -11,11 +20,15 @@ import { ChartComponent } from 'ng-apexcharts'; import { DataService } from '@services/data.service'; import { Dex } from '@services/dex.service'; +import { takeUntil } from "rxjs/operators"; +import { Subject } from "rxjs"; + @Component({ selector: 'app-value', templateUrl: './value.component.html', }) -export class ValueComponent implements OnInit, OnChanges { +export class ValueComponent implements OnInit, OnChanges, OnDestroy { + private destroy$: Subject = new Subject(); @ViewChild('chart') chart: ChartComponent; public chartOptions: Partial; @@ -95,6 +108,7 @@ export class ValueComponent implements OnInit, OnChanges { loadColTokens(): void { this.dexService.getCollateralTokens() + .pipe(takeUntil(this.destroy$)) .subscribe(tokens => { this.collTokens = tokens; this.dusdFactor = +this.collTokens?.data?.find(a => a.token.id === '15').factor; @@ -670,4 +684,10 @@ export class ValueComponent implements OnInit, OnChanges { getTheme(): string { return localStorage.getItem('theme'); } + + @HostListener('unloaded') + public ngOnDestroy(): void { + this.destroy$.next(true); + this.destroy$.complete(); + } }