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({
action: 'getAccounts',
});
console.log({
caller: 'Account / handleClickOpen',
newAccounts,
});
setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]);
setAccount(
cloneDeep(getAccountById(accounts(), settings().currentAccountId))

View File

@ -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,10 +34,28 @@ 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,
})
});
}
}
};
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) {
// handle change
console.log({ caller: 'Sync / change', info });
@ -67,34 +81,17 @@ const initDb = async () => {
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 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<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 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 bySequence = iDb.transaction('docs', 'readonly');
// const store = bySequence.objectStore('docs');
// console.log({
// caller: 'watchDb',
// message: 'transaction opened',
// bySequence,
// store,
// });
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);
}
});
// 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);
// }
// });
};

View File

@ -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,7 +67,7 @@ const dispatch = (
}
});
});
}
} else {
/** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = {
@ -79,7 +80,11 @@ const dispatch = (
message,
dispatcherQueue,
});
}
});
if (returnValue) {
return returnValue;
}
};
export const cancelDispatch = (id: number) => {