react-native-mmkv 4.0.0-beta.1 → 4.0.0-beta.11
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/NitroMmkv.podspec +44 -0
- package/README.md +1 -3
- package/android/CMakeLists.txt +31 -30
- package/android/build.gradle +66 -21
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +5 -5
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/mmkv/HybridMMKVPlatformContext.kt +19 -0
- package/android/src/main/java/com/margelo/nitro/mmkv/NitroMmkvPackage.java +33 -0
- package/cpp/HybridMMKV.cpp +184 -0
- package/cpp/HybridMMKV.hpp +47 -0
- package/cpp/HybridMMKVFactory.cpp +33 -0
- package/cpp/HybridMMKVFactory.hpp +24 -0
- package/cpp/{MmkvTypes.h → MMKVTypes.hpp} +1 -1
- package/cpp/MMKVValueChangedListenerRegistry.cpp +58 -0
- package/cpp/MMKVValueChangedListenerRegistry.hpp +43 -0
- package/cpp/{ManagedMMBuffer.h → ManagedMMBuffer.hpp} +13 -5
- package/ios/HybridMMKVPlatformContext.swift +43 -0
- package/lib/__tests__/hooks.test.d.ts +1 -0
- package/lib/__tests__/hooks.test.js +69 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
- package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
- package/lib/createMMKV/createMMKV.d.ts +3 -0
- package/lib/createMMKV/createMMKV.js +40 -0
- package/lib/createMMKV/createMMKV.web.d.ts +3 -0
- package/lib/createMMKV/createMMKV.web.js +117 -0
- package/lib/createMMKV/createMockMMKV.d.ts +5 -0
- package/lib/createMMKV/createMockMMKV.js +74 -0
- package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
- package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
- package/lib/hooks/createMMKVHook.d.ts +2 -0
- package/lib/hooks/createMMKVHook.js +49 -0
- package/lib/hooks/useMMKV.d.ts +11 -0
- package/lib/hooks/useMMKV.js +23 -0
- package/lib/hooks/useMMKVBoolean.d.ts +11 -0
- package/lib/hooks/useMMKVBoolean.js +12 -0
- package/lib/hooks/useMMKVBuffer.d.ts +11 -0
- package/lib/hooks/useMMKVBuffer.js +12 -0
- package/lib/hooks/useMMKVKeys.d.ts +12 -0
- package/lib/hooks/useMMKVKeys.js +33 -0
- package/lib/hooks/useMMKVListener.d.ts +15 -0
- package/lib/hooks/useMMKVListener.js +26 -0
- package/lib/hooks/useMMKVNumber.d.ts +11 -0
- package/lib/hooks/useMMKVNumber.js +12 -0
- package/lib/hooks/useMMKVObject.d.ts +17 -0
- package/lib/hooks/useMMKVObject.js +38 -0
- package/lib/hooks/useMMKVString.d.ts +11 -0
- package/lib/hooks/useMMKVString.js +12 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +11 -0
- package/lib/isTest.d.ts +1 -0
- package/lib/isTest.js +7 -0
- package/lib/specs/MMKV.nitro.d.ts +94 -0
- package/lib/specs/MMKV.nitro.js +1 -0
- package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +26 -33
- package/lib/specs/MMKVFactory.nitro.js +1 -0
- package/lib/specs/MMKVPlatformContext.nitro.d.ts +18 -0
- package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
- package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
- package/lib/web/createTextEncoder.js +17 -0
- package/nitro.json +28 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
- package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +54 -0
- package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +52 -0
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +77 -0
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +45 -0
- package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
- package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +82 -0
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +141 -0
- package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +64 -0
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
- package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
- package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
- package/package.json +74 -122
- package/react-native.config.js +2 -15
- package/src/__tests__/hooks.test.tsx +39 -34
- package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
- package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
- package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
- package/src/createMMKV/createMMKV.ts +51 -0
- package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +58 -46
- package/src/createMMKV/createMockMMKV.ts +78 -0
- package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
- package/src/hooks/createMMKVHook.ts +66 -0
- package/src/hooks/useMMKV.ts +45 -0
- package/src/hooks/useMMKVBoolean.ts +15 -0
- package/src/hooks/useMMKVBuffer.ts +15 -0
- package/src/hooks/useMMKVKeys.ts +36 -0
- package/src/hooks/useMMKVListener.ts +33 -0
- package/src/hooks/useMMKVNumber.ts +15 -0
- package/src/hooks/useMMKVObject.ts +53 -0
- package/src/hooks/useMMKVString.ts +15 -0
- package/src/index.ts +15 -3
- package/src/{PlatformChecker.ts → isTest.ts} +2 -2
- package/src/specs/MMKV.nitro.ts +93 -0
- package/src/specs/MMKVFactory.nitro.ts +87 -0
- package/src/specs/MMKVPlatformContext.nitro.ts +17 -0
- package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
- package/android/src/main/cpp/AndroidLogger.cpp +0 -16
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
- package/cpp/MmkvHostObject.cpp +0 -360
- package/cpp/MmkvHostObject.h +0 -31
- package/cpp/MmkvLogger.h +0 -35
- package/cpp/NativeMmkvModule.cpp +0 -43
- package/cpp/NativeMmkvModule.h +0 -31
- package/ios/AppleLogger.mm +0 -16
- package/ios/MmkvOnLoad.mm +0 -25
- package/ios/MmkvPlatformContext.h +0 -19
- package/ios/MmkvPlatformContextModule.mm +0 -55
- package/lib/commonjs/MMKV.js +0 -124
- package/lib/commonjs/MMKV.js.map +0 -1
- package/lib/commonjs/MemoryWarningListener.js +0 -31
- package/lib/commonjs/MemoryWarningListener.js.map +0 -1
- package/lib/commonjs/MemoryWarningListener.web.js +0 -11
- package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
- package/lib/commonjs/ModuleNotFoundError.js +0 -75
- package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
- package/lib/commonjs/NativeMmkv.js +0 -47
- package/lib/commonjs/NativeMmkv.js.map +0 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
- package/lib/commonjs/PlatformChecker.js +0 -14
- package/lib/commonjs/PlatformChecker.js.map +0 -1
- package/lib/commonjs/Types.js +0 -26
- package/lib/commonjs/Types.js.map +0 -1
- package/lib/commonjs/createMMKV.js +0 -43
- package/lib/commonjs/createMMKV.js.map +0 -1
- package/lib/commonjs/createMMKV.mock.js +0 -43
- package/lib/commonjs/createMMKV.mock.js.map +0 -1
- package/lib/commonjs/createMMKV.web.js +0 -110
- package/lib/commonjs/createMMKV.web.js.map +0 -1
- package/lib/commonjs/createTextEncoder.js +0 -23
- package/lib/commonjs/createTextEncoder.js.map +0 -1
- package/lib/commonjs/hooks.js +0 -198
- package/lib/commonjs/hooks.js.map +0 -1
- package/lib/commonjs/index.js +0 -40
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
- package/lib/module/MMKV.js +0 -119
- package/lib/module/MMKV.js.map +0 -1
- package/lib/module/MemoryWarningListener.js +0 -27
- package/lib/module/MemoryWarningListener.js.map +0 -1
- package/lib/module/MemoryWarningListener.web.js +0 -6
- package/lib/module/MemoryWarningListener.web.js.map +0 -1
- package/lib/module/ModuleNotFoundError.js +0 -70
- package/lib/module/ModuleNotFoundError.js.map +0 -1
- package/lib/module/NativeMmkv.js +0 -45
- package/lib/module/NativeMmkv.js.map +0 -1
- package/lib/module/NativeMmkvPlatformContext.js +0 -18
- package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
- package/lib/module/PlatformChecker.js +0 -10
- package/lib/module/PlatformChecker.js.map +0 -1
- package/lib/module/Types.js +0 -25
- package/lib/module/Types.js.map +0 -1
- package/lib/module/createMMKV.js +0 -38
- package/lib/module/createMMKV.js.map +0 -1
- package/lib/module/createMMKV.mock.js +0 -38
- package/lib/module/createMMKV.mock.js.map +0 -1
- package/lib/module/createMMKV.web.js +0 -105
- package/lib/module/createMMKV.web.js.map +0 -1
- package/lib/module/createTextEncoder.js +0 -19
- package/lib/module/createTextEncoder.js.map +0 -1
- package/lib/module/hooks.js +0 -189
- package/lib/module/hooks.js.map +0 -1
- package/lib/module/index.js +0 -6
- package/lib/module/index.js.map +0 -1
- package/lib/module/package.json +0 -1
- package/lib/typescript/src/MMKV.d.ts +0 -34
- package/lib/typescript/src/MMKV.d.ts.map +0 -1
- package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
- package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
- package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
- package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
- package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
- package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
- package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
- package/lib/typescript/src/PlatformChecker.d.ts +0 -2
- package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
- package/lib/typescript/src/Types.d.ts +0 -172
- package/lib/typescript/src/Types.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
- package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.d.ts +0 -3
- package/lib/typescript/src/createMMKV.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
- package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
- package/lib/typescript/src/createMMKV.web.d.ts +0 -3
- package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
- package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
- package/lib/typescript/src/hooks.d.ts +0 -86
- package/lib/typescript/src/hooks.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -4
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/react-native-mmkv.podspec +0 -32
- package/src/MMKV.ts +0 -142
- package/src/MemoryWarningListener.web.ts +0 -5
- package/src/ModuleNotFoundError.ts +0 -95
- package/src/NativeMmkv.ts +0 -118
- package/src/NativeMmkvPlatformContext.ts +0 -38
- package/src/Types.ts +0 -178
- package/src/createMMKV.mock.ts +0 -38
- package/src/createMMKV.ts +0 -42
- package/src/hooks.ts +0 -247
package/src/hooks.ts
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
|
|
2
|
-
import { MMKV } from './MMKV';
|
|
3
|
-
import type { Configuration } from './Types';
|
|
4
|
-
|
|
5
|
-
function isConfigurationEqual(
|
|
6
|
-
left?: Configuration,
|
|
7
|
-
right?: Configuration
|
|
8
|
-
): boolean {
|
|
9
|
-
if (left == null || right == null) return left == null && right == null;
|
|
10
|
-
|
|
11
|
-
return (
|
|
12
|
-
left.encryptionKey === right.encryptionKey &&
|
|
13
|
-
left.id === right.id &&
|
|
14
|
-
left.path === right.path &&
|
|
15
|
-
left.mode === right.mode
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
let defaultInstance: MMKV | null = null;
|
|
20
|
-
function getDefaultInstance(): MMKV {
|
|
21
|
-
if (defaultInstance == null) {
|
|
22
|
-
defaultInstance = new MMKV();
|
|
23
|
-
}
|
|
24
|
-
return defaultInstance;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Use the default, shared MMKV instance.
|
|
29
|
-
*/
|
|
30
|
-
export function useMMKV(): MMKV;
|
|
31
|
-
/**
|
|
32
|
-
* Use a custom MMKV instance with the given configuration.
|
|
33
|
-
* @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
|
|
34
|
-
*/
|
|
35
|
-
export function useMMKV(configuration: Configuration): MMKV;
|
|
36
|
-
export function useMMKV(configuration?: Configuration): MMKV {
|
|
37
|
-
const instance = useRef<MMKV>(undefined);
|
|
38
|
-
const lastConfiguration = useRef<Configuration>(undefined);
|
|
39
|
-
|
|
40
|
-
if (configuration == null) return getDefaultInstance();
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
instance.current == null ||
|
|
44
|
-
!isConfigurationEqual(lastConfiguration.current, configuration)
|
|
45
|
-
) {
|
|
46
|
-
lastConfiguration.current = configuration;
|
|
47
|
-
instance.current = new MMKV(configuration);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return instance.current;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function createMMKVHook<
|
|
54
|
-
T extends (boolean | number | string | ArrayBufferLike) | undefined,
|
|
55
|
-
TSet extends T | undefined,
|
|
56
|
-
TSetAction extends TSet | ((current: T) => TSet),
|
|
57
|
-
>(getter: (instance: MMKV, key: string) => T) {
|
|
58
|
-
return (
|
|
59
|
-
key: string,
|
|
60
|
-
instance?: MMKV
|
|
61
|
-
): [value: T, setValue: (value: TSetAction) => void] => {
|
|
62
|
-
const mmkv = instance ?? getDefaultInstance();
|
|
63
|
-
|
|
64
|
-
const [bump, setBump] = useState(0);
|
|
65
|
-
const value = useMemo(() => {
|
|
66
|
-
// bump is here as an additional outside dependency, so this useMemo
|
|
67
|
-
// re-computes the value each time bump changes, effectively acting as a hint
|
|
68
|
-
// that the outside value (storage) has changed. setting bump refreshes this value.
|
|
69
|
-
bump;
|
|
70
|
-
return getter(mmkv, key);
|
|
71
|
-
}, [mmkv, key, bump]);
|
|
72
|
-
|
|
73
|
-
// update value by user set
|
|
74
|
-
const set = useCallback(
|
|
75
|
-
(v: TSetAction) => {
|
|
76
|
-
const newValue = typeof v === 'function' ? v(getter(mmkv, key)) : v;
|
|
77
|
-
switch (typeof newValue) {
|
|
78
|
-
case 'number':
|
|
79
|
-
case 'string':
|
|
80
|
-
case 'boolean':
|
|
81
|
-
mmkv.set(key, newValue);
|
|
82
|
-
break;
|
|
83
|
-
case 'undefined':
|
|
84
|
-
mmkv.delete(key);
|
|
85
|
-
break;
|
|
86
|
-
case 'object':
|
|
87
|
-
if (newValue instanceof ArrayBuffer) {
|
|
88
|
-
mmkv.set(key, newValue);
|
|
89
|
-
break;
|
|
90
|
-
} else {
|
|
91
|
-
throw new Error(
|
|
92
|
-
`MMKV: Type object (${newValue}) is not supported!`
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
default:
|
|
96
|
-
throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
[key, mmkv]
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
// update value if it changes somewhere else (second hook, same key)
|
|
103
|
-
useEffect(() => {
|
|
104
|
-
const listener = mmkv.addOnValueChangedListener((changedKey) => {
|
|
105
|
-
if (changedKey === key) {
|
|
106
|
-
setBump((b) => b + 1);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
return () => listener.remove();
|
|
110
|
-
}, [key, mmkv]);
|
|
111
|
-
|
|
112
|
-
return [value, set];
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Use the string value of the given `key` from the given MMKV storage instance.
|
|
118
|
-
*
|
|
119
|
-
* If no instance is provided, a shared default instance will be used.
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```ts
|
|
123
|
-
* const [username, setUsername] = useMMKVString("user.name")
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
export const useMMKVString = createMMKVHook((instance, key) =>
|
|
127
|
-
instance.getString(key)
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Use the number value of the given `key` from the given MMKV storage instance.
|
|
132
|
-
*
|
|
133
|
-
* If no instance is provided, a shared default instance will be used.
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```ts
|
|
137
|
-
* const [age, setAge] = useMMKVNumber("user.age")
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
export const useMMKVNumber = createMMKVHook((instance, key) =>
|
|
141
|
-
instance.getNumber(key)
|
|
142
|
-
);
|
|
143
|
-
/**
|
|
144
|
-
* Use the boolean value of the given `key` from the given MMKV storage instance.
|
|
145
|
-
*
|
|
146
|
-
* If no instance is provided, a shared default instance will be used.
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* ```ts
|
|
150
|
-
* const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
export const useMMKVBoolean = createMMKVHook((instance, key) =>
|
|
154
|
-
instance.getBoolean(key)
|
|
155
|
-
);
|
|
156
|
-
/**
|
|
157
|
-
* Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
|
|
158
|
-
*
|
|
159
|
-
* If no instance is provided, a shared default instance will be used.
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```ts
|
|
163
|
-
* const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
export const useMMKVBuffer = createMMKVHook((instance, key) =>
|
|
167
|
-
instance.getBuffer(key)
|
|
168
|
-
);
|
|
169
|
-
/**
|
|
170
|
-
* Use an object value of the given `key` from the given MMKV storage instance.
|
|
171
|
-
*
|
|
172
|
-
* If no instance is provided, a shared default instance will be used.
|
|
173
|
-
*
|
|
174
|
-
* The object will be serialized using `JSON`.
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```ts
|
|
178
|
-
* const [user, setUser] = useMMKVObject<User>("user")
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
export function useMMKVObject<T>(
|
|
182
|
-
key: string,
|
|
183
|
-
instance?: MMKV
|
|
184
|
-
): [
|
|
185
|
-
value: T | undefined,
|
|
186
|
-
setValue: (
|
|
187
|
-
value: T | undefined | ((prevValue: T | undefined) => T | undefined)
|
|
188
|
-
) => void,
|
|
189
|
-
] {
|
|
190
|
-
const [json, setJson] = useMMKVString(key, instance);
|
|
191
|
-
|
|
192
|
-
const value = useMemo(() => {
|
|
193
|
-
if (json == null) return undefined;
|
|
194
|
-
return JSON.parse(json) as T;
|
|
195
|
-
}, [json]);
|
|
196
|
-
|
|
197
|
-
const setValue = useCallback(
|
|
198
|
-
(v: (T | undefined) | ((prev: T | undefined) => T | undefined)) => {
|
|
199
|
-
if (v instanceof Function) {
|
|
200
|
-
setJson((currentJson) => {
|
|
201
|
-
const currentValue =
|
|
202
|
-
currentJson != null ? (JSON.parse(currentJson) as T) : undefined;
|
|
203
|
-
const newValue = v(currentValue);
|
|
204
|
-
// Store the Object as a serialized Value or clear the value
|
|
205
|
-
return newValue != null ? JSON.stringify(newValue) : undefined;
|
|
206
|
-
});
|
|
207
|
-
} else {
|
|
208
|
-
// Store the Object as a serialized Value or clear the value
|
|
209
|
-
const newValue = v != null ? JSON.stringify(v) : undefined;
|
|
210
|
-
setJson(newValue);
|
|
211
|
-
}
|
|
212
|
-
},
|
|
213
|
-
[setJson]
|
|
214
|
-
);
|
|
215
|
-
|
|
216
|
-
return [value, setValue];
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Listen for changes in the given MMKV storage instance.
|
|
221
|
-
* If no instance is passed, the default instance will be used.
|
|
222
|
-
* @param valueChangedListener The function to call whenever a value inside the storage instance changes
|
|
223
|
-
* @param instance The instance to listen to changes to (or the default instance)
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```ts
|
|
227
|
-
* useMMKVListener((key) => {
|
|
228
|
-
* console.log(`Value for "${key}" changed!`)
|
|
229
|
-
* })
|
|
230
|
-
* ```
|
|
231
|
-
*/
|
|
232
|
-
export function useMMKVListener(
|
|
233
|
-
valueChangedListener: (key: string) => void,
|
|
234
|
-
instance?: MMKV
|
|
235
|
-
): void {
|
|
236
|
-
const ref = useRef(valueChangedListener);
|
|
237
|
-
ref.current = valueChangedListener;
|
|
238
|
-
|
|
239
|
-
const mmkv = instance ?? getDefaultInstance();
|
|
240
|
-
|
|
241
|
-
useEffect(() => {
|
|
242
|
-
const listener = mmkv.addOnValueChangedListener((changedKey) => {
|
|
243
|
-
ref.current(changedKey);
|
|
244
|
-
});
|
|
245
|
-
return () => listener.remove();
|
|
246
|
-
}, [mmkv]);
|
|
247
|
-
}
|