Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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));
});

}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1381,9 +1384,9 @@ export class AppComponent implements OnInit {
}

this.successBackend = 'Data Loaded!';
setInterval(() => {
AppComponent.intervalIDs.push(setInterval(() => {
this.successBackend = null;
}, 5000);
}, 5000));

}

Expand All @@ -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));
});
}

Expand Down Expand Up @@ -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));
});
}
}
Expand Down Expand Up @@ -1954,9 +1957,9 @@ export class AppComponent implements OnInit {
this.clearWallet();
this.loadAddressesAndDexData();

setTimeout(() => {
AppComponent.timeoutIDs.push(setTimeout(() => {
this.update();
}, 10000);
}, 10000));

}

Expand Down Expand Up @@ -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));
}

}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2158,10 +2161,10 @@ export class AppComponent implements OnInit {
this.loadAddressesAndDexData();
}

setTimeout(() => {
AppComponent.timeoutIDs.push(setTimeout(() => {
/** after 5 seconds update */
this.update();
}, 5000);
}, 5000));

}

Expand Down Expand Up @@ -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));
}
}
24 changes: 22 additions & 2 deletions src/app/pages/value/value.component.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<any> = new Subject<any>();

@ViewChild('chart') chart: ChartComponent;
public chartOptions: Partial<ChartOptions>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}