Add assets/js/sync.js
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
import { getDB } from './db.js';
|
||||||
|
|
||||||
|
export async function syncQueue() {
|
||||||
|
|
||||||
|
const db = getDB();
|
||||||
|
|
||||||
|
const tx =
|
||||||
|
db.transaction(
|
||||||
|
'sync_queue',
|
||||||
|
'readwrite'
|
||||||
|
);
|
||||||
|
|
||||||
|
const store =
|
||||||
|
tx.objectStore('sync_queue');
|
||||||
|
|
||||||
|
const getAll =
|
||||||
|
store.getAll();
|
||||||
|
|
||||||
|
getAll.onsuccess = async () => {
|
||||||
|
|
||||||
|
const queue =
|
||||||
|
getAll.result;
|
||||||
|
|
||||||
|
if (!queue.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const res =
|
||||||
|
await fetch('/api/sync.php', {
|
||||||
|
|
||||||
|
method: 'POST',
|
||||||
|
|
||||||
|
headers: {
|
||||||
|
'Content-Type':
|
||||||
|
'application/json'
|
||||||
|
},
|
||||||
|
|
||||||
|
body:
|
||||||
|
JSON.stringify(queue)
|
||||||
|
});
|
||||||
|
|
||||||
|
const data =
|
||||||
|
await res.json();
|
||||||
|
|
||||||
|
if (data.ok) {
|
||||||
|
store.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'offline, sync later'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user