This commit is contained in:
Magnus Åhall 2024-12-03 13:56:38 +01:00
parent ac8b334eee
commit 04c101982f
6 changed files with 98 additions and 39 deletions

View file

@ -97,35 +97,38 @@ export class NodeStore {
async updateTreeRecords(records) {//{{{
return new Promise((resolve, reject) => {
try {
let max = 0
const t = this.db.transaction('treeNodes', 'readwrite')
const nodeStore = t.objectStore('treeNodes')
t.onerror = (event) => {
console.log('transaction error', event.target.error)
const t = this.db.transaction('treeNodes', 'readwrite')
const nodeStore = t.objectStore('treeNodes')
t.onerror = (event) => {
console.log('transaction error', event.target.error)
reject(event.target.error)
}
t.oncomplete = () => {
resolve()
}
// records is an object, not an array.
for (const i in records) {
const record = records[i]
let addReq
let op
if (record.Deleted) {
op = 'deleting'
addReq = nodeStore.delete(record.UUID)
} else {
op = 'upserting'
addReq = nodeStore.put(record)
}
addReq.onsuccess = () => {
console.log(`${op} ${record.UUID} (${record.Name})`)
}
addReq.onerror = (event) => {
console.log(`error ${op} ${record.UUID}`, event.target.error)
reject(event.target.error)
}
t.oncomplete = () => {
console.log(max)
resolve(max)
}
// records is an object, not an array.
for (const i in records) {
const record = records[i]
const addReq = nodeStore.put(record)
addReq.onsuccess = () => {
max = Math.max(max, record.CreatedSeq, record.UpdatedSeq, record.DeletedSeq.Int64)
console.log('OK!', record.UUID, record.Name)
}
addReq.onerror = (event) => {
console.log('Error!', event.target.error, record.UUID)
}
}
} catch (e) {
console.log(e)
}
})
}//}}}
async add(records) {//{{{

View file

@ -2,7 +2,30 @@ import { API } from 'api'
export class Sync {
static async tree() {
let oldMax = 0
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)
}
/*
nodeStore.getAppState('latest_sync')
.then(state => {
if (state !== null) {
@ -11,9 +34,22 @@ export class Sync {
}
return 0
})
.then(sequence => API.query('POST', `/node/tree/${sequence}`, {}))
.then(res => nodeStore.updateTreeRecords(res.Nodes))
.then(newMax => nodeStore.setAppState('latest_sync', Math.max(oldMax, newMax)))
.catch(e => alert(e))
.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))
*/
}
}