Skip to content

Commit

Permalink
EditDialog: invalidate fetchCache after save (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Jan 12, 2025
1 parent dcd5505 commit a96b183
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
38 changes: 25 additions & 13 deletions src/services/fetchCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,47 @@ const cache = {};

const fetchCache = isBrowser()
? {
get: (key) => sessionStorage.getItem(key),
remove: (key) => sessionStorage.removeItem(key),
put: (key, value) => sessionStorage.setItem(key, value),
clear: () => sessionStorage.clear(),
get: (key: string) => sessionStorage.getItem(key),
remove: (key: string) => sessionStorage.removeItem(key),
put: (key: string, value: string) => sessionStorage.setItem(key, value),
clear: () => sessionStorage.clear(), // this is little dirty, but we use sessionStorage only for this
}
: {
get: (key) => cache[key],
remove: (key) => delete cache[key],
put: (key, value) => {
get: (key: string) => cache[key],
remove: (key: string) => delete cache[key],
put: (key: string, value: string) => {
cache[key] = value;
},
clear: () => {},
clear: () => {
Object.keys(cache).forEach((key) => delete cache[key]);
},
};

export const getKey = (url, opts) => {
export const getKey = (url: string, opts: Record<string, any>) => {
if (['POST', 'PUT', 'DELETE'].includes(opts.method)) {
return false;
}

return url + JSON.stringify(opts);
};

export const getCache = (key) => {
export const getCache = (key: string | false) => {
if (key) {
return fetchCache.get(key);
}
};

export const removeFetchCache = (url, opts = {}) => {
fetchCache.remove(getKey(url, opts));
export const removeFetchCache = (
url: string,
opts: Record<string, any> = {},
) => {
const key = getKey(url, opts);
if (key) {
fetchCache.remove(key);
}
};

export const writeCacheSafe = (key, value) => {
export const writeCacheSafe = (key: string | false, value: string) => {
if (!key) return;

try {
Expand All @@ -48,3 +56,7 @@ export const writeCacheSafe = (key, value) => {
console.warn(`Item ${key} was not saved to cache: `, e); // eslint-disable-line no-console
}
};

export const clearFetchCache = () => {
fetchCache.clear();
};
7 changes: 5 additions & 2 deletions src/services/osm/osmApiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Xml2JsSingleDoc,
} from '../helpers';
import { join } from '../../utils';
import { clearFeatureCache } from './osmApi';
import { clearFetchCache } from '../fetchCache';
import { isBrowser } from '../../components/helpers';
import { getLabel } from '../../helpers/featureLabel';
import {
Expand Down Expand Up @@ -389,7 +389,8 @@ export const saveChanges = async (
const ids = [...savedNodesIds, ...savedWaysIds, ...savedRelationsIds];
const redirectId = original.point ? ids[0] : original.osmMeta;

// TODO invalidate all changed items in browser AND server !
// TODO invalidate all changed also in server (?)
clearFetchCache();

return {
type: 'edit',
Expand Down Expand Up @@ -452,6 +453,8 @@ export const editCrag = async (
);
await putChangesetClose(changesetId);

clearFetchCache();

return {
type: 'edit',
text: changesetComment,
Expand Down

0 comments on commit a96b183

Please sign in to comment.