Skip to content

Commit 9483e47

Browse files
Update README file and add callback to the rehydrate function
1 parent 83957ac commit 9483e47

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

README.md

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,18 @@ Whichever option you choose, you must use `configurePersistedStore` and provide
119119
```
120120
// app/store.ts
121121
import { configurePersistedStore } from 'rtk-persist';
122-
// Import your slice reducer (Option 1)
123-
import counterSliceReducer from '../features/counter/counterSlice';
124-
// OR import your persisted reducer (Option 2)
122+
import { counterSlice } from '../features/counter/counterSlice';
125123
import { reducer as counterReducer } from '../features/counter/counterReducer';
126124
127125
// For web, use localStorage or sessionStorage
128126
const storage = localStorage;
129127
130-
// For React Native, you would use:
131-
// import AsyncStorage from '@react-native-async-storage/async-storage';
132-
// const storage = AsyncStorage;
133-
134128
export const store = configurePersistedStore(
135129
{
136130
reducer: {
137-
// For Option 1 (createPersistedSlice)
138-
counter: counterSliceReducer,
139-
140-
// For Option 2 (createPersistedReducer)
141-
// counter: counterReducer,
131+
// IMPORTANT: The key must match the slice's `reducerPath` (which defaults to the slice's name) or the reducer's `reducerName`.
132+
[counterSlice.reducerPath]: counterSlice.reducer,
133+
// [counterReducer.reducerName]: counterReducer,
142134
},
143135
},
144136
'applicationId',
@@ -158,41 +150,58 @@ export type AppDispatch = typeof store.dispatch;
158150

159151
## 🛠️ API
160152

161-
### `createPersistedSlice(sliceOptions)`
153+
<div class="api-section">
162154

155+
### `createPersistedSlice`
163156
A wrapper around RTK's `createSlice`.
164157

158+
<h4>Arguments</h4>
159+
165160
* **`sliceOptions`**: The standard `CreateSliceOptions` object.
166161

167-
### `createPersistedReducer(name, initialState, builderCallback)`
162+
<h4>Returns</h4>
163+
164+
* A standard `Slice` object from Redux Toolkit. You can access its name via `slice.name` or `slice.reducerPath`.
168165

166+
---
167+
168+
### `createPersistedReducer`
169169
A wrapper around RTK's `createReducer`.
170170

171-
* **`name`**: A unique string to identify this reducer in storage.
171+
<h4>Arguments</h4>
172172

173+
* **`name`**: A unique string to identify this reducer in storage.
173174
* **`initialState`**: The initial state for the reducer.
174-
175175
* **`builderCallback`**: A callback that receives a `builder` object to define case reducers.
176176

177-
### `configurePersistedStore(storeOptions, applicationId, storageHandler, persistenceOptions?)`
177+
<h4>Returns</h4>
178178

179+
* A standard `Reducer` function, enhanced with the following property:
180+
* **`reducerName`**: A property that holds the provided `name`.
181+
182+
---
183+
184+
### `configurePersistedStore`
179185
A wrapper around RTK's `configureStore`.
180186

181-
* **`storeOptions`**: The standard `ConfigureStoreOptions` object.
187+
<h4>Arguments</h4>
182188

189+
* **`storeOptions`**: The standard `ConfigureStoreOptions` object.
183190
* **`applicationId`**: A unique string that identifies the application.
184-
185191
* **`storageHandler`**: A storage object that implements `getItem`, `setItem`, and `removeItem`.
186-
187192
* **`persistenceOptions`** (optional): An object to control the persistence behavior:
193+
* `rehydrationTimeout` (optional, `number`): Max time in ms to wait for rehydration. Defaults to `5000`.
194+
* `onRehydrationStart` (optional, `() => void`): Callback invoked when rehydration begins.
195+
* `onRehydrationSuccess` (optional, `() => void`): Callback invoked on successful rehydration.
196+
* `onRehydrationError` (optional, `(error: unknown) => void`): Callback invoked on rehydration error.
188197

189-
* `rehydrationTimeout` (optional, `number`): The maximum time in milliseconds to wait for rehydration to complete before timing out. Defaults to `5000`.
190-
191-
* `onRehydrationStart` (optional, `() => void`): A callback invoked when the rehydration process begins.
198+
<h4>Returns</h4>
192199

193-
* `onRehydrationSuccess` (optional, `() => void`): A callback invoked when the rehydration process completes successfully.
200+
* A `PersistedStore` object, which is a standard Redux store enhanced with the following methods:
201+
* **`rehydrate()`**: A function to manually trigger rehydration from storage.
202+
* **`clearPersistedState()`**: A function that clears all persisted data for the application from storage.
194203

195-
* `onRehydrationError` (optional, `(error: unknown) => void`): A callback invoked if an error occurs during rehydration.
204+
</div>
196205

197206
<br />
198207

src/store.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,20 @@ export const configurePersistedStore: <
8282
* @public
8383
*/
8484
const rehydrate = () => new Promise<void>(async (resolve, reject) => {
85+
persistenceOptions?.onRehydrationStart?.();
86+
8587
const signalTimeout = setTimeout(() => {
86-
reject(new Error("Rehydration timed out"));
88+
const timeoutError = new Error(`Rehydration timed out after ${persistenceOptions?.rehydrationTimeout}ms`);
89+
persistenceOptions?.onRehydrationError?.(timeoutError);
90+
reject(timeoutError);
8791
}, persistenceOptions?.rehydrationTimeout);
8892
const m = createListenerMiddleware();
8993
m.startListening({
9094
actionCreator: REHYDRATE,
9195
effect: (_, l) => {
9296
clearTimeout(signalTimeout);
9397
l.unsubscribe();
98+
persistenceOptions?.onRehydrationSuccess?.();
9499
resolve();
95100
},
96101
});
@@ -105,6 +110,7 @@ export const configurePersistedStore: <
105110
persistedStore.dispatch(REHYDRATE(storedState) as any);
106111
} catch (error) {
107112
clearTimeout(signalTimeout);
113+
persistenceOptions?.onRehydrationError?.(error);
108114
reject(error);
109115
}
110116
});
@@ -133,11 +139,8 @@ export const configurePersistedStore: <
133139
rehydrate();
134140
}
135141

136-
// Asynchronously trigger the initial rehydration and execute callbacks.
137-
persistenceOptions?.onRehydrationStart?.();
138-
rehydrate()
139-
.then(persistenceOptions?.onRehydrationSuccess)
140-
.catch(persistenceOptions?.onRehydrationError);
142+
// Asynchronously trigger the initial rehydration.
143+
rehydrate();
141144

142145

143146

0 commit comments

Comments
 (0)