More fun with web workers...

This commit is contained in:
Eric van der Vlist 2022-11-07 11:44:35 +01:00
parent 94de17938e
commit ebf4f39331
5 changed files with 38 additions and 10 deletions

View File

@ -1,6 +1,10 @@
import _ from 'lodash'; import _ from 'lodash';
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';
import uri from '../lib/ids'; import uri from '../lib/ids';
PouchDB.plugin(PouchDBFind);
const dbDefinitionId = uri('dbdef', {}); const dbDefinitionId = uri('dbdef', {});
const currentDbDefinition = { const currentDbDefinition = {
@ -9,7 +13,19 @@ const currentDbDefinition = {
def: { version: '0.000001' }, 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 = { var previousDbDefinition = {
_id: dbDefinitionId, _id: dbDefinitionId,
type: 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(); /* const indexes = await db.getIndexes();
console.log(`indexes: ${JSON.stringify(indexes)}`); console.log(`indexes: ${JSON.stringify(indexes)}`);

View File

@ -1,6 +1,4 @@
import dispatch, { init, worker } from './dispatcher-main'; import dispatch, { init, worker } from './dispatcher-main'
var mockPostMessage = jest.fn();
jest.mock('./get-worker', () => ({ jest.mock('./get-worker', () => ({
getWorker: () => ({ getWorker: () => ({

View File

@ -11,7 +11,7 @@ export const init = () => {
worker = getWorker(); worker = getWorker();
console.log(`worker: ${worker}`) console.log(`worker: ${worker}`);
worker.port.onmessage = (event: any) => { worker.port.onmessage = (event: any) => {
const { id, payload } = event.data; const { id, payload } = event.data;

View File

@ -14,6 +14,9 @@ describe('The dispatcher-worker ', () => {
}); });
test('receives a ping and sends a pong', () => { test('receives a ping and sends a pong', () => {
port.onmessage({ data: { id: 5, payload: 'ping' } }); port.onmessage({ data: { id: 5, payload: 'ping' } });
expect(port.postMessage).toBeCalledWith({ id: 5, payload: '"ping" pong' }); expect(port.postMessage).toBeCalledWith({
id: 5,
payload: 'unknownAction',
});
}); });
}); });

View File

@ -1,12 +1,23 @@
import { initDb } from '../db';
const self = globalThis as unknown as SharedWorkerGlobalScope; const self = globalThis as unknown as SharedWorkerGlobalScope;
const actions = { initDb };
self.onconnect = function (e) { self.onconnect = function (e) {
var port = e.ports[0]; var port = e.ports[0];
port.onmessage = function (e) { port.onmessage = async function (e) {
console.log(`Worker received ${JSON.stringify(e)}`); console.log(`Worker received ${JSON.stringify(e.data)}`);
const { id, payload } = 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[<keyof typeof actions>payload.action](
payload.params
);
}
port.postMessage({ id: id, payload: returnValue });
}; };
}; };