Skip to content

Commit

Permalink
Merge 5cb2fcf into 1281b73
Browse files Browse the repository at this point in the history
  • Loading branch information
anandsinghparihar committed Jan 22, 2018
2 parents 1281b73 + 5cb2fcf commit 99da791
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 86 deletions.
17 changes: 11 additions & 6 deletions src/app/core-ui/main/main-view.component.ts
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { Log } from 'ng2-logger';
import { MatDialog } from '@angular/material';
Expand All @@ -9,7 +9,6 @@ import { environment } from '../../../environments/environment';
import { RpcService } from '../../core/core.module';
import { ModalsService } from '../../modals/modals.module';
import { TransactionService } from '../../wallet/wallet/shared/transaction.service';
import { DaemonConnectionComponent } from '../../modals/shared/daemon-connection/daemon-connection.component';
/*
* The MainView is basically:
* sidebar + router-outlet.
Expand All @@ -21,7 +20,7 @@ import { DaemonConnectionComponent } from '../../modals/shared/daemon-connection
templateUrl: './main-view.component.html',
styleUrls: ['./main-view.component.scss']
})
export class MainViewComponent implements OnInit {
export class MainViewComponent implements OnInit, OnDestroy {
log: any = Log.create('main-view.component');

/* UI States */
Expand All @@ -38,6 +37,7 @@ export class MainViewComponent implements OnInit {
unSubscribeTimer: any;
time: string = '5:00';
public unlocked_until: number = 0;
private destroyed: boolean = false;
constructor(
private _router: Router,
private _route: ActivatedRoute,
Expand Down Expand Up @@ -77,10 +77,12 @@ export class MainViewComponent implements OnInit {

// Updates the error box in the sidenav if wallet is not initialized.
this._rpc.state.observe('ui:walletInitialized')
.subscribe(status => this.walletInitialized = status);
.takeWhile(() => !this.destroyed)
.subscribe(status => this.walletInitialized = status);


this._rpc.state.observe('unlocked_until')
.takeWhile(() => !this.destroyed)
.subscribe(status => {
this.unlocked_until = status;
if (this.unlocked_until > 0) {
Expand All @@ -95,10 +97,13 @@ export class MainViewComponent implements OnInit {
/* versions */
// Obtains the current daemon version
this._rpc.state.observe('subversion')
.subscribe(
subversion => this.daemonVersion = subversion.match(/\d+\.\d+.\d+.\d+/)[0]);
.takeWhile(() => !this.destroyed)
.subscribe(subversion => this.daemonVersion = subversion.match(/\d+\.\d+.\d+.\d+/)[0]);
}

ngOnDestroy() {
this.destroyed = true;
}
/** Open createwallet modal when clicking on error in sidenav */
createWallet() {
this._modals.open('createWallet', {forceOpen: true});
Expand Down
14 changes: 11 additions & 3 deletions src/app/core-ui/main/status/status.component.ts
@@ -1,5 +1,5 @@

import { Component, OnInit } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';
import { Log } from 'ng2-logger';
Expand All @@ -14,12 +14,13 @@ import { ConsoleModalComponent } from './modal/help-modal/console-modal.componen
templateUrl: './status.component.html',
styleUrls: ['./status.component.scss']
})
export class StatusComponent implements OnInit {
export class StatusComponent implements OnInit, OnDestroy {

peerListCount: number = 0;
public coldStakingStatus: boolean;
public encryptionStatus: string = 'Locked';
private _sub: Subscription;
private destroyed: boolean = false;

private log: any = Log.create('status.component');

Expand All @@ -31,16 +32,23 @@ export class StatusComponent implements OnInit {

ngOnInit() {
this._rpc.state.observe('connections')
.takeWhile(() => !this.destroyed)
.subscribe(connections => this.peerListCount = connections);

this._rpc.state.observe('encryptionstatus')
.takeWhile(() => !this.destroyed)
.subscribe(status => this.encryptionStatus = status);

this._rpc.state.observe('ui:coldstaking')
.takeWhile(() => !this.destroyed)
.subscribe(status => this.coldStakingStatus = status);

/* Bug: If you remove this line, then the state of 'txcount' doesn't update in the Transaction.service */
this._rpc.state.observe('txcount').subscribe(txcount => { });
this._rpc.state.observe('txcount').takeWhile(() => !this.destroyed).subscribe(txcount => { });
}

ngOnDestroy() {
this.destroyed = true;
}

getIconNumber(): number {
Expand Down
102 changes: 57 additions & 45 deletions src/app/core/rpc/rpc-state/rpc-state.class.ts
@@ -1,10 +1,11 @@
import { Log } from 'ng2-logger';
import { RpcService } from '../rpc.service';
import { OnDestroy } from '@angular/core';


export class RpcStateClass {
export class RpcStateClass implements OnDestroy {
private log: any = Log.create('rpc-state.class');

private destroyed: boolean = false;
constructor(private _rpc: RpcService) {

// Start polling...
Expand All @@ -20,19 +21,25 @@ export class RpcStateClass {
this.initWalletState();
}

ngOnDestroy() {
this.destroyed = true;
}

private lastBlockTimeState() {
let _checkLastBlock = false;
this._rpc.state.observe('mediantime').subscribe(mediantime => {
const now = new Date().getTime() - (4 * 60 * 1000);
const lastblocktime = new Date(mediantime * 1000);
if (!_checkLastBlock && now > lastblocktime.getTime()) {
_checkLastBlock = true;
setTimeout(() => {
_checkLastBlock = false;
this._rpc.stateCall('getblockchaininfo');
}, 100);
}
});
this._rpc.state.observe('mediantime')
.takeWhile(() => !this.destroyed)
.subscribe(mediantime => {
const now = new Date().getTime() - (4 * 60 * 1000);
const lastblocktime = new Date(mediantime * 1000);
if (!_checkLastBlock && now > lastblocktime.getTime()) {
_checkLastBlock = true;
setTimeout(() => {
_checkLastBlock = false;
this._rpc.stateCall('getblockchaininfo');
}, 100);
}
});
}

private blockLoop() {
Expand All @@ -44,12 +51,13 @@ export class RpcStateClass {

private walletLockedState() {
this._rpc.state.observe('encryptionstatus')
.subscribe(status => {
this._rpc.state
.set('locked', ['Locked', 'Unlocked, staking only'].includes(status));
this._rpc.state
.set('ui:coldstaking:stake', ['Unencrypted', 'Unlocked', 'Unlocked, staking only'].includes(status));
});
.takeWhile(() => !this.destroyed)
.subscribe(status => {
this._rpc.state
.set('locked', ['Locked', 'Unlocked, staking only'].includes(status));
this._rpc.state
.set('ui:coldstaking:stake', ['Unencrypted', 'Unlocked', 'Unlocked, staking only'].includes(status));
});
}

/*
Expand All @@ -58,34 +66,38 @@ export class RpcStateClass {
* update the coldstaking state.
*/
private coldStakeHook() {
this._rpc.state.observe('locked').subscribe(locked => {
// TODO: replace with getcoldstakinginfo.enabled
if (locked === false) {
// only available if unlocked
this._rpc.call('walletsettings', ['changeaddress'])
.subscribe(
// set state for coldstaking
response => this._rpc.state.set('ui:coldstaking',
response.changeaddress === 'default'
? undefined
: !!response.changeaddress.coldstakingaddress
),
error => this.log.er('walletsettings changeaddress', error)
);
}
});
this._rpc.state.observe('locked')
.takeWhile(() => !this.destroyed)
.subscribe(locked => {
// TODO: replace with getcoldstakinginfo.enabled
if (locked === false) {
// only available if unlocked
this._rpc.call('walletsettings', ['changeaddress'])
.subscribe(
// set state for coldstaking
response => this._rpc.state.set('ui:coldstaking',
response.changeaddress === 'default'
? undefined
: !!response.changeaddress.coldstakingaddress
),
error => this.log.er('walletsettings changeaddress', error)
);
}
});
}

private initWalletState() {
this._rpc.state.observe('encryptionstatus').take(1).subscribe(status => {
this._rpc.call('getwalletinfo').subscribe(response => {
// check if account is active
if (!!response.hdmasterkeyid) {
this._rpc.state.set('ui:walletInitialized', true);
} else {
this._rpc.state.set('ui:walletInitialized', false);
}
}, error => this.log.er('RPC Call returned an error', error));
});
this._rpc.state.observe('encryptionstatus')
.takeWhile(() => !this.destroyed)
.subscribe(status => {
this._rpc.call('getwalletinfo').subscribe(response => {
// check if account is active
if (!!response.hdmasterkeyid) {
this._rpc.state.set('ui:walletInitialized', true);
} else {
this._rpc.state.set('ui:walletInitialized', false);
}
}, error => this.log.er('RPC Call returned an error', error));
});
}
}
17 changes: 13 additions & 4 deletions src/app/core/rpc/rpc.service.ts
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, OnDestroy } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Log } from 'ng2-logger';
import { Subject } from 'rxjs/Subject';
Expand Down Expand Up @@ -26,7 +26,7 @@ declare global {
*/

@Injectable()
export class RpcService {
export class RpcService implements OnDestroy {
/**
* IP/URL for daemon (default = localhost)
*/
Expand All @@ -53,6 +53,7 @@ export class RpcService {
public errorsStateCall: Subject<any> = new Subject<any>();

private _rpcState: RpcStateClass;
private destroyed: boolean = false;

constructor(
private _http: HttpClient,
Expand All @@ -64,6 +65,10 @@ export class RpcService {
this.toggleState(true);
}

ngOnDestroy() {
this.destroyed = true;
}

/**
* The call method will perform a single call to the particld daemon and perform a callback to
* the instance through the function as defined in the params.
Expand Down Expand Up @@ -170,8 +175,12 @@ export class RpcService {
// initiate loop
_call();
} else {
this.state.observe('blocks') .subscribe(success => this.stateCall(method, true));
this.state.observe('txcount').subscribe(success => this.stateCall(method, true));
this.state.observe('blocks')
.takeWhile(() => !this.destroyed)
.subscribe(success => this.stateCall(method, true));
this.state.observe('txcount')
.takeWhile(() => !this.destroyed)
.subscribe(success => this.stateCall(method, true));
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/app/modals/createwallet/createwallet.component.ts
@@ -1,4 +1,7 @@
import { Component, Inject, forwardRef, ViewChild, ElementRef, ComponentRef, HostListener } from '@angular/core';
import {
Component, Inject, forwardRef, ViewChild, ElementRef, ComponentRef, HostListener,
OnDestroy
} from '@angular/core';
import { Log } from 'ng2-logger';
import { MatDialogRef } from '@angular/material';

Expand All @@ -20,7 +23,7 @@ import { SnackbarService } from '../../core/snackbar/snackbar.service';
styleUrls: ['./createwallet.component.scss'],
animations: [slideDown()]
})
export class CreateWalletComponent {
export class CreateWalletComponent implements OnDestroy {

log: any = Log.create('createwallet.component');

Expand All @@ -47,6 +50,7 @@ export class CreateWalletComponent {
private validating: boolean = false;

errorString: string = '';
private destroyed: boolean = false;

constructor (
@Inject(forwardRef(() => ModalsService))
Expand All @@ -59,6 +63,10 @@ export class CreateWalletComponent {
this.reset();
}

ngOnDestroy() {
this.destroyed = true;
}

reset(): void {
this._modalsService.enableClose = true;
this.state.set('modal:fullWidth:enableClose', true);
Expand All @@ -69,7 +77,8 @@ export class CreateWalletComponent {
this.passwordVerify = '';
this.errorString = '';
this.step = 0;
this.state.observe('encryptionstatus').take(2)
this.state.observe('encryptionstatus')
.takeWhile(() => !this.destroyed)
.subscribe(status => this.isCrypted = status !== 'Unencrypted');
}

Expand Down
11 changes: 9 additions & 2 deletions src/app/modals/modals.component.ts
Expand Up @@ -7,7 +7,7 @@ import {
ElementRef,
HostListener,
ViewChild,
ViewContainerRef
ViewContainerRef, OnDestroy
} from '@angular/core';
import { Log } from 'ng2-logger'

Expand All @@ -33,7 +33,7 @@ import { StateService } from '../core/core.module';
EncryptwalletComponent
]
})
export class ModalsComponent implements DoCheck, OnInit {
export class ModalsComponent implements DoCheck, OnInit, OnDestroy {

@ViewChild('modalContainer', { read: ViewContainerRef })
private modalContainer: ViewContainerRef;
Expand All @@ -44,6 +44,7 @@ export class ModalsComponent implements DoCheck, OnInit {
enableClose: boolean;
loadSpinner: boolean;
private log: any = Log.create('modals.component');
private destroyed: boolean = false;

constructor(
private _element: ElementRef,
Expand All @@ -54,9 +55,11 @@ export class ModalsComponent implements DoCheck, OnInit {

ngOnInit(): void {
this.state.observe('modal:fullWidth:enableClose')
.takeWhile(() => !this.destroyed)
.subscribe(status => this.enableClose = status);

this.state.observe('ui:spinner')
.takeWhile(() => !this.destroyed)
.subscribe(status => this.loadSpinner = status);
}

Expand All @@ -70,6 +73,10 @@ export class ModalsComponent implements DoCheck, OnInit {
}
}

ngOnDestroy() {
this.destroyed = true;
}

// open modal
open(message: any, data?: any): void {
this.log.d(`open modal ${message.name}` + (data ? ` with data ${data}` : ''));
Expand Down

0 comments on commit 99da791

Please sign in to comment.