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

View File

@ -38,11 +38,27 @@ let _enabledBucketIds: number[] | null = null;
*/ */
export async function go() { export async function go() {
// Clear local database // Clear local database
// await db.local.clear(); await db.local.clear();
for await (const sysImg of localSyncIter(0)) { // Mark all entries
await db.local.where({ flag: 0 }).modify({ flag: 1 });
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 // Check if file already exists with same mtime
if (await db.local.where({ fileid: sysImg.fileid, mtime: sysImg.mtime }).first()) continue; 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 // Insert new file
await db.transaction('rw', db.local, async () => { await db.transaction('rw', db.local, async () => {
@ -53,7 +69,8 @@ export async function go() {
flag: 0, flag: 0,
}); });
}); });
}
console.log('nativex, synced file', sysImg.basename);
} }
/** /**