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

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 (219) hide show
  1. package/{react-native-mmkv.podspec → NitroMmkv.podspec} +17 -13
  2. package/README.md +1 -3
  3. package/android/CMakeLists.txt +31 -30
  4. package/android/build.gradle +64 -19
  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/ios/HybridMMKVPlatformContext.swift +32 -0
  11. package/lib/__tests__/hooks.test.d.ts +1 -0
  12. package/lib/__tests__/hooks.test.js +66 -0
  13. package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
  14. package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
  15. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
  16. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
  17. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
  18. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
  19. package/lib/createMMKV/createMMKV.d.ts +3 -0
  20. package/lib/createMMKV/createMMKV.js +40 -0
  21. package/lib/createMMKV/createMMKV.mock.d.ts +5 -0
  22. package/lib/createMMKV/createMMKV.mock.js +53 -0
  23. package/lib/createMMKV/createMMKV.web.d.ts +3 -0
  24. package/lib/createMMKV/createMMKV.web.js +117 -0
  25. package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
  26. package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
  27. package/lib/hooks/createMMKVHook.d.ts +2 -0
  28. package/lib/hooks/createMMKVHook.js +49 -0
  29. package/lib/hooks/useMMKV.d.ts +11 -0
  30. package/lib/hooks/useMMKV.js +23 -0
  31. package/lib/hooks/useMMKVBoolean.d.ts +11 -0
  32. package/lib/hooks/useMMKVBoolean.js +12 -0
  33. package/lib/hooks/useMMKVBuffer.d.ts +11 -0
  34. package/lib/hooks/useMMKVBuffer.js +12 -0
  35. package/lib/hooks/useMMKVKeys.d.ts +12 -0
  36. package/lib/hooks/useMMKVKeys.js +33 -0
  37. package/lib/hooks/useMMKVListener.d.ts +15 -0
  38. package/lib/hooks/useMMKVListener.js +26 -0
  39. package/lib/hooks/useMMKVNumber.d.ts +11 -0
  40. package/lib/hooks/useMMKVNumber.js +12 -0
  41. package/lib/hooks/useMMKVObject.d.ts +17 -0
  42. package/lib/hooks/useMMKVObject.js +38 -0
  43. package/lib/hooks/useMMKVString.d.ts +11 -0
  44. package/lib/hooks/useMMKVString.js +12 -0
  45. package/lib/index.d.ts +11 -0
  46. package/lib/index.js +11 -0
  47. package/lib/isTest.d.ts +1 -0
  48. package/lib/isTest.js +7 -0
  49. package/lib/specs/MMKV.nitro.d.ts +93 -0
  50. package/lib/specs/MMKV.nitro.js +1 -0
  51. package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +22 -29
  52. package/lib/specs/MMKVFactory.nitro.js +1 -0
  53. package/lib/specs/MMKVPlatformContext.nitro.d.ts +15 -0
  54. package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
  55. package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
  56. package/lib/web/createTextEncoder.js +17 -0
  57. package/nitro.json +27 -0
  58. package/nitrogen/generated/.gitattributes +1 -0
  59. package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
  60. package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
  61. package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +55 -0
  62. package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
  63. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +51 -0
  64. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
  65. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
  66. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
  67. package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
  68. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
  69. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +52 -0
  70. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +44 -0
  71. package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
  72. package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
  73. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
  74. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +81 -0
  75. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
  76. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +135 -0
  77. package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
  78. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
  79. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
  80. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
  81. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +63 -0
  82. package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
  83. package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
  84. package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
  85. package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
  86. package/package.json +71 -122
  87. package/react-native.config.js +2 -15
  88. package/src/__tests__/hooks.test.tsx +34 -34
  89. package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
  90. package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
  91. package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
  92. package/src/createMMKV/createMMKV.mock.ts +56 -0
  93. package/src/createMMKV/createMMKV.ts +51 -0
  94. package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +56 -46
  95. package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
  96. package/src/hooks/createMMKVHook.ts +66 -0
  97. package/src/hooks/useMMKV.ts +45 -0
  98. package/src/hooks/useMMKVBoolean.ts +15 -0
  99. package/src/hooks/useMMKVBuffer.ts +15 -0
  100. package/src/hooks/useMMKVKeys.ts +36 -0
  101. package/src/hooks/useMMKVListener.ts +33 -0
  102. package/src/hooks/useMMKVNumber.ts +15 -0
  103. package/src/hooks/useMMKVObject.ts +53 -0
  104. package/src/hooks/useMMKVString.ts +15 -0
  105. package/src/index.ts +15 -3
  106. package/src/{PlatformChecker.ts → isTest.ts} +2 -2
  107. package/src/specs/MMKV.nitro.ts +92 -0
  108. package/src/specs/MMKVFactory.nitro.ts +87 -0
  109. package/src/specs/MMKVPlatformContext.nitro.ts +14 -0
  110. package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
  111. package/android/src/main/cpp/AndroidLogger.cpp +0 -16
  112. package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
  113. package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
  114. package/cpp/ManagedMMBuffer.h +0 -32
  115. package/cpp/MmkvHostObject.cpp +0 -360
  116. package/cpp/MmkvHostObject.h +0 -31
  117. package/cpp/MmkvLogger.h +0 -35
  118. package/cpp/MmkvTypes.h +0 -50
  119. package/cpp/NativeMmkvModule.cpp +0 -43
  120. package/cpp/NativeMmkvModule.h +0 -31
  121. package/ios/AppleLogger.mm +0 -16
  122. package/ios/MmkvOnLoad.mm +0 -25
  123. package/ios/MmkvPlatformContext.h +0 -19
  124. package/ios/MmkvPlatformContextModule.mm +0 -55
  125. package/lib/commonjs/MMKV.js +0 -124
  126. package/lib/commonjs/MMKV.js.map +0 -1
  127. package/lib/commonjs/MemoryWarningListener.js +0 -31
  128. package/lib/commonjs/MemoryWarningListener.js.map +0 -1
  129. package/lib/commonjs/MemoryWarningListener.web.js +0 -11
  130. package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
  131. package/lib/commonjs/ModuleNotFoundError.js +0 -75
  132. package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
  133. package/lib/commonjs/NativeMmkv.js +0 -47
  134. package/lib/commonjs/NativeMmkv.js.map +0 -1
  135. package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
  136. package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
  137. package/lib/commonjs/PlatformChecker.js +0 -14
  138. package/lib/commonjs/PlatformChecker.js.map +0 -1
  139. package/lib/commonjs/Types.js +0 -26
  140. package/lib/commonjs/Types.js.map +0 -1
  141. package/lib/commonjs/createMMKV.js +0 -43
  142. package/lib/commonjs/createMMKV.js.map +0 -1
  143. package/lib/commonjs/createMMKV.mock.js +0 -43
  144. package/lib/commonjs/createMMKV.mock.js.map +0 -1
  145. package/lib/commonjs/createMMKV.web.js +0 -110
  146. package/lib/commonjs/createMMKV.web.js.map +0 -1
  147. package/lib/commonjs/createTextEncoder.js +0 -23
  148. package/lib/commonjs/createTextEncoder.js.map +0 -1
  149. package/lib/commonjs/hooks.js +0 -198
  150. package/lib/commonjs/hooks.js.map +0 -1
  151. package/lib/commonjs/index.js +0 -40
  152. package/lib/commonjs/index.js.map +0 -1
  153. package/lib/commonjs/package.json +0 -1
  154. package/lib/module/MMKV.js +0 -119
  155. package/lib/module/MMKV.js.map +0 -1
  156. package/lib/module/MemoryWarningListener.js +0 -27
  157. package/lib/module/MemoryWarningListener.js.map +0 -1
  158. package/lib/module/MemoryWarningListener.web.js +0 -6
  159. package/lib/module/MemoryWarningListener.web.js.map +0 -1
  160. package/lib/module/ModuleNotFoundError.js +0 -70
  161. package/lib/module/ModuleNotFoundError.js.map +0 -1
  162. package/lib/module/NativeMmkv.js +0 -45
  163. package/lib/module/NativeMmkv.js.map +0 -1
  164. package/lib/module/NativeMmkvPlatformContext.js +0 -18
  165. package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
  166. package/lib/module/PlatformChecker.js +0 -10
  167. package/lib/module/PlatformChecker.js.map +0 -1
  168. package/lib/module/Types.js +0 -25
  169. package/lib/module/Types.js.map +0 -1
  170. package/lib/module/createMMKV.js +0 -38
  171. package/lib/module/createMMKV.js.map +0 -1
  172. package/lib/module/createMMKV.mock.js +0 -38
  173. package/lib/module/createMMKV.mock.js.map +0 -1
  174. package/lib/module/createMMKV.web.js +0 -105
  175. package/lib/module/createMMKV.web.js.map +0 -1
  176. package/lib/module/createTextEncoder.js +0 -19
  177. package/lib/module/createTextEncoder.js.map +0 -1
  178. package/lib/module/hooks.js +0 -189
  179. package/lib/module/hooks.js.map +0 -1
  180. package/lib/module/index.js +0 -6
  181. package/lib/module/index.js.map +0 -1
  182. package/lib/module/package.json +0 -1
  183. package/lib/typescript/src/MMKV.d.ts +0 -34
  184. package/lib/typescript/src/MMKV.d.ts.map +0 -1
  185. package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
  186. package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
  187. package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
  188. package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
  189. package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
  190. package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
  191. package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
  192. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
  193. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
  194. package/lib/typescript/src/PlatformChecker.d.ts +0 -2
  195. package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
  196. package/lib/typescript/src/Types.d.ts +0 -172
  197. package/lib/typescript/src/Types.d.ts.map +0 -1
  198. package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
  199. package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
  200. package/lib/typescript/src/createMMKV.d.ts +0 -3
  201. package/lib/typescript/src/createMMKV.d.ts.map +0 -1
  202. package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
  203. package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
  204. package/lib/typescript/src/createMMKV.web.d.ts +0 -3
  205. package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
  206. package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
  207. package/lib/typescript/src/hooks.d.ts +0 -86
  208. package/lib/typescript/src/hooks.d.ts.map +0 -1
  209. package/lib/typescript/src/index.d.ts +0 -4
  210. package/lib/typescript/src/index.d.ts.map +0 -1
  211. package/src/MMKV.ts +0 -142
  212. package/src/MemoryWarningListener.web.ts +0 -5
  213. package/src/ModuleNotFoundError.ts +0 -95
  214. package/src/NativeMmkv.ts +0 -118
  215. package/src/NativeMmkvPlatformContext.ts +0 -38
  216. package/src/Types.ts +0 -178
  217. package/src/createMMKV.mock.ts +0 -38
  218. package/src/createMMKV.ts +0 -42
  219. package/src/hooks.ts +0 -247
@@ -0,0 +1,33 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import type { MMKV } from '../specs/MMKV.nitro'
3
+ import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance'
4
+
5
+ /**
6
+ * Listen for changes in the given MMKV storage instance.
7
+ * If no instance is passed, the default instance will be used.
8
+ * @param valueChangedListener The function to call whenever a value inside the storage instance changes
9
+ * @param instance The instance to listen to changes to (or the default instance)
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * useMMKVListener((key) => {
14
+ * console.log(`Value for "${key}" changed!`)
15
+ * })
16
+ * ```
17
+ */
18
+ export function useMMKVListener(
19
+ valueChangedListener: (key: string) => void,
20
+ instance?: MMKV
21
+ ): void {
22
+ const ref = useRef(valueChangedListener)
23
+ ref.current = valueChangedListener
24
+
25
+ const mmkv = instance ?? getDefaultMMKVInstance()
26
+
27
+ useEffect(() => {
28
+ const listener = mmkv.addOnValueChangedListener((changedKey) => {
29
+ ref.current(changedKey)
30
+ })
31
+ return () => listener.remove()
32
+ }, [mmkv])
33
+ }
@@ -0,0 +1,15 @@
1
+ import { createMMKVHook } from './createMMKVHook'
2
+
3
+ /**
4
+ * Use the number 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 [age, setAge] = useMMKVNumber("user.age")
11
+ * ```
12
+ */
13
+ export const useMMKVNumber = createMMKVHook((instance, key) =>
14
+ instance.getNumber(key)
15
+ )
@@ -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,92 @@
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
+ */
45
+ remove(key: string): void
46
+ /**
47
+ * Get all keys.
48
+ *
49
+ * @default []
50
+ */
51
+ getAllKeys(): string[]
52
+ /**
53
+ * Clears all keys/values.
54
+ */
55
+ clearAll(): void
56
+ /**
57
+ * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
58
+ *
59
+ * To remove encryption, pass `undefined` as a key.
60
+ *
61
+ * Encryption keys can have a maximum length of 16 bytes.
62
+ *
63
+ * @throws an Error if the instance cannot be recrypted.
64
+ */
65
+ recrypt(key: string | undefined): void
66
+ /**
67
+ * Trims the storage space and clears memory cache.
68
+ *
69
+ * Since MMKV does not resize itself after deleting keys, you can call `trim()`
70
+ * after deleting a bunch of keys to manually trim the memory- and
71
+ * disk-file to reduce storage and memory usage.
72
+ *
73
+ * In most applications, this is not needed at all.
74
+ */
75
+ trim(): void
76
+ /**
77
+ * Get the current total size of the storage, in bytes.
78
+ */
79
+ readonly size: number
80
+ /**
81
+ * Returns whether this instance is in read-only mode or not.
82
+ * If this is `true`, you can only use "get"-functions.
83
+ */
84
+ readonly isReadOnly: boolean
85
+ /**
86
+ * Adds a value changed listener. The Listener will be called whenever any value
87
+ * in this storage instance changes (set or delete).
88
+ *
89
+ * To unsubscribe from value changes, call `remove()` on the Listener.
90
+ */
91
+ addOnValueChangedListener(onValueChanged: (key: string) => void): Listener
92
+ }
@@ -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 = new MMKV({ id: `user-${userId}-storage` })
21
+ * const globalStorage = new MMKV({ 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 = new MMKV({ 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 = new MMKV({ 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,14 @@
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
+ * @platform iOS
12
+ */
13
+ getAppGroupDirectory(): string
14
+ }
@@ -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
- }
@@ -1,32 +0,0 @@
1
- //
2
- // ManagedMMBuffer.h
3
- // react-native-mmkv
4
- //
5
- // Created by Marc Rousavy on 25.03.24.
6
- //
7
-
8
- #pragma once
9
-
10
- #include "MmkvTypes.h" // IWYU pragma: keep
11
- #include <jsi/jsi.h>
12
-
13
- using namespace facebook;
14
-
15
- /**
16
- A jsi::MutableBuffer that manages MMBuffer memory (by ownership).
17
- */
18
- class ManagedMMBuffer : public jsi::MutableBuffer {
19
- public:
20
- explicit ManagedMMBuffer(MMBuffer&& buffer) : _buffer(std::move(buffer)) {}
21
-
22
- uint8_t* data() override {
23
- return static_cast<uint8_t*>(_buffer.getPtr());
24
- }
25
-
26
- size_t size() const override {
27
- return _buffer.length();
28
- }
29
-
30
- private:
31
- MMBuffer _buffer;
32
- };