pinafore/src/routes/_store/observers/onlineObservers.js

30 lines
788 B
JavaScript
Raw Normal View History

import debounce from 'lodash-es/debounce'
2018-12-23 00:37:51 +01:00
import { toast } from '../../_components/toast/toast'
2018-03-09 03:08:14 +01:00
const OFFLINE_DELAY = 5000
const NOTIFY_OFFLINE_LIMIT = 1
2018-03-09 03:08:14 +01:00
let notifyCount = 0
// debounce to avoid notifying for a short connection issue
2018-03-09 03:08:14 +01:00
const notifyOffline = debounce(() => {
if (process.browser && !navigator.onLine && ++notifyCount <= NOTIFY_OFFLINE_LIMIT) {
toast.say('You seem to be offline. You can still read toots while offline.')
}
2018-03-09 03:08:14 +01:00
}, OFFLINE_DELAY)
2018-03-09 03:09:35 +01:00
export function onlineObservers (store) {
2018-03-09 03:08:14 +01:00
if (!process.browser) {
return
}
store.observe('online', online => {
if (!online) {
2018-03-09 03:08:14 +01:00
notifyOffline()
}
})
window.addEventListener('offline', () => store.set({ online: false }))
window.addEventListener('online', () => store.set({ online: true }))
2018-03-09 03:09:35 +01:00
}