From 80e090da860cd00eed6dfad33f2dbe92de2fedc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Lord?= <7397743+pelord@users.noreply.github.com> Date: Thu, 20 Apr 2023 09:25:50 -0400 Subject: [PATCH] fix(core): Missing languageService randomly occur (#1223) * fix(core): Missing languageService randomly occur * wip remove debounceTime * wip --- demo/src/app/app.module.ts | 26 ++++++++++++------- .../lib/language/shared/language.loader.ts | 7 ++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/demo/src/app/app.module.ts b/demo/src/app/app.module.ts index 587ad94422..e3bf60c8c2 100644 --- a/demo/src/app/app.module.ts +++ b/demo/src/app/app.module.ts @@ -1,6 +1,6 @@ import { BrowserModule, DomSanitizer } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; -import { APP_INITIALIZER, NgModule } from '@angular/core'; +import { APP_INITIALIZER, ApplicationRef, Injector, NgModule } from '@angular/core'; import { HammerModule } from '@angular/platform-browser'; import { MatButtonModule } from '@angular/material/button'; import { MatIconRegistry, MatIconModule } from '@angular/material/icon'; @@ -52,6 +52,7 @@ import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { IgoCoreModule, LanguageService } from '@igo2/core'; import { MatTooltipDefaultOptions, MAT_TOOLTIP_DEFAULT_OPTIONS } from '@angular/material/tooltip'; +import { concatMap, first } from 'rxjs'; export const defaultTooltipOptions: MatTooltipDefaultOptions = { showDelay: 500, @@ -118,7 +119,7 @@ export const defaultTooltipOptions: MatTooltipDefaultOptions = { HammerModule ], providers: [ - {provide: APP_INITIALIZER, useFactory: appInitializerFactory, deps: [LanguageService], multi: true}, + { provide: APP_INITIALIZER, useFactory: appInitializerFactory, deps: [Injector, ApplicationRef], multi: true }, { provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: defaultTooltipOptions } ], bootstrap: [AppComponent] @@ -133,14 +134,21 @@ export class AppModule { } } -export function appInitializerFactory(languageService: LanguageService) { +export function appInitializerFactory(injector: Injector, + applicationRef: ApplicationRef) { return () => new Promise((resolve: any) => { - languageService.translate.getTranslation(languageService.getLanguage()).subscribe(() => { - console.info(`Successfully initialized '${languageService.getLanguage()}' language.'`); - }, err => { - console.error(`Problem with '${languageService.getLanguage()}' language initialization.'`); - }, () => { - resolve(null); + applicationRef.isStable.pipe( + first(isStable => isStable === true), + concatMap(() => { + const languageService = injector.get(LanguageService); + const lang = languageService.getLanguage(); + return languageService.translate.getTranslation(lang); + })) + .subscribe((translations) => { + const languageService = injector.get(LanguageService); + const lang = languageService.getLanguage(); + languageService.translate.setTranslation(lang, translations); + resolve(); }); }); } diff --git a/packages/core/src/lib/language/shared/language.loader.ts b/packages/core/src/lib/language/shared/language.loader.ts index 5ef98972b9..8cfcfeb7bc 100644 --- a/packages/core/src/lib/language/shared/language.loader.ts +++ b/packages/core/src/lib/language/shared/language.loader.ts @@ -1,7 +1,6 @@ import { HttpClient } from '@angular/common/http'; -import { combineLatest } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { forkJoin, map } from 'rxjs'; import { TranslateLoader } from '@ngx-translate/core'; @@ -9,8 +8,6 @@ import { ObjectUtils } from '@igo2/utils'; import { ConfigService } from '../../config/config.service'; -declare function require(arg: string): any; - export class LanguageLoader implements TranslateLoader { constructor( private http: HttpClient, @@ -35,7 +32,7 @@ export class LanguageLoader implements TranslateLoader { this.http.get(`${prefix}${lang}${this.suffix}`) ); - const locale$ = combineLatest([igoLocale$, ...appLocale$]); + const locale$ = forkJoin([igoLocale$, ...appLocale$]); return locale$.pipe( map((translations) => {