Skip to content

Commit

Permalink
fix(core): remove duplicated load of config BREAKING CHANGE
Browse files Browse the repository at this point in the history
- The behavior is change call the forRoot to call the provideConfig and provideTranslation.
- provideConfigLoader and provideConfigOptions is remove call provideConfig directly
  • Loading branch information
alecarn committed Apr 11, 2024
1 parent 04fd431 commit ae2dd97
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 39 deletions.
7 changes: 5 additions & 2 deletions packages/core/config/src/config.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ModuleWithProviders, NgModule } from '@angular/core';

import { provideConfigLoader, provideConfigOptions } from './config.provider';
import { provideConfig } from './config.provider';

/**
* @deprecated import the provideConfig directly
*/
@NgModule({
imports: [],
declarations: [],
Expand All @@ -11,7 +14,7 @@ export class IgoConfigModule {
static forRoot(): ModuleWithProviders<IgoConfigModule> {
return {
ngModule: IgoConfigModule,
providers: [provideConfigOptions({}), provideConfigLoader()]
providers: [provideConfig({})]
};
}
}
39 changes: 20 additions & 19 deletions packages/core/config/src/config.provider.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { APP_INITIALIZER, InjectionToken } from '@angular/core';
import {
APP_INITIALIZER,
EnvironmentProviders,
InjectionToken,
makeEnvironmentProviders
} from '@angular/core';

import { ConfigOptions } from './config.interface';
import { ConfigService } from './config.service';

export let CONFIG_OPTIONS = new InjectionToken<ConfigOptions>('configOptions');

export function provideConfigOptions(options: ConfigOptions) {
return {
provide: CONFIG_OPTIONS,
useValue: options
};
export function provideConfig(options: ConfigOptions): EnvironmentProviders {
return makeEnvironmentProviders([
{
provide: CONFIG_OPTIONS,
useValue: options
},
{
provide: APP_INITIALIZER,
useFactory: configFactory,
multi: true,
deps: [ConfigService, CONFIG_OPTIONS]
}
]);
}

export function configFactory(
configService: ConfigService,
options: ConfigOptions
) {
function configFactory(configService: ConfigService, options: ConfigOptions) {
return () => configService.load(options);
}

export function provideConfigLoader() {
return {
provide: APP_INITIALIZER,
useFactory: configFactory,
multi: true,
deps: [ConfigService, CONFIG_OPTIONS]
};
}
1 change: 1 addition & 0 deletions packages/core/config/src/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class ConfigService<T extends object = { [key: string]: any }> {
.pipe(
catchError((error: any): any => {
console.log(`Configuration file ${options.path} could not be read`);
this._isLoaded$.next(false);
resolve(true);
return throwError(error.error || 'Server error');
})
Expand Down
17 changes: 13 additions & 4 deletions packages/core/language/src/shared/language.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,35 @@ import {
TranslateModuleConfig
} from '@ngx-translate/core';
import { first } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';

import { LanguageLoaderBase } from './language.interface';
import { LanguageLoader } from './language.loader';
import { LanguageService } from './language.service';
import { IgoMissingTranslationHandler } from './missing-translation.guard';

// 5 seconds
const TIMEOUT_DURATION = 5000;

/**
* Make sure you only call this method in the root module of your application, most of the time called AppModule.
*/
export function provideRootTranslation(
loader?: Provider
): EnvironmentProviders {
export function provideTranslation(loader?: Provider): EnvironmentProviders {
return makeEnvironmentProviders([
importProvidersFrom(TranslateModule.forRoot(setTranslationConfig(loader))),
{
provide: APP_INITIALIZER,
useFactory: (languageService: LanguageService) => () => {
return (
languageService.translate.currentLoader as LanguageLoaderBase
).isLoaded$?.pipe(first((isLoaded) => isLoaded === true));
).isLoaded$?.pipe(
timeout(TIMEOUT_DURATION),
first((isLoaded) => isLoaded === true),
catchError((error) => {
error.message += ` - Request timed out for language loader after: ${TIMEOUT_DURATION}`;
throw error;
})
);
},
deps: [LanguageService],
multi: true
Expand Down
17 changes: 7 additions & 10 deletions packages/core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { HttpClientModule } from '@angular/common/http';
import { ModuleWithProviders, NgModule } from '@angular/core';

import { IgoActivityModule } from '@igo2/core/activity';
import { IgoConfigModule } from '@igo2/core/config';
import { IgoLanguageModule, provideRootTranslation } from '@igo2/core/language';
import { ConfigOptions, provideConfig } from '@igo2/core/config';
import { IgoLanguageModule, provideTranslation } from '@igo2/core/language';
import { IgoMessageModule } from '@igo2/core/message';
import { IgoErrorModule } from '@igo2/core/request';

Expand Down Expand Up @@ -44,29 +44,26 @@ const dbConfig: DBConfig = {
CommonModule,
HttpClientModule,
IgoActivityModule.forRoot(),
IgoConfigModule.forRoot(),
IgoErrorModule.forRoot(),
IgoMessageModule,
NgxIndexedDBModule.forRoot(dbConfig)
],
providers: [provideRootTranslation()],
providers: [],
declarations: [],
exports: [
IgoActivityModule,
IgoConfigModule,
IgoErrorModule,
IgoLanguageModule,
IgoMessageModule
]
})
export class IgoCoreModule {
/**
* @deprecated it has no effect
*/
static forRoot(): ModuleWithProviders<IgoCoreModule> {
static forRoot(
options: ConfigOptions = {}
): ModuleWithProviders<IgoCoreModule> {
return {
ngModule: IgoCoreModule,
providers: []
providers: [provideConfig(options), provideTranslation()]
};
}

Expand Down
8 changes: 4 additions & 4 deletions projects/demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
import { provideAuthentification } from '@igo2/auth';
import { provideIcon } from '@igo2/common';
import { IgoCoreModule } from '@igo2/core';
import { provideConfigOptions } from '@igo2/core/config';
import { provideRootTranslation } from '@igo2/core/language';
import { provideConfig } from '@igo2/core/config';
import { provideTranslation } from '@igo2/core/language';
import {
IgoDirectionsModule,
IgoGeoWorkspaceModule,
Expand Down Expand Up @@ -67,10 +67,10 @@ bootstrapApplication(AppComponent, {
provideHttpClient(withJsonpSupport()),
provideRouter(routes, withPreloading(PreloadAllModules)),
provideAnimations(),
provideConfigOptions({
provideConfig({
default: environment.igo
}),
provideRootTranslation(),
provideTranslation(),
provideAuthentification(),
provideOsrmDirectionsSource(),
provideIChercheSearchSource(),
Expand Down

0 comments on commit ae2dd97

Please sign in to comment.