diff --git a/packages/bruno-app/src/components/Preferences/Cache/index.js b/packages/bruno-app/src/components/Preferences/Cache/index.js index 57e1ae49f..ab369b824 100644 --- a/packages/bruno-app/src/components/Preferences/Cache/index.js +++ b/packages/bruno-app/src/components/Preferences/Cache/index.js @@ -1,7 +1,10 @@ -import React, { useEffect, useCallback } from 'react'; +import React, { useEffect, useCallback, useRef } from 'react'; import { useFormik } from 'formik'; import { useSelector, useDispatch } from 'react-redux'; -import { savePreferences } from 'providers/ReduxStore/slices/app'; +import { + savePreferences, + clearHttpHttpsAgentCache +} from 'providers/ReduxStore/slices/app'; import toast from 'react-hot-toast'; import StyledWrapper from './StyledWrapper'; import * as Yup from 'yup'; @@ -18,6 +21,21 @@ const Cache = () => { const preferences = useSelector((state) => state.app.preferences); const dispatch = useDispatch(); + const handleSave = useCallback( + (newCachePreferences) => { + dispatch( + savePreferences({ + ...preferences, + cache: newCachePreferences + }) + ).catch(() => toast.error('Failed to update cache preferences')); + }, + [dispatch, preferences] + ); + + const handleSaveRef = useRef(handleSave); + handleSaveRef.current = handleSave; + const formik = useFormik({ initialValues: { httpHttpsAgents: { @@ -35,26 +53,14 @@ const Cache = () => { } }); - const handleSave = useCallback( - (newCachePreferences) => { - dispatch( - savePreferences({ - ...preferences, - cache: newCachePreferences - }) - ).catch(() => toast.error('Failed to update cache preferences')); - }, - [dispatch, preferences] - ); - const debouncedSave = useCallback( debounce((values) => { cacheSchema .validate(values, { abortEarly: true }) - .then((validatedValues) => handleSave(validatedValues)) + .then((validatedValues) => handleSaveRef.current(validatedValues)) .catch(() => {}); }, 500), - [handleSave] + [] ); useEffect(() => { @@ -70,13 +76,12 @@ const Cache = () => { formik.handleChange(e); // Immediately evict all cached agents when caching is disabled if (!e.target.checked) { - window.ipcRenderer.invoke('renderer:clear-http-https-agent-cache').catch(() => {}); + dispatch(clearHttpHttpsAgentCache()).catch(() => {}); } }; const handleResetCache = () => { - window.ipcRenderer - .invoke('renderer:clear-http-https-agent-cache') + dispatch(clearHttpHttpsAgentCache()) .then(() => toast.success('Agent cache cleared')) .catch(() => toast.error('Failed to clear agent cache')); }; diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/app.js b/packages/bruno-app/src/providers/ReduxStore/slices/app.js index 3474187ef..a674c7968 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/app.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/app.js @@ -306,4 +306,25 @@ export const refreshSystemProxy = () => (dispatch, getState) => { }); }; +export const getCacheStats = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:get-cache-stats').then(resolve).catch(reject); + }); +}; + +export const purgeCache = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:purge-cache').then(resolve).catch(reject); + }); +}; + +export const clearHttpHttpsAgentCache = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:clear-http-https-agent-cache').then(resolve).catch(reject); + }); +}; + export default appSlice.reducer;