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
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Use the boolean value of the given `key` from the given MMKV storage instance.
3
+ *
4
+ * If no instance is provided, a shared default instance will be used.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
9
+ * ```
10
+ */
11
+ export declare const useMMKVBoolean: (key: string, instance?: import("..").MMKV) => [value: boolean | undefined, setValue: (value: boolean | ((current: boolean | undefined) => boolean | undefined) | undefined) => void];
@@ -0,0 +1,12 @@
1
+ import { createMMKVHook } from './createMMKVHook';
2
+ /**
3
+ * Use the boolean value of the given `key` from the given MMKV storage instance.
4
+ *
5
+ * If no instance is provided, a shared default instance will be used.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
10
+ * ```
11
+ */
12
+ export const useMMKVBoolean = createMMKVHook((instance, key) => instance.getBoolean(key));
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
3
+ *
4
+ * If no instance is provided, a shared default instance will be used.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
9
+ * ```
10
+ */
11
+ export declare const useMMKVBuffer: (key: string, instance?: import("..").MMKV) => [value: ArrayBuffer | undefined, setValue: (value: ArrayBuffer | ((current: ArrayBuffer | undefined) => ArrayBuffer | undefined) | undefined) => void];
@@ -0,0 +1,12 @@
1
+ import { createMMKVHook } from './createMMKVHook';
2
+ /**
3
+ * Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
4
+ *
5
+ * If no instance is provided, a shared default instance will be used.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
10
+ * ```
11
+ */
12
+ export const useMMKVBuffer = createMMKVHook((instance, key) => instance.getBuffer(key));
@@ -0,0 +1,12 @@
1
+ import type { MMKV } from '../specs/MMKV.nitro';
2
+ /**
3
+ * Get a list of all keys that exist in the given MMKV {@linkcode instance}.
4
+ * The keys update when new keys are added or removed.
5
+ * @param instance The instance to listen to changes to (or the default instance)
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * useMMKVKeys(instance)
10
+ * ```
11
+ */
12
+ export declare function useMMKVKeys(instance?: MMKV): string[];
@@ -0,0 +1,33 @@
1
+ import { useState } from 'react';
2
+ import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
3
+ import { useMMKVListener } from './useMMKVListener';
4
+ /**
5
+ * Get a list of all keys that exist in the given MMKV {@linkcode instance}.
6
+ * The keys update when new keys are added or removed.
7
+ * @param instance The instance to listen to changes to (or the default instance)
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * useMMKVKeys(instance)
12
+ * ```
13
+ */
14
+ export function useMMKVKeys(instance) {
15
+ const mmkv = instance ?? getDefaultMMKVInstance();
16
+ const [allKeys, setKeys] = useState(() => mmkv.getAllKeys());
17
+ useMMKVListener((key) => {
18
+ // a key changed
19
+ setKeys((keys) => {
20
+ const currentlyHasKey = keys.includes(key);
21
+ const hasKey = mmkv.contains(key);
22
+ if (hasKey !== currentlyHasKey) {
23
+ // Re-fetch the keys from native
24
+ return mmkv.getAllKeys();
25
+ }
26
+ else {
27
+ // We are up-to-date.
28
+ return keys;
29
+ }
30
+ });
31
+ }, mmkv);
32
+ return allKeys;
33
+ }
@@ -0,0 +1,15 @@
1
+ import type { MMKV } from '../specs/MMKV.nitro';
2
+ /**
3
+ * Listen for changes in the given MMKV storage instance.
4
+ * If no instance is passed, the default instance will be used.
5
+ * @param valueChangedListener The function to call whenever a value inside the storage instance changes
6
+ * @param instance The instance to listen to changes to (or the default instance)
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * useMMKVListener((key) => {
11
+ * console.log(`Value for "${key}" changed!`)
12
+ * })
13
+ * ```
14
+ */
15
+ export declare function useMMKVListener(valueChangedListener: (key: string) => void, instance?: MMKV): void;
@@ -0,0 +1,26 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
3
+ /**
4
+ * Listen for changes in the given MMKV storage instance.
5
+ * If no instance is passed, the default instance will be used.
6
+ * @param valueChangedListener The function to call whenever a value inside the storage instance changes
7
+ * @param instance The instance to listen to changes to (or the default instance)
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * useMMKVListener((key) => {
12
+ * console.log(`Value for "${key}" changed!`)
13
+ * })
14
+ * ```
15
+ */
16
+ export function useMMKVListener(valueChangedListener, instance) {
17
+ const ref = useRef(valueChangedListener);
18
+ ref.current = valueChangedListener;
19
+ const mmkv = instance ?? getDefaultMMKVInstance();
20
+ useEffect(() => {
21
+ const listener = mmkv.addOnValueChangedListener((changedKey) => {
22
+ ref.current(changedKey);
23
+ });
24
+ return () => listener.remove();
25
+ }, [mmkv]);
26
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Use the number value of the given `key` from the given MMKV storage instance.
3
+ *
4
+ * If no instance is provided, a shared default instance will be used.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const [age, setAge] = useMMKVNumber("user.age")
9
+ * ```
10
+ */
11
+ export declare const useMMKVNumber: (key: string, instance?: import("..").MMKV) => [value: number | undefined, setValue: (value: number | ((current: number | undefined) => number | undefined) | undefined) => void];
@@ -0,0 +1,12 @@
1
+ import { createMMKVHook } from './createMMKVHook';
2
+ /**
3
+ * Use the number value of the given `key` from the given MMKV storage instance.
4
+ *
5
+ * If no instance is provided, a shared default instance will be used.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const [age, setAge] = useMMKVNumber("user.age")
10
+ * ```
11
+ */
12
+ export const useMMKVNumber = createMMKVHook((instance, key) => instance.getNumber(key));
@@ -0,0 +1,17 @@
1
+ import type { MMKV } from '../specs/MMKV.nitro';
2
+ /**
3
+ * Use an object value of the given `key` from the given MMKV storage instance.
4
+ *
5
+ * If no instance is provided, a shared default instance will be used.
6
+ *
7
+ * The object will be serialized using `JSON`.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const [user, setUser] = useMMKVObject<User>("user")
12
+ * ```
13
+ */
14
+ export declare function useMMKVObject<T>(key: string, instance?: MMKV): [
15
+ value: T | undefined,
16
+ setValue: (value: T | undefined | ((prevValue: T | undefined) => T | undefined)) => void
17
+ ];
@@ -0,0 +1,38 @@
1
+ import { useCallback, useMemo } from 'react';
2
+ import { useMMKVString } from './useMMKVString';
3
+ /**
4
+ * Use an object value of the given `key` from the given MMKV storage instance.
5
+ *
6
+ * If no instance is provided, a shared default instance will be used.
7
+ *
8
+ * The object will be serialized using `JSON`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const [user, setUser] = useMMKVObject<User>("user")
13
+ * ```
14
+ */
15
+ export function useMMKVObject(key, instance) {
16
+ const [json, setJson] = useMMKVString(key, instance);
17
+ const value = useMemo(() => {
18
+ if (json == null)
19
+ return undefined;
20
+ return JSON.parse(json);
21
+ }, [json]);
22
+ const setValue = useCallback((v) => {
23
+ if (v instanceof Function) {
24
+ setJson((currentJson) => {
25
+ const currentValue = currentJson != null ? JSON.parse(currentJson) : undefined;
26
+ const newValue = v(currentValue);
27
+ // Store the Object as a serialized Value or clear the value
28
+ return newValue != null ? JSON.stringify(newValue) : undefined;
29
+ });
30
+ }
31
+ else {
32
+ // Store the Object as a serialized Value or clear the value
33
+ const newValue = v != null ? JSON.stringify(v) : undefined;
34
+ setJson(newValue);
35
+ }
36
+ }, [setJson]);
37
+ return [value, setValue];
38
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Use the string value of the given `key` from the given MMKV storage instance.
3
+ *
4
+ * If no instance is provided, a shared default instance will be used.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const [username, setUsername] = useMMKVString("user.name")
9
+ * ```
10
+ */
11
+ export declare const useMMKVString: (key: string, instance?: import("..").MMKV) => [value: string | undefined, setValue: (value: string | ((current: string | undefined) => string | undefined) | undefined) => void];
@@ -0,0 +1,12 @@
1
+ import { createMMKVHook } from './createMMKVHook';
2
+ /**
3
+ * Use the string value of the given `key` from the given MMKV storage instance.
4
+ *
5
+ * If no instance is provided, a shared default instance will be used.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const [username, setUsername] = useMMKVString("user.name")
10
+ * ```
11
+ */
12
+ export const useMMKVString = createMMKVHook((instance, key) => instance.getString(key));
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type { MMKV } from './specs/MMKV.nitro';
2
+ export type { Configuration, Mode } from './specs/MMKVFactory.nitro';
3
+ export { createMMKV } from './createMMKV/createMMKV';
4
+ export { useMMKV } from './hooks/useMMKV';
5
+ export { useMMKVBoolean } from './hooks/useMMKVBoolean';
6
+ export { useMMKVBuffer } from './hooks/useMMKVBuffer';
7
+ export { useMMKVNumber } from './hooks/useMMKVNumber';
8
+ export { useMMKVObject } from './hooks/useMMKVObject';
9
+ export { useMMKVString } from './hooks/useMMKVString';
10
+ export { useMMKVListener } from './hooks/useMMKVListener';
11
+ export { useMMKVKeys } from './hooks/useMMKVKeys';
package/lib/index.js ADDED
@@ -0,0 +1,11 @@
1
+ // The create function
2
+ export { createMMKV } from './createMMKV/createMMKV';
3
+ // All the hooks
4
+ export { useMMKV } from './hooks/useMMKV';
5
+ export { useMMKVBoolean } from './hooks/useMMKVBoolean';
6
+ export { useMMKVBuffer } from './hooks/useMMKVBuffer';
7
+ export { useMMKVNumber } from './hooks/useMMKVNumber';
8
+ export { useMMKVObject } from './hooks/useMMKVObject';
9
+ export { useMMKVString } from './hooks/useMMKVString';
10
+ export { useMMKVListener } from './hooks/useMMKVListener';
11
+ export { useMMKVKeys } from './hooks/useMMKVKeys';
@@ -0,0 +1 @@
1
+ export declare function isTest(): boolean;
package/lib/isTest.js ADDED
@@ -0,0 +1,7 @@
1
+ export function isTest() {
2
+ if (global.process == null) {
3
+ // In a WebBrowser/Electron the `process` variable does not exist
4
+ return false;
5
+ }
6
+ return (process.env.JEST_WORKER_ID != null || process.env.VITEST_WORKER_ID != null);
7
+ }
@@ -0,0 +1,94 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+ export interface Listener {
3
+ remove: () => void;
4
+ }
5
+ export interface MMKV extends HybridObject<{
6
+ ios: 'c++';
7
+ android: 'c++';
8
+ }> {
9
+ /**
10
+ * Set a value for the given `key`.
11
+ *
12
+ * @throws an Error if the value cannot be set.
13
+ */
14
+ set(key: string, value: boolean | string | number | ArrayBuffer): void;
15
+ /**
16
+ * Get the boolean value for the given `key`, or `undefined` if it does not exist.
17
+ *
18
+ * @default undefined
19
+ */
20
+ getBoolean(key: string): boolean | undefined;
21
+ /**
22
+ * Get the string value for the given `key`, or `undefined` if it does not exist.
23
+ *
24
+ * @default undefined
25
+ */
26
+ getString(key: string): string | undefined;
27
+ /**
28
+ * Get the number value for the given `key`, or `undefined` if it does not exist.
29
+ *
30
+ * @default undefined
31
+ */
32
+ getNumber(key: string): number | undefined;
33
+ /**
34
+ * Get a raw buffer of unsigned 8-bit (0-255) data.
35
+ *
36
+ * @default undefined
37
+ */
38
+ getBuffer(key: string): ArrayBuffer | undefined;
39
+ /**
40
+ * Checks whether the given `key` is being stored in this MMKV instance.
41
+ */
42
+ contains(key: string): boolean;
43
+ /**
44
+ * Removes the given `key`.
45
+ * @returns true if the key was removed, false otherwise
46
+ */
47
+ remove(key: string): boolean;
48
+ /**
49
+ * Get all keys.
50
+ *
51
+ * @default []
52
+ */
53
+ getAllKeys(): string[];
54
+ /**
55
+ * Clears all keys/values.
56
+ */
57
+ clearAll(): void;
58
+ /**
59
+ * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
60
+ *
61
+ * To remove encryption, pass `undefined` as a key.
62
+ *
63
+ * Encryption keys can have a maximum length of 16 bytes.
64
+ *
65
+ * @throws an Error if the instance cannot be recrypted.
66
+ */
67
+ recrypt(key: string | undefined): void;
68
+ /**
69
+ * Trims the storage space and clears memory cache.
70
+ *
71
+ * Since MMKV does not resize itself after deleting keys, you can call `trim()`
72
+ * after deleting a bunch of keys to manually trim the memory- and
73
+ * disk-file to reduce storage and memory usage.
74
+ *
75
+ * In most applications, this is not needed at all.
76
+ */
77
+ trim(): void;
78
+ /**
79
+ * Get the current total size of the storage, in bytes.
80
+ */
81
+ readonly size: number;
82
+ /**
83
+ * Returns whether this instance is in read-only mode or not.
84
+ * If this is `true`, you can only use "get"-functions.
85
+ */
86
+ readonly isReadOnly: boolean;
87
+ /**
88
+ * Adds a value changed listener. The Listener will be called whenever any value
89
+ * in this storage instance changes (set or delete).
90
+ *
91
+ * To unsubscribe from value changes, call `remove()` on the Listener.
92
+ */
93
+ addOnValueChangedListener(onValueChanged: (key: string) => void): Listener;
94
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,23 +1,11 @@
1
- import type { TurboModule } from 'react-native';
2
- import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
3
- /**
4
- * IMPORTANT: These types are also in the Types.ts file.
5
- * Due to how react-native-codegen works these are required here as the spec types can not be separated from spec.
6
- * We also need the types separate to allow bypassing importing turbo module registry in web
7
- */
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+ import type { MMKV } from './MMKV.nitro';
8
3
  /**
9
4
  * Configures the mode of the MMKV instance.
5
+ * - `single-process`: The MMKV instance is only used from a single process (this app).
6
+ * - `multi-process`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
10
7
  */
11
- export declare enum Mode {
12
- /**
13
- * The MMKV instance is only used from a single process (this app).
14
- */
15
- SINGLE_PROCESS = 0,
16
- /**
17
- * The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
18
- */
19
- MULTI_PROCESS = 1
20
- }
8
+ export type Mode = 'single-process' | 'multi-process';
21
9
  /**
22
10
  * Used for configuration of a single MMKV instance.
23
11
  */
@@ -27,8 +15,8 @@ export interface Configuration {
27
15
  *
28
16
  * @example
29
17
  * ```ts
30
- * const userStorage = new MMKV({ id: `user-${userId}-storage` })
31
- * const globalStorage = new MMKV({ id: 'global-app-storage' })
18
+ * const userStorage = createMMKV({ id: `user-${userId}-storage` })
19
+ * const globalStorage = createMMKV({ id: 'global-app-storage' })
32
20
  * ```
33
21
  *
34
22
  * @default 'mmkv.default'
@@ -39,7 +27,7 @@ export interface Configuration {
39
27
 
40
28
  * @example
41
29
  * ```ts
42
- * const temporaryStorage = new MMKV({ path: '/tmp/' })
30
+ * const temporaryStorage = createMMKV({ path: '/tmp/' })
43
31
  * ```
44
32
  *
45
33
  * @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
@@ -56,7 +44,7 @@ export interface Configuration {
56
44
  *
57
45
  * @example
58
46
  * ```ts
59
- * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
47
+ * const secureStorage = createMMKV({ encryptionKey: 'my-encryption-key!' })
60
48
  * ```
61
49
  *
62
50
  * @default undefined
@@ -64,28 +52,33 @@ export interface Configuration {
64
52
  encryptionKey?: string;
65
53
  /**
66
54
  * Configure the processing mode for MMKV.
67
- * - `SINGLE_PROCESS`: The MMKV instance is only used from a single process (this app).
68
- * - `MULTI_PROCESS`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
69
55
  *
70
- * @default SINGLE_PROCESS
56
+ * @default 'single-process'
71
57
  */
72
58
  mode?: Mode;
73
59
  /**
74
60
  * If `true`, the MMKV instance can only read from the storage, but not write to it.
61
+ * This is more efficient if you do not need to write to it.
62
+ * @default false
75
63
  */
76
64
  readOnly?: boolean;
77
65
  }
78
- export interface Spec extends TurboModule {
66
+ export interface MMKVFactory extends HybridObject<{
67
+ ios: 'c++';
68
+ android: 'c++';
69
+ }> {
79
70
  /**
80
- * Initialize MMKV with the given base storage directory.
81
- * This should be the documents directory by default.
71
+ * Create a new {@linkcode MMKV} instance with the given {@linkcode Configuration}
82
72
  */
83
- initialize(basePath: string): boolean;
73
+ createMMKV(configuration: Configuration): MMKV;
84
74
  /**
85
- * Create a new instance of MMKV.
86
- * The returned {@linkcode UnsafeObject} is a `jsi::HostObject`.
75
+ * Initialize the MMKV library with the given root path.
76
+ * This has to be called once, before using {@linkcode createMMKV}.
77
+ */
78
+ initializeMMKV(rootPath: string): void;
79
+ /**
80
+ * Get the default MMKV instance's ID.
81
+ * @default 'mmkv.default'
87
82
  */
88
- createMMKV(configuration: Configuration): UnsafeObject;
83
+ readonly defaultMMKVInstanceId: string;
89
84
  }
90
- export declare function getMMKVTurboModule(): Spec;
91
- //# sourceMappingURL=NativeMmkv.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+ export interface MMKVPlatformContext extends HybridObject<{
3
+ ios: 'swift';
4
+ android: 'kotlin';
5
+ }> {
6
+ /**
7
+ * Get the MMKV base directory
8
+ */
9
+ getBaseDirectory(): string;
10
+ /**
11
+ * Get the MMKV AppGroup's directory.
12
+ * The AppGroup can be set in your App's `Info.plist`, and will enable
13
+ * data sharing between main app, companions (e.g. watch app) and extensions.
14
+ * @platform iOS
15
+ * @default undefined
16
+ */
17
+ getAppGroupDirectory(): string | undefined;
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1 @@
1
1
  export declare function createTextEncoder(): TextEncoder;
2
- //# sourceMappingURL=createTextEncoder.d.ts.map
@@ -0,0 +1,17 @@
1
+ export function createTextEncoder() {
2
+ const g = global ?? globalThis ?? window;
3
+ if (g.TextEncoder != null) {
4
+ return new g.TextEncoder();
5
+ }
6
+ else {
7
+ return {
8
+ encode: () => {
9
+ throw new Error('TextEncoder is not supported in this environment!');
10
+ },
11
+ encodeInto: () => {
12
+ throw new Error('TextEncoder is not supported in this environment!');
13
+ },
14
+ encoding: 'utf-8',
15
+ };
16
+ }
17
+ }
package/nitro.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://nitro.margelo.com/nitro.schema.json",
3
+ "cxxNamespace": [
4
+ "mmkv"
5
+ ],
6
+ "ios": {
7
+ "iosModuleName": "NitroMmkv"
8
+ },
9
+ "android": {
10
+ "androidNamespace": [
11
+ "mmkv"
12
+ ],
13
+ "androidCxxLibName": "NitroMmkv"
14
+ },
15
+ "autolinking": {
16
+ "MMKVFactory": {
17
+ "cpp": "HybridMMKVFactory"
18
+ },
19
+ "MMKVPlatformContext": {
20
+ "swift": "HybridMMKVPlatformContext",
21
+ "kotlin": "HybridMMKVPlatformContext"
22
+ }
23
+ },
24
+ "ignorePaths": [
25
+ "**/node_modules"
26
+ ],
27
+ "gitAttributesGeneratedFlag": false
28
+ }
@@ -0,0 +1 @@
1
+ ** linguist-generated=false
@@ -0,0 +1,80 @@
1
+ #
2
+ # NitroMmkv+autolinking.cmake
3
+ # This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ # https://github.com/mrousavy/nitro
5
+ # Copyright © 2025 Marc Rousavy @ Margelo
6
+ #
7
+
8
+ # This is a CMake file that adds all files generated by Nitrogen
9
+ # to the current CMake project.
10
+ #
11
+ # To use it, add this to your CMakeLists.txt:
12
+ # ```cmake
13
+ # include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/NitroMmkv+autolinking.cmake)
14
+ # ```
15
+
16
+ # Add all headers that were generated by Nitrogen
17
+ include_directories(
18
+ "../nitrogen/generated/shared/c++"
19
+ "../nitrogen/generated/android/c++"
20
+ "../nitrogen/generated/android/"
21
+ )
22
+
23
+ # Add all .cpp sources that were generated by Nitrogen
24
+ target_sources(
25
+ # CMake project name (Android C++ library name)
26
+ NitroMmkv PRIVATE
27
+ # Autolinking Setup
28
+ ../nitrogen/generated/android/NitroMmkvOnLoad.cpp
29
+ # Shared Nitrogen C++ sources
30
+ ../nitrogen/generated/shared/c++/HybridMMKVSpec.cpp
31
+ ../nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp
32
+ ../nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp
33
+ # Android-specific Nitrogen C++ sources
34
+ ../nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp
35
+ )
36
+
37
+ # Define a flag to check if we are building properly
38
+ add_definitions(-DBUILDING_NITROMMKV_WITH_GENERATED_CMAKE_PROJECT)
39
+
40
+ # From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake
41
+ # Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake
42
+ target_compile_definitions(
43
+ NitroMmkv PRIVATE
44
+ -DFOLLY_NO_CONFIG=1
45
+ -DFOLLY_HAVE_CLOCK_GETTIME=1
46
+ -DFOLLY_USE_LIBCPP=1
47
+ -DFOLLY_CFG_NO_COROUTINES=1
48
+ -DFOLLY_MOBILE=1
49
+ -DFOLLY_HAVE_RECVMMSG=1
50
+ -DFOLLY_HAVE_PTHREAD=1
51
+ # Once we target android-23 above, we can comment
52
+ # the following line. NDK uses GNU style stderror_r() after API 23.
53
+ -DFOLLY_HAVE_XSI_STRERROR_R=1
54
+ )
55
+
56
+ # Add all libraries required by the generated specs
57
+ find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
58
+ find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
59
+ find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library
60
+
61
+ # Link all libraries together
62
+ target_link_libraries(
63
+ NitroMmkv
64
+ fbjni::fbjni # <-- Facebook C++ JNI helpers
65
+ ReactAndroid::jsi # <-- RN: JSI
66
+ react-native-nitro-modules::NitroModules # <-- NitroModules Core :)
67
+ )
68
+
69
+ # Link react-native (different prefab between RN 0.75 and RN 0.76)
70
+ if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
71
+ target_link_libraries(
72
+ NitroMmkv
73
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
74
+ )
75
+ else()
76
+ target_link_libraries(
77
+ NitroMmkv
78
+ ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
79
+ )
80
+ endif()