From ef909b475b8727d278b2f7df9778dd12aea52055 Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Sat, 11 Mar 2023 18:35:14 +0100 Subject: [PATCH] Adding a self test to check that web workers are supported --- src/components/system-info/SystemInfo.tsx | 87 ++++++++++++++--------- src/components/system-info/index.ts | 1 + src/workers/dispatcher-worker.ts | 3 +- 3 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 src/components/system-info/index.ts diff --git a/src/components/system-info/SystemInfo.tsx b/src/components/system-info/SystemInfo.tsx index a70cab7..a5dbc4c 100644 --- a/src/components/system-info/SystemInfo.tsx +++ b/src/components/system-info/SystemInfo.tsx @@ -1,55 +1,66 @@ import { - Icon, ListItem, ListItemButton, ListItemIcon, ListItemText, - SvgIcon, } from '@suid/material'; -import { Component, createSignal, Show } from 'solid-js'; +import { Component, createEffect, createSignal, For, Show } from 'solid-js'; import SystemSecurityUpdateWarningIcon from '@suid/icons-material/SystemSecurityUpdateWarning'; import Dialog from '../dialog'; import dispatch from '../../workers/dispatcher-main'; +const [error, setError] = createSignal([]); + +export const reportError = (err: any) => { + setError([...error(), err]); +}; + interface Props {} const SystemInfo: Component = (props) => { const [open, setOpen] = createSignal(false); const [settings, setSettings] = createSignal(); const [account, setAccount] = createSignal(); - const [error, setError] = createSignal(); - const openDialog = () => { - setOpen(true); - dispatch( - { - action: 'getSettings', - }, - (error, result) => { - console.log({ caller: 'SystemInfo', error, result }); - if (!!error) { - setError(error); - } - if (!!result) { - setSettings(result); - dispatch( - { - action: 'getAccount', - params: { id: settings().currentAccountId }, - }, - (error, result) => { - console.log({ caller: 'SystemInfo', error, result }); - if (!!error) { - setError(error); - } - if (!!result) { - setAccount(result); - } - } - ); - } + createEffect(() => { + if (error().length > 0 && !open()) { + openDialog(); + } + }); + + const selfTest = async () => { + try { + const pong = await dispatch({ + action: 'ping', + params: { what: 'ping' }, + }); + if (pong !== 'ping pong') { + throw new Error( + `web worker test error, received "${pong}" instead of expected "ping pong"` + ); } - ); + } catch (err) { + reportError(err); + } + }; + + selfTest(); + + const openDialog = async () => { + setOpen(true); + try { + const sets: any = await dispatch({ + action: 'getSettings', + }); + setSettings(sets); + const acc: any = await dispatch({ + action: 'getAccount', + params: { id: settings().currentAccountId }, + }); + setAccount(acc); + } catch (err) { + reportError(err); + } }; return ( @@ -77,6 +88,14 @@ const SystemInfo: Component = (props) => {

Remote DB user: {account()?.remoteDbUser}

Remote DB server: {account()?.remoteDbServer}

+ 0}> +
+

Errors:

+
    + {(item) =>
  • {item.message}
  • }
    +
+
+
diff --git a/src/components/system-info/index.ts b/src/components/system-info/index.ts new file mode 100644 index 0000000..3d4ec06 --- /dev/null +++ b/src/components/system-info/index.ts @@ -0,0 +1 @@ +export { default, reportError } from './SystemInfo'; diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 8e36257..5c89486 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -27,7 +27,6 @@ import { deleteTrk, getTrk, putNewTrk } from '../db/trk'; import { putTrkpt } from '../db/trkpt'; import { getTrkseg, appendTrkpt } from '../db/trkseg'; import { getWpt, putWpt, deleteWpt } from '../db/wpt'; -import { until } from '../lib/async-wait'; //const self = globalThis as unknown as WorkerGlobalScope; @@ -75,6 +74,8 @@ onmessage = async function (e) { putAccount, getCurrentAccount, putCurrentAccount, + + ping: (params: any) => `${params.what} pong`, }; // console.log({ caller: 'dispatcher-worker / onmessage', e });