react-native-mmkv 4.0.0-beta.1 → 4.0.0-beta.10

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 (234) 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 +65 -20
  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/app.plugin.js +1 -0
  11. package/cpp/HybridMMKV.cpp +184 -0
  12. package/cpp/HybridMMKV.hpp +47 -0
  13. package/cpp/HybridMMKVFactory.cpp +33 -0
  14. package/cpp/HybridMMKVFactory.hpp +24 -0
  15. package/cpp/{MmkvTypes.h → MMKVTypes.hpp} +1 -1
  16. package/cpp/MMKVValueChangedListenerRegistry.cpp +58 -0
  17. package/cpp/MMKVValueChangedListenerRegistry.hpp +43 -0
  18. package/cpp/{ManagedMMBuffer.h → ManagedMMBuffer.hpp} +13 -5
  19. package/ios/HybridMMKVPlatformContext.swift +43 -0
  20. package/lib/__tests__/hooks.test.d.ts +1 -0
  21. package/lib/__tests__/hooks.test.js +69 -0
  22. package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
  23. package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
  24. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
  25. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
  26. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
  27. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
  28. package/lib/createMMKV/createMMKV.d.ts +3 -0
  29. package/lib/createMMKV/createMMKV.js +40 -0
  30. package/lib/createMMKV/createMMKV.mock.d.ts +5 -0
  31. package/lib/createMMKV/createMMKV.mock.js +74 -0
  32. package/lib/createMMKV/createMMKV.web.d.ts +3 -0
  33. package/lib/createMMKV/createMMKV.web.js +117 -0
  34. package/lib/createMMKV/createMockMMKV.d.ts +5 -0
  35. package/lib/createMMKV/createMockMMKV.js +74 -0
  36. package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
  37. package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
  38. package/lib/expo-plugin/withMMKV.cjs +26 -0
  39. package/lib/expo-plugin/withMMKV.d.cts +3 -0
  40. package/lib/expo-plugin/withMMKV.d.ts +3 -0
  41. package/lib/expo-plugin/withMMKV.js +17 -0
  42. package/lib/hooks/createMMKVHook.d.ts +2 -0
  43. package/lib/hooks/createMMKVHook.js +49 -0
  44. package/lib/hooks/useMMKV.d.ts +11 -0
  45. package/lib/hooks/useMMKV.js +23 -0
  46. package/lib/hooks/useMMKVBoolean.d.ts +11 -0
  47. package/lib/hooks/useMMKVBoolean.js +12 -0
  48. package/lib/hooks/useMMKVBuffer.d.ts +11 -0
  49. package/lib/hooks/useMMKVBuffer.js +12 -0
  50. package/lib/hooks/useMMKVKeys.d.ts +12 -0
  51. package/lib/hooks/useMMKVKeys.js +33 -0
  52. package/lib/hooks/useMMKVListener.d.ts +15 -0
  53. package/lib/hooks/useMMKVListener.js +26 -0
  54. package/lib/hooks/useMMKVNumber.d.ts +11 -0
  55. package/lib/hooks/useMMKVNumber.js +12 -0
  56. package/lib/hooks/useMMKVObject.d.ts +17 -0
  57. package/lib/hooks/useMMKVObject.js +38 -0
  58. package/lib/hooks/useMMKVString.d.ts +11 -0
  59. package/lib/hooks/useMMKVString.js +12 -0
  60. package/lib/index.d.ts +11 -0
  61. package/lib/index.js +11 -0
  62. package/lib/isTest.d.ts +1 -0
  63. package/lib/isTest.js +7 -0
  64. package/lib/specs/MMKV.nitro.d.ts +94 -0
  65. package/lib/specs/MMKV.nitro.js +1 -0
  66. package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +26 -33
  67. package/lib/specs/MMKVFactory.nitro.js +1 -0
  68. package/lib/specs/MMKVPlatformContext.nitro.d.ts +18 -0
  69. package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
  70. package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
  71. package/lib/web/createTextEncoder.js +17 -0
  72. package/nitro.json +28 -0
  73. package/nitrogen/generated/.gitattributes +1 -0
  74. package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
  75. package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
  76. package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +54 -0
  77. package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
  78. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +52 -0
  79. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
  80. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
  82. package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
  83. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
  84. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +77 -0
  85. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +45 -0
  86. package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
  87. package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
  88. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
  89. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +82 -0
  90. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
  91. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +141 -0
  92. package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
  93. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
  94. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
  95. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
  96. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +64 -0
  97. package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
  98. package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
  99. package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
  100. package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
  101. package/package.json +74 -122
  102. package/react-native.config.js +2 -15
  103. package/src/__tests__/hooks.test.tsx +39 -34
  104. package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
  105. package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
  106. package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
  107. package/src/createMMKV/createMMKV.ts +51 -0
  108. package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +58 -46
  109. package/src/createMMKV/createMockMMKV.ts +78 -0
  110. package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
  111. package/src/expo-plugin/withMMKV.cts +31 -0
  112. package/src/hooks/createMMKVHook.ts +66 -0
  113. package/src/hooks/useMMKV.ts +45 -0
  114. package/src/hooks/useMMKVBoolean.ts +15 -0
  115. package/src/hooks/useMMKVBuffer.ts +15 -0
  116. package/src/hooks/useMMKVKeys.ts +36 -0
  117. package/src/hooks/useMMKVListener.ts +33 -0
  118. package/src/hooks/useMMKVNumber.ts +15 -0
  119. package/src/hooks/useMMKVObject.ts +53 -0
  120. package/src/hooks/useMMKVString.ts +15 -0
  121. package/src/index.ts +15 -3
  122. package/src/{PlatformChecker.ts → isTest.ts} +2 -2
  123. package/src/specs/MMKV.nitro.ts +93 -0
  124. package/src/specs/MMKVFactory.nitro.ts +87 -0
  125. package/src/specs/MMKVPlatformContext.nitro.ts +17 -0
  126. package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
  127. package/android/src/main/cpp/AndroidLogger.cpp +0 -16
  128. package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
  129. package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
  130. package/cpp/MmkvHostObject.cpp +0 -360
  131. package/cpp/MmkvHostObject.h +0 -31
  132. package/cpp/MmkvLogger.h +0 -35
  133. package/cpp/NativeMmkvModule.cpp +0 -43
  134. package/cpp/NativeMmkvModule.h +0 -31
  135. package/ios/AppleLogger.mm +0 -16
  136. package/ios/MmkvOnLoad.mm +0 -25
  137. package/ios/MmkvPlatformContext.h +0 -19
  138. package/ios/MmkvPlatformContextModule.mm +0 -55
  139. package/lib/commonjs/MMKV.js +0 -124
  140. package/lib/commonjs/MMKV.js.map +0 -1
  141. package/lib/commonjs/MemoryWarningListener.js +0 -31
  142. package/lib/commonjs/MemoryWarningListener.js.map +0 -1
  143. package/lib/commonjs/MemoryWarningListener.web.js +0 -11
  144. package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
  145. package/lib/commonjs/ModuleNotFoundError.js +0 -75
  146. package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
  147. package/lib/commonjs/NativeMmkv.js +0 -47
  148. package/lib/commonjs/NativeMmkv.js.map +0 -1
  149. package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
  150. package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
  151. package/lib/commonjs/PlatformChecker.js +0 -14
  152. package/lib/commonjs/PlatformChecker.js.map +0 -1
  153. package/lib/commonjs/Types.js +0 -26
  154. package/lib/commonjs/Types.js.map +0 -1
  155. package/lib/commonjs/createMMKV.js +0 -43
  156. package/lib/commonjs/createMMKV.js.map +0 -1
  157. package/lib/commonjs/createMMKV.mock.js +0 -43
  158. package/lib/commonjs/createMMKV.mock.js.map +0 -1
  159. package/lib/commonjs/createMMKV.web.js +0 -110
  160. package/lib/commonjs/createMMKV.web.js.map +0 -1
  161. package/lib/commonjs/createTextEncoder.js +0 -23
  162. package/lib/commonjs/createTextEncoder.js.map +0 -1
  163. package/lib/commonjs/hooks.js +0 -198
  164. package/lib/commonjs/hooks.js.map +0 -1
  165. package/lib/commonjs/index.js +0 -40
  166. package/lib/commonjs/index.js.map +0 -1
  167. package/lib/commonjs/package.json +0 -1
  168. package/lib/module/MMKV.js +0 -119
  169. package/lib/module/MMKV.js.map +0 -1
  170. package/lib/module/MemoryWarningListener.js +0 -27
  171. package/lib/module/MemoryWarningListener.js.map +0 -1
  172. package/lib/module/MemoryWarningListener.web.js +0 -6
  173. package/lib/module/MemoryWarningListener.web.js.map +0 -1
  174. package/lib/module/ModuleNotFoundError.js +0 -70
  175. package/lib/module/ModuleNotFoundError.js.map +0 -1
  176. package/lib/module/NativeMmkv.js +0 -45
  177. package/lib/module/NativeMmkv.js.map +0 -1
  178. package/lib/module/NativeMmkvPlatformContext.js +0 -18
  179. package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
  180. package/lib/module/PlatformChecker.js +0 -10
  181. package/lib/module/PlatformChecker.js.map +0 -1
  182. package/lib/module/Types.js +0 -25
  183. package/lib/module/Types.js.map +0 -1
  184. package/lib/module/createMMKV.js +0 -38
  185. package/lib/module/createMMKV.js.map +0 -1
  186. package/lib/module/createMMKV.mock.js +0 -38
  187. package/lib/module/createMMKV.mock.js.map +0 -1
  188. package/lib/module/createMMKV.web.js +0 -105
  189. package/lib/module/createMMKV.web.js.map +0 -1
  190. package/lib/module/createTextEncoder.js +0 -19
  191. package/lib/module/createTextEncoder.js.map +0 -1
  192. package/lib/module/hooks.js +0 -189
  193. package/lib/module/hooks.js.map +0 -1
  194. package/lib/module/index.js +0 -6
  195. package/lib/module/index.js.map +0 -1
  196. package/lib/module/package.json +0 -1
  197. package/lib/typescript/src/MMKV.d.ts +0 -34
  198. package/lib/typescript/src/MMKV.d.ts.map +0 -1
  199. package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
  200. package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
  201. package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
  202. package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
  203. package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
  204. package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
  205. package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
  206. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
  207. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
  208. package/lib/typescript/src/PlatformChecker.d.ts +0 -2
  209. package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
  210. package/lib/typescript/src/Types.d.ts +0 -172
  211. package/lib/typescript/src/Types.d.ts.map +0 -1
  212. package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
  213. package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
  214. package/lib/typescript/src/createMMKV.d.ts +0 -3
  215. package/lib/typescript/src/createMMKV.d.ts.map +0 -1
  216. package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
  217. package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
  218. package/lib/typescript/src/createMMKV.web.d.ts +0 -3
  219. package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
  220. package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
  221. package/lib/typescript/src/hooks.d.ts +0 -86
  222. package/lib/typescript/src/hooks.d.ts.map +0 -1
  223. package/lib/typescript/src/index.d.ts +0 -4
  224. package/lib/typescript/src/index.d.ts.map +0 -1
  225. package/react-native-mmkv.podspec +0 -32
  226. package/src/MMKV.ts +0 -142
  227. package/src/MemoryWarningListener.web.ts +0 -5
  228. package/src/ModuleNotFoundError.ts +0 -95
  229. package/src/NativeMmkv.ts +0 -118
  230. package/src/NativeMmkvPlatformContext.ts +0 -38
  231. package/src/Types.ts +0 -178
  232. package/src/createMMKV.mock.ts +0 -38
  233. package/src/createMMKV.ts +0 -42
  234. package/src/hooks.ts +0 -247
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ const { createRunOncePlugin, withGradleProperties } = require('@expo/config-plugins');
3
+ const pkg = require('../../package.json');
4
+ const withMMKV = (config) => {
5
+ // remove 32-bit architectures from gradle.properties
6
+ return withGradleProperties(config, (cfg) => {
7
+ // Define the wanted property
8
+ const property = {
9
+ type: 'property',
10
+ key: 'reactNativeArchitectures',
11
+ value: 'arm64-v8a,x86_64',
12
+ };
13
+ // If it exists, update its value
14
+ const index = cfg.modResults.findIndex((p) => p.type === 'property' && p.key === property.key);
15
+ if (index !== -1) {
16
+ // Overwrite it
17
+ cfg.modResults[index] = property;
18
+ }
19
+ else {
20
+ // Append it
21
+ cfg.modResults.push(property);
22
+ }
23
+ return cfg;
24
+ });
25
+ };
26
+ module.exports = createRunOncePlugin(withMMKV, pkg.name, pkg.version);
@@ -0,0 +1,3 @@
1
+ import type { ConfigPlugin } from '@expo/config-plugins';
2
+ declare const _default: ConfigPlugin<{}>;
3
+ export = _default;
@@ -0,0 +1,3 @@
1
+ import type { ConfigPlugin } from '@expo/config-plugins';
2
+ declare const _default: ConfigPlugin<{}>;
3
+ export default _default;
@@ -0,0 +1,17 @@
1
+ import { createRunOncePlugin, withGradleProperties } from '@expo/config-plugins';
2
+ const pkg = require('../../package.json');
3
+ const withMMKV = (config) => {
4
+ // remove 32-bit architectures from gradle.properties
5
+ return withGradleProperties(config, (cfg) => {
6
+ // Drop any existing entry…
7
+ cfg.modResults = cfg.modResults.filter((p) => !(p.type === 'property' && p.key === 'reactNativeArchitectures'));
8
+ // …and force 64-bit only.
9
+ cfg.modResults.push({
10
+ type: 'property',
11
+ key: 'reactNativeArchitectures',
12
+ value: 'arm64-v8a,x86_64',
13
+ });
14
+ return cfg;
15
+ });
16
+ };
17
+ export default createRunOncePlugin(withMMKV, pkg.name, pkg.version);
@@ -0,0 +1,2 @@
1
+ import type { MMKV } from '../specs/MMKV.nitro';
2
+ export declare function createMMKVHook<T extends (boolean | number | string | ArrayBufferLike) | undefined, TSet extends T | undefined, TSetAction extends TSet | ((current: T) => TSet)>(getter: (instance: MMKV, key: string) => T): (key: string, instance?: MMKV) => [value: T, setValue: (value: TSetAction) => void];
@@ -0,0 +1,49 @@
1
+ import { useCallback, useEffect, useMemo, useState } from 'react';
2
+ import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
3
+ export function createMMKVHook(getter) {
4
+ return (key, instance) => {
5
+ const mmkv = instance ?? getDefaultMMKVInstance();
6
+ const [bump, setBump] = useState(0);
7
+ const value = useMemo(() => {
8
+ // bump is here as an additional outside dependency, so this useMemo
9
+ // re-computes the value each time bump changes, effectively acting as a hint
10
+ // that the outside value (storage) has changed. setting bump refreshes this value.
11
+ bump;
12
+ return getter(mmkv, key);
13
+ }, [mmkv, key, bump]);
14
+ // update value by user set
15
+ const set = useCallback((v) => {
16
+ const newValue = typeof v === 'function' ? v(getter(mmkv, key)) : v;
17
+ switch (typeof newValue) {
18
+ case 'number':
19
+ case 'string':
20
+ case 'boolean':
21
+ mmkv.set(key, newValue);
22
+ break;
23
+ case 'undefined':
24
+ mmkv.remove(key);
25
+ break;
26
+ case 'object':
27
+ if (newValue instanceof ArrayBuffer) {
28
+ mmkv.set(key, newValue);
29
+ break;
30
+ }
31
+ else {
32
+ throw new Error(`MMKV: Type object (${newValue}) is not supported!`);
33
+ }
34
+ default:
35
+ throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
36
+ }
37
+ }, [key, mmkv]);
38
+ // update value if it changes somewhere else (second hook, same key)
39
+ useEffect(() => {
40
+ const listener = mmkv.addOnValueChangedListener((changedKey) => {
41
+ if (changedKey === key) {
42
+ setBump((b) => b + 1);
43
+ }
44
+ });
45
+ return () => listener.remove();
46
+ }, [key, mmkv]);
47
+ return [value, set];
48
+ };
49
+ }
@@ -0,0 +1,11 @@
1
+ import type { MMKV } from '../specs/MMKV.nitro';
2
+ import type { Configuration } from '../specs/MMKVFactory.nitro';
3
+ /**
4
+ * Use the default, shared MMKV instance.
5
+ */
6
+ export declare function useMMKV(): MMKV;
7
+ /**
8
+ * Use a custom MMKV instance with the given configuration.
9
+ * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
10
+ */
11
+ export declare function useMMKV(configuration: Configuration): MMKV;
@@ -0,0 +1,23 @@
1
+ import { useRef } from 'react';
2
+ import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance';
3
+ import { createMMKV } from '../createMMKV/createMMKV';
4
+ function isConfigurationEqual(left, right) {
5
+ if (left == null || right == null)
6
+ return left == null && right == null;
7
+ return (left.encryptionKey === right.encryptionKey &&
8
+ left.id === right.id &&
9
+ left.path === right.path &&
10
+ left.mode === right.mode);
11
+ }
12
+ export function useMMKV(configuration) {
13
+ const instance = useRef(undefined);
14
+ const lastConfiguration = useRef(undefined);
15
+ if (configuration == null)
16
+ return getDefaultMMKVInstance();
17
+ if (instance.current == null ||
18
+ !isConfigurationEqual(lastConfiguration.current, configuration)) {
19
+ lastConfiguration.current = configuration;
20
+ instance.current = createMMKV(configuration);
21
+ }
22
+ return instance.current;
23
+ }
@@ -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
+ }