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.
Files changed (226) hide show
  1. package/NitroMmkv.podspec +44 -0
  2. package/README.md +1 -3
  3. package/android/CMakeLists.txt +31 -30
  4. package/android/build.gradle +66 -21
  5. package/android/fix-prefab.gradle +51 -0
  6. package/android/gradle.properties +5 -5
  7. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  8. package/android/src/main/java/com/margelo/nitro/mmkv/HybridMMKVPlatformContext.kt +19 -0
  9. package/android/src/main/java/com/margelo/nitro/mmkv/NitroMmkvPackage.java +33 -0
  10. package/cpp/HybridMMKV.cpp +184 -0
  11. package/cpp/HybridMMKV.hpp +47 -0
  12. package/cpp/HybridMMKVFactory.cpp +33 -0
  13. package/cpp/HybridMMKVFactory.hpp +24 -0
  14. package/cpp/{MmkvTypes.h → MMKVTypes.hpp} +1 -1
  15. package/cpp/MMKVValueChangedListenerRegistry.cpp +58 -0
  16. package/cpp/MMKVValueChangedListenerRegistry.hpp +43 -0
  17. package/cpp/{ManagedMMBuffer.h → ManagedMMBuffer.hpp} +13 -5
  18. package/ios/HybridMMKVPlatformContext.swift +43 -0
  19. package/lib/__tests__/hooks.test.d.ts +1 -0
  20. package/lib/__tests__/hooks.test.js +69 -0
  21. package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
  22. package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
  23. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
  24. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
  25. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
  26. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
  27. package/lib/createMMKV/createMMKV.d.ts +3 -0
  28. package/lib/createMMKV/createMMKV.js +40 -0
  29. package/lib/createMMKV/createMMKV.web.d.ts +3 -0
  30. package/lib/createMMKV/createMMKV.web.js +117 -0
  31. package/lib/createMMKV/createMockMMKV.d.ts +5 -0
  32. package/lib/createMMKV/createMockMMKV.js +74 -0
  33. package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
  34. package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
  35. package/lib/hooks/createMMKVHook.d.ts +2 -0
  36. package/lib/hooks/createMMKVHook.js +49 -0
  37. package/lib/hooks/useMMKV.d.ts +11 -0
  38. package/lib/hooks/useMMKV.js +23 -0
  39. package/lib/hooks/useMMKVBoolean.d.ts +11 -0
  40. package/lib/hooks/useMMKVBoolean.js +12 -0
  41. package/lib/hooks/useMMKVBuffer.d.ts +11 -0
  42. package/lib/hooks/useMMKVBuffer.js +12 -0
  43. package/lib/hooks/useMMKVKeys.d.ts +12 -0
  44. package/lib/hooks/useMMKVKeys.js +33 -0
  45. package/lib/hooks/useMMKVListener.d.ts +15 -0
  46. package/lib/hooks/useMMKVListener.js +26 -0
  47. package/lib/hooks/useMMKVNumber.d.ts +11 -0
  48. package/lib/hooks/useMMKVNumber.js +12 -0
  49. package/lib/hooks/useMMKVObject.d.ts +17 -0
  50. package/lib/hooks/useMMKVObject.js +38 -0
  51. package/lib/hooks/useMMKVString.d.ts +11 -0
  52. package/lib/hooks/useMMKVString.js +12 -0
  53. package/lib/index.d.ts +11 -0
  54. package/lib/index.js +11 -0
  55. package/lib/isTest.d.ts +1 -0
  56. package/lib/isTest.js +7 -0
  57. package/lib/specs/MMKV.nitro.d.ts +94 -0
  58. package/lib/specs/MMKV.nitro.js +1 -0
  59. package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +26 -33
  60. package/lib/specs/MMKVFactory.nitro.js +1 -0
  61. package/lib/specs/MMKVPlatformContext.nitro.d.ts +18 -0
  62. package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
  63. package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
  64. package/lib/web/createTextEncoder.js +17 -0
  65. package/nitro.json +28 -0
  66. package/nitrogen/generated/.gitattributes +1 -0
  67. package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
  68. package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
  69. package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +54 -0
  70. package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
  71. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +52 -0
  72. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
  73. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
  75. package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
  76. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
  77. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +77 -0
  78. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +45 -0
  79. package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
  80. package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
  81. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
  82. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +82 -0
  83. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
  84. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +141 -0
  85. package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
  86. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
  87. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
  88. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
  89. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +64 -0
  90. package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
  91. package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
  92. package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
  93. package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
  94. package/package.json +74 -122
  95. package/react-native.config.js +2 -15
  96. package/src/__tests__/hooks.test.tsx +39 -34
  97. package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
  98. package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
  99. package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
  100. package/src/createMMKV/createMMKV.ts +51 -0
  101. package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +58 -46
  102. package/src/createMMKV/createMockMMKV.ts +78 -0
  103. package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
  104. package/src/hooks/createMMKVHook.ts +66 -0
  105. package/src/hooks/useMMKV.ts +45 -0
  106. package/src/hooks/useMMKVBoolean.ts +15 -0
  107. package/src/hooks/useMMKVBuffer.ts +15 -0
  108. package/src/hooks/useMMKVKeys.ts +36 -0
  109. package/src/hooks/useMMKVListener.ts +33 -0
  110. package/src/hooks/useMMKVNumber.ts +15 -0
  111. package/src/hooks/useMMKVObject.ts +53 -0
  112. package/src/hooks/useMMKVString.ts +15 -0
  113. package/src/index.ts +15 -3
  114. package/src/{PlatformChecker.ts → isTest.ts} +2 -2
  115. package/src/specs/MMKV.nitro.ts +93 -0
  116. package/src/specs/MMKVFactory.nitro.ts +87 -0
  117. package/src/specs/MMKVPlatformContext.nitro.ts +17 -0
  118. package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
  119. package/android/src/main/cpp/AndroidLogger.cpp +0 -16
  120. package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
  121. package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
  122. package/cpp/MmkvHostObject.cpp +0 -360
  123. package/cpp/MmkvHostObject.h +0 -31
  124. package/cpp/MmkvLogger.h +0 -35
  125. package/cpp/NativeMmkvModule.cpp +0 -43
  126. package/cpp/NativeMmkvModule.h +0 -31
  127. package/ios/AppleLogger.mm +0 -16
  128. package/ios/MmkvOnLoad.mm +0 -25
  129. package/ios/MmkvPlatformContext.h +0 -19
  130. package/ios/MmkvPlatformContextModule.mm +0 -55
  131. package/lib/commonjs/MMKV.js +0 -124
  132. package/lib/commonjs/MMKV.js.map +0 -1
  133. package/lib/commonjs/MemoryWarningListener.js +0 -31
  134. package/lib/commonjs/MemoryWarningListener.js.map +0 -1
  135. package/lib/commonjs/MemoryWarningListener.web.js +0 -11
  136. package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
  137. package/lib/commonjs/ModuleNotFoundError.js +0 -75
  138. package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
  139. package/lib/commonjs/NativeMmkv.js +0 -47
  140. package/lib/commonjs/NativeMmkv.js.map +0 -1
  141. package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
  142. package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
  143. package/lib/commonjs/PlatformChecker.js +0 -14
  144. package/lib/commonjs/PlatformChecker.js.map +0 -1
  145. package/lib/commonjs/Types.js +0 -26
  146. package/lib/commonjs/Types.js.map +0 -1
  147. package/lib/commonjs/createMMKV.js +0 -43
  148. package/lib/commonjs/createMMKV.js.map +0 -1
  149. package/lib/commonjs/createMMKV.mock.js +0 -43
  150. package/lib/commonjs/createMMKV.mock.js.map +0 -1
  151. package/lib/commonjs/createMMKV.web.js +0 -110
  152. package/lib/commonjs/createMMKV.web.js.map +0 -1
  153. package/lib/commonjs/createTextEncoder.js +0 -23
  154. package/lib/commonjs/createTextEncoder.js.map +0 -1
  155. package/lib/commonjs/hooks.js +0 -198
  156. package/lib/commonjs/hooks.js.map +0 -1
  157. package/lib/commonjs/index.js +0 -40
  158. package/lib/commonjs/index.js.map +0 -1
  159. package/lib/commonjs/package.json +0 -1
  160. package/lib/module/MMKV.js +0 -119
  161. package/lib/module/MMKV.js.map +0 -1
  162. package/lib/module/MemoryWarningListener.js +0 -27
  163. package/lib/module/MemoryWarningListener.js.map +0 -1
  164. package/lib/module/MemoryWarningListener.web.js +0 -6
  165. package/lib/module/MemoryWarningListener.web.js.map +0 -1
  166. package/lib/module/ModuleNotFoundError.js +0 -70
  167. package/lib/module/ModuleNotFoundError.js.map +0 -1
  168. package/lib/module/NativeMmkv.js +0 -45
  169. package/lib/module/NativeMmkv.js.map +0 -1
  170. package/lib/module/NativeMmkvPlatformContext.js +0 -18
  171. package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
  172. package/lib/module/PlatformChecker.js +0 -10
  173. package/lib/module/PlatformChecker.js.map +0 -1
  174. package/lib/module/Types.js +0 -25
  175. package/lib/module/Types.js.map +0 -1
  176. package/lib/module/createMMKV.js +0 -38
  177. package/lib/module/createMMKV.js.map +0 -1
  178. package/lib/module/createMMKV.mock.js +0 -38
  179. package/lib/module/createMMKV.mock.js.map +0 -1
  180. package/lib/module/createMMKV.web.js +0 -105
  181. package/lib/module/createMMKV.web.js.map +0 -1
  182. package/lib/module/createTextEncoder.js +0 -19
  183. package/lib/module/createTextEncoder.js.map +0 -1
  184. package/lib/module/hooks.js +0 -189
  185. package/lib/module/hooks.js.map +0 -1
  186. package/lib/module/index.js +0 -6
  187. package/lib/module/index.js.map +0 -1
  188. package/lib/module/package.json +0 -1
  189. package/lib/typescript/src/MMKV.d.ts +0 -34
  190. package/lib/typescript/src/MMKV.d.ts.map +0 -1
  191. package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
  192. package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
  193. package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
  194. package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
  195. package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
  196. package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
  197. package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
  198. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
  199. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
  200. package/lib/typescript/src/PlatformChecker.d.ts +0 -2
  201. package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
  202. package/lib/typescript/src/Types.d.ts +0 -172
  203. package/lib/typescript/src/Types.d.ts.map +0 -1
  204. package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
  205. package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
  206. package/lib/typescript/src/createMMKV.d.ts +0 -3
  207. package/lib/typescript/src/createMMKV.d.ts.map +0 -1
  208. package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
  209. package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
  210. package/lib/typescript/src/createMMKV.web.d.ts +0 -3
  211. package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
  212. package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
  213. package/lib/typescript/src/hooks.d.ts +0 -86
  214. package/lib/typescript/src/hooks.d.ts.map +0 -1
  215. package/lib/typescript/src/index.d.ts +0 -4
  216. package/lib/typescript/src/index.d.ts.map +0 -1
  217. package/react-native-mmkv.podspec +0 -32
  218. package/src/MMKV.ts +0 -142
  219. package/src/MemoryWarningListener.web.ts +0 -5
  220. package/src/ModuleNotFoundError.ts +0 -95
  221. package/src/NativeMmkv.ts +0 -118
  222. package/src/NativeMmkvPlatformContext.ts +0 -38
  223. package/src/Types.ts +0 -178
  224. package/src/createMMKV.mock.ts +0 -38
  225. package/src/createMMKV.ts +0 -42
  226. 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
- }