Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EditDialog: invalidate fetchCache after save #889

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading