@umituz/react-native-storage 2.0.0 → 2.3.2
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 +23 -7
- package/src/__tests__/integration.test.ts +391 -0
- package/src/__tests__/mocks/asyncStorage.mock.ts +52 -0
- package/src/__tests__/performance.test.ts +351 -0
- package/src/__tests__/setup.ts +63 -0
- package/src/application/ports/IStorageRepository.ts +0 -12
- package/src/domain/entities/StorageResult.ts +1 -3
- package/src/domain/entities/__tests__/CachedValue.test.ts +149 -0
- package/src/domain/entities/__tests__/StorageResult.test.ts +122 -0
- package/src/domain/errors/StorageError.ts +0 -2
- package/src/domain/errors/__tests__/StorageError.test.ts +127 -0
- package/src/domain/utils/__tests__/devUtils.test.ts +97 -0
- package/src/domain/utils/devUtils.ts +37 -0
- package/src/domain/value-objects/StorageKey.ts +27 -29
- package/src/index.ts +9 -1
- package/src/infrastructure/adapters/StorageService.ts +8 -6
- package/src/infrastructure/repositories/AsyncStorageRepository.ts +27 -108
- package/src/infrastructure/repositories/BaseStorageOperations.ts +101 -0
- package/src/infrastructure/repositories/BatchStorageOperations.ts +42 -0
- package/src/infrastructure/repositories/StringStorageOperations.ts +44 -0
- package/src/infrastructure/repositories/__tests__/AsyncStorageRepository.test.ts +169 -0
- package/src/infrastructure/repositories/__tests__/BaseStorageOperations.test.ts +200 -0
- package/src/presentation/hooks/CacheStorageOperations.ts +95 -0
- package/src/presentation/hooks/__tests__/usePersistentCache.test.ts +404 -0
- package/src/presentation/hooks/__tests__/useStorage.test.ts +246 -0
- package/src/presentation/hooks/__tests__/useStorageState.test.ts +292 -0
- package/src/presentation/hooks/useCacheState.ts +55 -0
- package/src/presentation/hooks/usePersistentCache.ts +30 -39
- package/src/presentation/hooks/useStorage.ts +4 -3
- package/src/presentation/hooks/useStorageState.ts +24 -8
- package/src/presentation/hooks/useStore.ts +3 -1
- package/src/types/global.d.ts +40 -0
- package/LICENSE +0 -22
- package/src/presentation/hooks/usePersistedState.ts +0 -34
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Provides clean API for components to interact with storage domain
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { useCallback } from 'react';
|
|
8
|
+
import { useCallback, useMemo } from 'react';
|
|
9
9
|
import { storageRepository } from '../../infrastructure/repositories/AsyncStorageRepository';
|
|
10
10
|
import type { StorageResult } from '../../domain/entities/StorageResult';
|
|
11
11
|
import { unwrap } from '../../domain/entities/StorageResult';
|
|
@@ -88,7 +88,8 @@ export const useStorage = () => {
|
|
|
88
88
|
return storageRepository.getItem(keyString, defaultValue);
|
|
89
89
|
}, []);
|
|
90
90
|
|
|
91
|
-
return
|
|
91
|
+
// Memoize return object to prevent unnecessary re-renders
|
|
92
|
+
return useMemo(() => ({
|
|
92
93
|
getItem,
|
|
93
94
|
setItem,
|
|
94
95
|
getString,
|
|
@@ -97,5 +98,5 @@ export const useStorage = () => {
|
|
|
97
98
|
hasItem,
|
|
98
99
|
clearAll,
|
|
99
100
|
getItemWithResult,
|
|
100
|
-
};
|
|
101
|
+
}), [getItem, setItem, getString, setString, removeItem, hasItem, clearAll, getItemWithResult]);
|
|
101
102
|
};
|
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Domain-Driven Design: Presentation layer hook for state + storage sync
|
|
5
5
|
* Combines React state with automatic storage persistence
|
|
6
|
-
*
|
|
7
|
-
* Theme: {{THEME_NAME}} ({{CATEGORY}} category)
|
|
8
6
|
*/
|
|
9
7
|
|
|
10
8
|
import { useState, useEffect, useCallback } from 'react';
|
|
@@ -18,7 +16,7 @@ import type { StorageKey } from '../../domain/value-objects/StorageKey';
|
|
|
18
16
|
*
|
|
19
17
|
* @example
|
|
20
18
|
* ```typescript
|
|
21
|
-
* const [
|
|
19
|
+
* const [settings, setSettings] = useStorageState('user_settings', { theme: 'light' });
|
|
22
20
|
* // State is automatically persisted to storage
|
|
23
21
|
* ```
|
|
24
22
|
*/
|
|
@@ -32,15 +30,33 @@ export const useStorageState = <T>(
|
|
|
32
30
|
|
|
33
31
|
// Load initial value from storage
|
|
34
32
|
useEffect(() => {
|
|
33
|
+
let isMounted = true;
|
|
34
|
+
|
|
35
35
|
const loadFromStorage = async () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
try {
|
|
37
|
+
const result = await storageRepository.getItem(keyString, defaultValue);
|
|
38
|
+
const value = unwrap(result, defaultValue);
|
|
39
|
+
|
|
40
|
+
// Memory leak önlemek için component mount kontrolü
|
|
41
|
+
if (isMounted) {
|
|
42
|
+
setState(value);
|
|
43
|
+
setIsLoading(false);
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
// Hata durumunda bile cleanup yap
|
|
47
|
+
if (isMounted) {
|
|
48
|
+
setIsLoading(false);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
40
51
|
};
|
|
41
52
|
|
|
42
53
|
loadFromStorage();
|
|
43
|
-
|
|
54
|
+
|
|
55
|
+
// Cleanup function
|
|
56
|
+
return () => {
|
|
57
|
+
isMounted = false;
|
|
58
|
+
};
|
|
59
|
+
}, [keyString]); // defaultValue'ı dependency array'den çıkar
|
|
44
60
|
|
|
45
61
|
// Update state and persist to storage
|
|
46
62
|
const updateState = useCallback(
|
|
@@ -8,6 +8,8 @@ import { createStore } from '../../domain/factories/StoreFactory';
|
|
|
8
8
|
import type { StoreConfig } from '../../domain/types/Store';
|
|
9
9
|
|
|
10
10
|
export function useStore<T extends object>(config: StoreConfig<T>) {
|
|
11
|
-
|
|
11
|
+
// Config objesini stabilize et ki sonsuz re-render olmasın
|
|
12
|
+
const stableConfig = useMemo(() => config, [config.name, JSON.stringify(config)]);
|
|
13
|
+
const store = useMemo(() => createStore(stableConfig), [stableConfig]);
|
|
12
14
|
return store;
|
|
13
15
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
|
|
4
|
+
declare module '@react-native-async-storage/async-storage' {
|
|
5
|
+
export interface AsyncStorageStatic {
|
|
6
|
+
getItem(key: string): Promise<string | null>;
|
|
7
|
+
setItem(key: string, value: string): Promise<void>;
|
|
8
|
+
removeItem(key: string): Promise<void>;
|
|
9
|
+
clear(): Promise<void>;
|
|
10
|
+
getAllKeys(): Promise<readonly string[]>;
|
|
11
|
+
multiGet(keys: readonly string[]): Promise<readonly (readonly [string, string | null])[]>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const AsyncStorage: AsyncStorageStatic;
|
|
15
|
+
export default AsyncStorage;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare module 'react' {
|
|
19
|
+
export function useState<T>(initialState: T | (() => T)): [T, (value: T | ((prev: T) => T)) => void];
|
|
20
|
+
export function useEffect(effect: () => void | (() => void), deps?: readonly any[]): void;
|
|
21
|
+
export function useCallback<T extends (...args: any[]) => any>(callback: T, deps: readonly any[]): T;
|
|
22
|
+
export function useMemo<T>(factory: () => T, deps: readonly any[]): T;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare module 'react-native' {
|
|
26
|
+
// Add React Native specific types if needed
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare global {
|
|
30
|
+
namespace jest {
|
|
31
|
+
interface Matchers<R> {
|
|
32
|
+
toBeValidStorageKey(): R;
|
|
33
|
+
toBeExpired(): R;
|
|
34
|
+
toHaveValidCache(): R;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var __DEV__: boolean | undefined;
|
|
39
|
+
var global: any;
|
|
40
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Ümit UZ
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* usePersistedState Hook
|
|
3
|
-
* Like useState but persisted to AsyncStorage
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { useEffect, useState, useCallback } from 'react';
|
|
7
|
-
import { storageRepository } from '../../infrastructure/repositories/AsyncStorageRepository';
|
|
8
|
-
|
|
9
|
-
export function usePersistedState<T>(
|
|
10
|
-
key: string,
|
|
11
|
-
initialValue: T
|
|
12
|
-
): [T, (value: T) => void, boolean] {
|
|
13
|
-
const [state, setState] = useState<T>(initialValue);
|
|
14
|
-
const [isLoaded, setIsLoaded] = useState(false);
|
|
15
|
-
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
storageRepository.getItem(key, initialValue).then((result) => {
|
|
18
|
-
if (result.success) {
|
|
19
|
-
setState(result.data);
|
|
20
|
-
}
|
|
21
|
-
setIsLoaded(true);
|
|
22
|
-
});
|
|
23
|
-
}, [key]);
|
|
24
|
-
|
|
25
|
-
const setPersistedState = useCallback(
|
|
26
|
-
(value: T) => {
|
|
27
|
-
setState(value);
|
|
28
|
-
storageRepository.setItem(key, value);
|
|
29
|
-
},
|
|
30
|
-
[key]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
return [state, setPersistedState, isLoaded];
|
|
34
|
-
}
|