firstValueFrom instead of toPromise()

This commit is contained in:
Edi Cziszter 2022-01-21 11:00:10 +02:00
parent e6779d8a2b
commit df28e3578b
4 changed files with 25 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import * as moment from 'moment';
import { AutoUnsubscribe, IqserEventTarget } from '../utils';
import { ConfirmOptions } from '../misc';
import { ConfirmationDialogService } from './confirmation-dialog.service';
import { firstValueFrom } from 'rxjs';
export interface SaveOptions {
closeAfterSave?: boolean;
@ -23,25 +24,15 @@ export interface SaveOptions {
* (otherwise the save request will be triggered twice).
* */
export abstract class BaseDialogComponent extends AutoUnsubscribe implements OnInit {
form!: FormGroup;
initialFormValue;
protected readonly _dialogService: ConfirmationDialogService = this._injector.get(ConfirmationDialogService);
protected _waitingForConfirmation = false;
form!: FormGroup;
initialFormValue;
constructor(protected readonly _injector: Injector, protected readonly _dialogRef: MatDialogRef<BaseDialogComponent>) {
super();
}
abstract save(options?: SaveOptions): void;
ngOnInit(): void {
this.addSubscription = this._dialogRef.backdropClick().subscribe(() => {
this.close();
});
}
get valid(): boolean {
return this.form.valid;
}
@ -81,6 +72,14 @@ export abstract class BaseDialogComponent extends AutoUnsubscribe implements OnI
return !this.valid || !this.changed;
}
abstract save(options?: SaveOptions): void;
ngOnInit(): void {
this.addSubscription = this._dialogRef.backdropClick().subscribe(() => {
this.close();
});
}
close(): void {
if (this.changed) {
this._openConfirmDialog().then(result => {
@ -98,12 +97,6 @@ export abstract class BaseDialogComponent extends AutoUnsubscribe implements OnI
}
}
protected _openConfirmDialog() {
this._waitingForConfirmation = true;
const dialogRef = this._dialogService.openDialog({ disableConfirm: !this.valid });
return dialogRef.afterClosed().toPromise();
}
@HostListener('window:keydown.Enter', ['$event'])
onEnter(event: KeyboardEvent): void {
const node = (event.target as IqserEventTarget).localName;
@ -118,4 +111,10 @@ export abstract class BaseDialogComponent extends AutoUnsubscribe implements OnI
this.close();
}
}
protected _openConfirmDialog() {
this._waitingForConfirmation = true;
const dialogRef = this._dialogService.openDialog({ disableConfirm: !this.valid });
return firstValueFrom(dialogRef.afterClosed());
}
}

View File

@ -1,7 +1,7 @@
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { TranslateService } from '@ngx-translate/core';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { HelpModeDialogComponent } from './help-mode-dialog/help-mode-dialog.component';
import { HELP_DOCS } from './tokens';
@ -47,11 +47,9 @@ export class HelpModeService {
width: '600px',
});
ref.afterClosed()
.toPromise()
.then(() => {
this._helpModeDialogIsOpened$.next(false);
});
firstValueFrom(ref.afterClosed()).then(() => {
this._helpModeDialogIsOpened$.next(false);
});
return ref;
}

View File

@ -1,5 +1,5 @@
import { Inject, Injectable, InjectionToken, Injector, Optional } from '@angular/core';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { BehaviorSubject, firstValueFrom, Observable, Subject } from 'rxjs';
import { filter, map, startWith, tap } from 'rxjs/operators';
import { IListable } from '../models';
import { GenericService, QueryParam } from '../../services';
@ -57,7 +57,7 @@ export class EntitiesService<E extends IListable, I = E> extends GenericService<
loadAllIfEmpty(modelPath = this._defaultModelPath, queryParams?: List<QueryParam>): Promise<unknown> | void {
if (!this.all.length) {
return this.loadAll(modelPath, queryParams).toPromise();
return firstValueFrom(this.loadAll(modelPath, queryParams));
}
}

View File

@ -1,6 +1,6 @@
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Injectable, InjectionToken, Injector } from '@angular/core';
import { from, of } from 'rxjs';
import { firstValueFrom, from, of } from 'rxjs';
import { LoadingService } from '../loading';
@Injectable({
@ -25,7 +25,7 @@ export class CompositeRouteGuard implements CanActivate {
canActivateResult = of(canActivateResult);
}
const result = await canActivateResult.toPromise();
const result = await firstValueFrom(canActivateResult);
if (!result) {
this._loadingService.stop();
return false;