File size: 964 Bytes
377fc96 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // Online state: the browser's own signal (navigator.onLine plus the online/offline
// events) AND a switch the reader can flip to rehearse the offline path.
import { derived, writable } from 'svelte/store';
const KEY = 'lifekit.forceOffline';
const read = () => {
try {
return localStorage.getItem(KEY) === '1';
} catch {
return false;
}
};
/** True while the reader has switched the app to offline on purpose. */
export const forceOffline = writable(read());
forceOffline.subscribe((v) => {
try {
localStorage.setItem(KEY, v ? '1' : '0');
} catch {
/* storage unavailable */
}
});
const browserOnline = writable(typeof navigator === 'undefined' ? true : navigator.onLine);
if (typeof addEventListener === 'function') {
addEventListener('online', () => browserOnline.set(true));
addEventListener('offline', () => browserOnline.set(false));
}
export const online = derived([browserOnline, forceOffline], ([b, f]) => b && !f);
|