Skip to content

Commit

Permalink
Manage Record Cache State During Page Reload for Active Downloads (#3816
Browse files Browse the repository at this point in the history
)

* rehydrate recordsets on reload

* update other states related to caching
  • Loading branch information
meghna0593 authored Jan 29, 2025
1 parent d5f6f15 commit 4ce8ec8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions app/src/state/reducers/rootReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { combineReducers } from 'redux';
import localForage from 'localforage';
import autoMergeLevel1 from 'redux-persist/lib/stateReconciler/autoMergeLevel1';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { persistReducer } from 'redux-persist';
import { persistReducer, createTransform } from 'redux-persist';
import appMode from './appMode';
import { ActivityState, createActivityReducer } from './activity';
import { AuthState, createAuthReducer } from './auth';
Expand All @@ -23,6 +23,8 @@ import { AppConfig } from 'state/config';
import { CURRENT_MIGRATION_VERSION, MIGRATION_VERSION_KEY } from 'constants/offline_state_version';
import { createTileCacheReducer } from 'state/reducers/tile_cache';
import { MOBILE } from 'state/build-time-config';
import { UserRecordCacheStatus } from 'interfaces/UserRecordSet';
import { CacheDownloadMode } from 'utils/record-cache';

// it will try indexdb first, then fall back to localstorage if not available.

Expand All @@ -49,6 +51,27 @@ const purgeOldStateOnVersionUpgrade = async (state: any) => {
}
};

// executes during app restart or when the page reloads
const pauseDownloadOnRehydration = createTransform(
(inboundState) => inboundState,

(outboundState) => {
if (outboundState && typeof outboundState === 'object') {
Object.keys(outboundState).forEach((key) => {
// updates state correctly when page reloads during an active download
if (outboundState[key]?.cacheMetadataStatus === UserRecordCacheStatus.DOWNLOADING) {
outboundState[key].cacheMetadataStatus = UserRecordCacheStatus.PAUSED;
outboundState[key].cacheDownloadProgress.downloadMode = CacheDownloadMode.PAUSE;
outboundState[key].cacheDownloadProgress.message =
`Mode: ${CacheDownloadMode.PAUSE.toLocaleString().toUpperCase()} Caching`;
}
});
}
return outboundState;
},
{ whitelist: ['recordSets'] }
);

function createRootReducer(config: AppConfig) {
return combineReducers({
AppMode: appMode,
Expand Down Expand Up @@ -103,7 +126,8 @@ function createRootReducer(config: AppConfig) {
'boundaries',
'layerPickerIsAccordion',
'mapCenter'
]
],
transforms: [pauseDownloadOnRehydration]
},
createUserSettingsReducer(config)
),
Expand Down

0 comments on commit 4ce8ec8

Please sign in to comment.