diff --git a/src/db/index.ts b/src/db/index.ts index 87a2a6f..d243570 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,6 +1,10 @@ import _ from 'lodash'; +import PouchDB from 'pouchdb'; +import PouchDBFind from 'pouchdb-find'; import uri from '../lib/ids'; +PouchDB.plugin(PouchDBFind); + const dbDefinitionId = uri('dbdef', {}); const currentDbDefinition = { @@ -9,7 +13,19 @@ const currentDbDefinition = { def: { version: '0.000001' }, }; -export const initDb = async (db: any, setDbReady?: any) => { +declare global { + var db: any; + var dbReady: boolean; +} + +export const initDb = async (params: any) => { + if (globalThis.db === undefined) { + globalThis.db = new PouchDB('dyomedea', { + auto_compaction: true, + revs_limit: 10, + }); + } + const db = globalThis.db; var previousDbDefinition = { _id: dbDefinitionId, type: dbDefinitionId, @@ -81,7 +97,7 @@ export const initDb = async (db: any, setDbReady?: any) => { } } - if (setDbReady !== undefined) setDbReady(true); + globalThis.dbReady = true; /* const indexes = await db.getIndexes(); console.log(`indexes: ${JSON.stringify(indexes)}`); diff --git a/src/workers/dispatcher-main.test.js b/src/workers/dispatcher-main.test.js index 0b9504c..283d078 100644 --- a/src/workers/dispatcher-main.test.js +++ b/src/workers/dispatcher-main.test.js @@ -1,6 +1,4 @@ -import dispatch, { init, worker } from './dispatcher-main'; - -var mockPostMessage = jest.fn(); +import dispatch, { init, worker } from './dispatcher-main' jest.mock('./get-worker', () => ({ getWorker: () => ({ diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index 57f9ca3..eccb8fd 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -11,7 +11,7 @@ export const init = () => { worker = getWorker(); - console.log(`worker: ${worker}`) + console.log(`worker: ${worker}`); worker.port.onmessage = (event: any) => { const { id, payload } = event.data; diff --git a/src/workers/dispatcher-worker.test.js b/src/workers/dispatcher-worker.test.js index 8e35a5f..f9d1e8f 100644 --- a/src/workers/dispatcher-worker.test.js +++ b/src/workers/dispatcher-worker.test.js @@ -14,6 +14,9 @@ describe('The dispatcher-worker ', () => { }); test('receives a ping and sends a pong', () => { port.onmessage({ data: { id: 5, payload: 'ping' } }); - expect(port.postMessage).toBeCalledWith({ id: 5, payload: '"ping" pong' }); + expect(port.postMessage).toBeCalledWith({ + id: 5, + payload: 'unknownAction', + }); }); }); diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 4660867..4ff2eee 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -1,12 +1,23 @@ +import { initDb } from '../db'; + const self = globalThis as unknown as SharedWorkerGlobalScope; +const actions = { initDb }; + self.onconnect = function (e) { var port = e.ports[0]; - port.onmessage = function (e) { - console.log(`Worker received ${JSON.stringify(e)}`); + port.onmessage = async function (e) { + console.log(`Worker received ${JSON.stringify(e.data)}`); const { id, payload } = e.data; - port.postMessage({ id: id, payload: `${JSON.stringify(payload)} pong` }); + console.log(`payload.action in actions: ${payload.action in actions}`); + var returnValue: any = 'unknownAction'; + if (payload.action in actions) { + returnValue = await actions[payload.action]( + payload.params + ); + } + port.postMessage({ id: id, payload: returnValue }); }; };