Using peekCachedSignal to speedup GpxDialog

This commit is contained in:
Eric van der Vlist 2023-01-07 15:14:44 +01:00
parent 516d09e3c6
commit 2ca519a53c
6 changed files with 23 additions and 19 deletions

View File

@ -3,11 +3,9 @@ import {
Dialog as MuiDialog, Dialog as MuiDialog,
DialogContent, DialogContent,
DialogTitle, DialogTitle,
Grid,
IconButton, IconButton,
} from '@suid/material'; } from '@suid/material';
import CloseIcon from '@suid/icons-material/Close'; import CloseIcon from '@suid/icons-material/Close';
import { debounce } from 'lodash';
const Dialog: Component<{ const Dialog: Component<{
open: boolean; open: boolean;

View File

@ -2,6 +2,8 @@ import { useI18n } from '@solid-primitives/i18n';
import { Box, Button, IconButton, SvgIcon, TextField } from '@suid/material'; import { Box, Button, IconButton, SvgIcon, TextField } from '@suid/material';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { Component, createSignal, Show } from 'solid-js'; import { Component, createSignal, Show } from 'solid-js';
import { newEmptyGpx } from '../../db/gpx';
import { peekCachedSignal } from '../../workers/cached-signals';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
import Dialog from '../dialog'; import Dialog from '../dialog';
import GpxChooser from '../gpx-chooser'; import GpxChooser from '../gpx-chooser';
@ -18,19 +20,24 @@ const GpxDialog: Component<{}> = (props) => {
const [selectedGpxId, setSelectedGpxId] = createSignal<string>(''); const [selectedGpxId, setSelectedGpxId] = createSignal<string>('');
const [gpx, setGpx] = createSignal<Gpx>(); const [gpx, setGpx] = createSignal<Gpx>();
console.log({ caller: 'GpxDialog', currentGpx: currentGpxId() }); console.log({ caller: 'GpxDialog', currentGpxId: currentGpxId() });
const fetchGpx = (gpxId: string) => {
if (gpxId === 'new') {
return newEmptyGpx();
}
const newGpx = peekCachedSignal({
id: gpxId,
action: 'getGpx',
});
return newGpx();
};
const handleClickOpen = async () => { const handleClickOpen = async () => {
setOpen(true); setOpen(true);
setSelectedGpxId(currentGpxId()); setSelectedGpxId(currentGpxId());
if (currentGpxId() !== '') { if (currentGpxId() !== '') {
const newGpx = await dispatch({ setGpx(fetchGpx(currentGpxId()) as Gpx);
action: 'getGpx',
params: {
id: currentGpxId(),
},
});
setGpx(newGpx as Gpx);
} }
console.log({ console.log({
caller: 'GpxDialog / handleClickOpen', caller: 'GpxDialog / handleClickOpen',
@ -38,6 +45,7 @@ const GpxDialog: Component<{}> = (props) => {
gpx: gpx(), gpx: gpx(),
}); });
}; };
const handleClose = (event: any, reason?: string) => { const handleClose = (event: any, reason?: string) => {
console.log({ console.log({
caller: 'GpxDialog / handleClose', caller: 'GpxDialog / handleClose',
@ -50,15 +58,10 @@ const GpxDialog: Component<{}> = (props) => {
}; };
const gpxChangeHandler = async (gpxId: string) => { const gpxChangeHandler = async (gpxId: string) => {
setSelectedGpxId(gpxId); setSelectedGpxId(gpxId);
const newGpx = await dispatch({ setGpx(fetchGpx(gpxId) as Gpx);
action: 'getGpx',
params: {
id: gpxId,
},
});
setGpx(newGpx as Gpx);
console.log({ console.log({
caller: 'GpxDialog / gpxChangeHandler', caller: 'GpxDialog / gpxChangeHandler',
gpxId,
gpx: gpx(), gpx: gpx(),
}); });
}; };

View File

@ -12,7 +12,7 @@ interface Props {
} }
const GpxViewer: Component<Props> = ({ gpxId, restrictToHierarchy }) => { const GpxViewer: Component<Props> = ({ gpxId, restrictToHierarchy }) => {
const gpx = peekCachedSignal({ id: gpxId, method: 'getGpx' }); const gpx = peekCachedSignal({ id: gpxId, action: 'getGpx' });
console.log({ caller: 'GpxViewer', gpxId, restrictToHierarchy, gpx: gpx() }); console.log({ caller: 'GpxViewer', gpxId, restrictToHierarchy, gpx: gpx() });
const title = () => { const title = () => {
return gpx().metadata.name; return gpx().metadata.name;

View File

@ -37,6 +37,8 @@ export const emptyGpx: Gpx = {
extensions: undefined, extensions: undefined,
}; };
export const newEmptyGpx = () => cloneDeep(emptyGpx);
export const putNewGpx = async ( export const putNewGpx = async (
id: IdGpx = { gpx: intToGpxId(Date.now()) } id: IdGpx = { gpx: intToGpxId(Date.now()) }
) => { ) => {

View File

@ -37,7 +37,7 @@ export const getVillageOrTown = (address: any) => {
]; ];
for (let synonym of citySynonyms) { for (let synonym of citySynonyms) {
console.log({ caller: 'getVillageOrTown', address, synonym }); // console.log({ caller: 'getVillageOrTown', address, synonym });
if (synonym in address?.address) { if (synonym in address?.address) {
return address?.address[synonym]; return address?.address[synonym];
} }

View File

@ -32,6 +32,7 @@ export const createCachedSignal = (params: any) => {
export const peekCachedSignal = (params: any) => { export const peekCachedSignal = (params: any) => {
const cachedSignal = cache.get({ cacheId: 'signals', key: params.id }); const cachedSignal = cache.get({ cacheId: 'signals', key: params.id });
console.log({ caller: 'peekCachedSignal', params, cachedSignal });
if (cachedSignal !== null) { if (cachedSignal !== null) {
return cachedSignal; return cachedSignal;
} }