From 6dfe98b655cecfca563e178f17c37f9cda34cc98 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 13 Feb 2023 15:44:02 +0100 Subject: [PATCH] Refactoring the worker / db init flow. --- src/db/gpx.ts | 16 ++++--- src/db/index.ts | 2 + src/index.tsx | 7 +-- src/workers/dispatcher-main.ts | 76 ++++++++++++++++++-------------- src/workers/dispatcher-worker.ts | 4 +- 5 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/db/gpx.ts b/src/db/gpx.ts index 288cdaf..babaad0 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -439,12 +439,16 @@ export const putGpx = async (params: any) => { await put(id, 'gpx', (doc) => gpx, gpx); if (extensions !== undefined) { - await put( - `${id}/4extensions`, - 'extensions', - (doc) => extensions, - extensions - ); + try { + await put( + `${id}/4extensions`, + 'extensions', + (doc) => extensions, + extensions + ); + } catch (error) { + console.error({ caller: 'putGpx / extensions', error }); + } } if (previousShared !== extensions?.shared) { diff --git a/src/db/index.ts b/src/db/index.ts index 6b507cd..e1982fd 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -99,5 +99,7 @@ export const initDb = async () => { globalThis.dbReady = true; watchDbLegacy(); + console.log({ caller: 'initDb / done' }); + return; } }; diff --git a/src/index.tsx b/src/index.tsx index 5a60fda..4af0597 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,18 +1,13 @@ /* @refresh reload */ import { render } from 'solid-js/web'; -import { Router, hashIntegration } from '@solidjs/router'; +import { Router } from '@solidjs/router'; import { I18nContext, createI18nContext, - useI18n, } from '@solid-primitives/i18n'; import dict from './i18n'; import App from './App'; -import { Language } from '@suid/icons-material'; -import { createWorkerSignal } from './solid-workers/solid-worker-main'; -import { createEffect } from 'solid-js'; -import dispatch from './workers/dispatcher-main'; // See https://stackoverflow.com/questions/71538643/property-wakelock-does-not-exist-on-type-navigator const requestWakeLock = async () => { diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index b20d320..ef58275 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -1,23 +1,21 @@ -import { createEffect } from 'solid-js'; import { until } from '../lib/async-wait'; -import { createWorkerSignal } from '../solid-workers/solid-worker-main'; import { getWorker } from './get-worker'; declare global { var dispatcherQueue: { index: number; queue: Map }; + var dbReady: boolean; + var worker: any; } -export var worker: any; - export const init = () => { - console.log({ caller: 'dispatcher-main / init' }); + // console.log({ caller: 'dispatcher-main / init' }); globalThis.dispatcherQueue = { index: 0, queue: new Map() }; - console.log({ caller: 'dispatcher-main / init', globalThis }); + // console.log({ caller: 'dispatcher-main / init', globalThis }); - worker = getWorker(); - console.log({ caller: 'dispatcher-main / init', worker }); + globalThis.worker = getWorker(); + // console.log({ caller: 'dispatcher-main / init', worker }); - worker.onmessage = (event: any) => { + globalThis.worker.onmessage = (event: any) => { const { id, payload } = event.data; // console.log({ // caller: 'dispatcher-main / message received', @@ -25,35 +23,42 @@ export const init = () => { // payload, // dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)), // }); - dispatcherQueue.queue.get(id).callBack(null, payload, id); - if (!dispatcherQueue.queue.get(id).live) { + globalThis.dispatcherQueue.queue.get(id).callBack(null, payload, id); + if (!globalThis.dispatcherQueue.queue.get(id).live) { cancelDispatch(id); } }; }; -const [watchDbStatus] = createWorkerSignal({ - provider: 'watchDb', -}); -const dbReady = () => { - // console.log({ - // caller: 'dispatcher-main / waiting', - // dbStatus: watchDbStatus(), - // dbReady: watchDbStatus()?.opened, - // }); - return watchDbStatus()?.opened; -}; - const dispatch = async ( payload: any, callBack?: (error: any, result: any, id?: number | undefined) => void, live?: boolean ) => { - // console.log({ caller: 'dispatcher-main / dispatch', payload, callBack }); - if (worker === undefined) { + let initializing = false; + console.log({ + caller: 'dispatcher-main / dispatch', + payload, + callBack, + initializing, + worker: globalThis.worker, + }); + if (globalThis.worker === undefined && !initializing) { + initializing = true; init(); + await until(() => !!globalThis.worker, 100); // wait until the worker is initialized + dispatch({ action: 'initDb' }, (error, result) => { + if (error) { + console.error({ caller: 'worker-main / init', error }); + return; + } + globalThis.dbReady = true; + }); + initializing = false; + } + if (payload.action !== 'initDb') { + await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized } - await until(dbReady, 100); // Wait until databases have been created by health.ts if (callBack === undefined) { /** If a callback function is not provided, return a promise */ return new Promise((resolve, reject) => { @@ -67,21 +72,24 @@ const dispatch = async ( }); } /** Otherwise, use the callback function */ - dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); + globalThis.dispatcherQueue.queue.set(globalThis.dispatcherQueue.index, { + callBack, + live, + }); const message = { - id: dispatcherQueue.index++, + id: globalThis.dispatcherQueue.index++, payload: payload, }; worker.postMessage(message); - // console.log({ - // caller: 'dispatcher-main / message sent', - // message, - // dispatcherQueue, - // }); + console.log({ + caller: 'dispatcher-main / message sent', + message, + dispatcherQueue: globalThis.dispatcherQueue, + }); }; export const cancelDispatch = (id: number) => { - dispatcherQueue.queue.delete(id); + globalThis.dispatcherQueue.queue.delete(id); }; export default dispatch; diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 58d81bc..1043097 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -68,9 +68,9 @@ onmessage = async function (e) { }; // console.log({ caller: 'dispatcher-worker / onmessage', e }); - initDb(); + // initDb(); - await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized in this worker + // await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized in this worker const { id, payload } = e.data; var returnValue: any = 'unknownAction'; if (payload.action in actions) {