Skip to content

Commit d952971

Browse files
Remove unused functions from the slice and the reducer extenders
1 parent 3451325 commit d952971

File tree

4 files changed

+30
-65
lines changed

4 files changed

+30
-65
lines changed

src/reducer.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { Builder } from './extraReducersBuilder';
77
import { listenerMiddleware } from './middleware';
88
import Settings from './settings';
9-
import { DEFAULT_INIT_ACTION_TYPE, NotFunction, PersistedReducer } from './types';
9+
import { DEFAULT_INIT_ACTION_TYPE, NotFunction, ReducerWithInitialState } from './types';
1010
import UpdatedAtHelper from './updatedAtHelper';
1111
import { getStorageName } from './utils';
1212

@@ -79,7 +79,15 @@ const { reducer } = createPersistedReducer(
7979
```
8080
* @public
8181
*/
82-
export const createPersistedReducer: <ReducerName extends string, S extends NotFunction<any>>(reducerName: ReducerName, initialState: S | (() => S), mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void, filtersSlice?: (state: S) => Partial<S>) => PersistedReducer<ReducerName, S> = <ReducerName extends string, S extends NotFunction<any>>(
82+
export const createPersistedReducer: <
83+
ReducerName extends string,
84+
S extends NotFunction<any>
85+
>(
86+
reducerName: ReducerName,
87+
initialState: S | (() => S),
88+
mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void,
89+
filtersSlice?: (state: S) => Partial<S>
90+
) => ReducerWithInitialState<S> = <ReducerName extends string, S extends NotFunction<any>>(
8391
reducerName: ReducerName,
8492
initialState: S | (() => S),
8593
mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void,
@@ -113,15 +121,6 @@ export const createPersistedReducer: <ReducerName extends string, S extends NotF
113121
UpdatedAtHelper.onSave(reducerName);
114122
}
115123

116-
/**
117-
* Clears the stored data from the selected storage
118-
*
119-
* @public
120-
*/
121-
async function clearPersistedStorage() {
122-
await Settings.storageHandler.removeItem(storageName);
123-
}
124-
125124
/**
126125
* Creates the reducer using the default options passed by the user.
127126
*
@@ -163,5 +162,5 @@ export const createPersistedReducer: <ReducerName extends string, S extends NotF
163162
},
164163
});
165164

166-
return { reducer, reducerName, listenerMiddleware, clearPersistedStorage };
165+
return reducer;
167166
};

src/slice.ts

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import {
22
createSlice,
33
CreateSliceOptions,
4-
ListenerMiddlewareInstance,
54
PayloadAction,
65
Slice,
76
SliceCaseReducers,
8-
SliceSelectors,
7+
SliceSelectors
98
} from '@reduxjs/toolkit';
109
import { Builder } from './extraReducersBuilder';
1110
import { listenerMiddleware } from './middleware';
1211
import Settings from './settings';
1312
import { DEFAULT_INIT_ACTION_TYPE } from './types';
1413
import UpdatedAtHelper from './updatedAtHelper';
15-
import { getStorageName, getStoredState } from './utils';
14+
import { getStorageName } from './utils';
1615

1716
/**
1817
* A function that accepts an initial state, an object full of reducer
@@ -45,14 +44,7 @@ export const createPersistedSlice: <
4544
PeristedSelectors
4645
>,
4746
filtersSlice?: (state: SliceState) => Partial<SliceState>,
48-
) => Omit<
49-
Slice<SliceState, PCR, Name, ReducerPath, PeristedSelectors>,
50-
'getInitialState'
51-
> & {
52-
getInitialState: () => Promise<SliceState>;
53-
listenerMiddleware: ListenerMiddlewareInstance;
54-
clearPersistedStorage: () => void;
55-
} = <
47+
) => Slice<SliceState, PCR, Name, ReducerPath, PeristedSelectors> = <
5648
SliceState,
5749
Name extends string = string,
5850
PCR extends
@@ -83,24 +75,6 @@ export const createPersistedSlice: <
8375
Record<Name, SliceState>
8476
>();
8577

86-
/**
87-
* Overrides the getInitialState function to return the stored data
88-
*
89-
* @returns The initial state of the slice merged with the stored data
90-
*
91-
* @public
92-
*/
93-
// TODO: verify if we should inject the persisted state when reinizializing (I think we should remove this override)
94-
async function getInitialState(): Promise<SliceState> {
95-
let storage: Partial<SliceState> = slice.getInitialState();
96-
try {
97-
storage = await getStoredState(sliceOptions.name) ?? storage;
98-
} catch (e) {
99-
// console.error(e);
100-
}
101-
return { ...slice.getInitialState(), ...storage };
102-
}
103-
10478
/**
10579
* Writes the updated state to the selected storage
10680
*
@@ -116,15 +90,6 @@ export const createPersistedSlice: <
11690
UpdatedAtHelper.onSave(sliceOptions.name);
11791
}
11892

119-
/**
120-
* Clears the stored data from the selected storage
121-
*
122-
* @public
123-
*/
124-
async function clearPersistedStorage() {
125-
await Settings.storageHandler.removeItem(storageName);
126-
}
127-
12893
/**
12994
* Creates the slice using the default options passed by the user.
13095
*
@@ -184,10 +149,5 @@ export const createPersistedSlice: <
184149
},
185150
});
186151

187-
return {
188-
...slice,
189-
getInitialState,
190-
listenerMiddleware,
191-
clearPersistedStorage,
192-
};
152+
return slice;
193153
};

src/types.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActionCreatorInvariantMiddlewareOptions, Draft, ImmutableStateInvariantMiddlewareOptions, ListenerMiddlewareInstance, Middleware, SerializableStateInvariantMiddlewareOptions, StoreEnhancer, ThunkMiddleware, Tuple, UnknownAction } from "@reduxjs/toolkit";
1+
import { ActionCreatorInvariantMiddlewareOptions, Draft, ImmutableStateInvariantMiddlewareOptions, Middleware, SerializableStateInvariantMiddlewareOptions, StoreEnhancer, ThunkMiddleware, Tuple, UnknownAction } from "@reduxjs/toolkit";
22

33
/**
44
* This is the description of the storage handler used to persist the data
@@ -119,13 +119,7 @@ type CaseReducer<S = any, A extends Action = UnknownAction> = (state: Draft<S>,
119119

120120
export type NotFunction<T> = T extends Function ? never : T;
121121
export type ReducerWithInitialState<S extends NotFunction<any>> = Reducer<S> & {
122-
getInitialState: () => S;
123-
};
124-
export type PersistedReducer<ReducerName extends String, S extends NotFunction<any>> = {
125-
reducer: ReducerWithInitialState<S>;
126-
reducerName: ReducerName;
127-
listenerMiddleware: ListenerMiddlewareInstance;
128-
clearPersistedStorage: () => void;
122+
getInitialState: () => S;
129123
};
130124

131125
interface TypeGuard<T> {

src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ export async function getStoredState<T>(name: string): Promise<Partial<T> | null
1818
// console.error(e);
1919
}
2020
return null;
21-
}
21+
}
22+
23+
/**
24+
* Clears the stored data from the selected storage
25+
*
26+
* @param name - string: a uniq name for the state slice implemented.
27+
*
28+
* @public
29+
*/
30+
export async function clearPersistedStorage(name: string) {
31+
const storageName = getStorageName(name);
32+
await Settings.storageHandler.removeItem(storageName);
33+
}

0 commit comments

Comments
 (0)