src/lib/config/core-init.service.ts
Main initialization service of the eoFrameworkCore module. The load()
function will contain
all steps that needs to be executed before the application starts. Therefore it will be used
as APP_INITIALIZER
from the providers section of the app module.
Later on this service can be used by other components or services to fetch the result of the initialization process (e.g. infos on the current user).
Methods |
|
Public initialize |
initialize()
|
Defined in src/lib/config/core-init.service.ts:35
|
Initializes core module. Executed in context of
Returns :
Promise<boolean>
Promise that resolves after yuuvis® RAD core has been initialized |
import {Inject, Injectable} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {EnaioConfig} from '../service/config/config.interface';
import {Config} from '../service/config/config.service';
import {AuthService} from '../service/auth/auth.service';
import {of as observableOf, forkJoin} from 'rxjs';
import {mergeMap, map, catchError} from 'rxjs/operators';
import {Logger} from '../service/logger/logger';
import {CoreConfig} from './core-config';
import {CORE_CONFIG} from './core-init.tokens';
/**
* Main initialization service of the eoFrameworkCore module. The `load()` function will contain
* all steps that needs to be executed before the application starts. Therefore it will be used
* as `APP_INITIALIZER` from the providers section of the app module.
*
* Later on this service can be used by other components or services to fetch the result of the
* initialization process (e.g. infos on the current user).
*/
@Injectable()
export class CoreInit {
/**
* @ignore
*/
constructor(@Inject(CORE_CONFIG) private coreConfig: CoreConfig, private logger: Logger,
private http: HttpClient, private configService: Config, private authService: AuthService) {
}
/**
* Initializes core module. Executed in context of `APP_INITIALIZER`.
* @returns Promise that resolves after yuuvis<sup>®</sup> RAD core has been initialized
*/
public initialize(): Promise<boolean> {
return new Promise((resolve, reject) => {
/**
* getting a string means that we got an URL to load the config from
*/
let config = !Array.isArray(this.coreConfig.main) ? observableOf([this.coreConfig.main]) :
forkJoin(this.coreConfig.main.map(c => this.http.get(c, {
withCredentials: this.coreConfig.withCredentials
}).pipe(
catchError(e => {
this.logger.error('failed to catch config file', e);
return observableOf({});
})
)));
config.pipe(
map((res) => res.reduce((acc, x) => {
// merge object values on 2nd level
Object.keys(x).forEach(k => !acc[k] || Array.isArray(x[k]) || typeof x[k] !== 'object' ? acc[k] = x[k] : Object.assign(acc[k], x[k]));
return acc;
}, {})
),
mergeMap((res: EnaioConfig) => {
this.configService.set(res);
return this.authService.initUser()
.pipe(catchError(e => observableOf(true)));
})
).subscribe(res => {
resolve(true);
}, err => {
this.logger.error(err);
reject();
});
});
}
}