Skip to content

Commit

Permalink
fix(normalization): use cancelable observables instead of promises
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitMY committed Nov 7, 2023
1 parent 28abe6f commit c50ea70
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
8 changes: 4 additions & 4 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
"firebase-functions": "4.5.0",
"http-errors": "2.0.0",
"node-fetch": "2.6.7",
"openai": "4.15.1"
"openai": "4.16.1"
},
"devDependencies": {
"@firebase/firestore-types": "3.0.0",
"@firebase/rules-unit-testing": "3.0.1",
"@types/http-errors": "2.0.3",
"@types/http-errors": "2.0.4",
"@types/jest": "29.5.7",
"@types/node-fetch": "2.6.8",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"eslint": "8.53.0",
"firebase-functions-test": "3.1.0",
"firebase-tools": "12.8.0",
Expand Down
5 changes: 1 addition & 4 deletions functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"extends": "./tsconfig.spec.json",
"exclude": ["**/*.spec.ts", "**/*.extend-spec.ts"],
"compilerOptions": {
"noImplicitAny": false
}
"exclude": ["**/*.spec.ts", "**/*.extend-spec.ts"]
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"comlink": "4.4.1",
"filesize": "9.0.11",
"firebase": "10.5.2",
"flag-icons": "6.12.0",
"flag-icons": "6.14.0",
"ionicons": "7.2.1",
"leaflet": "1.9.4",
"mp4-muxer": "3.0.1",
Expand Down Expand Up @@ -114,18 +114,18 @@
"@playwright/test": "1.37.1",
"@sign-mt/configuration": "git://github.com/sign/.github.git",
"@trapezedev/project": "7.0.10",
"@types/dom-mediacapture-transform": "0.1.8",
"@types/dom-speech-recognition": "0.0.3",
"@types/dom-mediacapture-transform": "0.1.9",
"@types/dom-speech-recognition": "0.0.4",
"@types/dom-webcodecs": "0.1.5",
"@types/jasmine": "5.1.1",
"@types/jasminewd2": "2.0.12",
"@types/jasminewd2": "2.0.13",
"@types/offscreencanvas": "2019.7.2",
"@types/three": "0.158.1",
"@types/web-app-manifest": "1.0.6",
"@types/webgl2": "0.0.9",
"@types/wicg-file-system-access": "2023.10.2",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"deepmerge": "4.3.1",
"dotenv": "16.3.1",
"eslint": "8.53.0",
Expand Down
16 changes: 8 additions & 8 deletions src/app/modules/translate/translate.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {Injectable} from '@angular/core';
import {LanguageIdentifier} from 'cld3-asm';
import {GoogleAnalyticsService} from '../../core/modules/google-analytics/google-analytics.service';
import {firstValueFrom, Observable} from 'rxjs';
import {TranslationResponse} from '@sign-mt/browsermt';
import {firstValueFrom, from, Observable, switchMap} from 'rxjs';
import {map} from 'rxjs/operators';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {HttpClient} from '@angular/common/http';
import {AppCheck} from '../../core/helpers/app-check/app-check';

const OBSOLETE_LANGUAGE_CODES = {
Expand Down Expand Up @@ -197,17 +196,18 @@ export class TranslationService {
return this.spokenLanguages.includes(correctedCode) ? correctedCode : DEFAULT_SPOKEN_LANGUAGE;
}

async normalizeSpokenLanguageText(language: string, text: string): Promise<string> {
normalizeSpokenLanguageText(language: string, text: string): Observable<string> {
const params = new URLSearchParams();
params.set('lang', language);
params.set('text', text);
const url = 'https://sign.mt/api/text-normalization?' + params.toString();

const appCheckToken = await AppCheck.getToken();
const headers = {'X-AppCheck-Token': appCheckToken};
const appCheckToken$ = from(AppCheck.getToken());

const response = await firstValueFrom(this.http.get<{text: string}>(url, {headers}));
return response.text;
return appCheckToken$.pipe(
switchMap(token => this.http.get<{text: string}>(url, {headers: {'X-AppCheck-Token': token}})),
map(response => response.text)
);
}

translateSpokenToSigned(text: string, spokenLanguage: string, signedLanguage: string): string {
Expand Down
18 changes: 9 additions & 9 deletions src/app/modules/translate/translate.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './translate.actions';
import {TranslationService} from './translate.service';
import {SetVideo, StartCamera, StopVideo} from '../../core/modules/ngxs/store/video/video.actions';
import {EMPTY, Observable} from 'rxjs';
import {EMPTY, filter, Observable} from 'rxjs';
import {PoseViewerSetting} from '../settings/settings.state';
import {tap} from 'rxjs/operators';
import {Capacitor} from '@capacitor/core';
Expand Down Expand Up @@ -187,22 +187,22 @@ export class TranslateState implements NgxsOnInit {
dispatch(ChangeTranslation);
}

@Action(SuggestAlternativeText)
async suggestAlternativeText({patchState, getState}: StateContext<TranslateStateModel>): Promise<void> {
@Action(SuggestAlternativeText, {cancelUncompleted: true})
suggestAlternativeText({patchState, getState}: StateContext<TranslateStateModel>) {
const {spokenToSigned, spokenLanguageText, spokenLanguage, detectedLanguage} = getState();
const trimmedText = spokenLanguageText.trim();
if (!spokenToSigned || !trimmedText || spokenLanguage !== detectedLanguage) {
return;
return EMPTY;
}

if ('navigator' in globalThis && !navigator.onLine) {
return;
return EMPTY;
}

const alternativeText = await this.service.normalizeSpokenLanguageText(spokenLanguage, trimmedText);
if (alternativeText !== trimmedText) {
patchState({normalizedSpokenLanguageText: alternativeText});
}
return this.service.normalizeSpokenLanguageText(spokenLanguage, trimmedText).pipe(
filter(text => text !== trimmedText),
tap(text => patchState({normalizedSpokenLanguageText: text}))
);
}

@Action(SetSignedLanguageVideo)
Expand Down

0 comments on commit c50ea70

Please sign in to comment.