Commit 8ae7ee4f authored by Paul Herbert's avatar Paul Herbert

2100 - Breeze ICO updates. Introduced monitor and serialDisposable. Now complete

parent 9c83482d
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxJs/Subscription';
import { FormGroup, Validators, FormBuilder, AbstractControl } from '@angular/forms'; import { FormGroup, Validators, FormBuilder, AbstractControl } from '@angular/forms';
import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import './monitor';
import { AdvancedService } from './advanced.service'; import { AdvancedService } from './advanced.service';
import { LoadingState } from './loadingState'; import { LoadingState } from './loadingState';
import { SerialDisposable } from './serialDisposable';
@Component({ @Component({
selector: 'app-advanced', selector: 'app-advanced',
...@@ -13,9 +14,9 @@ import { LoadingState } from './loadingState'; ...@@ -13,9 +14,9 @@ import { LoadingState } from './loadingState';
}) })
export class AdvancedComponent implements OnInit, OnDestroy { export class AdvancedComponent implements OnInit, OnDestroy {
private addressCount = ""; private addressCount = "";
private extPubKeySubs: Subscription; private extPubKeySubs = new SerialDisposable();
private generateAddressesSubs: Subscription; private generateAddressesSubs = new SerialDisposable();
private resyncSubs: Subscription; private resyncSubs = new SerialDisposable();
private addresses = new Array<string>(); private addresses = new Array<string>();
private resyncActioned = false; private resyncActioned = false;
...@@ -42,47 +43,25 @@ export class AdvancedComponent implements OnInit, OnDestroy { ...@@ -42,47 +43,25 @@ export class AdvancedComponent implements OnInit, OnDestroy {
this.loadExtPubKey(); this.loadExtPubKey();
} }
public generateAddresses() { resync() {
this.addresses = new Array<string>(); this.resyncActioned = true;
this.generateAddressesLoadingState.loading = true;
if (this.generateAddressesSubs) {
this.generateAddressesSubs.unsubscribe();
}
this.generateAddressesSubs = this.advancedService.generateAddresses(Number(this.addressCount))
.subscribe(x => this.onGenerateAddresses(x),
_ => this.generateAddressesLoadingState.errored = true);
}
public resync() {
if (this.resyncSubs) {
this.resyncSubs.unsubscribe();
}
this.resyncLoadingState.loading = this.resyncActioned = true;
const date = new Date(this.resyncDate.year, this.resyncDate.month-1, this.resyncDate.day); const date = new Date(this.resyncDate.year, this.resyncDate.month-1, this.resyncDate.day);
this.resyncSubs = this.advancedService.resyncFromDate(date) this.resyncSubs.disposable = this.advancedService.resyncFromDate(date)
.subscribe(_ => this.onResync(), .monitor(this.resyncLoadingState)
_ => this.resyncLoadingState.errored = true); .subscribe();
}
private loadExtPubKey() {
this.extPubKeyLoadingState.loading = true;
this.extPubKeySubs = this.advancedService.getExtPubKey()
.subscribe(x => this.onExtPubKey(x),
_ => this.extPubKeyLoadingState.errored = true);
} }
private onExtPubKey(key: string) { generateAddresses() {
this.extPubKey = key; this.addresses = [];
this.extPubKeyLoadingState.loading = false; this.generateAddressesSubs.disposable = this.advancedService.generateAddresses(Number(this.addressCount))
.monitor(this.generateAddressesLoadingState)
.subscribe(x => this.addresses = x);
} }
private onGenerateAddresses(addresses: string[]) { private loadExtPubKey() {
this.generateAddressesLoadingState.loading = false; this.extPubKeySubs.disposable = this.advancedService.getExtPubKey()
this.addresses = addresses; .monitor(this.extPubKeyLoadingState)
} .subscribe(x => this.extPubKey = x);
private onResync() {
this.resyncLoadingState.loading = false;
} }
private registerFormControls() { private registerFormControls() {
...@@ -102,20 +81,13 @@ export class AdvancedComponent implements OnInit, OnDestroy { ...@@ -102,20 +81,13 @@ export class AdvancedComponent implements OnInit, OnDestroy {
private setResyncDates() { private setResyncDates() {
const now = new Date(); const now = new Date();
this.maxResyncDate = {year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate()} this.maxResyncDate = { year: now.getFullYear(), month: now.getMonth()+1, day: now.getDate() }
this.minResyncDate = {year: now.getFullYear(), month: 1, day: 1} this.minResyncDate = { year: now.getFullYear(), month: 1, day: 1 }
} }
ngOnDestroy() { ngOnDestroy() {
if (this.extPubKeySubs) { this.extPubKeySubs.dispose();
this.extPubKeySubs.unsubscribe(); this.generateAddressesSubs.dispose();
} this.resyncSubs.dispose();
if (this.generateAddressesSubs) {
this.generateAddressesSubs.unsubscribe();
}
if (this.resyncSubs) {
this.resyncSubs.unsubscribe();
}
} }
} }
import { Observable } from 'rxjs/Observable';
import { LoadingState } from './loadingState';
function monitor<T>(this: Observable<T>, loadingState: LoadingState): Observable<T> {
return Observable.create(observer => {
loadingState.loading = true;
loadingState.errored = false;
const subs = this.subscribe(x => {
loadingState.loading = false;
observer.next(x);
}, e => {
loadingState.loading = false;
loadingState.errored = true;
observer.error(e);
}, () => {
loadingState.loading = false;
observer.complete();
});
return () => subs.unsubscribe();
});
};
Observable.prototype.monitor = monitor;
declare module 'rxjs/Observable' {
interface Observable<T> {
monitor: typeof monitor;
}
}
import { Subscription } from 'rxJs/Subscription';
export class SerialDisposable {
private subscription: Subscription;
set disposable(value: Subscription) {
this.dispose();
this.subscription = value;
}
public dispose() {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = null;
}
}
}
\ No newline at end of file
...@@ -48,8 +48,9 @@ export class HistoryComponent { ...@@ -48,8 +48,9 @@ export class HistoryComponent {
.subscribe( .subscribe(
response => { response => {
if (response.status >= 200 && response.status < 400) { if (response.status >= 200 && response.status < 400) {
if (response.json().transactionsHistory.length > 0) { const json = response.json();
historyResponse = response.json().transactionsHistory; if (json && json.transactionsHistory) {
historyResponse = json.transactionsHistory;
this.getTransactionInfo(historyResponse); this.getTransactionInfo(historyResponse);
} }
} }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
}, },
"types": [ "types": [
"node", "node",
"jasmine" "jasmine",
], ],
"typeRoots": [ "typeRoots": [
"node_modules/@types" "node_modules/@types"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment