More work with web workers (pffff!)

This commit is contained in:
Eric van der Vlist 2022-11-06 22:29:33 +01:00
parent 9089b08c6e
commit 94de17938e
4 changed files with 32 additions and 17 deletions

View File

@ -26,6 +26,9 @@ import '@ionic/react/css/display.css';
/* Theme variables */
import './theme/variables.css';
import dispatch from './workers/dispatcher-main';
import LiveMap from './components/map/LiveMap';
import { atom, useAtom } from 'jotai';
@ -37,10 +40,10 @@ import { geoPoint } from './components/map/types';
import Back from './components/buttons/Back';
import Forward from './components/buttons/Forward';
import CurrentLocation from './components/map/CurrentLocation';
import { initDb } from './db';
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';
PouchDB.plugin(PouchDBFind);
// import { initDb } from './db';
// import PouchDB from 'pouchdb';
// import PouchDBFind from 'pouchdb-find';
// PouchDB.plugin(PouchDBFind);
setupIonicReact();
@ -88,8 +91,10 @@ export const setCenterAtom = atom(null, (get, set, center: geoPoint) => {
set(scopeAtom, newScope);
});
const db = new PouchDB('dyomedea', { auto_compaction: true, revs_limit: 10 });
initDb(db);
// const db = new PouchDB('dyomedea', { auto_compaction: true, revs_limit: 10 });
// initDb(db);
dispatch({ action: 'initDb' });
/**
*

View File

@ -1,19 +1,21 @@
import dispatch, { init, worker } from './dispatcher-main';
var mockPostMessage = jest.fn();
jest.mock('./get-worker', () => ({
getWorker: () => ({
port: {
postMessage: jest.fn(),
},
}),
}));
describe('The dispatcher-main', () => {
var postMessage = jest.fn();
beforeEach(() => {
global.SharedWorker = jest.fn(function (source) {
this.source = source;
this.port = {
postMessage,
};
return this;
});
init();
});
test('should create a new shared web worker', () => {
expect(SharedWorker).toBeCalled();
expect(worker).toBeDefined();
});
test('should create a onmessage function', () => {
expect(worker.port.onmessage).toBeDefined();
@ -23,6 +25,6 @@ describe('The dispatcher-main', () => {
});
test('should send back the message', () => {
dispatch('ping');
expect(postMessage).toBeCalledWith({ id: 0, payload: 'ping' });
expect(worker.port.postMessage).toBeCalledWith({ id: 0, payload: 'ping' });
});
});

View File

@ -1,3 +1,5 @@
import { getWorker } from './get-worker';
declare global {
var dispatcherQueue: { index: number; queue: Map<number, any> };
}
@ -7,7 +9,9 @@ export var worker: any;
export const init = () => {
globalThis.dispatcherQueue = { index: 0, queue: new Map() };
worker = new SharedWorker('./dispatcher-worker');
worker = getWorker();
console.log(`worker: ${worker}`)
worker.port.onmessage = (event: any) => {
const { id, payload } = event.data;

View File

@ -0,0 +1,4 @@
export const getWorker = () =>
new SharedWorker(new URL('./dispatcher-worker', import.meta.url));
export default getWorker;