Refactoring to watch WPT updates.

This commit is contained in:
vdv committed 2022-12-06 14:36:50 +01:00
1 parent 6465a371cb
commit 5829f93c91
5 files changed
+111 -41

No files matched your search

+11 -4
View File
@@ -22,8 +22,10 @@ export const init = () => {
payload,
dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
});
dispatcherQueue.queue.get(id)(null, payload);
dispatcherQueue.queue.delete(id);
dispatcherQueue.queue.get(id).callBack(null, payload);
if (!dispatcherQueue.queue.get(id).live) {
cancel(id);
}
};
dispatch({ action: 'initDb' });
@@ -31,7 +33,8 @@ export const init = () => {
const dispatch = (
payload: any,
callBack?: (error: any, result: any) => void
callBack?: (error: any, result: any) => void,
live?: boolean
) => {
console.log({ caller: 'dispatcher-main / dispatch', payload });
if (worker === undefined) {
@@ -50,7 +53,7 @@ const dispatch = (
});
}
/** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, callBack);
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = {
id: dispatcherQueue.index++,
payload: payload,
@@ -63,4 +66,8 @@ const dispatch = (
});
};
export const cancel = (id: number) => {
dispatcherQueue.queue.delete(id);
};
export default dispatch;
+28 -18
View File
@@ -1,5 +1,6 @@
/// <reference lib="webworker" />
import { initDb } from '../db';
import { cancelWatch, getAndWatch } from '../db/change-handler';
import {
putNewGpx,
existsGpx,
@@ -14,34 +15,43 @@ import { getWpt } from '../db/wpt';
//const self = globalThis as unknown as WorkerGlobalScope;
const actions = {
initDb,
putNewGpx,
putNewTrk,
existsGpx,
pruneAndSaveImportedGpx,
getAllGpxes,
getGpxesForViewport,
getGpx,
getTrk,
getTrkseg,
getWpt
};
console.log({ caller: 'dispatcher-worker', actions });
console.log({ caller: 'dispatcher-worker' });
onmessage = async function (e) {
const actions = {
initDb,
putNewGpx,
putNewTrk,
existsGpx,
pruneAndSaveImportedGpx,
getAllGpxes,
getGpxesForViewport,
getGpx,
getTrk,
getTrkseg,
getWpt,
getAndWatch,
cancelWatch,
};
console.log({ caller: 'dispatcher-worker / onmessage', e });
const { id, payload } = e.data;
var returnValue: any = 'unknownAction';
if (payload.action in actions) {
console.log({ caller: 'dispatcher-worker / awaiting', id, payload });
returnValue = await actions[<keyof typeof actions>payload.action](
payload.params
);
returnValue = await actions[<keyof typeof actions>payload.action]({
...payload.params,
_dispatchId: id,
});
}
postMessage({ id: id, payload: returnValue });
console.log({ caller: 'dispatcher-worker / response sent', id, returnValue });
};
export const returnAgain = (id: number, returnValue: any) => {
console.log({ caller: 'dispatcher-worker / returnAgain', id, returnValue });
postMessage({ id: id, payload: returnValue });
};
//export default self;