Skip to content

Commit

Permalink
clear cache data
Browse files Browse the repository at this point in the history
  • Loading branch information
salazarm committed Jun 10, 2024
1 parent 552d1ec commit 7b0be14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ export class CacheManager<TQuery> {
return null;
}

set(data: TQuery, version: number): Promise<void> {
async set(data: TQuery, version: number): Promise<void> {
return this.cache.set('cache', {data, version}, {expiry: new Date('3030-01-01')});
}

async clear() {
await this.cache.delete('cache');
}
}

interface QueryHookParams<TVariables extends OperationVariables, TQuery> {
Expand Down Expand Up @@ -158,6 +162,16 @@ export function useGetCachedData() {
[],
);
}
export function useClearCachedData() {
const {getCacheManager} = useContext(IndexedDBCacheContext);
return useCallback(
async <TQuery,>({key}: {key: string}) => {
const cacheManager = getCacheManager<TQuery>(key);
await cacheManager.clear();
},
[getCacheManager],
);
}

const contextValue = createIndexedDBCacheContextValue();
export const IndexedDBCacheContext = createContext(contextValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {useStateWithStorage} from '../hooks/useStateWithStorage';
import {useUpdatingRef} from '../hooks/useUpdatingRef';
import {codeLocationStatusAtom} from '../nav/useCodeLocationsStatus';
import {
useClearCachedData,
useGetCachedData,
useGetData,
useIndexedDBCachedQuery,
Expand Down Expand Up @@ -114,6 +115,7 @@ export const WorkspaceProvider = ({children}: {children: React.ReactNode}) => {

const getCachedData = useGetCachedData();
const getData = useGetData();
const clearCacheData = useClearCachedData();

useLayoutEffect(() => {
// Load data from the cache
Expand Down Expand Up @@ -250,12 +252,12 @@ export const WorkspaceProvider = ({children}: {children: React.ReactNode}) => {
const copy = {...locationsData};
locationsRemoved.forEach((loc) => {
delete copy[loc.name];
indexedDB.deleteDatabase(`${localCacheIdPrefix}${locationWorkspaceKey(loc.name)}`);
clearCacheData({key: `${localCacheIdPrefix}${locationWorkspaceKey(loc.name)}`});
});
if (Object.keys(copy).length !== Object.keys(locationsData).length) {
setLocationsData(copy);
}
}, [localCacheIdPrefix, locationsData, locationsRemoved]);
}, [clearCacheData, localCacheIdPrefix, locationsData, locationsRemoved]);

const locationEntries = useMemo(
() =>
Expand Down

0 comments on commit 7b0be14

Please sign in to comment.