UNPKG

sync-client

Version:

Module which uses Dexie to save data and can later use a server to synchronize the data in the database with other devices

50 lines (42 loc) 1.12 kB
const CONNECTION_CHECK_PATH = 'check'; export default function initConnectionStatus(global) { function checkServerConnection(url) { return global.fetch(url, { method: 'HEAD' }); } function isOnline(url) { // If we have no URL we are offline if (!url) { return Promise.resolve(false); } const serverUrl = url[url.length - 1] === '/' ? `${url}${CONNECTION_CHECK_PATH}` : `${url}/${CONNECTION_CHECK_PATH}`; if (global.navigator.onLine) { return checkServerConnection(serverUrl) .then(() => { return true; }) .catch(() => { return false; }); } return Promise.resolve(false); } function onlineStatusChanged(url, cb) { global.addEventListener('online', () => { isOnline(url) .then((status) => { cb(status); }); }); global.addEventListener('offline', () => { cb(false); }); // Set initial status isOnline(url) .then((status) => { cb(status); }); } return { isOnline, onlineStatusChanged }; }