react-native-mmkv 4.0.0-beta.0 → 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 +78 -125
  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,53 @@
1
+ import { useCallback, useMemo } from 'react'
2
+ import type { MMKV } from '../specs/MMKV.nitro'
3
+ import { useMMKVString } from './useMMKVString'
4
+
5
+ /**
6
+ * Use an object value of the given `key` from the given MMKV storage instance.
7
+ *
8
+ * If no instance is provided, a shared default instance will be used.
9
+ *
10
+ * The object will be serialized using `JSON`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const [user, setUser] = useMMKVObject<User>("user")
15
+ * ```
16
+ */
17
+ export function useMMKVObject<T>(
18
+ key: string,
19
+ instance?: MMKV
20
+ ): [
21
+ value: T | undefined,
22
+ setValue: (
23
+ value: T | undefined | ((prevValue: T | undefined) => T | undefined)
24
+ ) => void,
25
+ ] {
26
+ const [json, setJson] = useMMKVString(key, instance)
27
+
28
+ const value = useMemo(() => {
29
+ if (json == null) return undefined
30
+ return JSON.parse(json) as T
31
+ }, [json])
32
+
33
+ const setValue = useCallback(
34
+ (v: (T | undefined) | ((prev: T | undefined) => T | undefined)) => {
35
+ if (v instanceof Function) {
36
+ setJson((currentJson) => {
37
+ const currentValue =
38
+ currentJson != null ? (JSON.parse(currentJson) as T) : undefined
39
+ const newValue = v(currentValue)
40
+ // Store the Object as a serialized Value or clear the value
41
+ return newValue != null ? JSON.stringify(newValue) : undefined
42
+ })
43
+ } else {
44
+ // Store the Object as a serialized Value or clear the value
45
+ const newValue = v != null ? JSON.stringify(v) : undefined
46
+ setJson(newValue)
47
+ }
48
+ },
49
+ [setJson]
50
+ )
51
+
52
+ return [value, setValue]
53
+ }
@@ -0,0 +1,15 @@
1
+ import { createMMKVHook } from './createMMKVHook'
2
+
3
+ /**
4
+ * Use the string 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
+ * @example
9
+ * ```ts
10
+ * const [username, setUsername] = useMMKVString("user.name")
11
+ * ```
12
+ */
13
+ export const useMMKVString = createMMKVHook((instance, key) =>
14
+ instance.getString(key)
15
+ )
package/src/index.ts CHANGED
@@ -1,4 +1,16 @@
1
- export * from './MMKV';
2
- export * from './hooks';
1
+ // All types
2
+ export type { MMKV } from './specs/MMKV.nitro'
3
+ export type { Configuration, Mode } from './specs/MMKVFactory.nitro'
3
4
 
4
- export { Mode, type Configuration } from './Types';
5
+ // The create function
6
+ export { createMMKV } from './createMMKV/createMMKV'
7
+
8
+ // All the hooks
9
+ export { useMMKV } from './hooks/useMMKV'
10
+ export { useMMKVBoolean } from './hooks/useMMKVBoolean'
11
+ export { useMMKVBuffer } from './hooks/useMMKVBuffer'
12
+ export { useMMKVNumber } from './hooks/useMMKVNumber'
13
+ export { useMMKVObject } from './hooks/useMMKVObject'
14
+ export { useMMKVString } from './hooks/useMMKVString'
15
+ export { useMMKVListener } from './hooks/useMMKVListener'
16
+ export { useMMKVKeys } from './hooks/useMMKVKeys'
@@ -1,9 +1,9 @@
1
1
  export function isTest(): boolean {
2
2
  if (global.process == null) {
3
3
  // In a WebBrowser/Electron the `process` variable does not exist
4
- return false;
4
+ return false
5
5
  }
6
6
  return (
7
7
  process.env.JEST_WORKER_ID != null || process.env.VITEST_WORKER_ID != null
8
- );
8
+ )
9
9
  }
@@ -0,0 +1,93 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules'
2
+
3
+ export interface Listener {
4
+ remove: () => void
5
+ }
6
+
7
+ export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
8
+ /**
9
+ * Set a value for the given `key`.
10
+ *
11
+ * @throws an Error if the value cannot be set.
12
+ */
13
+ set(key: string, value: boolean | string | number | ArrayBuffer): void
14
+ /**
15
+ * Get the boolean value for the given `key`, or `undefined` if it does not exist.
16
+ *
17
+ * @default undefined
18
+ */
19
+ getBoolean(key: string): boolean | undefined
20
+ /**
21
+ * Get the string value for the given `key`, or `undefined` if it does not exist.
22
+ *
23
+ * @default undefined
24
+ */
25
+ getString(key: string): string | undefined
26
+ /**
27
+ * Get the number value for the given `key`, or `undefined` if it does not exist.
28
+ *
29
+ * @default undefined
30
+ */
31
+ getNumber(key: string): number | undefined
32
+ /**
33
+ * Get a raw buffer of unsigned 8-bit (0-255) data.
34
+ *
35
+ * @default undefined
36
+ */
37
+ getBuffer(key: string): ArrayBuffer | undefined
38
+ /**
39
+ * Checks whether the given `key` is being stored in this MMKV instance.
40
+ */
41
+ contains(key: string): boolean
42
+ /**
43
+ * Removes the given `key`.
44
+ * @returns true if the key was removed, false otherwise
45
+ */
46
+ remove(key: string): boolean
47
+ /**
48
+ * Get all keys.
49
+ *
50
+ * @default []
51
+ */
52
+ getAllKeys(): string[]
53
+ /**
54
+ * Clears all keys/values.
55
+ */
56
+ clearAll(): void
57
+ /**
58
+ * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
59
+ *
60
+ * To remove encryption, pass `undefined` as a key.
61
+ *
62
+ * Encryption keys can have a maximum length of 16 bytes.
63
+ *
64
+ * @throws an Error if the instance cannot be recrypted.
65
+ */
66
+ recrypt(key: string | undefined): void
67
+ /**
68
+ * Trims the storage space and clears memory cache.
69
+ *
70
+ * Since MMKV does not resize itself after deleting keys, you can call `trim()`
71
+ * after deleting a bunch of keys to manually trim the memory- and
72
+ * disk-file to reduce storage and memory usage.
73
+ *
74
+ * In most applications, this is not needed at all.
75
+ */
76
+ trim(): void
77
+ /**
78
+ * Get the current total size of the storage, in bytes.
79
+ */
80
+ readonly size: number
81
+ /**
82
+ * Returns whether this instance is in read-only mode or not.
83
+ * If this is `true`, you can only use "get"-functions.
84
+ */
85
+ readonly isReadOnly: boolean
86
+ /**
87
+ * Adds a value changed listener. The Listener will be called whenever any value
88
+ * in this storage instance changes (set or delete).
89
+ *
90
+ * To unsubscribe from value changes, call `remove()` on the Listener.
91
+ */
92
+ addOnValueChangedListener(onValueChanged: (key: string) => void): Listener
93
+ }
@@ -0,0 +1,87 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules'
2
+ import type { MMKV } from './MMKV.nitro'
3
+
4
+ /**
5
+ * Configures the mode of the MMKV instance.
6
+ * - `single-process`: The MMKV instance is only used from a single process (this app).
7
+ * - `multi-process`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
8
+ */
9
+ export type Mode = 'single-process' | 'multi-process'
10
+
11
+ /**
12
+ * Used for configuration of a single MMKV instance.
13
+ */
14
+ export interface Configuration {
15
+ /**
16
+ * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const userStorage = createMMKV({ id: `user-${userId}-storage` })
21
+ * const globalStorage = createMMKV({ id: 'global-app-storage' })
22
+ * ```
23
+ *
24
+ * @default 'mmkv.default'
25
+ */
26
+ id: string
27
+ /**
28
+ * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
29
+
30
+ * @example
31
+ * ```ts
32
+ * const temporaryStorage = createMMKV({ path: '/tmp/' })
33
+ * ```
34
+ *
35
+ * @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
36
+ * App Groups allow you to share MMKV storage between apps, widgets and extensions within the same AppGroup bundle.
37
+ * For more information, see [the `Configuration` section](https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration).
38
+ *
39
+ * @default undefined
40
+ */
41
+ path?: string
42
+ /**
43
+ * The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.
44
+ *
45
+ * Encryption keys can have a maximum length of 16 bytes.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const secureStorage = createMMKV({ encryptionKey: 'my-encryption-key!' })
50
+ * ```
51
+ *
52
+ * @default undefined
53
+ */
54
+ encryptionKey?: string
55
+ /**
56
+ * Configure the processing mode for MMKV.
57
+ *
58
+ * @default 'single-process'
59
+ */
60
+ mode?: Mode
61
+ /**
62
+ * If `true`, the MMKV instance can only read from the storage, but not write to it.
63
+ * This is more efficient if you do not need to write to it.
64
+ * @default false
65
+ */
66
+ readOnly?: boolean
67
+ }
68
+
69
+ export interface MMKVFactory
70
+ extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
71
+ /**
72
+ * Create a new {@linkcode MMKV} instance with the given {@linkcode Configuration}
73
+ */
74
+ createMMKV(configuration: Configuration): MMKV
75
+
76
+ /**
77
+ * Initialize the MMKV library with the given root path.
78
+ * This has to be called once, before using {@linkcode createMMKV}.
79
+ */
80
+ initializeMMKV(rootPath: string): void
81
+
82
+ /**
83
+ * Get the default MMKV instance's ID.
84
+ * @default 'mmkv.default'
85
+ */
86
+ readonly defaultMMKVInstanceId: string
87
+ }
@@ -0,0 +1,17 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules'
2
+
3
+ export interface MMKVPlatformContext
4
+ extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
5
+ /**
6
+ * Get the MMKV base directory
7
+ */
8
+ getBaseDirectory(): string
9
+ /**
10
+ * Get the MMKV AppGroup's directory.
11
+ * The AppGroup can be set in your App's `Info.plist`, and will enable
12
+ * data sharing between main app, companions (e.g. watch app) and extensions.
13
+ * @platform iOS
14
+ * @default undefined
15
+ */
16
+ getAppGroupDirectory(): string | undefined
17
+ }
@@ -1,16 +1,16 @@
1
- /* global TextEncoder */
2
- export function createTextEncoder(): TextEncoder {
3
- if (global.TextEncoder != null) {
4
- return new global.TextEncoder();
1
+ export function createTextEncoder() {
2
+ const g = global ?? globalThis ?? window
3
+ if (g.TextEncoder != null) {
4
+ return new g.TextEncoder()
5
5
  } else {
6
6
  return {
7
7
  encode: () => {
8
- throw new Error('TextEncoder is not supported in this environment!');
8
+ throw new Error('TextEncoder is not supported in this environment!')
9
9
  },
10
10
  encodeInto: () => {
11
- throw new Error('TextEncoder is not supported in this environment!');
11
+ throw new Error('TextEncoder is not supported in this environment!')
12
12
  },
13
13
  encoding: 'utf-8',
14
- };
14
+ }
15
15
  }
16
16
  }
@@ -1,16 +0,0 @@
1
- //
2
- // AndroidMmkvLogger.cpp
3
- // react-native-mmkv
4
- //
5
- // Created by Marc Rousavy on 05.03.24.
6
- //
7
-
8
- #include "MmkvLogger.h"
9
- #include <android/log.h>
10
-
11
- void MmkvLogger::log(const std::string& tag, const std::string& message) {
12
- #pragma clang diagnostic push
13
- #pragma clang diagnostic ignored "-Wformat-security"
14
- __android_log_print(ANDROID_LOG_INFO, tag.c_str(), message.c_str());
15
- #pragma clang diagnostic pop
16
- }
@@ -1,44 +0,0 @@
1
- package com.mrousavy.mmkv;
2
-
3
- import androidx.annotation.NonNull;
4
- import androidx.annotation.Nullable;
5
-
6
- import com.facebook.react.bridge.NativeModule;
7
- import com.facebook.react.bridge.ReactApplicationContext;
8
- import com.facebook.react.module.model.ReactModuleInfo;
9
- import com.facebook.react.module.model.ReactModuleInfoProvider;
10
- import com.facebook.react.TurboReactPackage;
11
-
12
- import java.util.HashMap;
13
- import java.util.Map;
14
-
15
- public class MmkvPackage extends TurboReactPackage {
16
- @Nullable
17
- @Override
18
- public NativeModule getModule(String name, @NonNull ReactApplicationContext reactContext) {
19
- if (name.equals(MmkvPlatformContextModule.NAME)) {
20
- return new MmkvPlatformContextModule(reactContext);
21
- } else {
22
- return null;
23
- }
24
- }
25
-
26
- @Override
27
- public ReactModuleInfoProvider getReactModuleInfoProvider() {
28
- return () -> {
29
- final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
30
- moduleInfos.put(
31
- MmkvPlatformContextModule.NAME,
32
- new ReactModuleInfo(
33
- MmkvPlatformContextModule.NAME,
34
- MmkvPlatformContextModule.NAME,
35
- false, // canOverrideExistingModule
36
- false, // needsEagerInit
37
- true, // hasConstants
38
- false, // isCxxModule
39
- true // isTurboModule
40
- ));
41
- return moduleInfos;
42
- };
43
- }
44
- }
@@ -1,26 +0,0 @@
1
- package com.mrousavy.mmkv;
2
-
3
- import androidx.annotation.Nullable;
4
-
5
- import com.facebook.react.bridge.ReactApplicationContext;
6
-
7
- public class MmkvPlatformContextModule extends NativeMmkvPlatformContextSpec {
8
- private final ReactApplicationContext context;
9
-
10
- public MmkvPlatformContextModule(ReactApplicationContext reactContext) {
11
- super(reactContext);
12
- context = reactContext;
13
- }
14
-
15
- @Override
16
- public String getBaseDirectory() {
17
- return context.getFilesDir().getAbsolutePath() + "/mmkv";
18
- }
19
-
20
- @Nullable
21
- @Override
22
- public String getAppGroupDirectory() {
23
- // AppGroups do not exist on Android. It's iOS only.
24
- return null;
25
- }
26
- }