Skip to content

Commit

Permalink
Reduce number of calls to /api/configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcaunt committed Dec 18, 2024
1 parent 95e1872 commit 7f74636
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AppComponent implements OnInit, OnDestroy {
}

private checkForApplicationVersion(): Observable<Configuration> {
return from(this.configService.load());
return from(this.configService.reload());
}

private isNewVersionAvailable(version: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AnalyticsService {
}

public init(): Observable<void> {
return this.configService.load().pipe(
return this.configService.configuration$().pipe(
map(config => {
const analytics = config.analytics;
const {enabled, url, siteId} = analytics;
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class AuthenticationService {
public init(): Observable<void> {
this._removeCookie();

return this._configService.load().pipe(
return this._configService.configuration$().pipe(
map(config => ({
...config.login,
redirectUri: `${window.location.origin}/home`,
Expand Down
27 changes: 20 additions & 7 deletions src/app/core/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,48 @@ import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from 'environments/environment';
import {Configuration} from '../models';
import {Observable, throwError} from "rxjs";
import {Observable, of, throwError} from "rxjs";
import {catchError, map} from "rxjs/operators";
import {Response} from "./visa-response";

export function configServiceInitializerFactory(configurationService: ConfigService): () => Observable<Configuration> {
// a lambda is required here, otherwise `this` won't work inside ConfigurationService::load
return () => configurationService.load();
return () => configurationService.configuration$();
}

@Injectable()
export class ConfigService {
private _configuration: Configuration;

constructor(private http: HttpClient) {
}

// the return value (Promise) of this method is used as an APP_INITIALIZER,
// so the application's initialization will not complete until the Promise resolves.
public load(): Observable<Configuration> {
configuration$(): Observable<Configuration> {
if (this._configuration != null) {
return of(this._configuration);
} else {
return this._load();
}
}

reload(): Observable<Configuration> {
this._configuration = null;
return this.configuration$();
}


private _load(): Observable<Configuration> {
const configurationUrl = `${environment.paths.api}/configuration`;

return this.http.get<Response<Configuration>>(configurationUrl)
.pipe(
map(({data}) => {
this._configuration = data;
return data;
}),
catchError(error => {
console.error(`error loading configuration: ${JSON.stringify(error)}`);
return throwError(error);
})
}),
);
}
}
2 changes: 1 addition & 1 deletion src/app/documentation/documentation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class DocumentationComponent implements OnInit, OnDestroy {
this.titleService.setTitle(title);
this.analyticsService.trackPageView(title);

this.configService.load()
this.configService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((config) => {
this._contactEmail = config.contactEmail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class InvalidAccountDialogComponent implements OnInit, OnDestroy {
}

public ngOnInit(): void {
this._configService.load()
this._configService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((config) => {
this._contactEmail = config.contactEmail;
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/components/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LoginComponent implements OnInit, OnDestroy {
}

public ngOnInit(): void {
this._configService.load()
this._configService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((config) => {
this.config = config;
Expand Down
2 changes: 1 addition & 1 deletion src/app/user/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class HomeComponent implements OnInit, OnDestroy {
this._user = user;
});

this.configService.load()
this.configService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((configuration) => {
this._configuration = configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class InstanceKeyboardLayoutSelectComponent implements OnInit, OnDestroy
}

ngOnInit(): void {
this._configurationService.load()
this._configurationService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((config) => {
this._layouts = config.desktop.keyboardLayouts;
Expand Down
2 changes: 1 addition & 1 deletion src/app/user/instance-new/instance-new.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class InstanceNewComponent implements OnInit, OnDestroy, AfterViewChecked
this._user = user;
});

this.configurationService.load()
this.configurationService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((config) => {
this._openDataIncluded = config.experiments.openDataIncluded;
Expand Down
2 changes: 1 addition & 1 deletion src/app/user/instance/instance.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class InstanceComponent implements OnInit, OnDestroy {

private handleClipboardData(): void {
this.readAsyncClipboard();
this.configurationService.load()
this.configurationService.configuration$()
.pipe(takeUntil(this._destroy$))
.subscribe((configuration) => {
const isValidUri = (url) => {
Expand Down

0 comments on commit 7f74636

Please sign in to comment.