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,63 +34,64 @@ 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,
}) });
.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<any>; params: any }) => { export const watchDb = async (p: { signal: Signal<any>; params: any }) => {
const { signal } = p; const { signal } = p;
const [, setStatus] = signal; const [status, setStatus] = signal;
console.log({ caller: 'watchDb', p }); console.log({ caller: 'watchDb', p });
await initDb(); await initDb();
db = globalThis.db; db = globalThis.db;
localDb = globalThis.localDb; localDb = globalThis.localDb;
const dbinfo = await db.info(); setStatus({ opened: true });
const localDbInfo = await localDb.info();
const tasks = PouchDB.activeTasks.list();
setStatus({ opened: true, dbinfo, localDbinfo: localDbInfo, tasks }); if (globalThis.sync) {
globalThis.sync
return; .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 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[]; // const docs = (await getAll(store)) as any[];
// setStatus({
// status: 'docs',
// db: 'dyomedea',
// docs,
// });
docs.forEach(async (doc) => { // docs.forEach(async (doc) => {
const { id, rev, deleted } = doc; // const { id, rev, deleted } = doc;
if (deleted !== 0) { // if (deleted !== 0) {
// const purge = await db.purge(id, rev); // // const purge = await db.purge(id, rev);
console.log({ // console.log({
caller: 'watchDb', // caller: 'watchDb',
message: 'purging', // message: 'purging',
id, // id,
rev, // rev,
doc, // doc,
// purge, // // purge,
}); // });
await sleep(1000); // 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,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) => { export const cancelDispatch = (id: number) => {