@@ -2,35 +2,48 @@ import {HttpClient} from '@angular/common/http';
22import { Injectable } from '@angular/core' ;
33import { environment } from 'environments/environment' ;
44import { Configuration } from '../models' ;
5- import { Observable , throwError } from "rxjs" ;
5+ import { Observable , of , throwError } from "rxjs" ;
66import { catchError , map } from "rxjs/operators" ;
77import { Response } from "./visa-response" ;
88
99export function configServiceInitializerFactory ( configurationService : ConfigService ) : ( ) => Observable < Configuration > {
10- // a lambda is required here, otherwise `this` won't work inside ConfigurationService::load
11- return ( ) => configurationService . load ( ) ;
10+ return ( ) => configurationService . configuration$ ( ) ;
1211}
1312
1413@Injectable ( )
1514export class ConfigService {
15+ private _configuration : Configuration ;
1616
1717 constructor ( private http : HttpClient ) {
1818 }
1919
20- // the return value (Promise) of this method is used as an APP_INITIALIZER,
21- // so the application's initialization will not complete until the Promise resolves.
22- public load ( ) : Observable < Configuration > {
20+ configuration$ ( ) : Observable < Configuration > {
21+ if ( this . _configuration != null ) {
22+ return of ( this . _configuration ) ;
23+ } else {
24+ return this . _load ( ) ;
25+ }
26+ }
27+
28+ reload ( ) : Observable < Configuration > {
29+ this . _configuration = null ;
30+ return this . configuration$ ( ) ;
31+ }
32+
33+
34+ private _load ( ) : Observable < Configuration > {
2335 const configurationUrl = `${ environment . paths . api } /configuration` ;
2436
2537 return this . http . get < Response < Configuration > > ( configurationUrl )
2638 . pipe (
2739 map ( ( { data} ) => {
40+ this . _configuration = data ;
2841 return data ;
2942 } ) ,
3043 catchError ( error => {
3144 console . error ( `error loading configuration: ${ JSON . stringify ( error ) } ` ) ;
3245 return throwError ( error ) ;
33- } )
46+ } ) ,
3447 ) ;
3548 }
3649}
0 commit comments