From 0d86b789c178628200844d97bf0eb50e6ee87cf9 Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 11 Feb 2023 20:23:18 +0100 Subject: [PATCH] Moving sync back in the first worker (sync doesn't see updates done in other workers!) --- src/db-admin/health.ts | 101 ++++++++++++++++++++++------------------- src/db-admin/open.ts | 4 +- src/db/index.ts | 52 +++++++++++++++++++++ 3 files changed, 108 insertions(+), 49 deletions(-) diff --git a/src/db-admin/health.ts b/src/db-admin/health.ts index f138115..c6c02b7 100644 --- a/src/db-admin/health.ts +++ b/src/db-admin/health.ts @@ -22,24 +22,24 @@ const initDb = async () => { globalThis.currentAccount = await openDatabases(); const currentAccount = globalThis.currentAccount; - if (currentAccount.remoteDbServer) { - const url = `${currentAccount.remoteDbServer}/userdb-${toHex( - currentAccount.remoteDbUser - )}`; + // if (currentAccount.remoteDbServer) { + // const url = `${currentAccount.remoteDbServer}/userdb-${toHex( + // currentAccount.remoteDbUser + // )}`; - const remoteDb = new PouchDB(url, { - auth: { - username: currentAccount.remoteDbUser, - password: currentAccount.remoteDbPassword, - }, - skip_setup: true, - }); - globalThis.remoteDb = remoteDb; - globalThis.sync = PouchDB.sync(db, remoteDb, { - live: true, - retry: true, - }); - } + // const remoteDb = new PouchDB(url, { + // auth: { + // username: currentAccount.remoteDbUser, + // password: currentAccount.remoteDbPassword, + // }, + // skip_setup: true, + // }); + // globalThis.remoteDb = remoteDb; + // globalThis.sync = PouchDB.sync(db, remoteDb, { + // live: true, + // retry: true, + // }); + // } } }; @@ -54,33 +54,33 @@ export const watchDb = async (p: { signal: Signal; params: any }) => { setStatus({ opened: true }); - if (globalThis.sync) { - globalThis.sync - .on('change', function (info) { - // handle change - console.log({ caller: 'Sync / change', info }); - }) - .on('paused', function (err) { - // replication paused (e.g. replication up to date, user went offline) - console.log({ caller: 'Sync / paused', err }); - }) - .on('active', function () { - // replicate resumed (e.g. new changes replicating, user went back online) - console.log({ caller: 'Sync / active' }); - }) - .on('denied', function (err) { - // a document failed to replicate (e.g. due to permissions) - console.error({ caller: 'Sync / denied', err }); - }) - .on('complete', function (info) { - // handle complete - console.log({ caller: 'Sync / complete', info }); - }) - .on('error', function (err) { - // handle error - console.error({ caller: 'Sync / error', err }); - }); - } + // if (globalThis.sync) { + // globalThis.sync + // .on('change', function (info) { + // // handle change + // console.log({ caller: 'Sync / change', info }); + // }) + // .on('paused', function (err) { + // // replication paused (e.g. replication up to date, user went offline) + // console.log({ caller: 'Sync / paused', err }); + // }) + // .on('active', function () { + // // replicate resumed (e.g. new changes replicating, user went back online) + // console.log({ caller: 'Sync / active' }); + // }) + // .on('denied', function (err) { + // // a document failed to replicate (e.g. due to permissions) + // console.error({ caller: 'Sync / denied', err }); + // }) + // .on('complete', function (info) { + // // handle complete + // console.log({ caller: 'Sync / complete', info }); + // }) + // .on('error', function (err) { + // // handle error + // console.error({ caller: 'Sync / error', err }); + // }); + // } const timerId = setInterval(async () => { const dbInfo = await db.info(); @@ -89,14 +89,23 @@ export const watchDb = async (p: { signal: Signal; params: any }) => { ? await remoteDb.info() : undefined; const tasks = PouchDB.activeTasks.list(); - const newStatus = { ...status(), tasks, dbInfo, localDbInfo, remoteDbInfo }; + const newStatus = { + ...status(), + tasks, + dbInfo, + localDbInfo, + remoteDbInfo, + // date: new Date(), + }; if (!isEqual(status(), newStatus)) { setStatus(newStatus); } - }, 1000); + }, 10000); // db.compact(); + await sleep(10000); + const openIDb = (name: string) => new Promise((resolve, reject) => { const iDb = indexedDB.open(name); diff --git a/src/db-admin/open.ts b/src/db-admin/open.ts index 49bcd2e..c0e6eca 100644 --- a/src/db-admin/open.ts +++ b/src/db-admin/open.ts @@ -22,7 +22,6 @@ export const openDatabases = async () => { }); const accounts = await getAccounts(); - console.log({ caller: 'initDb', accounts }); if (accounts.length === 0) { accounts[0] = initialAccount; @@ -36,8 +35,7 @@ export const openDatabases = async () => { await putSettings({ settings }); currentAccount = accounts[0]; } - - console.log({ caller: 'initDb', settings, currentAccount }); + console.log({ caller: 'open', accounts, settings }); if (globalThis.db === undefined) { globalThis.db = new PouchDB(currentAccount.localDb, { diff --git a/src/db/index.ts b/src/db/index.ts index e516439..434ce50 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -17,6 +17,8 @@ declare global { var localDb: any; var db: any; var dbReady: boolean; + var currentAccount: any; + var sync: any; } export const initDb = async () => { @@ -27,6 +29,56 @@ export const initDb = async () => { await openDatabases(); + globalThis.currentAccount = await openDatabases(); + const currentAccount = globalThis.currentAccount; + + if (currentAccount.remoteDbServer) { + const url = `${currentAccount.remoteDbServer}/userdb-${toHex( + currentAccount.remoteDbUser + )}`; + + const remoteDb = new PouchDB(url, { + auth: { + username: currentAccount.remoteDbUser, + password: currentAccount.remoteDbPassword, + }, + skip_setup: true, + }); + globalThis.remoteDb = remoteDb; + globalThis.sync = PouchDB.sync(db, remoteDb, { + live: true, + retry: true, + }); + } + + if (globalThis.sync) { + globalThis.sync + .on('change', function (info) { + // handle change + console.log({ caller: 'Sync / change', info }); + }) + .on('paused', function (err) { + // replication paused (e.g. replication up to date, user went offline) + console.log({ caller: 'Sync / paused', err }); + }) + .on('active', function () { + // replicate resumed (e.g. new changes replicating, user went back online) + console.log({ caller: 'Sync / active' }); + }) + .on('denied', function (err) { + // a document failed to replicate (e.g. due to permissions) + console.error({ caller: 'Sync / denied', err }); + }) + .on('complete', function (info) { + // handle complete + console.log({ caller: 'Sync / complete', info }); + }) + .on('error', function (err) { + // handle error + console.error({ caller: 'Sync / error', err }); + }); + } + // console.log({ caller: 'initDb / before db.changes' }); const changes = db