IMPR: AJAX online/offline handling

This commit is contained in:
Tony Air 2020-09-11 02:48:14 +07:00
parent bcc4a131ce
commit 59af7f47fe
6 changed files with 535 additions and 520 deletions

2
dist/js/app.js vendored

File diff suppressed because one or more lines are too long

2
dist/js/app.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "@a2nt/ss-bootstrap-ui-webpack-boilerplate", "name": "@a2nt/ss-bootstrap-ui-webpack-boilerplate",
"version": "2.5.2", "version": "2.5.3",
"author": "Tony Air <tony@twma.pro>", "author": "Tony Air <tony@twma.pro>",
"license": "MIT", "license": "MIT",
"description": "This UI Kit allows you to build Bootstrap 4 webapp with some extra UI features. It's easy to extend and easy to convert HTML templates to CMS templates.", "description": "This UI Kit allows you to build Bootstrap 4 webapp with some extra UI features. It's easy to extend and easy to convert HTML templates to CMS templates.",

View File

@ -286,7 +286,7 @@ const AjaxUI = (($) => {
for (const url in $.xhrPool.requests) { for (const url in $.xhrPool.requests) {
const jqXHR = $.xhrPool.requests[url]; const jqXHR = $.xhrPool.requests[url];
$.ajax(jqXHR.opts); $.ajax(jqXHR.opts);
//console.log(`${NAME}: AJAX request is restored (${jqXHR.opts.url})`); console.log(`${NAME}: AJAX request is restored (${jqXHR.opts.url})`);
} }
$.xhrPool.paused = false; $.xhrPool.paused = false;
@ -308,7 +308,7 @@ const AjaxUI = (($) => {
$.xhrPool.pauseAll(); $.xhrPool.pauseAll();
}); });
$Body.on(`${Events.ONLINE}`, () => { $Body.on(`${Events.BACKONLINE}`, () => {
$.xhrPool.restoreAll(); $.xhrPool.restoreAll();
}); });

View File

@ -10,6 +10,7 @@ module.exports = {
TABFOCUSED: 'tab-focused', TABFOCUSED: 'tab-focused',
OFFLINE: 'offline', OFFLINE: 'offline',
ONLINE: 'online', ONLINE: 'online',
BACKONLINE: 'back-online',
TOUCHENABLE: 'touch-enabled', TOUCHENABLE: 'touch-enabled',
TOUCHDISABLED: 'touch-disabled', TOUCHDISABLED: 'touch-disabled',
LOADED: 'load', LOADED: 'load',

View File

@ -90,13 +90,15 @@ const MainUI = (($) => {
}); });
// session ping // session ping
let pingInterval;
let pingLock = false;
const sessionPing = () => { const sessionPing = () => {
if ($Body.hasClass('is-offline')) { if (pingLock || $Body.hasClass('is-offline')) {
return; return;
} }
console.log(`${NAME}: session ping`); console.log(`${NAME}: session ping`);
pingLock = true;
$.ajax({ $.ajax({
sync: false, sync: false,
async: true, async: true,
@ -105,37 +107,58 @@ const MainUI = (($) => {
global: false, global: false,
type: 'POST', type: 'POST',
complete: (data, datastatus) => { complete: (data, datastatus) => {
updateOnlineStatus();
if (datastatus !== 'success') { if (datastatus !== 'success') {
console.warn(`${NAME}: ping failed`); console.warn(`${NAME}: ping failed`);
clearInterval(pingInterval); clearInterval(pingInterval);
pingInterval = null;
} }
updateOnlineStatus();
pingLock = false;
}, },
}); });
}; };
let pingInterval = setInterval(sessionPing, 300000); // 5 min in ms
// update online/offline state // update online/offline state
const updateOnlineStatus = () => { const updateOnlineStatus = () => {
if (typeof navigator.onLine === 'undefined') { if (typeof navigator.onLine === 'undefined') {
return; return false;
} }
if (!navigator.onLine) { if (!navigator.onLine) {
console.log(`${NAME}: Tab: offline`); console.log(`${NAME}: Tab: offline`);
clearInterval(pingInterval); clearInterval(pingInterval);
pingInterval = null;
$Body.addClass('is-offline'); $Body.addClass('is-offline');
$Body.removeClass('is-online');
$Body.trigger(Events.OFFLINE); $Body.trigger(Events.OFFLINE);
$W.trigger(Events.OFFLINE); $W.trigger(Events.OFFLINE);
} else {
console.log(`${NAME}: Tab: online`);
pingInterval = setInterval(sessionPing, 300000); // 5 min in ms
return true;
}
if (!pingInterval) {
pingInterval = setInterval(sessionPing, 300000); // 5 min in ms
}
console.log(`${NAME}: Tab: online`);
if ($Body.hasClass('is-offline')) {
sessionPing();
$Body.trigger(Events.BACKONLINE);
}
$Body.addClass('is-online');
$Body.removeClass('is-offline'); $Body.removeClass('is-offline');
$Body.trigger(Events.ONLINE); $Body.trigger(Events.ONLINE);
$W.trigger(Events.ONLINE); $W.trigger(Events.ONLINE);
}
return true;
}; };
W.addEventListener( W.addEventListener(
@ -154,11 +177,7 @@ const MainUI = (($) => {
false, false,
); );
W.addEventListener(`${Events.LOADED}`, () => { $W.on(`${Events.LOADED} ${Events.AJAX}`, () => {
updateOnlineStatus();
});
$W.on(`${Events.AJAX}`, () => {
updateOnlineStatus(); updateOnlineStatus();
}); });
@ -441,10 +460,7 @@ const MainUI = (($) => {
}); });
} }
if ( if ($AlertNotify.length && typeof $AlertNotify[0].stop !== 'undefined') {
$AlertNotify.length &&
typeof $AlertNotify[0].stop !== 'undefined'
) {
$AlertNotify[0].stop(); $AlertNotify[0].stop();
} }
@ -581,9 +597,7 @@ const MainUI = (($) => {
// hide spinner on target _blank // hide spinner on target _blank
$('[target="_blank"],.external').on('click submit', (e) => { $('[target="_blank"],.external').on('click submit', (e) => {
if ( if (
$(e.currentTarget).is( $(e.currentTarget).is('[data-toggle="lightbox"],[data-lightbox-gallery]')
'[data-toggle="lightbox"],[data-lightbox-gallery]',
)
) { ) {
return false; return false;
} }