diff --git a/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts b/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts index 9f6e1e905f81cd6c5cf903008627727ccd52c3d6..e6b5257d8445fa962c914da3aa9a559857769fdf 100644 --- a/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts +++ b/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts @@ -108,6 +108,10 @@ export class CreateCommentComponent implements OnInit, AfterViewInit { this.dialogRef.close(comment); } this.isSendingToSpacy = false; + }, () => { + comment.language = CommentLanguage.auto; + this.dialogRef.close(comment); + this.isSendingToSpacy = false; }); } diff --git a/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts b/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts index cdf1392dd20bc280390610aa1f5c7ba42847b9d1..dd6c33c9308826aa764fcdae8a24ba0b1ebf5dd1 100644 --- a/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts +++ b/src/app/components/shared/_dialogs/spacy-dialog/spacy-dialog.component.ts @@ -94,6 +94,7 @@ export class SpacyDialogComponent implements OnInit, AfterContentInit { this.keywords = []; this.keywordsOriginal = []; this.hasKeywordsFromSpacy = false; + this.isLoading = false; }, () => { this.isLoading = false; }); diff --git a/src/app/services/http/base-http.service.ts b/src/app/services/http/base-http.service.ts index 9bb0703a3cb527b6401af73f63262fe91cc1a9dc..6da60736a0b3cadf74868321c75e9afc58a24647 100644 --- a/src/app/services/http/base-http.service.ts +++ b/src/app/services/http/base-http.service.ts @@ -1,16 +1,29 @@ import { Injectable } from '@angular/core'; -import { of , Observable, throwError } from 'rxjs'; +import { Observable, throwError, TimeoutError } from 'rxjs'; @Injectable() export class BaseHttpService { + private nextRequest = 0; + constructor() { } public handleError<T>(operation = 'operation', result?: T) { return (error: any): Observable<T> => { + if (error instanceof TimeoutError) { + this.nextRequest = new Date().getTime() + 1_000; + } console.error(error); return throwError(error); }; } + + protected checkCanSendRequest(operation = 'operation'): Observable<any> { + if (new Date().getTime() < this.nextRequest) { + console.error(operation + ' is in timeout'); + return throwError(new TimeoutError()); + } + return null; + } } diff --git a/src/app/services/http/languagetool.service.ts b/src/app/services/http/languagetool.service.ts index b7c066e32f789c77f72c687ebea2be75dbdbdbf6..8926ec14c52adb99fc7dea85ee57d1e390d1d48a 100644 --- a/src/app/services/http/languagetool.service.ts +++ b/src/app/services/http/languagetool.service.ts @@ -1,18 +1,18 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { BaseHttpService } from './base-http.service'; -import { catchError } from 'rxjs/operators'; +import { catchError, tap, timeout } from 'rxjs/operators'; import { Observable } from 'rxjs'; import { CURRENT_SUPPORTED_LANGUAGES, Model } from './spacy.interface'; -export type Language = 'de' | 'de-AT' | 'de-CH' | 'de-DE' | - 'en' | 'en-AU' | 'en-CA' | 'en-GB' | 'en-US' | - 'fr' | - 'es' | - 'it' | - 'nl' | 'nl-BE' | - 'pt' | 'pt-BR' | 'pt-PT' | - 'auto'; +export type Language = 'de' | 'de-AT' | 'de-CH' | 'de-DE' | + 'en' | 'en-AU' | 'en-CA' | 'en-GB' | 'en-US' | + 'fr' | + 'es' | + 'it' | + 'nl' | 'nl-BE' | + 'pt' | 'pt-BR' | 'pt-PT' | + 'auto'; export interface LanguagetoolResult { software: { @@ -96,7 +96,7 @@ export class LanguagetoolService extends BaseHttpService { case 'es': return 'es'; case 'fr': - return 'fr'; + return 'fr'; case 'it': return 'it'; case 'nl': @@ -117,12 +117,15 @@ export class LanguagetoolService extends BaseHttpService { checkSpellings(text: string, language: Language): Observable<LanguagetoolResult> { const url = '/languagetool'; - return this.http.get<LanguagetoolResult>(url, { - params: { - text, language - } - }).pipe( - catchError(this.handleError<any>('checkSpellings')) - ); + return this.checkCanSendRequest('checkSpellings') || this.http + .get<LanguagetoolResult>(url, { + params: { + text, language + } + }).pipe( + tap(_ => ''), + timeout(500), + catchError(this.handleError<any>('checkSpellings')) + ); } } diff --git a/src/app/services/http/spacy.service.ts b/src/app/services/http/spacy.service.ts index 5b5e155fc38f685b7e0a7978b044edd1122fe05e..7b2d776aec96ed8923fbd3ea8f40852106dd050f 100644 --- a/src/app/services/http/spacy.service.ts +++ b/src/app/services/http/spacy.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; import { BaseHttpService } from './base-http.service'; -import { catchError, map, tap } from 'rxjs/operators'; +import { catchError, map, tap, timeout } from 'rxjs/operators'; import { CreateCommentKeywords } from '../../utils/create-comment-keywords'; import { DEFAULT_NOUN_LABELS, Model } from './spacy.interface'; @@ -50,9 +50,11 @@ export class SpacyService extends BaseHttpService { getKeywords(text: string, model: Model): Observable<SpacyKeyword[]> { const url = '/spacy'; - return this.http.post<KeywordList>(url, { text, model }, httpOptions) + return this.checkCanSendRequest('getKeywords') || this.http + .post<KeywordList>(url, { text, model }, httpOptions) .pipe( tap(_ => ''), + timeout(500), catchError(this.handleError<any>('getKeywords')), map((elem: KeywordList) => { const keywordsMap = new Map<string, { lemma: string; dep: Set<string> }>(); diff --git a/src/app/utils/grammar-checker.ts b/src/app/utils/grammar-checker.ts index 50ea034c5d84ef2d6f182aaec894517baef635b8..760638023e4db051dedac29ea8e64db6943bb575 100644 --- a/src/app/utils/grammar-checker.ts +++ b/src/app/utils/grammar-checker.ts @@ -114,7 +114,9 @@ export class GrammarChecker { commentBody.prepend(unfilteredText.slice(0, lastFound)); } }); - }, () => '', () => { + }, () => { + this.isSpellchecking = false; + }, () => { this.isSpellchecking = false; }); }