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

View File

@ -1,19 +1,21 @@
import dispatch, { init, worker } from './dispatcher-main'; import dispatch, { init, worker } from './dispatcher-main';
var mockPostMessage = jest.fn();
jest.mock('./get-worker', () => ({
getWorker: () => ({
port: {
postMessage: jest.fn(),
},
}),
}));
describe('The dispatcher-main', () => { describe('The dispatcher-main', () => {
var postMessage = jest.fn();
beforeEach(() => { beforeEach(() => {
global.SharedWorker = jest.fn(function (source) {
this.source = source;
this.port = {
postMessage,
};
return this;
});
init(); init();
}); });
test('should create a new shared web worker', () => { test('should create a new shared web worker', () => {
expect(SharedWorker).toBeCalled(); expect(worker).toBeDefined();
}); });
test('should create a onmessage function', () => { test('should create a onmessage function', () => {
expect(worker.port.onmessage).toBeDefined(); expect(worker.port.onmessage).toBeDefined();
@ -23,6 +25,6 @@ describe('The dispatcher-main', () => {
}); });
test('should send back the message', () => { test('should send back the message', () => {
dispatch('ping'); 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 { declare global {
var dispatcherQueue: { index: number; queue: Map<number, any> }; var dispatcherQueue: { index: number; queue: Map<number, any> };
} }
@ -7,7 +9,9 @@ export var worker: any;
export const init = () => { export const init = () => {
globalThis.dispatcherQueue = { index: 0, queue: new Map() }; globalThis.dispatcherQueue = { index: 0, queue: new Map() };
worker = new SharedWorker('./dispatcher-worker'); worker = getWorker();
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

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