Skip to content

Commit

Permalink
[FIX]: Update storage on every actions which is need
Browse files Browse the repository at this point in the history
  • Loading branch information
masoud-shahpoori committed Nov 13, 2024
1 parent 5b3a2e9 commit 5aa0d19
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-persist-x",
"version": "1.1.4",
"version": "1.1.5",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -64,6 +64,7 @@
"typescript": "^5.6.3"
},
"dependencies": {
"redux-persist-x": "^1.1.3"
"@types/lodash.isequal": "^4.5.8",
"lodash.isequal": "^4.5.0"
}
}
30 changes: 0 additions & 30 deletions src/persistor/PersistorWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect } from 'react';
import {
isPaused,
PAUSE_PERSIST_ACTION,
persistReducerOptions,
PURGE_PERSIST_ACTION,
Expand All @@ -25,29 +24,6 @@ function PersistWrapper({
store: Persistor;
children: any;
}) {
const handleBeforeUnload = async () => {
if (
persistReducerOptions.whiteList?.length &&
Object.keys(store.getState()).length > 0
) {
const whiteListInStore = await persistReducerOptions.whiteList.reduce(
(acc: any, curr: string) => {
return {
...acc,
[curr as string]: store.getState()[curr as string] as any,
};
},
{}
);
if (!isPaused) {
const db =
persistReducerOptions.storage ||
storageBuilder(persistReducerOptions.storageType || 'localStorage');
db.setItem(PERSIST_STORAGE_NAME, whiteListInStore).then();
}
}
};

const handleInitPersistData = async () => {
if (
persistReducerOptions?.whiteList &&
Expand All @@ -73,12 +49,6 @@ function PersistWrapper({
useEffect(() => {
globalStore = store;
handleInitPersistData().then();

window.addEventListener('beforeunload', handleBeforeUnload);

return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, []);

return <>{children}</>;
Expand Down
49 changes: 49 additions & 0 deletions src/persistor/persistReducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PERSIST_STORAGE_NAME,
StorageInterface,
} from './persistStorage/storageInterface';
import isEqual from 'lodash.isequal';

export type Reducer<S = any, A extends Action = Action> = (
state: S,
Expand Down Expand Up @@ -31,15 +32,63 @@ export const REHYDRATE_PERSIST_ACTION = 'PERSIST/REHYDRATE';
export const PAUSE_PERSIST_ACTION = 'PERSIST/PAUSE';
export const PURGE_PERSIST_ACTION = 'PERSIST/PURGE';
export const PERSIST_ACTION = 'PERSIST/PERSIST';
function debounce<T extends (...args: any[]) => void>(
func: T,
delay: number
): (...args: Parameters<T>) => void {
let timeoutId: ReturnType<typeof setTimeout> | null = null;

return (...args: Parameters<T>) => {
if (timeoutId) {
clearTimeout(timeoutId);
}

timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
}
const handlePersistData = async (store: Record<any, any>) => {
if (
persistReducerOptions.whiteList?.length &&
Object.keys(store).length > 0
) {
const whiteListInStore = await persistReducerOptions.whiteList.reduce(
(acc: any, curr: string) => {
return {
...acc,
[curr as string]: store?.[curr as string] as any,
};
},
{}
);
if (!isPaused) {
const db =
persistReducerOptions.storage ||
storageBuilder(persistReducerOptions.storageType || 'localStorage');
db.setItem(PERSIST_STORAGE_NAME, whiteListInStore).then(() => {});
}
}
};
export function persistReducer<S, A extends Action>(
baseReducer: Reducer<S, A>,
options?: PersistReducerOptionsType
): Reducer<any> {
persistReducerOptions = options || {};
const storage =
options?.storage || storageBuilder(options?.storageType || 'localStorage');
const update = debounce(state => handlePersistData(state), 500);
let prevWhiteList = {};
return (state, action: any) => {
const newWhiteList = options?.whiteList?.reduce((acc: any, curr: any) => {
return { ...acc, [curr as string]: state?.[curr as string] };
}, {});

if (!isEqual(newWhiteList, prevWhiteList)) {
update(state);
}

prevWhiteList = newWhiteList;
switch (action.type) {
case REHYDRATE_PERSIST_ACTION:
if (!isPaused && action.payload) {
Expand Down

0 comments on commit 5aa0d19

Please sign in to comment.