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)}`); console.log(`Gpxes, visibles: ${JSON.stringify(gpxes)}`);
setVisibleGpxes(gpxes); setVisibleGpxes(gpxes);
}; };
if (props.viewPort !== undefined) {
getVisibleGpxes(); getVisibleGpxes();
}
}, [ }, [
props.viewPort?.bottomRight.x, props.viewPort?.bottomRight.x,
props.viewPort?.bottomRight.y, props.viewPort?.bottomRight.y,

View File

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

View File

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

View File

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