sync refactor

Signed-off-by: Varun Patil <radialapps@gmail.com>
dexie
Varun Patil 2023-10-01 18:42:10 -07:00
parent 0360a08b87
commit 14fa2e6c78
2 changed files with 33 additions and 17 deletions

View File

@ -146,9 +146,8 @@ export type NativeX = {
/**
* Initialize local sync.
* @param startTime Start time of the sync
*/
localSyncInit: (startTime: number) => void;
localSyncInit: () => void;
/**
* Get the next batch of local sync.
@ -324,11 +323,11 @@ export async function logout() {
/**
* Iterate the local files on the device.
*/
export async function* localSyncIter(startTime: number) {
export async function* localSyncIter() {
if (!has()) return;
// Initialize and iterate
nativex.localSyncInit(startTime);
nativex.localSyncInit();
while (true) {
const next = nativex.localSyncNext();

View File

@ -38,22 +38,39 @@ let _enabledBucketIds: number[] | null = null;
*/
export async function go() {
// Clear local database
// await db.local.clear();
await db.local.clear();
for await (const sysImg of localSyncIter(0)) {
// Check if file already exists with same mtime
if (await db.local.where({ fileid: sysImg.fileid, mtime: sysImg.mtime }).first()) continue;
// Mark all entries
await db.local.where({ flag: 0 }).modify({ flag: 1 });
// Insert new file
await db.transaction('rw', db.local, async () => {
await db.local.where({ fileid: sysImg.fileid }).delete();
await db.local.add({
...sysImg,
dayid: Math.floor(sysImg.datetaken / 86400),
flag: 0,
});
});
for await (const sysImg of localSyncIter()) {
await syncImage(sysImg);
}
// Delete all entries with flag=1
await db.local.where({ flag: 1 }).delete();
}
async function syncImage(sysImg: ISystemImage) {
// Check if file already exists with same mtime
const entry = await db.local.where({ fileid: sysImg.fileid, mtime: sysImg.mtime }).first();
if (entry) {
// Unmark this entry
await db.local.where({ id: entry.id }).modify({ flag: 0 });
return;
}
// Insert new file
await db.transaction('rw', db.local, async () => {
await db.local.where({ fileid: sysImg.fileid }).delete();
await db.local.add({
...sysImg,
dayid: Math.floor(sysImg.datetaken / 86400),
flag: 0,
});
});
console.log('nativex, synced file', sysImg.basename);
}
/**