Switching from shared web workers (not supported by chrome on android) to "normal" web workers.

This commit is contained in:
Eric van der Vlist 2022-11-17 16:10:44 +01:00
parent e4621e4651
commit ad3d2125bb
4 changed files with 19 additions and 21 deletions

View File

@ -23,7 +23,9 @@ export const Gpxes: react.FC<GpxesProperties> = (props: GpxesProperties) => {
console.log(`Gpxes, visibles: ${JSON.stringify(gpxes)}`);
setVisibleGpxes(gpxes);
};
getVisibleGpxes();
if (props.viewPort !== undefined) {
getVisibleGpxes();
}
}, [
props.viewPort?.bottomRight.x,
props.viewPort?.bottomRight.y,

View File

@ -13,7 +13,7 @@ export const init = () => {
console.log(`worker: ${worker}`);
worker.port.onmessage = (event: any) => {
worker.onmessage = (event: any) => {
const { id, payload } = event.data;
dispatcherQueue.queue.get(id)(null, payload);
dispatcherQueue.queue.delete(id);
@ -45,7 +45,7 @@ const dispatch = (
id: dispatcherQueue.index++,
payload: payload,
};
worker.port.postMessage(message);
worker.postMessage(message);
};
export default dispatch;

View File

@ -9,7 +9,7 @@ import {
import { getTrk, putNewTrk } from '../db/trk';
import { getTrkseg } from '../db/trkseg';
const self = globalThis as unknown as SharedWorkerGlobalScope;
const self = globalThis as unknown as WorkerGlobalScope;
const actions = {
initDb,
@ -20,24 +20,20 @@ const actions = {
getGpxesForViewport,
getGpx,
getTrk,
getTrkseg
getTrkseg,
};
self.onconnect = function (e) {
var port = e.ports[0];
port.onmessage = async function (e) {
console.log(`Worker received ${JSON.stringify(e.data)}`);
const { id, payload } = e.data;
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 });
};
onmessage = async function (e) {
console.log(`Worker received ${JSON.stringify(e.data)}`);
const { id, payload } = e.data;
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
);
}
postMessage({ id: id, payload: returnValue });
};
export default self;

View File

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