Skip to content

Commit

Permalink
fix(core): Missing languageService randomly occur (#1223)
Browse files Browse the repository at this point in the history
* fix(core): Missing languageService randomly occur

* wip remove debounceTime

* wip
  • Loading branch information
pelord authored Apr 20, 2023
1 parent 7862664 commit 80e090d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
26 changes: 17 additions & 9 deletions demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -133,14 +134,21 @@ export class AppModule {
}
}

export function appInitializerFactory(languageService: LanguageService) {
export function appInitializerFactory(injector: Injector,
applicationRef: ApplicationRef) {
return () => new Promise<any>((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();
});
});
}
7 changes: 2 additions & 5 deletions packages/core/src/lib/language/shared/language.loader.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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';

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,
Expand All @@ -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) => {
Expand Down

0 comments on commit 80e090d

Please sign in to comment.