Refactoring the worker / db init flow.
This commit is contained in:
parent
cfaf7ad72f
commit
6dfe98b655
|
@ -439,12 +439,16 @@ export const putGpx = async (params: any) => {
|
||||||
|
|
||||||
await put(id, 'gpx', (doc) => gpx, gpx);
|
await put(id, 'gpx', (doc) => gpx, gpx);
|
||||||
if (extensions !== undefined) {
|
if (extensions !== undefined) {
|
||||||
await put(
|
try {
|
||||||
`${id}/4extensions`,
|
await put(
|
||||||
'extensions',
|
`${id}/4extensions`,
|
||||||
(doc) => extensions,
|
'extensions',
|
||||||
extensions
|
(doc) => extensions,
|
||||||
);
|
extensions
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error({ caller: 'putGpx / extensions', error });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousShared !== extensions?.shared) {
|
if (previousShared !== extensions?.shared) {
|
||||||
|
|
|
@ -99,5 +99,7 @@ export const initDb = async () => {
|
||||||
|
|
||||||
globalThis.dbReady = true;
|
globalThis.dbReady = true;
|
||||||
watchDbLegacy();
|
watchDbLegacy();
|
||||||
|
console.log({ caller: 'initDb / done' });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,18 +1,13 @@
|
||||||
/* @refresh reload */
|
/* @refresh reload */
|
||||||
import { render } from 'solid-js/web';
|
import { render } from 'solid-js/web';
|
||||||
import { Router, hashIntegration } from '@solidjs/router';
|
import { Router } from '@solidjs/router';
|
||||||
import {
|
import {
|
||||||
I18nContext,
|
I18nContext,
|
||||||
createI18nContext,
|
createI18nContext,
|
||||||
useI18n,
|
|
||||||
} from '@solid-primitives/i18n';
|
} from '@solid-primitives/i18n';
|
||||||
import dict from './i18n';
|
import dict from './i18n';
|
||||||
|
|
||||||
import App from './App';
|
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
|
// See https://stackoverflow.com/questions/71538643/property-wakelock-does-not-exist-on-type-navigator
|
||||||
const requestWakeLock = async () => {
|
const requestWakeLock = async () => {
|
||||||
|
|
|
@ -1,23 +1,21 @@
|
||||||
import { createEffect } from 'solid-js';
|
|
||||||
import { until } from '../lib/async-wait';
|
import { until } from '../lib/async-wait';
|
||||||
import { createWorkerSignal } from '../solid-workers/solid-worker-main';
|
|
||||||
import { getWorker } from './get-worker';
|
import { getWorker } from './get-worker';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
var dispatcherQueue: { index: number; queue: Map<number, any> };
|
var dispatcherQueue: { index: number; queue: Map<number, any> };
|
||||||
|
var dbReady: boolean;
|
||||||
|
var worker: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export var worker: any;
|
|
||||||
|
|
||||||
export const init = () => {
|
export const init = () => {
|
||||||
console.log({ caller: 'dispatcher-main / init' });
|
// console.log({ caller: 'dispatcher-main / init' });
|
||||||
globalThis.dispatcherQueue = { index: 0, queue: new Map() };
|
globalThis.dispatcherQueue = { index: 0, queue: new Map() };
|
||||||
console.log({ caller: 'dispatcher-main / init', globalThis });
|
// console.log({ caller: 'dispatcher-main / init', globalThis });
|
||||||
|
|
||||||
worker = getWorker();
|
globalThis.worker = getWorker();
|
||||||
console.log({ caller: 'dispatcher-main / init', worker });
|
// console.log({ caller: 'dispatcher-main / init', worker });
|
||||||
|
|
||||||
worker.onmessage = (event: any) => {
|
globalThis.worker.onmessage = (event: any) => {
|
||||||
const { id, payload } = event.data;
|
const { id, payload } = event.data;
|
||||||
// console.log({
|
// console.log({
|
||||||
// caller: 'dispatcher-main / message received',
|
// caller: 'dispatcher-main / message received',
|
||||||
|
@ -25,35 +23,42 @@ export const init = () => {
|
||||||
// payload,
|
// payload,
|
||||||
// dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
|
// dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
|
||||||
// });
|
// });
|
||||||
dispatcherQueue.queue.get(id).callBack(null, payload, id);
|
globalThis.dispatcherQueue.queue.get(id).callBack(null, payload, id);
|
||||||
if (!dispatcherQueue.queue.get(id).live) {
|
if (!globalThis.dispatcherQueue.queue.get(id).live) {
|
||||||
cancelDispatch(id);
|
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 (
|
const dispatch = async (
|
||||||
payload: any,
|
payload: any,
|
||||||
callBack?: (error: any, result: any, id?: number | undefined) => void,
|
callBack?: (error: any, result: any, id?: number | undefined) => void,
|
||||||
live?: boolean
|
live?: boolean
|
||||||
) => {
|
) => {
|
||||||
// console.log({ caller: 'dispatcher-main / dispatch', payload, callBack });
|
let initializing = false;
|
||||||
if (worker === undefined) {
|
console.log({
|
||||||
|
caller: 'dispatcher-main / dispatch',
|
||||||
|
payload,
|
||||||
|
callBack,
|
||||||
|
initializing,
|
||||||
|
worker: globalThis.worker,
|
||||||
|
});
|
||||||
|
if (globalThis.worker === undefined && !initializing) {
|
||||||
|
initializing = true;
|
||||||
init();
|
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 (callBack === undefined) {
|
||||||
/** If a callback function is not provided, return a promise */
|
/** If a callback function is not provided, return a promise */
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -67,21 +72,24 @@ const dispatch = async (
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/** Otherwise, use the callback function */
|
/** Otherwise, use the callback function */
|
||||||
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
|
globalThis.dispatcherQueue.queue.set(globalThis.dispatcherQueue.index, {
|
||||||
|
callBack,
|
||||||
|
live,
|
||||||
|
});
|
||||||
const message = {
|
const message = {
|
||||||
id: dispatcherQueue.index++,
|
id: globalThis.dispatcherQueue.index++,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
};
|
};
|
||||||
worker.postMessage(message);
|
worker.postMessage(message);
|
||||||
// console.log({
|
console.log({
|
||||||
// caller: 'dispatcher-main / message sent',
|
caller: 'dispatcher-main / message sent',
|
||||||
// message,
|
message,
|
||||||
// dispatcherQueue,
|
dispatcherQueue: globalThis.dispatcherQueue,
|
||||||
// });
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelDispatch = (id: number) => {
|
export const cancelDispatch = (id: number) => {
|
||||||
dispatcherQueue.queue.delete(id);
|
globalThis.dispatcherQueue.queue.delete(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default dispatch;
|
export default dispatch;
|
||||||
|
|
|
@ -68,9 +68,9 @@ onmessage = async function (e) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// console.log({ caller: 'dispatcher-worker / onmessage', 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;
|
const { id, payload } = e.data;
|
||||||
var returnValue: any = 'unknownAction';
|
var returnValue: any = 'unknownAction';
|
||||||
if (payload.action in actions) {
|
if (payload.action in actions) {
|
||||||
|
|
Loading…
Reference in New Issue