From 853b158dcf9dcafeaa8a993010770880517abb5e Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 11 Feb 2023 17:52:11 +0100 Subject: [PATCH] Bug fix --- src/components/account/Account.tsx | 4 + src/db-admin/health.ts | 144 ++++++++++++++--------------- src/workers/dispatcher-main.ts | 33 ++++--- 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/src/components/account/Account.tsx b/src/components/account/Account.tsx index 95e7b91..ab3aac4 100644 --- a/src/components/account/Account.tsx +++ b/src/components/account/Account.tsx @@ -34,6 +34,10 @@ const Account: Component<{}> = (props) => { const newAccounts: any = await dispatch({ action: 'getAccounts', }); + console.log({ + caller: 'Account / handleClickOpen', + newAccounts, + }); setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]); setAccount( cloneDeep(getAccountById(accounts(), settings().currentAccountId)) diff --git a/src/db-admin/health.ts b/src/db-admin/health.ts index 2f400f9..f138115 100644 --- a/src/db-admin/health.ts +++ b/src/db-admin/health.ts @@ -2,23 +2,19 @@ import { Signal } from 'solid-js'; import PouchDB from 'pouchdb'; import indexeddb from 'pouchdb-adapter-indexeddb'; -import { getSettings, putSettings } from '../db/settings'; import { sleep } from '../lib/async-wait'; -import { - getAccounts, - initialAccount, - putAccount, - getAccountById, -} from '../db/account'; import { openDatabases } from './open'; import { toHex } from '../lib/to-hex'; +import { isEqual } from 'lodash'; PouchDB.plugin(indexeddb); declare global { var localDb: any; var db: any; + var remoteDb: any; var currentAccount: any; + var sync: any; } const initDb = async () => { @@ -38,63 +34,64 @@ const initDb = async () => { }, skip_setup: true, }); - const sync = PouchDB.sync(db, remoteDb, { + globalThis.remoteDb = remoteDb; + globalThis.sync = PouchDB.sync(db, remoteDb, { live: true, retry: true, - }) - .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 }); - }); + }); } } }; export const watchDb = async (p: { signal: Signal; params: any }) => { const { signal } = p; - const [, setStatus] = signal; + const [status, setStatus] = signal; console.log({ caller: 'watchDb', p }); await initDb(); db = globalThis.db; localDb = globalThis.localDb; - const dbinfo = await db.info(); - const localDbInfo = await localDb.info(); - const tasks = PouchDB.activeTasks.list(); + setStatus({ opened: true }); - setStatus({ opened: true, dbinfo, localDbinfo: localDbInfo, tasks }); - - return; + 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(); + const dbInfo = await db.info(); + const localDbInfo = await localDb.info(); + const remoteDbInfo = globalThis.remoteDb + ? await remoteDb.info() + : undefined; const tasks = PouchDB.activeTasks.list(); - if (tasks.length > 0) { - setStatus({ status: 'ongoing', db: 'dyomedea', dbinfo, tasks }); - } else { - setStatus({ status: 'terminated', db: 'dyomedea', dbinfo, tasks }); - clearInterval(timerId); + const newStatus = { ...status(), tasks, dbInfo, localDbInfo, remoteDbInfo }; + if (!isEqual(status(), newStatus)) { + setStatus(newStatus); } }, 1000); @@ -144,37 +141,32 @@ export const watchDb = async (p: { signal: Signal; params: any }) => { }; }); - const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase; + // const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase; - const bySequence = iDb.transaction('docs', 'readonly'); - const store = bySequence.objectStore('docs'); - console.log({ - caller: 'watchDb', - message: 'transaction opened', - bySequence, - store, - }); + // const bySequence = iDb.transaction('docs', 'readonly'); + // const store = bySequence.objectStore('docs'); + // console.log({ + // caller: 'watchDb', + // message: 'transaction opened', + // bySequence, + // store, + // }); - const docs = (await getAll(store)) as any[]; - // setStatus({ - // status: 'docs', - // db: 'dyomedea', - // docs, - // }); + // const docs = (await getAll(store)) as any[]; - docs.forEach(async (doc) => { - const { id, rev, deleted } = doc; - if (deleted !== 0) { - // const purge = await db.purge(id, rev); - console.log({ - caller: 'watchDb', - message: 'purging', - id, - rev, - doc, - // purge, - }); - await sleep(1000); - } - }); + // docs.forEach(async (doc) => { + // const { id, rev, deleted } = doc; + // if (deleted !== 0) { + // // const purge = await db.purge(id, rev); + // console.log({ + // caller: 'watchDb', + // message: 'purging', + // id, + // rev, + // doc, + // // purge, + // }); + // await sleep(1000); + // } + // }); }; diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index 1f075bf..f0ab67c 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -53,11 +53,12 @@ const dispatch = ( if (worker === undefined) { init(); } - until(dbReady, 1000).then(() => { + let returnValue; + until(dbReady, 100).then(() => { // Wait until databases have been created by health.ts if (callBack === undefined) { /** If a callback function is not provided, return a promise */ - return new Promise((resolve, reject) => { + returnValue = new Promise((resolve, reject) => { dispatch(payload, (error, result) => { if (error) { reject(error); @@ -66,20 +67,24 @@ const dispatch = ( } }); }); + } else { + /** Otherwise, use the callback function */ + dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); + const message = { + id: dispatcherQueue.index++, + payload: payload, + }; + worker.postMessage(message); + console.log({ + caller: 'dispatcher-main / message sent', + message, + dispatcherQueue, + }); } - /** Otherwise, use the callback function */ - dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); - const message = { - id: dispatcherQueue.index++, - payload: payload, - }; - worker.postMessage(message); - console.log({ - caller: 'dispatcher-main / message sent', - message, - dispatcherQueue, - }); }); + if (returnValue) { + return returnValue; + } }; export const cancelDispatch = (id: number) => {