Skip to content

Commit

Permalink
Refresh cache on re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jul 19, 2024
1 parent bb1d5cc commit 9d12661
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions libs/windy-sounding/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Prevent an infinite loop when height === 0
- Better handling of burst of wheel events
- Refresh windy data cache on re-render

## 4.1.1 - July 17, 2024

Expand Down
4 changes: 4 additions & 0 deletions libs/windy-sounding/src/containers/containers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function Plugin() {
fetchStatus,
availableVersion,
isWindyDataAvailableAtCurrentTime,
modelName,
location,
} = useSelector((state: RootState) => {
const modelName = pluginSlice.selModelName(state);
const location = pluginSlice.selLocation(state);
Expand Down Expand Up @@ -67,6 +69,8 @@ export function Plugin() {
let heightOnStartDrag = 0;

const dispatch: AppDispatch = useDispatch();
// Fetch data when the cache has expired.
dispatch(forecastSlice.fetchForecast({ modelName, location }));
const selectFavorite = useCallback((location: LatLon) => {
dispatch(changeLocation(location));
centerMap(location);
Expand Down
12 changes: 8 additions & 4 deletions libs/windy-sounding/src/redux/forecast-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,19 @@ export const fetchForecast = createAsyncThunk<Forecast, ModelAndLocation, { stat
},
{
condition: (modelAndLocation, api: AppThunkAPI) => {
// Prevent fetching again while loading.
// Prevent fetching again while loading or when data is already cached.
const { modelName, location } = modelAndLocation;
const state = api.getState();
const pluginStatus = pluginSlice.selStatus(state);
const windyData = selMaybeLoadedWindyData(state, modelName, location);
return (
pluginStatus === pluginSlice.PluginStatus.Ready &&
(windyData === undefined ||
!(windyData.fetchStatus == FetchStatus.Loading || windyData.fetchStatus === FetchStatus.ErrorOutOfBounds))
!(
windyData.fetchStatus == FetchStatus.Loaded ||
windyData.fetchStatus == FetchStatus.Loading ||
windyData.fetchStatus === FetchStatus.ErrorOutOfBounds
))
);
},
},
Expand All @@ -207,7 +211,7 @@ function windyDataKey(modelName: string, location: LatLon): string {
* @param state - The state object containing forecast data.
* @param key - The key to identify the forecast data.
* */
function isDataCached(state: ForecastState, key: string) {
function isWindyDataCached(state: ForecastState, key: string) {
const forecast = state.data[key];
if (forecast == null) {
return false;
Expand Down Expand Up @@ -311,7 +315,7 @@ const selMaybeWindyData = (state: RootState, modelName: string, location: LatLon

const selMaybeLoadedWindyData = (state: RootState, modelName: string, location: LatLon): Forecast | undefined => {
const key = windyDataKey(modelName, location);
return isDataCached(state[slice.name], key) ? state[slice.name].data[key] : undefined;
return isWindyDataCached(state[slice.name], key) ? state[slice.name].data[key] : undefined;
};

/**
Expand Down

0 comments on commit 9d12661

Please sign in to comment.