From ad3d2125bb2c8a590e83fee0abb41b00ee0ac27f Mon Sep 17 00:00:00 2001 From: evlist Date: Thu, 17 Nov 2022 16:10:44 +0100 Subject: [PATCH] Switching from shared web workers (not supported by chrome on android) to "normal" web workers. --- src/components/map/Gpxes.tsx | 4 +++- src/workers/dispatcher-main.ts | 4 ++-- src/workers/dispatcher-worker.ts | 30 +++++++++++++----------------- src/workers/get-worker.ts | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/components/map/Gpxes.tsx b/src/components/map/Gpxes.tsx index 30d37c9..90b6bd7 100644 --- a/src/components/map/Gpxes.tsx +++ b/src/components/map/Gpxes.tsx @@ -23,7 +23,9 @@ export const Gpxes: react.FC = (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, diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index eccb8fd..8eccf14 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -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; diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index da2d311..1795320 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -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[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[payload.action]( + payload.params + ); + } + postMessage({ id: id, payload: returnValue }); }; export default self; diff --git a/src/workers/get-worker.ts b/src/workers/get-worker.ts index fff7835..d280573 100644 --- a/src/workers/get-worker.ts +++ b/src/workers/get-worker.ts @@ -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;