Relying on the change handler for AllGpxes and FPX import.

This commit is contained in:
Eric van der Vlist 2022-12-09 14:57:54 +01:00
parent 21a63ec54c
commit c2d6f6f285
4 changed files with 57 additions and 20 deletions

View File

@ -1,28 +1,59 @@
import { import {
Component, Component,
createEffect, createEffect,
createResource, createSignal,
For, For,
onCleanup,
Suspense, Suspense,
} from 'solid-js'; } from 'solid-js';
import OlMap from 'ol/Map'; import OlMap from 'ol/Map';
import dispatch from '../../workers/dispatcher-main'; import dispatch, { cancelDispatch } from '../../workers/dispatcher-main';
import Gpx from '../gpx'; import Gpx from '../gpx';
interface Props { interface Props {
map: () => OlMap | null; map: () => OlMap | null;
} }
const [allGpxIds, { refetch }] = createResource( const [allGpxIds, setAllGpxIds] = createSignal<string[]>([]);
async () => const [dispatchId, setDispatchId] = createSignal<number>(-1);
await dispatch({
action: 'getAllGpxes', const allGpxesCallBack = (error: any, result: any, id?: number) => {
params: {}, if (error) {
}) console.error({ caller: 'allGpxesCallBack', error });
} else {
console.log({ caller: 'allGpxesCallBack', result });
setAllGpxIds(result);
}
if (id) {
setDispatchId(id);
}
};
dispatch(
{
action: 'getAndWatch',
params: {
id: 'gpx',
method: 'getAllGpxes',
},
},
allGpxesCallBack,
true
); );
onCleanup(() => {
dispatch({
action: 'cancelWatch',
params: {
id: 'gpx',
method: 'getAllGpxes',
},
});
cancelDispatch(dispatchId());
});
export const AllGpxes: Component<Props> = ({ map }) => { export const AllGpxes: Component<Props> = ({ map }) => {
createEffect(() => { createEffect(() => {
console.log({ caller: 'AllGpxes', allGpxes: allGpxIds(), map: map() }); console.log({ caller: 'AllGpxes', allGpxes: allGpxIds(), map: map() });
@ -44,4 +75,3 @@ export const AllGpxes: Component<Props> = ({ map }) => {
}; };
export default AllGpxes; export default AllGpxes;
export { refetch };

View File

@ -1 +1 @@
export { default, refetch as refetchGpxList } from './AllGpxes'; export { default } from './AllGpxes';

View File

@ -9,7 +9,6 @@ import css from './GpxImport.module.css';
import { findStartTime } from '../../lib/gpx'; import { findStartTime } from '../../lib/gpx';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
import { intToGpxId } from '../../lib/ids'; import { intToGpxId } from '../../lib/ids';
import { refetchGpxList } from '../all-gpxes';
const GpxImport: Component = () => { const GpxImport: Component = () => {
const onChangeHandler = (event: any) => { const onChangeHandler = (event: any) => {
@ -41,7 +40,6 @@ const GpxImport: Component = () => {
}, },
}, },
}); });
refetchGpxList();
} else { } else {
console.error({ console.error({
message: "can't parse GPX file", message: "can't parse GPX file",

View File

@ -1,28 +1,37 @@
import { getWpt } from '../db/wpt'; import { getWpt } from '../db/wpt';
import { returnAgain } from '../workers/dispatcher-worker'; import { returnAgain } from '../workers/dispatcher-worker';
import { getAllGpxes } from './gpx';
declare global { declare global {
var watches: Map<string, any>; var watches: Map<string, any>;
} }
const methods = { const methods = {
getAllGpxes,
getWpt, getWpt,
}; };
const sendUpdate = async (params: any) => {
if (params) {
console.log({ caller: 'ChangeHandler / sendUpdate', params });
const { method, _dispatchId, id, ...otherParams } = params;
const returnValue = await methods[<keyof typeof methods>method](params);
if (returnValue) {
returnAgain(_dispatchId, returnValue);
}
}
};
const changeHandler = async (change: any) => { const changeHandler = async (change: any) => {
console.log({ caller: 'ChangeHandler', change }); console.log({ caller: 'ChangeHandler', change });
const { id } = change; const { id } = change;
if (!globalThis.watches) { if (!globalThis.watches) {
globalThis.watches = new Map(); globalThis.watches = new Map();
} }
const params = globalThis.watches.get(id); sendUpdate(globalThis.watches.get(id));
if (params) {
const { method, _dispatchId, id, ...otherParams } = params; const parentId = id.substring(0, id.lastIndexOf('/'));
const returnValue = await methods[<keyof typeof methods>method](params); sendUpdate(globalThis.watches.get(parentId));
if (returnValue) {
returnAgain(_dispatchId, returnValue);
}
}
}; };
export default changeHandler; export default changeHandler;