Notes2/static/js/sync.mjs

32 lines
698 B
JavaScript
Raw Normal View History

2024-12-03 06:53:31 +01:00
import { API } from 'api'
export class Sync {
2024-12-03 22:08:45 +01:00
constructor() {
this.foo = ''
}
2024-12-03 06:53:31 +01:00
static async tree() {
2024-12-03 13:56:38 +01:00
try {
const state = await nodeStore.getAppState('latest_sync')
2024-12-03 22:08:45 +01:00
const oldMax = (state?.value ? state.value : 0)
2024-12-03 13:56:38 +01:00
let newMax = 0
let offset = 0
let res = { Continue: false }
let batch = 0
do {
batch++
console.log(`Batch #${batch}`)
res = await API.query('POST', `/node/tree/${oldMax}/${offset}`, {})
offset += res.Nodes.length
newMax = res.MaxSeq
2024-12-03 22:08:45 +01:00
await nodeStore.upsertTreeRecords(res.Nodes)
2024-12-03 13:56:38 +01:00
} while (res.Continue)
nodeStore.setAppState('latest_sync', Math.max(oldMax, newMax))
} catch (e) {
console.log('sync node tree', e)
}
2024-12-03 06:53:31 +01:00
}
}