Sync from and to server

This commit is contained in:
Magnus Åhall 2025-01-12 12:21:49 +01:00
parent e07258e014
commit 25179ffd15
6 changed files with 125 additions and 76 deletions

View file

@ -6,7 +6,7 @@ export class Sync {
this.foo = ''
}
static async nodes() {
static async nodesFromServer() {//{{{
let duration = 0
const syncStart = Date.now()
try {
@ -22,7 +22,7 @@ export class Sync {
let batch = 0
do {
batch++
res = await API.query('POST', `/sync/node/${oldMax}/${offset}`, { ClientUUID: clientUUID.value })
res = await API.query('POST', `/sync/from_server/${oldMax}/${offset}`, { ClientUUID: clientUUID.value })
if (res.Nodes.length > 0)
console.log(`Node sync batch #${batch}`)
offset += res.Nodes.length
@ -55,8 +55,8 @@ export class Sync {
console.log(`Node sync took ${duration}s`, count)
}
return duration
}
static async handleNode(backendNode) {
}//}}}
static async handleNode(backendNode) {//{{{
try {
/* Retrieving the local copy of this node from IndexedDB.
* The backend node can be discarded if it is older than
@ -69,16 +69,38 @@ export class Sync {
return
}
// local node is older than the backend node
// and moved into the send_queue table for later sync to backend.
return nodeStore.moveToSendQueue(localNode, backendNode)
/* If the local node hasn't seen unsynchronized change,
* it can be replaced without anything else being done
* since it is already on the backend server.
*
* If the local node has seen change, the change is already
* placed into the send_queue anyway. */
return nodeStore.add([backendNode])
})
.catch(async e => {
.catch(async () => {
// Not found in IndexedDB - OK to just insert since it only exists in backend.
return nodeStore.add([backendNode])
})
} catch (e) {
console.error(e)
}
}
}//}}}
static async nodesToServer() {//{{{
try {
const nodesToSend = await nodeStore.sendQueue.retrieve(100)
const clientUUID = await nodeStore.getAppState('client_uuid')
const request = {
Nodes: nodesToSend,
ClientUUID: clientUUID.value,
}
res = await API.query('POST', `/sync/from_server/${oldMax}/${offset}`, { ClientUUID: clientUUID.value })
console.log(res)
} catch (e) {
console.log(e)
alert(e)
}
}//}}}
}