pwa
This commit is contained in:
24
listify-client/src/app/offline/online-status.service.ts
Normal file
24
listify-client/src/app/offline/online-status.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user