Switch to async/await schema for all asynchronous function calls.

This commit is contained in:
Spencer Ofwiti 2021-03-18 12:10:55 +03:00
parent 6dc44be4eb
commit 9ec50e9c1a
10 changed files with 61 additions and 68 deletions

View File

@ -76,11 +76,11 @@ export class AuthService {
const xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1));
xhr.onload = (e) => {
xhr.onload = async (e) => {
if (xhr.status === 401) {
const authHeader = xhr.getResponseHeader('WWW-Authenticate');
const o = hobaParseChallengeHeader(authHeader);
this.loginResponse(o).then();
await this.loginResponse(o);
}
};
xhr.send();

View File

@ -19,8 +19,11 @@ export class AppComponent {
private transactionService: TransactionService,
private loggingService: LoggingService,
) {
this.authService.mutableKeyStore.loadKeyring().then(() => this.authService.getPublicKeys().then());
this.tokenService.getTokens().then(async r => loggingService.sendInfoLevelMessage(await r));
(async () => {
await this.authService.mutableKeyStore.loadKeyring();
await this.authService.getPublicKeys();
this.loggingService.sendInfoLevelMessage(await this.tokenService.getTokens());
})();
this.mediaQuery.addListener(this.onResize);
this.onResize(this.mediaQuery);
}
@ -51,14 +54,14 @@ export class AppComponent {
}
@HostListener('window:cic_transfer', ['$event'])
cicTransfer(event: CustomEvent): void {
async cicTransfer(event: CustomEvent): Promise<void> {
const transaction = event.detail.tx;
this.transactionService.setTransaction(transaction, 100).then();
await this.transactionService.setTransaction(transaction, 100);
}
@HostListener('window:cic_convert', ['$event'])
cicConvert(event: CustomEvent): void {
async cicConvert(event: CustomEvent): Promise<void> {
const conversion = event.detail.tx;
this.transactionService.setConversion(conversion, 100).then();
await this.transactionService.setConversion(conversion, 100);
}
}

View File

@ -22,35 +22,33 @@ export class AuthComponent implements OnInit {
private router: Router
) { }
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this.keyForm = this.formBuilder.group({
key: ['', Validators.required],
});
if (this.authService.privateKey !== undefined) {
this.authService.setKey(this.authService.privateKey).then(r => {
if (this.authService.sessionToken !== undefined) {
const setKey = await this.authService.setKey(this.authService.privateKey);
if (setKey && this.authService.sessionToken !== undefined) {
this.authService.setState(
'Click button to perform login ' + this.authService.sessionLoginCount + ' with token ' + this.authService.sessionToken);
}
});
}
}
get keyFormStub(): any { return this.keyForm.controls; }
onSubmit(): void {
async onSubmit(): Promise<void> {
this.submitted = true;
if (this.keyForm.invalid) { return; }
this.loading = true;
this.authService.setKey(this.keyFormStub.key.value).then();
await this.authService.setKey(this.keyFormStub.key.value);
this.loading = false;
}
login(): void {
const loginStatus = this.authService.login();
console.log(loginStatus);
if (loginStatus) {
this.router.navigate(['/home']);
}

View File

@ -1,6 +1,5 @@
import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';
import {SelectionModel} from '@angular/cdk/collections';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {BlockSyncService, LocationService, LoggingService, TokenService, TransactionService, UserService} from '@app/_services';
@ -81,11 +80,11 @@ export class AccountDetailsComponent implements OnInit {
this.route.paramMap.subscribe(async (params: Params) => {
this.accountAddress = params.get('id');
this.bloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions';
this.userService.getAccountDetailsFromMeta(await User.toKey(this.accountAddress)).pipe(first()).subscribe(res => {
this.userService.getAccountDetailsFromMeta(await User.toKey(this.accountAddress)).pipe(first()).subscribe(async res => {
this.metaAccount = Envelope.fromJSON(JSON.stringify(res.body)).unwrap();
this.account = this.metaAccount.m.data;
this.loggingService.sendInfoLevelMessage(this.account);
this.tokenService.getTokenBalance(this.accountAddress).then(r => this.accountBalance = r);
this.accountBalance = await this.tokenService.getTokenBalance(this.accountAddress);
this.account.vcard = vCard.parse(atob(this.account.vcard));
this.accountInfoForm.patchValue({
name: this.account.vcard?.fn[0].value,
@ -159,10 +158,10 @@ export class AccountDetailsComponent implements OnInit {
get accountInfoFormStub(): any { return this.accountInfoForm.controls; }
saveInfo(): void {
async saveInfo(): Promise<void> {
this.submitted = true;
if (this.accountInfoForm.invalid) { return; }
this.userService.changeAccountInfo(
const accountKey = await this.userService.changeAccountInfo(
this.account.address,
this.accountInfoFormStub.name.value,
this.accountInfoFormStub.phoneNumber.value,
@ -175,7 +174,8 @@ export class AccountDetailsComponent implements OnInit {
this.accountInfoFormStub.location.value,
this.accountInfoFormStub.locationType.value,
this.metaAccount
).then(res => this.loggingService.sendInfoLevelMessage(`Response: ${res}`));
);
this.loggingService.sendInfoLevelMessage(`Response: ${accountKey}`);
this.submitted = false;
}

View File

@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/co
import {MatTableDataSource} from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {UserService} from '@app/_services';
import {LoggingService, UserService} from '@app/_services';
import {Router} from '@angular/router';
@Component({
@ -24,9 +24,16 @@ export class AccountsComponent implements OnInit {
constructor(
private userService: UserService,
private loggingService: LoggingService,
private router: Router
) {
this.userService.loadAccounts(100).then();
(async () => {
try {
await this.userService.loadAccounts(100);
} catch (error) {
this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, {error});
}
})();
}
ngOnInit(): void {
@ -42,8 +49,8 @@ export class AccountsComponent implements OnInit {
this.dataSource.filter = value.trim().toLocaleLowerCase();
}
viewAccount(account): void {
this.router.navigateByUrl(`/accounts/${account.identities.evm['bloxberg:8996']}`).then();
async viewAccount(account): Promise<void> {
await this.router.navigateByUrl(`/accounts/${account.identities.evm['bloxberg:8996']}`);
}
filterAccounts(): void {

View File

@ -1,7 +1,6 @@
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {LocationService, UserService} from '@app/_services';
import {LocationService} from '@app/_services';
import {CustomErrorStateMatcher} from '@app/_helpers';
@Component({
@ -18,8 +17,6 @@ export class CreateAccountComponent implements OnInit {
constructor(
private formBuilder: FormBuilder,
private router: Router,
private userService: UserService,
private locationService: LocationService
) { }
@ -47,21 +44,6 @@ export class CreateAccountComponent implements OnInit {
onSubmit(): void {
this.submitted = true;
if (this.createForm.invalid) { return; }
// this.userService.createAccount(
// this.createFormStub.accountType.value,
// this.createFormStub.idNumber.value,
// this.createFormStub.phoneNumber.value,
// this.createFormStub.givenName.value,
// this.createFormStub.surname.value,
// this.createFormStub.directoryEntry.value,
// this.createFormStub.location.value,
// this.createFormStub.gender.value,
// this.createFormStub.referrer.value,
// this.createFormStub.businessCategory.value,
// ).pipe(first()).subscribe(res => {
// console.log(res);
// });
// this.router.navigateByUrl(`/accounts`);
this.submitted = false;
}

View File

@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/co
import {MatTableDataSource} from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {UserService} from '@app/_services';
import {LoggingService, UserService} from '@app/_services';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {first} from 'rxjs/operators';
@ -28,7 +28,8 @@ export class AdminComponent implements OnInit {
@ViewChild(MatSort) sort: MatSort;
constructor(
private userService: UserService
private userService: UserService,
private loggingService: LoggingService
) {
this.userService.getActions();
this.userService.actionsSubject.subscribe(actions => {
@ -50,12 +51,12 @@ export class AdminComponent implements OnInit {
}
approveAction(action: any): void {
this.userService.approveAction(action.id).pipe(first()).subscribe(res => console.log(res.body));
this.userService.approveAction(action.id).pipe(first()).subscribe(res => this.loggingService.sendInfoLevelMessage(res.body));
this.userService.getActions();
}
revertAction(action: any): void {
this.userService.revokeAction(action.id).pipe(first()).subscribe(res => console.log(res.body));
this.userService.revokeAction(action.id).pipe(first()).subscribe(res => this.loggingService.sendInfoLevelMessage(res.body));
this.userService.getActions();
}

View File

@ -1,7 +1,7 @@
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {Color, Label} from 'ng2-charts';
import {ChartDataSets, ChartOptions, ChartType} from 'chart.js';
import {LocationService, UserService} from '@app/_services';
import {LocationService, LoggingService} from '@app/_services';
import {ArraySum} from '@app/_helpers';
@Component({
@ -149,8 +149,8 @@ export class PagesComponent implements OnInit {
];
constructor(
private userService: UserService,
private locationService: LocationService
private locationService: LocationService,
private loggingService: LoggingService
) {
this.locationService.getLocations();
this.locationService.locationsSubject.subscribe(locations => {
@ -177,11 +177,11 @@ export class PagesComponent implements OnInit {
}
public chartClicked({ event, active}: { event: MouseEvent, active: {}[] }): void {
console.log(event, active);
this.loggingService.sendInfoLevelMessage(`Event: ${event}, ${active}`);
}
public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void {
console.log(event, active);
this.loggingService.sendInfoLevelMessage(`Event: ${event}, ${active}`);
}
public trackByName(index, item): string {

View File

@ -1,7 +1,7 @@
import {ChangeDetectionStrategy, Component, OnInit, ViewChild} from '@angular/core';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {TokenService} from '@app/_services';
import {LoggingService, TokenService} from '@app/_services';
import {MatTableDataSource} from '@angular/material/table';
import {Router} from '@angular/router';
@ -16,15 +16,17 @@ export class TokensComponent implements OnInit {
columnsToDisplay = ['name', 'symbol', 'address', 'supply'];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
tokens: any;
constructor(
private tokenService: TokenService,
private loggingService: LoggingService,
private router: Router
) {
tokenService.getTokens();
}
) { }
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this.tokens = await this.tokenService.getTokens();
this.loggingService.sendInfoLevelMessage(this.tokens);
this.tokenService.tokensSubject.subscribe(tokens => {
this.dataSource = new MatTableDataSource(tokens);
this.dataSource.paginator = this.paginator;
@ -36,7 +38,7 @@ export class TokensComponent implements OnInit {
this.dataSource.filter = value.trim().toLocaleLowerCase();
}
viewToken(token): void {
this.router.navigateByUrl(`/tokens/${token.symbol}`).then();
async viewToken(token): Promise<void> {
await this.router.navigateByUrl(`/tokens/${token.symbol}`);
}
}

View File

@ -19,11 +19,11 @@ export class TransactionDetailsComponent implements OnInit {
this.recipientBloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.transaction?.to + '/transactions';
}
viewSender(): void {
this.router.navigateByUrl(`/accounts/${this.transaction.from}`).then();
async viewSender(): Promise<void> {
await this.router.navigateByUrl(`/accounts/${this.transaction.from}`);
}
viewRecipient(): void {
this.router.navigateByUrl(`/accounts/${this.transaction.to}`).then();
async viewRecipient(): Promise<void> {
await this.router.navigateByUrl(`/accounts/${this.transaction.to}`);
}
}