Skip to content

Commit

Permalink
Replaced toPromise with lastValueFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmyers committed Dec 19, 2024
1 parent 1897259 commit 1393ca2
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TranslocoService } from '@ngneat/transloco';
import { Canon, VerseRef } from '@sillsdev/scripture';
import { Question } from 'realtime-server/lib/esm/scriptureforge/models/question';
import { fromVerseRef, toVerseRef } from 'realtime-server/lib/esm/scriptureforge/models/verse-ref-data';
import { lastValueFrom } from 'rxjs';
import { CsvService } from 'xforge-common/csv-service.service';
import { DialogService } from 'xforge-common/dialog.service';
import { ExternalUrlService } from 'xforge-common/external-url.service';
Expand Down Expand Up @@ -475,7 +476,7 @@ export class ImportQuestionsDialogComponent extends SubscriptionDisposable imple
ImportQuestionsConfirmationDialogComponent,
data
) as MatDialogRef<ImportQuestionsConfirmationDialogComponent, ImportQuestionsConfirmationDialogResult>;
(await dialogRef.afterClosed().toPromise())!.forEach(
(await lastValueFrom(dialogRef.afterClosed())).forEach(
(checked, index) => (changesToConfirm[index].checked = checked)
);
this.updateSelectAllCheckbox();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { MatDialogConfig, MatDialogRef } from '@angular/material/dialog';
import { TranslocoService } from '@ngneat/transloco';
import { Operation } from 'realtime-server/lib/esm/common/models/project-rights';
import { Question } from 'realtime-server/lib/esm/scriptureforge/models/question';
import { SFProjectDomain, SF_PROJECT_RIGHTS } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights';
import { SF_PROJECT_RIGHTS, SFProjectDomain } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights';
import { fromVerseRef } from 'realtime-server/lib/esm/scriptureforge/models/verse-ref-data';
import { lastValueFrom } from 'rxjs';
import { DialogService } from 'xforge-common/dialog.service';
import { FileType } from 'xforge-common/models/file-offline-data';
import { NoticeService } from 'xforge-common/notice.service';
Expand Down Expand Up @@ -38,7 +39,7 @@ export class QuestionDialogService {
>;
// ENHANCE: Put the audio upload logic into QuestionDialogComponent so we can detect if the upload
// fails and notify the user without discarding the question. For example, see chapter-audio-dialog.component.ts.
const result: QuestionDialogResult | 'close' | undefined = await dialogRef.afterClosed().toPromise();
const result: QuestionDialogResult | 'close' | undefined = await lastValueFrom(dialogRef.afterClosed());
if (result == null || result === 'close') {
return questionDoc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ export class RemoteTranslationEngine implements InteractiveTranslationEngine {

async trainSegment(sourceSegment: string, targetSegment: string, sentenceStart: boolean = true): Promise<void> {
const pairDto: SegmentPairDto = { sourceSegment, targetSegment, sentenceStart };
await this.httpClient
.post(`translation/engines/project:${this.projectId}/actions/trainSegment`, pairDto)
.toPromise();
await lastValueFrom(
this.httpClient.post(`translation/engines/project:${this.projectId}/actions/trainSegment`, pairDto)
);
}

train(): Observable<ProgressStatus> {
Expand All @@ -126,8 +126,8 @@ export class RemoteTranslationEngine implements InteractiveTranslationEngine {
}

async startTraining(): Promise<BuildDto | undefined> {
return this.createBuild(this.projectId)
.pipe(
return lastValueFrom(
this.createBuild(this.projectId).pipe(
catchError(err => {
if (err.status === 404) {
return of(undefined);
Expand All @@ -136,7 +136,7 @@ export class RemoteTranslationEngine implements InteractiveTranslationEngine {
}
})
)
.toPromise();
);
}

listenForTrainingStatus(): Observable<ProgressStatus> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router';
import { Canon } from '@sillsdev/scripture';
import { SFProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/sf-project';
import { SFProjectUserConfig } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-user-config';
import { Observable } from 'rxjs';
import { lastValueFrom, Observable } from 'rxjs';
import { distinctUntilChanged, filter, first, map } from 'rxjs/operators';
import { DataLoadingComponent } from 'xforge-common/data-loading-component';
import { DialogService } from 'xforge-common/dialog.service';
Expand Down Expand Up @@ -138,7 +138,7 @@ export class ProjectComponent extends DataLoadingComponent implements OnInit {

private async navigateToChecking(projectId: string, task: TaskType = 'checking'): Promise<void> {
const defaultCheckingLink: string[] = ['/projects', projectId, task];
const link = await this.resumeCheckingService.checkingLink$.pipe(first()).toPromise();
const link = await lastValueFrom(this.resumeCheckingService.checkingLink$.pipe(first()));

this.router.navigate(link ?? defaultCheckingLink, { replaceUrl: true });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { timer } from 'rxjs';
import { lastValueFrom, timer } from 'rxjs';
import { map, takeWhile } from 'rxjs/operators';

// All times in milliseconds
Expand All @@ -20,7 +20,7 @@ export class PageNotFoundComponent {
);

constructor(readonly router: Router) {
this.progress.toPromise().then(() => {
lastValueFrom(this.progress).then(() => {
this.router.navigateByUrl('/projects');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ import { EditorTabPersistData } from 'realtime-server/lib/esm/scriptureforge/mod
import { Note } from 'realtime-server/lib/esm/scriptureforge/models/note';
import { BIBLICAL_TERM_TAG_ICON, NoteTag } from 'realtime-server/lib/esm/scriptureforge/models/note-tag';
import {
getNoteThreadDocId,
NoteConflictType,
NoteStatus,
NoteThread,
NoteType,
getNoteThreadDocId
NoteType
} from 'realtime-server/lib/esm/scriptureforge/models/note-thread';
import { ParatextUserProfile } from 'realtime-server/lib/esm/scriptureforge/models/paratext-user-profile';
import { SFProjectDomain, SF_PROJECT_RIGHTS } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights';
import { SF_PROJECT_RIGHTS, SFProjectDomain } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights';
import { SFProjectRole } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-role';
import { TextAnchor } from 'realtime-server/lib/esm/scriptureforge/models/text-anchor';
import { TextType } from 'realtime-server/lib/esm/scriptureforge/models/text-data';
Expand All @@ -56,16 +56,17 @@ import { TranslateSource } from 'realtime-server/lib/esm/scriptureforge/models/t
import { fromVerseRef } from 'realtime-server/lib/esm/scriptureforge/models/verse-ref-data';
import { DeltaOperation } from 'rich-text';
import {
BehaviorSubject,
Observable,
Subject,
Subscription,
asyncScheduler,
BehaviorSubject,
combineLatest,
firstValueFrom,
fromEvent,
lastValueFrom,
merge,
Observable,
of,
Subject,
Subscription,
timer
} from 'rxjs';
import { debounceTime, filter, first, map, repeat, retry, switchMap, take, tap, throttleTime } from 'rxjs/operators';
Expand All @@ -87,7 +88,7 @@ import { filterNullish } from 'xforge-common/util/rxjs-util';
import { browserLinks, getLinkHTML, isBlink, issuesEmailTemplate, objectId } from 'xforge-common/utils';
import { XFValidators } from 'xforge-common/xfvalidators';
import { environment } from '../../../environments/environment';
import { NoteThreadDoc, NoteThreadIcon, defaultNoteThreadIcon } from '../../core/models/note-thread-doc';
import { defaultNoteThreadIcon, NoteThreadDoc, NoteThreadIcon } from '../../core/models/note-thread-doc';
import { SFProjectDoc } from '../../core/models/sf-project-doc';
import { SFProjectProfileDoc } from '../../core/models/sf-project-profile-doc';
import { SF_DEFAULT_TRANSLATE_SHARE_ROLE } from '../../core/models/sf-project-role-info';
Expand All @@ -111,15 +112,15 @@ import {
TextComponent
} from '../../shared/text/text.component';
import {
RIGHT_TO_LEFT_MARK,
VERSE_REGEX,
XmlUtils,
canInsertNote,
formatFontSizeToRems,
getUnsupportedTags,
getVerseRefFromSegmentRef,
RIGHT_TO_LEFT_MARK,
threadIdFromMouseEvent,
verseRefFromMouseEvent
VERSE_REGEX,
verseRefFromMouseEvent,
XmlUtils
} from '../../shared/utils';
import { DraftGenerationService } from '../draft-generation/draft-generation.service';
import { EditorHistoryService } from './editor-history/editor-history.service';
Expand Down Expand Up @@ -1551,7 +1552,7 @@ export class EditorComponent extends DataLoadingComponent implements OnDestroy,

const currentVerseRef: VerseRef | undefined = this.commenterSelectedVerseRef;
this.setNoteFabVisibility('hidden');
const result: NoteDialogResult | undefined = await dialogRef.afterClosed().toPromise();
const result: NoteDialogResult | undefined = await lastValueFrom(dialogRef.afterClosed());

if (result != null) {
if (result.noteContent != null || result.status != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OverlayRef } from '@angular/cdk/overlay';
import { ComponentType } from '@angular/cdk/portal';
import { Injectable } from '@angular/core';
import { MatDialog, MatDialogConfig, MatDialogRef } from '@angular/material/dialog';
import { Observable } from 'rxjs';
import { lastValueFrom, Observable } from 'rxjs';
import { hasObjectProp } from '../type-utils';
import {
GenericDialogComponent,
Expand Down Expand Up @@ -43,7 +43,7 @@ export class DialogService {

return {
dialogRef,
result: dialogRef.afterClosed().toPromise()
result: lastValueFrom(dialogRef.afterClosed())
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { lastValueFrom, Observable, Subject } from 'rxjs';
import { filter, take } from 'rxjs/operators';
import { CONSOLE, ConsoleInterface } from './browser-globals';
import { CommandErrorCode, CommandService } from './command.service';
Expand Down Expand Up @@ -96,7 +96,7 @@ export class RetryingRequest<T> {

private async invoke(options: FetchOptions): Promise<T | undefined> {
while (!this.canceled && this.status !== 'complete') {
const online = await this.online$.pipe(take(1)).toPromise();
const online = await lastValueFrom(this.online$.pipe(take(1)));
if (online !== true) {
this.status = 'offline';
await this.uponOnline();
Expand Down Expand Up @@ -124,12 +124,12 @@ export class RetryingRequest<T> {
}

private async uponOnline(): Promise<void> {
await this.online$
.pipe(
await lastValueFrom(
this.online$.pipe(
filter(isOnline => isOnline),
take(1)
)
.toPromise();
);
}

private isNetworkError(error: unknown): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { BehaviorSubject, lastValueFrom, Observable, Subject } from 'rxjs';
import { ConsoleInterface } from './browser-globals';
import { FetchOptions, JsonRpcInvocable, RetryingRequest } from './retrying-request.service';

Expand All @@ -12,7 +12,7 @@ export class TestingRetryingRequestService {
cancel$ = new Subject<void>()
): RetryingRequest<T> {
const invocable = {
onlineInvoke: (_url: string, _method: string, _params: string) => invoke.toPromise()
onlineInvoke: (_url: string, _method: string, _params: string) => lastValueFrom(invoke)
} as JsonRpcInvocable;
const mockConsole = { log: () => {}, error: () => {} } as ConsoleInterface;
return new RetryingRequest<T>(invocable, online, cancel$, {} as FetchOptions, mockConsole);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { escapeRegExp } from 'lodash-es';
import merge from 'lodash-es/merge';
import { User } from 'realtime-server/lib/esm/common/models/user';
import { obj } from 'realtime-server/lib/esm/common/utils/obj-path';
import { combineLatest, from, Observable } from 'rxjs';
import { combineLatest, from, lastValueFrom, Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { environment } from '../environments/environment';
import { AuthService } from './auth.service';
Expand Down Expand Up @@ -116,7 +116,7 @@ export class UserService {
disableClose: isConfirmation,
width: '280px'
}) as MatDialogRef<EditNameDialogComponent, EditNameDialogResult | 'close'>;
const result = await dialogRef.afterClosed().toPromise();
const result = await lastValueFrom(dialogRef.afterClosed());
if (result != null && result !== 'close') {
await currentUserDoc.submitJson0Op(op => {
op.set(u => u.displayName, result.displayName);
Expand Down

0 comments on commit 1393ca2

Please sign in to comment.