add offline support
This commit is contained in:
+27
-2
@@ -1,8 +1,33 @@
|
||||
(function (ns, $) {
|
||||
'use strict';
|
||||
|
||||
function request(path) {
|
||||
return $.ajax({ url: ns.Config.apiBase + path, method: 'GET', dataType: 'json', cache: false });
|
||||
async function offlineResponse(path) {
|
||||
if (!ns.OfflineStore) return null;
|
||||
if (path === '/health') {
|
||||
const available = await ns.OfflineStore.state() || (await ns.OfflineStore.routes()).length;
|
||||
return available ? { ok: true, offline: true } : null;
|
||||
}
|
||||
if (path.startsWith('/routes?')) {
|
||||
if (!await ns.OfflineStore.state() && !(await ns.OfflineStore.routes()).length) return null;
|
||||
return { routes: (await ns.OfflineStore.routes()).map(item => item.route), offline: true };
|
||||
}
|
||||
const response = await ns.OfflineStore.match(ns.Config.apiBase + path);
|
||||
return response ? response.json() : null;
|
||||
}
|
||||
|
||||
async function request(path) {
|
||||
if (globalThis.navigator?.onLine === false) {
|
||||
const response = await offlineResponse(path);
|
||||
if (response) return response;
|
||||
}
|
||||
try {
|
||||
return await $.ajax({ url: ns.Config.apiBase + path, method: 'GET', dataType: 'json', cache: false, timeout: 10000 });
|
||||
} catch (error) {
|
||||
if (error.status > 0) throw error;
|
||||
const response = await offlineResponse(path);
|
||||
if (response) return response;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function routeQuery(position) {
|
||||
|
||||
Reference in New Issue
Block a user