From d9c4837f82475b64543c0e1d324669fe66b769f0 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 3 Jan 2023 11:39:17 +0100 Subject: [PATCH] Slight refactoring. --- src/components/gpx/GpxViewer.tsx | 3 ++- src/components/infos/Infos.tsx | 13 +++---------- src/workers/cached-signals.ts | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/components/gpx/GpxViewer.tsx b/src/components/gpx/GpxViewer.tsx index 0dec52d..812e05a 100644 --- a/src/components/gpx/GpxViewer.tsx +++ b/src/components/gpx/GpxViewer.tsx @@ -1,5 +1,6 @@ import { Component } from 'solid-js'; import cache from '../../lib/cache'; +import { peekCachedSignal } from '../../workers/cached-signals'; import Tree from '../tree'; interface Props { @@ -7,7 +8,7 @@ interface Props { } const GpxViewer: Component = ({ gpxId }) => { - const gpx = cache.get({ cacheId: 'signals', key: gpxId }); + const gpx = peekCachedSignal({ id: gpxId, method: 'getGpx' }); console.log({ caller: 'GpxViewer', gpx: gpx() }); const title = () => {}; return ; diff --git a/src/components/infos/Infos.tsx b/src/components/infos/Infos.tsx index a5955a2..9cab708 100644 --- a/src/components/infos/Infos.tsx +++ b/src/components/infos/Infos.tsx @@ -61,29 +61,22 @@ export const clickHandler = (event: any) => { selectedFeatures.map((feature: Feature) => { const gpxId = feature.get('context').gpxId; if (!hierarchy.hasOwnProperty(gpxId)) { - hierarchy[gpxId] = { gpx: feature.get('context').gpx }; - hierarchy[gpxId].type = 'gpx'; + hierarchy[gpxId] = { type: 'gpx' }; } const l2key = level2Key(feature); if (!hierarchy[gpxId].hasOwnProperty(l2key)) { - hierarchy[gpxId][l2key] = { - wpt: feature.get('context').wpt, - rte: feature.get('context').rte, - trk: feature.get('context').trk, - }; + hierarchy[gpxId][l2key] = {}; if (feature.get('type') === 'trkseg') { hierarchy[gpxId][l2key].type = 'trk'; } else { hierarchy[gpxId][l2key].type = feature.get('type'); - feature; + hierarchy[gpxId][l2key].feature = feature; } } if (feature.get('type') === 'trkseg') { const trksegId = feature.get('context').trksegId; if (!hierarchy[gpxId][l2key].hasOwnProperty(trksegId)) { hierarchy[gpxId][l2key][trksegId] = { - trksegId, - trkseg: feature.get('context').trkseg, type: 'trkseg', feature, }; diff --git a/src/workers/cached-signals.ts b/src/workers/cached-signals.ts index af8a594..8203606 100644 --- a/src/workers/cached-signals.ts +++ b/src/workers/cached-signals.ts @@ -30,6 +30,24 @@ export const createCachedSignal = (params: any) => { return signal; }; +export const peekCachedSignal = (params: any) => { + const cachedSignal = cache.get({ cacheId: 'signals', key: params.id }); + if (cachedSignal !== null) { + return cachedSignal; + } + const [signal, setSignal] = createSignal(); + const callBack = (error: any, result: any, id?: number) => { + if (error) { + console.error({ caller: 'peekCachedSignal / callBack', error }); + } else { + console.log({ caller: 'peekCachedSignal / callBack', result }); + setSignal(result); + } + }; + dispatch(params, callBack, false); + return signal; +}; + export const destroyCachedSignal = (params: any) => { dispatch({ action: 'cancelWatch',