Skip to content

Commit 0270709

Browse files
feat: resync state
1 parent d7efde9 commit 0270709

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const App = () => {
100100
- pauses persistence
101101
- `.persist()`
102102
- resumes persistence
103+
- `.resync()`
104+
- overrides redux state with the value in storage
103105

104106
## State Reconciler
105107
State reconcilers define how incoming state is merged in with initial state. It is critical to choose the right state reconciler for your state. There are three options that ship out of the box, let's look at how each operates:

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export const KEY_PREFIX = 'persist:'
44
export const FLUSH = 'persist/FLUSH'
55
export const REHYDRATE = 'persist/REHYDRATE'
6+
export const RESYNC = 'persist/RESYNC'
67
export const PAUSE = 'persist/PAUSE'
78
export const PERSIST = 'persist/PERSIST'
89
export const PURGE = 'persist/PURGE'

src/persistReducer.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
PURGE,
77
REHYDRATE,
88
DEFAULT_VERSION,
9+
RESYNC,
910
} from './constants'
1011

1112
import type {
@@ -73,9 +74,7 @@ export default function persistReducer<State: Object, Action: Object>(
7374
// dev warning if we are already sealed
7475
if (process.env.NODE_ENV !== 'production' && _sealed)
7576
console.error(
76-
`redux-persist: rehydrate for "${
77-
config.key
78-
}" called after timeout.`,
77+
`redux-persist: rehydrate for "${config.key}" called after timeout.`,
7978
payload,
8079
err
8180
)
@@ -92,9 +91,7 @@ export default function persistReducer<State: Object, Action: Object>(
9291
_rehydrate(
9392
undefined,
9493
new Error(
95-
`redux-persist: persist timed out for persist key "${
96-
config.key
97-
}"`
94+
`redux-persist: persist timed out for persist key "${config.key}"`
9895
)
9996
)
10097
}, timeout)
@@ -112,7 +109,7 @@ export default function persistReducer<State: Object, Action: Object>(
112109
return {
113110
...baseReducer(restState, action),
114111
_persist,
115-
};
112+
}
116113
}
117114

118115
if (
@@ -155,6 +152,16 @@ export default function persistReducer<State: Object, Action: Object>(
155152
...baseReducer(restState, action),
156153
_persist,
157154
}
155+
} else if (action.type === RESYNC) {
156+
getStoredState(config).then(
157+
restoredState => action.rehydrate(config.key, restoredState, undefined),
158+
err => action.rehydrate(config.key, undefined, err)
159+
)
160+
161+
return {
162+
...baseReducer(restState, action),
163+
_persist: { version, rehydrated: false },
164+
}
158165
} else if (action.type === FLUSH) {
159166
action.result(_persistoid && _persistoid.flush())
160167
return {

src/persistStore.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import type {
1111
} from './types'
1212

1313
import { createStore } from 'redux'
14-
import { FLUSH, PAUSE, PERSIST, PURGE, REGISTER, REHYDRATE } from './constants'
14+
import {
15+
FLUSH,
16+
PAUSE,
17+
PERSIST,
18+
RESYNC,
19+
PURGE,
20+
REGISTER,
21+
REHYDRATE,
22+
} from './constants'
1523

1624
type PendingRehydrate = [Object, RehydrateErrorType, PersistConfig]
1725
type Persist = <R>(PersistConfig, MigrationManifest) => R => R
@@ -120,9 +128,12 @@ export default function persistStore(
120128
persist: () => {
121129
store.dispatch({ type: PERSIST, register, rehydrate })
122130
},
131+
resync: () => {
132+
store.dispatch({ type: RESYNC, rehydrate })
133+
},
123134
}
124135

125-
if (!(options && options.manualPersist)){
136+
if (!(options && options.manualPersist)) {
126137
persistor.persist()
127138
}
128139

types/constants.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare module "redux-persist/es/constants" {
22
const KEY_PREFIX: 'persist:';
33
const FLUSH: 'persist/FLUSH';
44
const REHYDRATE: 'persist/REHYDRATE';
5+
const RESYNC: 'persist/RESYNC';
56
const PAUSE: 'persist/PAUSE';
67
const PERSIST: 'persist/PERSIST';
78
const PURGE: 'persist/PURGE';

types/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ declare module "redux-persist/es/types" {
137137
interface Persistor {
138138
pause(): void;
139139
persist(): void;
140+
resync(): void;
140141
purge(): Promise<any>;
141142
flush(): Promise<any>;
142143
dispatch(action: PersistorAction): PersistorAction;

0 commit comments

Comments
 (0)