This commit is contained in:
Eric van der Vlist 2023-02-11 17:52:11 +01:00
parent 79fed3cc28
commit 853b158dcf
3 changed files with 91 additions and 90 deletions

View File

@ -34,6 +34,10 @@ const Account: Component<{}> = (props) => {
const newAccounts: any = await dispatch({ const newAccounts: any = await dispatch({
action: 'getAccounts', action: 'getAccounts',
}); });
console.log({
caller: 'Account / handleClickOpen',
newAccounts,
});
setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]); setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]);
setAccount( setAccount(
cloneDeep(getAccountById(accounts(), settings().currentAccountId)) cloneDeep(getAccountById(accounts(), settings().currentAccountId))

View File

@ -2,23 +2,19 @@ import { Signal } from 'solid-js';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import indexeddb from 'pouchdb-adapter-indexeddb'; import indexeddb from 'pouchdb-adapter-indexeddb';
import { getSettings, putSettings } from '../db/settings';
import { sleep } from '../lib/async-wait'; import { sleep } from '../lib/async-wait';
import {
getAccounts,
initialAccount,
putAccount,
getAccountById,
} from '../db/account';
import { openDatabases } from './open'; import { openDatabases } from './open';
import { toHex } from '../lib/to-hex'; import { toHex } from '../lib/to-hex';
import { isEqual } from 'lodash';
PouchDB.plugin(indexeddb); PouchDB.plugin(indexeddb);
declare global { declare global {
var localDb: any; var localDb: any;
var db: any; var db: any;
var remoteDb: any;
var currentAccount: any; var currentAccount: any;
var sync: any;
} }
const initDb = async () => { const initDb = async () => {
@ -38,10 +34,28 @@ const initDb = async () => {
}, },
skip_setup: true, skip_setup: true,
}); });
const sync = PouchDB.sync(db, remoteDb, { globalThis.remoteDb = remoteDb;
globalThis.sync = PouchDB.sync(db, remoteDb, {
live: true, live: true,
retry: true, retry: true,
}) });
}
}
};
export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
const { signal } = p;
const [status, setStatus] = signal;
console.log({ caller: 'watchDb', p });
await initDb();
db = globalThis.db;
localDb = globalThis.localDb;
setStatus({ opened: true });
if (globalThis.sync) {
globalThis.sync
.on('change', function (info) { .on('change', function (info) {
// handle change // handle change
console.log({ caller: 'Sync / change', info }); console.log({ caller: 'Sync / change', info });
@ -67,34 +81,17 @@ const initDb = async () => {
console.error({ caller: 'Sync / error', err }); console.error({ caller: 'Sync / error', err });
}); });
} }
}
};
export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
const { signal } = p;
const [, 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, dbinfo, localDbinfo: localDbInfo, tasks });
return;
const timerId = setInterval(async () => { 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(); const tasks = PouchDB.activeTasks.list();
if (tasks.length > 0) { const newStatus = { ...status(), tasks, dbInfo, localDbInfo, remoteDbInfo };
setStatus({ status: 'ongoing', db: 'dyomedea', dbinfo, tasks }); if (!isEqual(status(), newStatus)) {
} else { setStatus(newStatus);
setStatus({ status: 'terminated', db: 'dyomedea', dbinfo, tasks });
clearInterval(timerId);
} }
}, 1000); }, 1000);
@ -144,37 +141,32 @@ export const watchDb = async (p: { signal: Signal<any>; 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 bySequence = iDb.transaction('docs', 'readonly');
const store = bySequence.objectStore('docs'); // const store = bySequence.objectStore('docs');
console.log({ // console.log({
caller: 'watchDb', // caller: 'watchDb',
message: 'transaction opened', // message: 'transaction opened',
bySequence, // bySequence,
store, // store,
});
const docs = (await getAll(store)) as any[];
// setStatus({
// status: 'docs',
// db: 'dyomedea',
// docs,
// }); // });
docs.forEach(async (doc) => { // const docs = (await getAll(store)) as any[];
const { id, rev, deleted } = doc;
if (deleted !== 0) { // docs.forEach(async (doc) => {
// const purge = await db.purge(id, rev); // const { id, rev, deleted } = doc;
console.log({ // if (deleted !== 0) {
caller: 'watchDb', // // const purge = await db.purge(id, rev);
message: 'purging', // console.log({
id, // caller: 'watchDb',
rev, // message: 'purging',
doc, // id,
// purge, // rev,
}); // doc,
await sleep(1000); // // purge,
} // });
}); // await sleep(1000);
// }
// });
}; };

View File

@ -53,11 +53,12 @@ const dispatch = (
if (worker === undefined) { if (worker === undefined) {
init(); init();
} }
until(dbReady, 1000).then(() => { let returnValue;
until(dbReady, 100).then(() => {
// Wait until databases have been created by health.ts // Wait until databases have been created by health.ts
if (callBack === undefined) { if (callBack === undefined) {
/** If a callback function is not provided, return a promise */ /** If a callback function is not provided, return a promise */
return new Promise((resolve, reject) => { returnValue = new Promise((resolve, reject) => {
dispatch(payload, (error, result) => { dispatch(payload, (error, result) => {
if (error) { if (error) {
reject(error); reject(error);
@ -66,7 +67,7 @@ const dispatch = (
} }
}); });
}); });
} } else {
/** Otherwise, use the callback function */ /** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = { const message = {
@ -79,7 +80,11 @@ const dispatch = (
message, message,
dispatcherQueue, dispatcherQueue,
}); });
}
}); });
if (returnValue) {
return returnValue;
}
}; };
export const cancelDispatch = (id: number) => { export const cancelDispatch = (id: number) => {