Relying on the change handler for Gpx, Trk and Trkseg too.

This commit is contained in:
Eric van der Vlist 2022-12-09 15:18:14 +01:00
parent c2d6f6f285
commit f84c82bc32
4 changed files with 122 additions and 33 deletions

View File

@ -2,13 +2,15 @@ import {
Component, Component,
createEffect, createEffect,
createResource, 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 Trk from '../trk'; import Trk from '../trk';
import getUri from '../../lib/ids'; import getUri from '../../lib/ids';
import VectorSource from 'ol/source/Vector'; import VectorSource from 'ol/source/Vector';
@ -23,17 +25,44 @@ interface Props {
} }
export const Gpx: Component<Props> = ({ map, gpxId }) => { export const Gpx: Component<Props> = ({ map, gpxId }) => {
const [gpx] = createResource( const [gpx, setGpx] = createSignal<Gpx>();
gpxId, const [dispatchId, setDispatchId] = createSignal<number>(-1);
async () =>
await dispatch({ const gpxCallBack = (error: any, result: any, id?: number) => {
action: 'getGpx', if (error) {
params: { console.error({ caller: 'gpxCallBack', error });
id: gpxId, } else {
}, console.log({ caller: 'gpxCallBack', result });
}) setGpx(result);
}
if (id) {
setDispatchId(id);
}
};
dispatch(
{
action: 'getAndWatch',
params: {
id: gpxId,
method: 'getGpx',
},
},
gpxCallBack,
true
); );
onCleanup(() => {
dispatch({
action: 'cancelWatch',
params: {
id: gpxId,
method: 'getGpx',
},
});
cancelDispatch(dispatchId());
});
const vectorSource = new VectorSource(); const vectorSource = new VectorSource();
const vectorLayer = new VectorLayer({ source: vectorSource, style }); const vectorLayer = new VectorLayer({ source: vectorSource, style });
createEffect(() => map()?.addLayer(vectorLayer)); createEffect(() => map()?.addLayer(vectorLayer));

View File

@ -2,13 +2,15 @@ import {
Component, Component,
createEffect, createEffect,
createResource, 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 getUri from '../../lib/ids'; import getUri from '../../lib/ids';
import Trkseg from '../trkseg'; import Trkseg from '../trkseg';
@ -19,17 +21,44 @@ interface Props {
vectorSource: VectorSource; vectorSource: VectorSource;
} }
export const Trk: Component<Props> = ({ vectorSource, trkId }) => { export const Trk: Component<Props> = ({ vectorSource, trkId }) => {
const [trk] = createResource( const [trk, setTrk] = createSignal<Trk>();
trkId, const [dispatchId, setDispatchId] = createSignal<number>(-1);
async () =>
await dispatch({ const trkCallBack = (error: any, result: any, id?: number) => {
action: 'getTrk', if (error) {
params: { console.error({ caller: 'trkCallBack', error });
id: trkId, } else {
}, console.log({ caller: 'trkCallBack', result });
}) setTrk(result);
}
if (id) {
setDispatchId(id);
}
};
dispatch(
{
action: 'getAndWatch',
params: {
id: trkId,
method: 'getTrk',
},
},
trkCallBack,
true
); );
onCleanup(() => {
dispatch({
action: 'cancelWatch',
params: {
id: trkId,
method: 'getTrk',
},
});
cancelDispatch(dispatchId());
});
createEffect(() => { createEffect(() => {
console.log({ caller: 'Trk', vectorSource, trkId, trk: trk() }); console.log({ caller: 'Trk', vectorSource, trkId, trk: trk() });
}); });

View File

@ -1,9 +1,8 @@
import { Component, createEffect, createResource } from 'solid-js'; import { Component, createEffect, createSignal, onCleanup } from 'solid-js';
import dispatch from '../../workers/dispatcher-main'; import dispatch, { cancelDispatch } from '../../workers/dispatcher-main';
import VectorSource from 'ol/source/Vector'; import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON'; import GeoJSON from 'ol/format/GeoJSON';
import { Feature } from 'ol';
interface Props { interface Props {
trksegId: string; trksegId: string;
@ -11,17 +10,44 @@ interface Props {
} }
export const Trkseg: Component<Props> = ({ vectorSource, trksegId }) => { export const Trkseg: Component<Props> = ({ vectorSource, trksegId }) => {
const [trkseg] = createResource( const [trkseg, setTrkseg] = createSignal<Trkseg>();
trksegId, const [dispatchId, setDispatchId] = createSignal<number>(-1);
async () =>
await dispatch({ const trksegCallBack = (error: any, result: any, id?: number) => {
action: 'getTrkseg', if (error) {
params: { console.error({ caller: 'trksegCallBack', error });
id: trksegId, } else {
}, console.log({ caller: 'trksegCallBack', result });
}) setTrkseg(result);
}
if (id) {
setDispatchId(id);
}
};
dispatch(
{
action: 'getAndWatch',
params: {
id: trksegId,
method: 'getTrkseg',
},
},
trksegCallBack,
true
); );
onCleanup(() => {
dispatch({
action: 'cancelWatch',
params: {
id: trksegId,
method: 'getTrkseg',
},
});
cancelDispatch(dispatchId());
});
createEffect(() => { createEffect(() => {
console.log({ caller: 'Trkseg', vectorSource, trksegId, trkseg: trkseg() }); console.log({ caller: 'Trkseg', vectorSource, trksegId, trkseg: trkseg() });

View File

@ -1,6 +1,8 @@
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'; import { getAllGpxes, getGpx } from './gpx';
import { getTrk } from './trk';
import { getTrkseg } from './trkseg';
declare global { declare global {
var watches: Map<string, any>; var watches: Map<string, any>;
@ -8,6 +10,9 @@ declare global {
const methods = { const methods = {
getAllGpxes, getAllGpxes,
getGpx,
getTrk,
getTrkseg,
getWpt, getWpt,
}; };