Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div class="global-loader" *ngIf="isLoading$ | async"></div>
<app-toast-container />
<app-slow-server-notice />
<router-outlet></router-outlet>
3 changes: 2 additions & 1 deletion frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down
72 changes: 72 additions & 0 deletions frontend/src/app/core/http/loading-state.services.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
}));
});
17 changes: 16 additions & 1 deletion frontend/src/app/core/http/loading-state.services.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
}
Expand Down
57 changes: 57 additions & 0 deletions frontend/src/app/core/ui/slow-server-notice.component.ts
Original file line number Diff line number Diff line change
@@ -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: `
<aside class="slow-notice" role="status" aria-live="polite" *ngIf="isSlow$ | async">
<strong>Waking up the server...</strong>
<span>
This demo runs on free hosting that sleeps when idle, so the first request can take up to
2 minutes. Thanks for waiting!
</span>
</aside>
`,
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$;
}
}