webpack-bootstrap-ui-kit/src/js/ajax/online.js

103 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-08-09 18:04:09 +02:00
// ping online/offline state switch and detection
2022-05-03 20:50:57 +02:00
import axios from 'redaxios'
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
import Events from '../_events'
import Consts from '../_consts'
2021-08-09 18:04:09 +02:00
export default ((W) => {
2022-05-03 20:50:57 +02:00
const NAME = 'main.online'
const D = document
const BODY = D.body
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
let pingInterval
const PING_META = D.querySelector('meta[name="ping"]')
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
let update_online_status_lock = false
2021-08-09 18:04:09 +02:00
const UPDATE_ONLINE_STATUS = (online) => {
if (update_online_status_lock) {
2022-05-03 20:50:57 +02:00
return
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
update_online_status_lock = true
2021-08-09 18:04:09 +02:00
if (online) {
2022-05-03 20:50:57 +02:00
if (BODY.classList.contains('is-offline')) {
console.log(`${NAME}: back Online`)
W.dispatchEvent(new Event(Events.BACKONLINE))
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: Online`)
W.dispatchEvent(new Event(Events.ONLINE))
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
BODY.classList.add('is-online')
BODY.classList.remove('is-offline')
2021-08-09 18:04:09 +02:00
if (PING_META && !pingInterval) {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: SESSION_PING is active`)
pingInterval = setInterval(SESSION_PING, 300000) // 5 min in ms
2021-08-09 18:04:09 +02:00
}
} else {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: Offline`)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
BODY.classList.add('is-offline')
BODY.classList.remove('is-online')
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
clearInterval(pingInterval)
pingInterval = null
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
W.dispatchEvent(new Event(Events.OFFLINE))
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
update_online_status_lock = false
}
2021-08-09 18:04:09 +02:00
// session ping
2022-05-03 20:50:57 +02:00
let session_ping_lock = false
2021-08-09 18:04:09 +02:00
const SESSION_PING = () => {
2022-05-03 20:50:57 +02:00
if (session_ping_lock || BODY.classList.contains('is-offline')) {
return
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
const PING_URL = PING_META.getAttribute('content')
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: session ping`)
session_ping_lock = true
2021-08-09 18:04:09 +02:00
axios
.post(PING_URL, {})
.then((resp) => {
2022-05-03 20:50:57 +02:00
session_ping_lock = false
UPDATE_ONLINE_STATUS(true)
2021-08-09 18:04:09 +02:00
})
.catch((error) => {
2022-05-03 20:50:57 +02:00
console.error(error)
console.warn(`${NAME}: SESSION_PING failed`)
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
session_ping_lock = false
UPDATE_ONLINE_STATUS(false)
})
}
2021-08-09 18:04:09 +02:00
// current browser online state
const navigatorStateUpdate = () => {
2022-05-03 20:50:57 +02:00
if (typeof navigator.onLine !== 'undefined') {
2021-08-09 18:04:09 +02:00
if (!navigator.onLine) {
2022-05-03 20:50:57 +02:00
UPDATE_ONLINE_STATUS(false)
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
UPDATE_ONLINE_STATUS(true)
2021-08-09 18:04:09 +02:00
}
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
W.addEventListener(`${Events.OFFLINE}`, () => {
2022-05-03 20:50:57 +02:00
UPDATE_ONLINE_STATUS(false)
})
2021-08-09 18:04:09 +02:00
W.addEventListener(`${Events.ONLINE}`, () => {
2022-05-03 20:50:57 +02:00
UPDATE_ONLINE_STATUS(true)
})
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
W.addEventListener(`${Events.LOADED}`, navigatorStateUpdate)
W.addEventListener(`${Events.AJAX}`, navigatorStateUpdate)
})(window)