Testingpouchdb change handler

This commit is contained in:
Eric van der Vlist 2022-12-05 18:49:36 +01:00
parent 063bee4e28
commit 6465a371cb
3 changed files with 40 additions and 1 deletions

6
src/db/change-handler.ts Normal file
View File

@ -0,0 +1,6 @@
const changeHandler = (change: any) => {
console.log({caller: 'ChangeHandler', change});
}
export default changeHandler;

View File

@ -2,6 +2,7 @@ import _ from 'lodash';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find'; import PouchDBFind from 'pouchdb-find';
import uri from '../lib/ids'; import uri from '../lib/ids';
import changeHandler from './change-handler';
PouchDB.plugin(PouchDBFind); PouchDB.plugin(PouchDBFind);
@ -19,6 +20,8 @@ declare global {
} }
export const initDb = async (params: any) => { export const initDb = async (params: any) => {
console.log({ caller: 'initDb' });
if (globalThis.db === undefined) { if (globalThis.db === undefined) {
globalThis.db = new PouchDB('dyomedea', { globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false, auto_compaction: false,
@ -93,6 +96,26 @@ export const initDb = async (params: any) => {
globalThis.dbReady = true; globalThis.dbReady = true;
console.log({ caller: 'initDb / before db.changes' });
const changes = db
.changes({ since: 'now', live: true, include_docs: true })
.on('change', changeHandler)
// .on('change', (info: any) => {
// console.log({ caller: 'changes / change', info });
// changeHandler(info);
// })
.on('complete', (info: any) => {
console.log({ caller: 'changes / complete', info });
})
.on('error', (error: any) => {
console.log({ caller: 'changes / complete', error });
});
console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel();
/* const indexes = await db.getIndexes(); /* const indexes = await db.getIndexes();
console.log(`indexes: ${JSON.stringify(indexes)}`); console.log(`indexes: ${JSON.stringify(indexes)}`);

View File

@ -16,6 +16,12 @@ export const init = () => {
worker.onmessage = (event: any) => { worker.onmessage = (event: any) => {
const { id, payload } = event.data; const { id, payload } = event.data;
console.log({
caller: 'dispatcher-main / message received',
id,
payload,
dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
});
dispatcherQueue.queue.get(id)(null, payload); dispatcherQueue.queue.get(id)(null, payload);
dispatcherQueue.queue.delete(id); dispatcherQueue.queue.delete(id);
}; };
@ -50,7 +56,11 @@ const dispatch = (
payload: payload, payload: payload,
}; };
worker.postMessage(message); worker.postMessage(message);
console.log({ caller: 'dispatcher-main / message sent', message }); console.log({
caller: 'dispatcher-main / message sent',
message,
dispatcherQueue,
});
}; };
export default dispatch; export default dispatch;