Implementing a mechanism to purge deleted docs.
This commit is contained in:
parent
534ec32312
commit
5a5931dbcd
|
@ -1,10 +1,8 @@
|
|||
import { createEffect, createRoot, createSignal, Signal } from 'solid-js';
|
||||
import { createEffect, createRoot, createSignal } from 'solid-js';
|
||||
import PouchDB from 'pouchdb';
|
||||
import indexeddb from 'pouchdb-adapter-indexeddb';
|
||||
|
||||
import { sleep, until } from '../lib/async-wait';
|
||||
import { openDatabases } from './open';
|
||||
import { toHex } from '../lib/to-hex';
|
||||
import { isEqual } from 'lodash';
|
||||
|
||||
PouchDB.plugin(indexeddb);
|
||||
|
@ -108,18 +106,15 @@ export const watchDbLegacy = async () => {
|
|||
});
|
||||
}
|
||||
|
||||
const timerId = setInterval(updateStatus, 10000);
|
||||
const timerId = setInterval(updateStatus, 15000);
|
||||
|
||||
// db.compact();
|
||||
|
||||
// await sleep(10000);
|
||||
|
||||
const openIDb = (name: string) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const iDb = indexedDB.open(name);
|
||||
iDb.onerror = (event: any) => {
|
||||
console.error({
|
||||
caller: 'watchDb',
|
||||
caller: 'watchDbLegacy',
|
||||
message: 'open db error',
|
||||
target: event.target,
|
||||
});
|
||||
|
@ -127,11 +122,6 @@ export const watchDbLegacy = async () => {
|
|||
};
|
||||
|
||||
iDb.onsuccess = (event: any) => {
|
||||
console.log({
|
||||
caller: 'watchDb',
|
||||
message: 'open db',
|
||||
target: event.target,
|
||||
});
|
||||
resolve(event.target.result);
|
||||
};
|
||||
});
|
||||
|
@ -141,7 +131,7 @@ export const watchDbLegacy = async () => {
|
|||
const request = store.getAll();
|
||||
request.onerror = (event: any) => {
|
||||
console.error({
|
||||
caller: 'watchDb',
|
||||
caller: 'watchDbLegacy',
|
||||
message: 'getAll error',
|
||||
target: event.target,
|
||||
});
|
||||
|
@ -149,15 +139,36 @@ export const watchDbLegacy = async () => {
|
|||
};
|
||||
|
||||
request.onsuccess = (event: any) => {
|
||||
console.log({
|
||||
caller: 'watchDb',
|
||||
message: 'getAll',
|
||||
target: event.target,
|
||||
});
|
||||
resolve(event.target.result as any[]);
|
||||
};
|
||||
});
|
||||
|
||||
const getDeletedDocs = async () => {
|
||||
const iDb = (await openIDb(`_pouch_${state().dbName}`)) as IDBDatabase;
|
||||
const bySequence = iDb.transaction('docs', 'readonly');
|
||||
const store = bySequence.objectStore('docs');
|
||||
const allDocs = (await getAll(store)) as any[];
|
||||
iDb.close();
|
||||
const docs = allDocs
|
||||
.filter(
|
||||
(doc) =>
|
||||
!doc.id.startsWith('_local') &&
|
||||
doc.deleted === 1 &&
|
||||
doc.seq <= state()?.sync?.lastSeq
|
||||
)
|
||||
.map((doc) => ({ id: doc.id, rev: doc.rev }));
|
||||
// console.log({
|
||||
// caller: 'watchDbLegacy / getDeletedDocs',
|
||||
// // bySequence,
|
||||
// // store,
|
||||
// // allDocs,
|
||||
// docs,
|
||||
// state: state(),
|
||||
// });
|
||||
return docs;
|
||||
};
|
||||
|
||||
while (true) {
|
||||
await until(
|
||||
() =>
|
||||
state()?.sync?.paused &&
|
||||
|
@ -166,33 +177,33 @@ export const watchDbLegacy = async () => {
|
|||
);
|
||||
|
||||
console.log({ caller: 'watchDbLegacy / ready to purge after sync ' });
|
||||
// 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',
|
||||
const deletedDocs = await getDeletedDocs();
|
||||
|
||||
if (deletedDocs.length > 0) {
|
||||
console.log({
|
||||
caller: 'watchDbLegacy / purge',
|
||||
// bySequence,
|
||||
// store,
|
||||
// });
|
||||
// allDocs,
|
||||
deletedDocs,
|
||||
state: state(),
|
||||
});
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// });
|
||||
deletedDocs.forEach(async (doc) => {
|
||||
await sleep(500);
|
||||
try {
|
||||
await db.purge(doc.id, doc.rev);
|
||||
} catch (err) {
|
||||
console.log({
|
||||
caller: 'watchDbLegacy / purge',
|
||||
doc,
|
||||
err,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
await sleep(60000);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue