From 6465a371cb480842c10acdbe439b957f056700a2 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 5 Dec 2022 18:49:36 +0100 Subject: [PATCH] Testingpouchdb change handler --- src/db/change-handler.ts | 6 ++++++ src/db/index.ts | 23 +++++++++++++++++++++++ src/workers/dispatcher-main.ts | 12 +++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/db/change-handler.ts diff --git a/src/db/change-handler.ts b/src/db/change-handler.ts new file mode 100644 index 0000000..63019fa --- /dev/null +++ b/src/db/change-handler.ts @@ -0,0 +1,6 @@ + +const changeHandler = (change: any) => { + console.log({caller: 'ChangeHandler', change}); +} + +export default changeHandler; \ No newline at end of file diff --git a/src/db/index.ts b/src/db/index.ts index 10a0aa9..da90125 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -2,6 +2,7 @@ import _ from 'lodash'; import PouchDB from 'pouchdb'; import PouchDBFind from 'pouchdb-find'; import uri from '../lib/ids'; +import changeHandler from './change-handler'; PouchDB.plugin(PouchDBFind); @@ -19,6 +20,8 @@ declare global { } export const initDb = async (params: any) => { + console.log({ caller: 'initDb' }); + if (globalThis.db === undefined) { globalThis.db = new PouchDB('dyomedea', { auto_compaction: false, @@ -93,6 +96,26 @@ export const initDb = async (params: any) => { 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(); console.log(`indexes: ${JSON.stringify(indexes)}`); diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index 4e035a8..60d1aea 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -16,6 +16,12 @@ export const init = () => { worker.onmessage = (event: any) => { 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.delete(id); }; @@ -50,7 +56,11 @@ const dispatch = ( payload: payload, }; worker.postMessage(message); - console.log({ caller: 'dispatcher-main / message sent', message }); + console.log({ + caller: 'dispatcher-main / message sent', + message, + dispatcherQueue, + }); }; export default dispatch;