@umituz/react-native-storage 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/domain/factories/StoreFactory.ts +57 -19
- package/src/domain/types/Store.ts +31 -5
- package/src/index.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-storage",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Zustand state management with AsyncStorage persistence and type-safe storage operations for React Native",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Store Factory
|
|
3
|
-
* Create Zustand stores with AsyncStorage persistence
|
|
3
|
+
* Create Zustand stores with AsyncStorage persistence and actions
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const useCounterStore = createStore({
|
|
8
|
+
* name: 'counter',
|
|
9
|
+
* initialState: { count: 0 },
|
|
10
|
+
* actions: (set, get) => ({
|
|
11
|
+
* increment: () => set({ count: get().count + 1 }),
|
|
12
|
+
* reset: () => set({ count: 0 }),
|
|
13
|
+
* }),
|
|
14
|
+
* persist: true,
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
4
17
|
*/
|
|
5
18
|
|
|
6
19
|
import { create } from 'zustand';
|
|
@@ -8,26 +21,51 @@ import { persist, createJSONStorage } from 'zustand/middleware';
|
|
|
8
21
|
import type { StoreConfig } from '../types/Store';
|
|
9
22
|
import { storageService } from '../../infrastructure/adapters/StorageService';
|
|
10
23
|
|
|
11
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Create a Zustand store with optional persistence and actions
|
|
26
|
+
*/
|
|
27
|
+
export function createStore<
|
|
28
|
+
TState extends object,
|
|
29
|
+
TActions extends object = object
|
|
30
|
+
>(config: StoreConfig<TState, TActions>) {
|
|
31
|
+
type Store = TState & TActions;
|
|
32
|
+
|
|
33
|
+
const stateCreator = (
|
|
34
|
+
set: (partial: Partial<Store>) => void,
|
|
35
|
+
get: () => Store
|
|
36
|
+
): Store => {
|
|
37
|
+
const state = config.initialState as Store;
|
|
38
|
+
const actions = config.actions ? config.actions(set as any, get as any) : ({} as TActions);
|
|
39
|
+
return { ...state, ...actions };
|
|
40
|
+
};
|
|
41
|
+
|
|
12
42
|
if (!config.persist) {
|
|
13
|
-
return create<
|
|
43
|
+
return create<Store>(stateCreator);
|
|
14
44
|
}
|
|
15
45
|
|
|
16
|
-
return create<
|
|
17
|
-
persist(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
return create<Store>()(
|
|
47
|
+
persist(stateCreator, {
|
|
48
|
+
name: config.name,
|
|
49
|
+
storage: createJSONStorage(() => storageService),
|
|
50
|
+
version: config.version || 1,
|
|
51
|
+
partialize: config.partialize
|
|
52
|
+
? (state) => config.partialize!(state) as Store
|
|
53
|
+
: (state) => {
|
|
54
|
+
// By default, exclude functions (actions) from persistence
|
|
55
|
+
const persisted: Record<string, unknown> = {};
|
|
56
|
+
for (const key of Object.keys(state)) {
|
|
57
|
+
if (typeof state[key as keyof Store] !== 'function') {
|
|
58
|
+
persisted[key] = state[key as keyof Store];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return persisted as Store;
|
|
62
|
+
},
|
|
63
|
+
onRehydrateStorage: () => (state) => {
|
|
64
|
+
if (state && config.onRehydrate) {
|
|
65
|
+
config.onRehydrate(state);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
migrate: config.migrate as any,
|
|
69
|
+
})
|
|
32
70
|
);
|
|
33
71
|
}
|
|
@@ -1,17 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Store Types
|
|
3
|
+
* Zustand store configuration with actions support
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
import type { StoreApi } from 'zustand';
|
|
7
|
+
|
|
8
|
+
/** Set function type for Zustand */
|
|
9
|
+
export type SetState<T> = StoreApi<T>['setState'];
|
|
10
|
+
|
|
11
|
+
/** Get function type for Zustand */
|
|
12
|
+
export type GetState<T> = StoreApi<T>['getState'];
|
|
13
|
+
|
|
14
|
+
/** Actions creator function */
|
|
15
|
+
export type ActionsCreator<TState, TActions> = (
|
|
16
|
+
set: SetState<TState & TActions>,
|
|
17
|
+
get: GetState<TState & TActions>
|
|
18
|
+
) => TActions;
|
|
19
|
+
|
|
20
|
+
/** Store configuration */
|
|
21
|
+
export interface StoreConfig<TState extends object, TActions extends object = object> {
|
|
22
|
+
/** Unique store name (used as storage key) */
|
|
6
23
|
name: string;
|
|
7
|
-
|
|
24
|
+
/** Initial state */
|
|
25
|
+
initialState: TState;
|
|
26
|
+
/** Actions creator function */
|
|
27
|
+
actions?: ActionsCreator<TState, TActions>;
|
|
28
|
+
/** Enable AsyncStorage persistence */
|
|
8
29
|
persist?: boolean;
|
|
30
|
+
/** State version for migrations */
|
|
9
31
|
version?: number;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
32
|
+
/** Select which state to persist */
|
|
33
|
+
partialize?: (state: TState & TActions) => Partial<TState>;
|
|
34
|
+
/** Callback after rehydration */
|
|
35
|
+
onRehydrate?: (state: TState & TActions) => void;
|
|
36
|
+
/** Migration function for version changes */
|
|
37
|
+
migrate?: (persistedState: unknown, version: number) => TState;
|
|
13
38
|
}
|
|
14
39
|
|
|
40
|
+
/** Persisted state wrapper */
|
|
15
41
|
export interface PersistedState<T> {
|
|
16
42
|
state: T;
|
|
17
43
|
version: number;
|
package/src/index.ts
CHANGED
|
@@ -88,7 +88,13 @@ export { isDev, devWarn, devError, devLog } from './domain/utils/devUtils';
|
|
|
88
88
|
// DOMAIN LAYER - Store Types
|
|
89
89
|
// =============================================================================
|
|
90
90
|
|
|
91
|
-
export type {
|
|
91
|
+
export type {
|
|
92
|
+
StoreConfig,
|
|
93
|
+
PersistedState,
|
|
94
|
+
SetState,
|
|
95
|
+
GetState,
|
|
96
|
+
ActionsCreator,
|
|
97
|
+
} from './domain/types/Store';
|
|
92
98
|
|
|
93
99
|
// =============================================================================
|
|
94
100
|
// DOMAIN LAYER - Store Factory
|