Skip to content

Commit 7f74636

Browse files
committed
Reduce number of calls to /api/configuration.
1 parent 95e1872 commit 7f74636

File tree

11 files changed

+30
-17
lines changed

11 files changed

+30
-17
lines changed

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class AppComponent implements OnInit, OnDestroy {
6464
}
6565

6666
private checkForApplicationVersion(): Observable<Configuration> {
67-
return from(this.configService.load());
67+
return from(this.configService.reload());
6868
}
6969

7070
private isNewVersionAvailable(version: string): boolean {

src/app/core/services/analytics.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class AnalyticsService {
2525
}
2626

2727
public init(): Observable<void> {
28-
return this.configService.load().pipe(
28+
return this.configService.configuration$().pipe(
2929
map(config => {
3030
const analytics = config.analytics;
3131
const {enabled, url, siteId} = analytics;

src/app/core/services/authentication.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class AuthenticationService {
8484
public init(): Observable<void> {
8585
this._removeCookie();
8686

87-
return this._configService.load().pipe(
87+
return this._configService.configuration$().pipe(
8888
map(config => ({
8989
...config.login,
9090
redirectUri: `${window.location.origin}/home`,

src/app/core/services/config.service.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,48 @@ import {HttpClient} from '@angular/common/http';
22
import {Injectable} from '@angular/core';
33
import {environment} from 'environments/environment';
44
import {Configuration} from '../models';
5-
import {Observable, throwError} from "rxjs";
5+
import {Observable, of, throwError} from "rxjs";
66
import {catchError, map} from "rxjs/operators";
77
import {Response} from "./visa-response";
88

99
export 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()
1514
export 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
}

src/app/documentation/documentation.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class DocumentationComponent implements OnInit, OnDestroy {
5858
this.titleService.setTitle(title);
5959
this.analyticsService.trackPageView(title);
6060

61-
this.configService.load()
61+
this.configService.configuration$()
6262
.pipe(takeUntil(this._destroy$))
6363
.subscribe((config) => {
6464
this._contactEmail = config.contactEmail;

src/app/shared/components/dialogs/invalid-account/dialog.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class InvalidAccountDialogComponent implements OnInit, OnDestroy {
2424
}
2525

2626
public ngOnInit(): void {
27-
this._configService.load()
27+
this._configService.configuration$()
2828
.pipe(takeUntil(this._destroy$))
2929
.subscribe((config) => {
3030
this._contactEmail = config.contactEmail;

src/app/shared/components/login/login.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class LoginComponent implements OnInit, OnDestroy {
3232
}
3333

3434
public ngOnInit(): void {
35-
this._configService.load()
35+
this._configService.configuration$()
3636
.pipe(takeUntil(this._destroy$))
3737
.subscribe((config) => {
3838
this.config = config;

src/app/user/home/home.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class HomeComponent implements OnInit, OnDestroy {
122122
this._user = user;
123123
});
124124

125-
this.configService.load()
125+
this.configService.configuration$()
126126
.pipe(takeUntil(this._destroy$))
127127
.subscribe((configuration) => {
128128
this._configuration = configuration;

src/app/user/instance-new/instance-keyboard-layout-select.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class InstanceKeyboardLayoutSelectComponent implements OnInit, OnDestroy
6060
}
6161

6262
ngOnInit(): void {
63-
this._configurationService.load()
63+
this._configurationService.configuration$()
6464
.pipe(takeUntil(this._destroy$))
6565
.subscribe((config) => {
6666
this._layouts = config.desktop.keyboardLayouts;

src/app/user/instance-new/instance-new.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class InstanceNewComponent implements OnInit, OnDestroy, AfterViewChecked
175175
this._user = user;
176176
});
177177

178-
this.configurationService.load()
178+
this.configurationService.configuration$()
179179
.pipe(takeUntil(this._destroy$))
180180
.subscribe((config) => {
181181
this._openDataIncluded = config.experiments.openDataIncluded;

0 commit comments

Comments
 (0)