2018-04-06 00:57:36 +00:00
|
|
|
import debounce from 'lodash-es/debounce'
|
2018-03-09 02:08:14 +00:00
|
|
|
import { toast } from '../../_utils/toast'
|
|
|
|
|
2018-04-15 23:00:16 +00:00
|
|
|
const OFFLINE_DELAY = 5000
|
|
|
|
const NOTIFY_OFFLINE_LIMIT = 1
|
2018-03-09 02:08:14 +00:00
|
|
|
|
2018-04-15 23:00:16 +00:00
|
|
|
let notifyCount = 0
|
|
|
|
|
2018-11-24 08:41:36 +00:00
|
|
|
let offlineStyle = process.browser && document.getElementById('theOfflineStyle')
|
|
|
|
|
2018-04-15 23:00:16 +00:00
|
|
|
// debounce to avoid notifying for a short connection issue
|
2018-03-09 02:08:14 +00:00
|
|
|
const notifyOffline = debounce(() => {
|
2018-04-15 23:00:16 +00:00
|
|
|
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 02:08:14 +00:00
|
|
|
}, OFFLINE_DELAY)
|
|
|
|
|
2018-03-09 02:09:35 +00:00
|
|
|
export function onlineObservers (store) {
|
2018-03-09 02:08:14 +00:00
|
|
|
if (!process.browser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let meta = document.getElementById('theThemeColor')
|
|
|
|
let oldTheme = meta.content
|
|
|
|
|
|
|
|
store.observe('online', online => {
|
2018-11-24 08:41:36 +00:00
|
|
|
// "only x" ensures the <style> tag does not have any effect
|
|
|
|
offlineStyle.setAttribute('media', online ? 'only x' : 'all')
|
2018-03-09 02:08:14 +00:00
|
|
|
if (online) {
|
|
|
|
meta.content = oldTheme
|
|
|
|
} else {
|
|
|
|
let offlineThemeColor = window.__themeColors.offline
|
|
|
|
if (meta.content !== offlineThemeColor) {
|
|
|
|
oldTheme = meta.content
|
|
|
|
}
|
|
|
|
meta.content = offlineThemeColor
|
|
|
|
notifyOffline()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-30 04:42:57 +00:00
|
|
|
window.addEventListener('offline', () => store.set({ online: false }))
|
|
|
|
window.addEventListener('online', () => store.set({ online: true }))
|
2018-03-09 02:09:35 +00:00
|
|
|
}
|