From 6796e912907c1eea6832ed21094f4e0beba38170 Mon Sep 17 00:00:00 2001 From: pitercoding Date: Sat, 28 Nov 2026 12:38:47 +0100 Subject: [PATCH] feat(frontend): show a notice when the server takes long to respond --- frontend/src/app/app.component.html | 1 + frontend/src/app/app.component.ts | 3 +- .../core/http/loading-state.services.spec.ts | 72 +++++++++++++++++++ .../app/core/http/loading-state.services.ts | 17 ++++- .../core/ui/slow-server-notice.component.ts | 57 +++++++++++++++ 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 frontend/src/app/core/http/loading-state.services.spec.ts create mode 100644 frontend/src/app/core/ui/slow-server-notice.component.ts diff --git a/frontend/src/app/app.component.html b/frontend/src/app/app.component.html index d5c0b14..0b9be14 100644 --- a/frontend/src/app/app.component.html +++ b/frontend/src/app/app.component.html @@ -1,3 +1,4 @@
+ diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 927bfb4..df6a3eb 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -2,11 +2,12 @@ import { AsyncPipe, NgIf } from '@angular/common'; import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; import { LoadingStateService } from './core/http/loading-state.services'; +import { SlowServerNoticeComponent } from './core/ui/slow-server-notice.component'; import { ToastContainerComponent } from './core/ui/toast-container.component'; @Component({ selector: 'app-root', - imports: [NgIf, AsyncPipe, RouterOutlet, ToastContainerComponent], + imports: [NgIf, AsyncPipe, RouterOutlet, ToastContainerComponent, SlowServerNoticeComponent], templateUrl: './app.component.html', styleUrl: './app.component.scss', }) diff --git a/frontend/src/app/core/http/loading-state.services.spec.ts b/frontend/src/app/core/http/loading-state.services.spec.ts new file mode 100644 index 0000000..bd501c1 --- /dev/null +++ b/frontend/src/app/core/http/loading-state.services.spec.ts @@ -0,0 +1,72 @@ +import { fakeAsync, tick, discardPeriodicTasks } from '@angular/core/testing'; +import { LoadingStateService, SLOW_REQUEST_THRESHOLD_MS } from './loading-state.services'; + +describe('LoadingStateService', () => { + let service: LoadingStateService; + let loading: boolean[]; + let slow: boolean[]; + + beforeEach(() => { + service = new LoadingStateService(); + loading = []; + slow = []; + service.isLoading$.subscribe((value) => loading.push(value)); + service.isSlow$.subscribe((value) => slow.push(value)); + }); + + it('is loading while at least one request is pending', () => { + service.show(); + service.show(); + service.hide(); + expect(loading[loading.length - 1]).toBeTrue(); + + service.hide(); + expect(loading[loading.length - 1]).toBeFalse(); + }); + + it('never goes below zero pending requests', () => { + service.hide(); + service.hide(); + service.show(); + + expect(loading[loading.length - 1]).toBeTrue(); + }); + + it('is not slow for requests that finish quickly', fakeAsync(() => { + service.show(); + tick(SLOW_REQUEST_THRESHOLD_MS - 1); + service.hide(); + tick(SLOW_REQUEST_THRESHOLD_MS * 2); + + expect(slow).not.toContain(true); + expect(slow[slow.length - 1]).toBeFalse(); + })); + + it('becomes slow when a request stays pending past the threshold', fakeAsync(() => { + service.show(); + tick(SLOW_REQUEST_THRESHOLD_MS - 1); + expect(slow[slow.length - 1]).toBeFalse(); + + tick(1); + expect(slow[slow.length - 1]).toBeTrue(); + })); + + it('stops being slow as soon as all requests finish', fakeAsync(() => { + service.show(); + tick(SLOW_REQUEST_THRESHOLD_MS); + expect(slow[slow.length - 1]).toBeTrue(); + + service.hide(); + expect(slow[slow.length - 1]).toBeFalse(); + })); + + it('does not restart the timer when another request starts while loading', fakeAsync(() => { + service.show(); + tick(SLOW_REQUEST_THRESHOLD_MS - 1000); + service.show(); // a second request must not postpone the notice + tick(1000); + + expect(slow[slow.length - 1]).toBeTrue(); + discardPeriodicTasks(); + })); +}); diff --git a/frontend/src/app/core/http/loading-state.services.ts b/frontend/src/app/core/http/loading-state.services.ts index eb4c919..ef37c61 100644 --- a/frontend/src/app/core/http/loading-state.services.ts +++ b/frontend/src/app/core/http/loading-state.services.ts @@ -1,5 +1,11 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, map } from 'rxjs'; +import { BehaviorSubject, distinctUntilChanged, map, of, startWith, switchMap, timer } from 'rxjs'; + +/** + * How long requests can stay pending before we tell the user the server is slow. + * The free hosting puts the backend to sleep; waking it up takes about 1-2 minutes. + */ +export const SLOW_REQUEST_THRESHOLD_MS = 5000; @Injectable({ providedIn: 'root', @@ -9,6 +15,15 @@ export class LoadingStateService { readonly isLoading$ = this.pendingRequestsSubject.pipe(map((count) => count > 0)); + /** True when there has been at least one pending request for longer than the threshold. */ + readonly isSlow$ = this.isLoading$.pipe( + distinctUntilChanged(), + switchMap((loading) => + loading ? timer(SLOW_REQUEST_THRESHOLD_MS).pipe(map(() => true), startWith(false)) : of(false) + ), + distinctUntilChanged() + ); + show(): void { this.pendingRequestsSubject.next(this.pendingRequestsSubject.value + 1); } diff --git a/frontend/src/app/core/ui/slow-server-notice.component.ts b/frontend/src/app/core/ui/slow-server-notice.component.ts new file mode 100644 index 0000000..08aeb8a --- /dev/null +++ b/frontend/src/app/core/ui/slow-server-notice.component.ts @@ -0,0 +1,57 @@ +import { AsyncPipe, NgIf } from '@angular/common'; +import { Component } from '@angular/core'; +import { LoadingStateService } from '../http/loading-state.services'; + +/** + * Shown when the API takes a long time to answer. On the free hosting the backend sleeps + * after a period without traffic, so the first request can take up to 2 minutes. + */ +@Component({ + selector: 'app-slow-server-notice', + standalone: true, + imports: [NgIf, AsyncPipe], + template: ` + + `, + styles: ` + .slow-notice { + position: fixed; + left: 50%; + bottom: 20px; + transform: translateX(-50%); + z-index: 1250; + display: grid; + gap: 4px; + width: min(520px, calc(100vw - 24px)); + padding: 12px 16px; + border-radius: var(--radius-md); + border: 1px solid var(--surface-border); + background: rgba(8, 15, 27, 0.96); + color: var(--text-main); + box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35); + font-size: 0.9rem; + line-height: 1.4; + } + + .slow-notice strong { + color: var(--accent); + } + + .slow-notice span { + color: var(--text-muted); + } + `, +}) +export class SlowServerNoticeComponent { + readonly isSlow$; + + constructor(loadingStateService: LoadingStateService) { + this.isSlow$ = loadingStateService.isSlow$; + } +}