Fixed tree reset after sync, optimized sync with IndexedDB

This commit is contained in:
Magnus Åhall 2026-05-03 19:45:39 +02:00
parent 454d065baa
commit 26ca510785
5 changed files with 72 additions and 52 deletions

View file

@ -65,9 +65,16 @@ export class Sync {
* sync be preserved in the backend. */
let backendNode = null
// Create a single transaction to be used in the chain of
// this sync. Otherwise it would take more time to create
// transactions for each node.
const trx = nodeStore.newTransaction('nodes', 'readwrite')
const objstore = trx.objectStore('nodes')
for (const i in res.Nodes) {
backendNode = new Node(res.Nodes[i], -1)
await window._sync.handleNode(backendNode)
await this.handleNode(backendNode, objstore)
handled++
if (handled % 100 === 0)
@ -88,16 +95,16 @@ export class Sync {
}
return (syncEnd - syncStart)
}//}}}
async handleNode(backendNode) {//{{{
async handleNode(backendNode, objstore) {//{{{
try {
/* Retrieving the local copy of this node from IndexedDB.
* The backend node can be discarded if it is older than
* the local copy since it is considered history preserved
* in the backend. */
return nodeStore.get(backendNode.UUID)
.then(async localNode => {
return nodeStore.get(backendNode.UUID, objstore)
.then(localNode => {
if (localNode.updated() >= backendNode.updated()) {
console.log(`History from backend: ${backendNode.UUID}`)
console.debug(`History from backend: ${backendNode.UUID}`)
return
}
@ -107,12 +114,12 @@ export class Sync {
*
* If the local node has seen change, the change is already
* placed into the send_queue anyway. */
return nodeStore.add([backendNode])
return nodeStore.add([backendNode], objstore)
})
.catch(async () => {
.catch(() => {
// Not found in IndexedDB - OK to just insert since it only exists in backend.
return nodeStore.add([backendNode])
return nodeStore.add([backendNode], objstore)
})
} catch (e) {
console.error(e)
@ -198,10 +205,7 @@ export class N2SyncProgress extends CustomHTMLElement {
break
// Reload the tree nodes to reflect the new/updated nodes.
if (window._notes2?.current?.reloadTree.value !== null) {
nodeStore.purgeCache()
window._notes2.current.reloadTree.value = window._notes2.current.reloadTree.value + 1
}
window._app.tree.reset()
break
}
this.render()