58 lines
1.0 KiB
JavaScript
58 lines
1.0 KiB
JavaScript
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'
|
|
);
|
|
}
|
|
};
|
|
} |