2024-12-03 06:53:31 +01:00
|
|
|
import { API } from 'api'
|
|
|
|
|
|
|
|
export class Sync {
|
|
|
|
static async tree() {
|
2024-12-03 13:56:38 +01:00
|
|
|
try {
|
|
|
|
const state = await nodeStore.getAppState('latest_sync')
|
|
|
|
let oldMax = (state?.value ? state.value : 0)
|
|
|
|
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
|
|
|
|
await nodeStore.updateTreeRecords(res.Nodes)
|
|
|
|
} 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
|
|
|
nodeStore.getAppState('latest_sync')
|
|
|
|
.then(state => {
|
|
|
|
if (state !== null) {
|
|
|
|
oldMax = state.value
|
|
|
|
return state.value
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
2024-12-03 13:56:38 +01:00
|
|
|
.then(async sequence => {
|
|
|
|
let offset = 0
|
|
|
|
let res = { Continue: false }
|
|
|
|
try {
|
|
|
|
do {
|
|
|
|
res = await API.query('POST', `/node/tree/${sequence}/${offset}`, {})
|
|
|
|
offset += res.Nodes.length
|
|
|
|
newMax = res.MaxSeq
|
|
|
|
await nodeStore.updateTreeRecords(res.Nodes)
|
|
|
|
} while (res.Continue)
|
|
|
|
} catch (e) {
|
|
|
|
return new Promise((_, reject) => reject(e))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => nodeStore.setAppState('latest_sync', Math.max(oldMax, newMax)))
|
|
|
|
.catch(e => console.log('sync', e))
|
|
|
|
*/
|
2024-12-03 06:53:31 +01:00
|
|
|
}
|
|
|
|
}
|