This commit is contained in:
Bastian Wagner
2026-06-15 11:05:16 +02:00
parent fd7245f1fb
commit b5d5378fc9
15 changed files with 547 additions and 13 deletions

View File

@@ -0,0 +1,24 @@
import { Injectable, NgZone, inject, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class OnlineStatusService {
private readonly zone = inject(NgZone);
readonly online = signal(this.readOnlineState());
constructor() {
if (typeof window === 'undefined') {
return;
}
window.addEventListener('online', () => this.setOnline(true));
window.addEventListener('offline', () => this.setOnline(false));
}
private setOnline(online: boolean): void {
this.zone.run(() => this.online.set(online));
}
private readOnlineState(): boolean {
return typeof navigator === 'undefined' ? true : navigator.onLine;
}
}