Skip to content

Commit 83957ac

Browse files
Update README files
1 parent 698ecd7 commit 83957ac

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

README.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@
66

77
The library works by wrapping standard Redux Toolkit functions, adding persistence logic without changing the way you write your reducers or actions.
88

9+
<br />
910

1011
## ✨ Features
1112

1213
* **Effortless Persistence**: Persist any Redux Toolkit slice or reducer with minimal configuration.
14+
1315
* **Flexible API**: Choose between a `createPersistedSlice` utility or a `createPersistedReducer` builder syntax.
16+
1417
* **Storage Agnostic**: Works with any storage provider that implements a simple `getItem`, `setItem`, and `removeItem` interface.
15-
* **Selective Persistence**: An optional filter function allows you to specify exactly which parts of a state should be persisted.
18+
19+
* **Rehydration Lifecycle**: Use optional callbacks (`onRehydrationStart`, `onRehydrationSuccess`, `onRehydrationError`) to react to the persistence lifecycle.
20+
1621
* **TypeScript Support**: Fully typed to ensure a great developer experience.
22+
1723
* **Minimal Footprint**: Extremely lightweight with a production size under 10 KB.
1824

25+
<br />
1926

2027
## ⚙️ Installation
2128

2229
You can install `rtk-persist` using either `yarn` or `npm`:
2330

24-
```bash
31+
```
2532
yarn add rtk-persist
2633
```
2734

2835
or
2936

30-
```bash
37+
```
3138
npm install --save rtk-persist
3239
```
3340

3441
The package has a peer dependency on `@reduxjs/toolkit`.
3542

43+
<br />
3644

3745
## 🚀 Quick Start
3846

@@ -46,7 +54,7 @@ This approach is best if you prefer the `createSlice` API from Redux Toolkit.
4654

4755
Replace `createSlice` with `createPersistedSlice`. The function accepts the same options.
4856

49-
```typescript
57+
```
5058
// features/counter/counterSlice.ts
5159
import { createPersistedSlice } from 'rtk-persist';
5260
import { PayloadAction } from '@reduxjs/toolkit';
@@ -81,10 +89,10 @@ This approach is ideal if you prefer the `createReducer` builder syntax.
8189

8290
Use `createPersistedReducer` and define your case reducers using the builder callback.
8391

84-
```typescript
92+
```
8593
// features/counter/counterReducer.ts
8694
import { createPersistedReducer } from 'rtk-persist';
87-
import { createAction, PayloadAction, UnknownAction } from '@reduxjs/toolkit';
95+
import { createAction } from '@reduxjs/toolkit';
8896
8997
const increment = createAction<number>('increment');
9098
const decrement = createAction<number>('decrement');
@@ -106,9 +114,9 @@ export const reducer = createPersistedReducer(
106114

107115
### 2. Configure the Store
108116

109-
Whichever option you choose, you must use `configurePersistedStore` and provide a storage handler.
117+
Whichever option you choose, you must use `configurePersistedStore` and provide a storage handler. The store is created synchronously, and rehydration from storage happens in the background.
110118

111-
```typescript
119+
```
112120
// app/store.ts
113121
import { configurePersistedStore } from 'rtk-persist';
114122
// Import your slice reducer (Option 1)
@@ -134,44 +142,69 @@ export const store = configurePersistedStore(
134142
},
135143
},
136144
'applicationId',
137-
storage
145+
storage,
146+
{
147+
onRehydrationStart: () => console.log('Rehydration started...'),
148+
onRehydrationSuccess: () => console.log('Rehydration successful!'),
149+
onRehydrationError: (error) => console.error('Rehydration failed:', error),
150+
}
138151
);
139152
140-
type Store = Awaited<typeof store>;
141-
export type RootState = ReturnType<Store['getState']>;
142-
export type AppDispatch = Store['dispatch'];
153+
export type RootState = ReturnType<typeof store.getState>;
154+
export type AppDispatch = typeof store.dispatch;
143155
```
144156

157+
<br />
145158

146159
## 🛠️ API
147160

148161
### `createPersistedSlice(sliceOptions)`
149162

150163
A wrapper around RTK's `createSlice`.
164+
151165
* **`sliceOptions`**: The standard `CreateSliceOptions` object.
152166

153167
### `createPersistedReducer(name, initialState, builderCallback)`
154168

155169
A wrapper around RTK's `createReducer`.
170+
156171
* **`name`**: A unique string to identify this reducer in storage.
172+
157173
* **`initialState`**: The initial state for the reducer.
174+
158175
* **`builderCallback`**: A callback that receives a `builder` object to define case reducers.
159176

160-
### `configurePersistedStore(storeOptions, applicationId, storageHandler)`
177+
### `configurePersistedStore(storeOptions, applicationId, storageHandler, persistenceOptions?)`
161178

162179
A wrapper around RTK's `configureStore`.
180+
163181
* **`storeOptions`**: The standard `ConfigureStoreOptions` object.
182+
164183
* **`applicationId`**: A unique string that identifies the application.
184+
165185
* **`storageHandler`**: A storage object that implements `getItem`, `setItem`, and `removeItem`.
166186

187+
* **`persistenceOptions`** (optional): An object to control the persistence behavior:
188+
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.
192+
193+
* `onRehydrationSuccess` (optional, `() => void`): A callback invoked when the rehydration process completes successfully.
194+
195+
* `onRehydrationError` (optional, `(error: unknown) => void`): A callback invoked if an error occurs during rehydration.
196+
197+
<br />
167198

168199
## ❤️ Author
169200

170-
This library is authored and maintained by **[Fancy Pixel srl](https://www.fancypixel.it)**.
201+
This library is authored and maintained by [**Fancy Pixel srl**](https://www.fancypixel.it).
171202

172203
This library was crafted from our daily experiences building modern web and mobile applications. Contributions are welcome!
173204

205+
<br />
174206

175207
## 📄 License
176208

177209
This project is licensed under the MIT License.
210+

example/README.md

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Use this project to understand the library's features or as a sandbox for testin
1010

1111
1. **Clone the repository** (if you haven't already):
1212
```bash
13-
git clone https://github.com/FancyPixel/rtk-persist.git
13+
git clone [https://github.com/FancyPixel/rtk-persist.git](https://github.com/FancyPixel/rtk-persist.git)
1414
```
1515

1616
2. **Navigate to the example app directory**:
@@ -19,59 +19,40 @@ Use this project to understand the library's features or as a sandbox for testin
1919
```
2020

2121
3. **Install dependencies**:
22+
This command will also link the local `rtk-persist` library from the parent directory, as specified in `package.json`.
2223
```bash
2324
yarn
2425
```
2526

2627
4. **Run the application**:
2728
```bash
28-
yarn start
29+
yarn dev
2930
```
3031

3132
***
3233

3334
## 🛠️ Developing and Testing Local Library Changes
3435

35-
If you have made changes to the `rtk-persist` library source code and want to test them in this example app without publishing to npm, follow these steps.
36-
37-
This workflow ensures that the example app uses your latest local code from the library.
36+
If you have made changes to the `rtk-persist` library source code, the example app can use them directly thanks to the local file path dependency. This workflow ensures that the example app always uses your latest local code from the library.
3837

3938
1. **Navigate to the Library's Root Directory**:
40-
Open a terminal and go to the root of the `rtk-persist` library, not the example app.
39+
Open a terminal and go to the root of the `rtk-persist` library (the parent directory of `example`).
4140
```bash
4241
# Assuming you are in the 'example' directory
4342
cd ..
4443
```
4544
4645
2. **Build the Library**:
47-
Compile the library's TypeScript source code into JavaScript.
46+
After making any changes to the library's source code, you must rebuild it for the changes to be reflected in the example app.
4847
```bash
4948
yarn build
5049
```
5150

52-
3. **Package the Library**:
53-
Create a local tarball (`.tgz` file) of the library. This is what you will install in the example app.
54-
```bash
55-
yarn pack
56-
```
57-
This command will create a file like `rtk-persist-v1.0.1.tgz` in the library's root directory. Note the exact filename.
58-
59-
4. **Navigate Back to the Example App Directory**:
51+
3. **Run the Example App**:
52+
Navigate back to the example app directory and start the development server. The app will automatically use the newly built version of the library.
6053
```bash
6154
cd example
55+
yarn dev
6256
```
6357

64-
5. **Install the Local Library Package**:
65-
Install the `.tgz` file you created in step 3. This will override the version from the npm registry.
66-
```bash
67-
# Replace the filename with the one generated in step 3
68-
yarn add file:../rtk-persist-v1.0.1.tgz
69-
```
70-
71-
6. **Run the Example App**:
72-
Start the example application to see your local changes in action.
73-
```bash
74-
yarn start
75-
```
76-
77-
Now, the example app is running with your modified version of the `rtk-persist` library. Repeat this process every time you want to test new changes.
58+
Now, the example app is running with your modified version of the `rtk-persist` library. Simply repeat step 2 every time you want to test new changes.

0 commit comments

Comments
 (0)