Skip to content

Commit b7a63dd

Browse files
adding function and updating docs
1 parent eef25d3 commit b7a63dd

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

docs/README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const loadState = (key) => ...
2525
/**
2626
* You define the structure that you want to save
2727
* in this object, and then pass it as an argument.
28-
* Instructions on how to create this object are above.
28+
* Instructions on how to create this object are bellow.
2929
*/
3030
const structure = {
3131
orders: {
@@ -68,23 +68,31 @@ persistMiddleware.run(store)
6868

6969
For the load and save method, you don't have to write them by yourself, they are [available as separate packages below](#providers).
7070

71-
Your reducer data will automatically saved when the values are changed. You can load each reducer using its load action (to see all the load actions generated in your console set the fourth parameter of `createPersistMachine` to `true`).
71+
Your reducer data will automatically save when the values are changed. You can load each reducer using its load action (to see all the load actions generated in your console set the fourth parameter of `createPersistMachine` to `true`).
7272

7373

7474
### Loading Data
7575

76-
You can receive actions in your reducers. The code below will apply the saved state to your current state:
76+
You can receive actions in your reducers.
77+
78+
This allows you to have loading on a per reducer basis separated across the application for stored data instead of having the full application wait for the data to be loaded.
79+
80+
The package exports a function that makes it easier for you to set up a case in the reducer, by generating an action name that matches the one that the package uses under the hood.
81+
82+
The code below will apply the saved state to your current state:
7783

7884
```js
79-
case "@ReduxPM/LoadSubscriptionOrders": {
80-
return {
81-
...state,
85+
import { getPersistMachineAction } from 'redux-persist-machine'
86+
87+
case getPersistMachineAction("load.subscriptionOrders"): {
88+
return {
89+
...state,
8290
...action.payload,
8391
}
8492
}
8593
```
8694

87-
This allows you to have loading on a per reducer basis separated across the application for stored data instead of having the full application wait for the data to be loaded.
95+
Of course, you can also define your own custom action name. This custom action name has to be passed oon the `action` property of the reducer in the structure object.
8896

8997
The middleware runs: `action.payload = { ...storedData, ...action.payload };` to add the saved data to the payload when the `@ReduxPM/LoadActions` is triggered. You can also pass additional data in your payload to add context to your `@ReduxPM/LoadActions` for complex conditional consummation of the loaded data in your reducers.
9098

@@ -105,14 +113,29 @@ Creates a Redux persist middleware with your store structure.
105113

106114
### `createPersistMiddleware(structure, saveState, loadState).run(store)`
107115

116+
- `store :` [Store](https://redux.js.org/api/store) - the redux store.
117+
108118
Starts the persist middleware. You should run this immediately after creating your store.
109119

110120
```js
111121
const persistMiddleware = createPersistMiddleware(structure, saveState, loadState)
112122
persistMiddleware.run(store)
113123
```
114124

115-
- `store :` [Store](https://redux.js.org/api/store) - the redux store.
125+
### `getPersistMachineAction(actionKey)`
126+
127+
Generates an action name based on the provided key. You can use this function to set the case in your reducer to handle the data loading.
128+
129+
```js
130+
case getPersistMachineAction("load.user.orders"): {
131+
return {
132+
...state,
133+
...action.payload,
134+
}
135+
}
136+
```
137+
138+
The example above would return `@ReduxPM/LoadUserOrders`.
116139

117140
## Providers
118141

lib/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ declare namespace persistMiddleware {
2323
* to persist.
2424
* @param store - Redux Store
2525
* @param debug - Debug data to the console
26-
*
2726
*/
2827
export declare function createPersistMachine(structure: any, save: SaveCallback, load: LoadCallback, debug: boolean): typeof persistMiddleware;
28+
/**
29+
* Builds a action type.
30+
* e.g. transforms "data.adminAuth" into @ReduxPM/LoadDataAdminAuth
31+
*
32+
* @param {string} key the key to generate the action name
33+
*/
34+
export declare function getPersistMachineAction(key: string): string;
2935
export {};

lib/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ function persistMiddleware() {
8080
* to persist.
8181
* @param store - Redux Store
8282
* @param debug - Debug data to the console
83-
*
8483
*/
8584
export function createPersistMachine(structure, save, load, debug) {
8685
// Assign our save and load methods
@@ -161,15 +160,15 @@ export function createPersistMachine(structure, save, load, debug) {
161160
// Get the static key for mapping
162161
const { key: asyncStorageKey, automatic = true } = object;
163162
// Builds the type from the reducer name, if a type has not been explicitly defined through the `action` value
164-
const action = object.action || buildAction(name);
163+
const action = object.action || getPersistMachineAction(name);
165164
// Initialize and empty currentValue, this is used to keep track of previous values
166165
currentValue[asyncStorageKey] = Object.assign(Object.assign({}, _get(currentValue, name, {})), { key: asyncStorageKey, action,
167166
automatic, isLoaded: false });
168167
});
169168
// If debug, we to log all the actions for loading the state
170169
if (debug) {
171170
_map(structure, (object, name) => __awaiter(this, void 0, void 0, function* () {
172-
console.log(object.action || buildAction(name));
171+
console.log(object.action || getPersistMachineAction(name));
173172
}));
174173
}
175174
return persistMiddleware;
@@ -202,11 +201,11 @@ function select(state, key) {
202201
return _get(state, key, {});
203202
}
204203
/**
205-
* Builds a action type e.g. transforms "data.adminAuth" into LOAD_DATA_ADMIN_AUTH
204+
* Builds a action type.
205+
* e.g. transforms "data.adminAuth" into @ReduxPM/LoadDataAdminAuth
206206
*
207-
* @param key
208-
* @return {string}
207+
* @param {string} key the key to generate the action name
209208
*/
210-
function buildAction(key) {
209+
export function getPersistMachineAction(key) {
211210
return `@ReduxPM/Load${_startCase(key).split(" ").join("")}`;
212211
}

lib/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ declare namespace persistMiddleware {
9393
* to persist.
9494
* @param store - Redux Store
9595
* @param debug - Debug data to the console
96-
*
9796
*/
9897
export function createPersistMachine(structure: any, save: SaveCallback, load: LoadCallback, debug: boolean) {
9998
// Assign our save and load methods
@@ -183,7 +182,7 @@ export function createPersistMachine(structure: any, save: SaveCallback, load: L
183182
automatic = true
184183
} = object;
185184
// Builds the type from the reducer name, if a type has not been explicitly defined through the `action` value
186-
const action = object.action || buildAction(name);
185+
const action = object.action || getPersistMachineAction(name);
187186

188187
// Initialize and empty currentValue, this is used to keep track of previous values
189188
currentValue[asyncStorageKey] = {
@@ -198,7 +197,7 @@ export function createPersistMachine(structure: any, save: SaveCallback, load: L
198197
// If debug, we to log all the actions for loading the state
199198
if (debug) {
200199
_map(structure, async (object: any, name: any) => {
201-
console.log(object.action || buildAction(name));
200+
console.log(object.action || getPersistMachineAction(name));
202201
});
203202
}
204203

@@ -235,11 +234,11 @@ function select(state: any, key: any): object {
235234
}
236235

237236
/**
238-
* Builds a action type e.g. transforms "data.adminAuth" into LOAD_DATA_ADMIN_AUTH
239-
*
240-
* @param key
241-
* @return {string}
237+
* Builds an action type.
238+
* e.g. transforms "data.adminAuth" into @ReduxPM/LoadDataAdminAuth
239+
*
240+
* @param {string} key the key to generate the action name
242241
*/
243-
function buildAction(key: string) {
242+
export function getPersistMachineAction(key: string): string {
244243
return `@ReduxPM/Load${_startCase(key).split(" ").join("")}`
245244
}

0 commit comments

Comments
 (0)