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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/NitroMmkv.podspec +44 -0
  2. package/README.md +1 -3
  3. package/android/CMakeLists.txt +31 -30
  4. package/android/build.gradle +66 -21
  5. package/android/fix-prefab.gradle +51 -0
  6. package/android/gradle.properties +5 -5
  7. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  8. package/android/src/main/java/com/margelo/nitro/mmkv/HybridMMKVPlatformContext.kt +19 -0
  9. package/android/src/main/java/com/margelo/nitro/mmkv/NitroMmkvPackage.java +33 -0
  10. package/cpp/HybridMMKV.cpp +184 -0
  11. package/cpp/HybridMMKV.hpp +47 -0
  12. package/cpp/HybridMMKVFactory.cpp +33 -0
  13. package/cpp/HybridMMKVFactory.hpp +24 -0
  14. package/cpp/{MmkvTypes.h → MMKVTypes.hpp} +1 -1
  15. package/cpp/MMKVValueChangedListenerRegistry.cpp +58 -0
  16. package/cpp/MMKVValueChangedListenerRegistry.hpp +43 -0
  17. package/cpp/{ManagedMMBuffer.h → ManagedMMBuffer.hpp} +13 -5
  18. package/ios/HybridMMKVPlatformContext.swift +43 -0
  19. package/lib/__tests__/hooks.test.d.ts +1 -0
  20. package/lib/__tests__/hooks.test.js +69 -0
  21. package/lib/addMemoryWarningListener/addMemoryWarningListener.d.ts +2 -0
  22. package/lib/addMemoryWarningListener/addMemoryWarningListener.js +25 -0
  23. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.d.ts +2 -0
  24. package/lib/addMemoryWarningListener/addMemoryWarningListener.mock.js +3 -0
  25. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.d.ts +2 -0
  26. package/lib/addMemoryWarningListener/addMemoryWarningListener.web.js +3 -0
  27. package/lib/createMMKV/createMMKV.d.ts +3 -0
  28. package/lib/createMMKV/createMMKV.js +40 -0
  29. package/lib/createMMKV/createMMKV.web.d.ts +3 -0
  30. package/lib/createMMKV/createMMKV.web.js +117 -0
  31. package/lib/createMMKV/createMockMMKV.d.ts +5 -0
  32. package/lib/createMMKV/createMockMMKV.js +74 -0
  33. package/lib/createMMKV/getDefaultMMKVInstance.d.ts +2 -0
  34. package/lib/createMMKV/getDefaultMMKVInstance.js +8 -0
  35. package/lib/hooks/createMMKVHook.d.ts +2 -0
  36. package/lib/hooks/createMMKVHook.js +49 -0
  37. package/lib/hooks/useMMKV.d.ts +11 -0
  38. package/lib/hooks/useMMKV.js +23 -0
  39. package/lib/hooks/useMMKVBoolean.d.ts +11 -0
  40. package/lib/hooks/useMMKVBoolean.js +12 -0
  41. package/lib/hooks/useMMKVBuffer.d.ts +11 -0
  42. package/lib/hooks/useMMKVBuffer.js +12 -0
  43. package/lib/hooks/useMMKVKeys.d.ts +12 -0
  44. package/lib/hooks/useMMKVKeys.js +33 -0
  45. package/lib/hooks/useMMKVListener.d.ts +15 -0
  46. package/lib/hooks/useMMKVListener.js +26 -0
  47. package/lib/hooks/useMMKVNumber.d.ts +11 -0
  48. package/lib/hooks/useMMKVNumber.js +12 -0
  49. package/lib/hooks/useMMKVObject.d.ts +17 -0
  50. package/lib/hooks/useMMKVObject.js +38 -0
  51. package/lib/hooks/useMMKVString.d.ts +11 -0
  52. package/lib/hooks/useMMKVString.js +12 -0
  53. package/lib/index.d.ts +11 -0
  54. package/lib/index.js +11 -0
  55. package/lib/isTest.d.ts +1 -0
  56. package/lib/isTest.js +7 -0
  57. package/lib/specs/MMKV.nitro.d.ts +94 -0
  58. package/lib/specs/MMKV.nitro.js +1 -0
  59. package/lib/{typescript/src/NativeMmkv.d.ts → specs/MMKVFactory.nitro.d.ts} +26 -33
  60. package/lib/specs/MMKVFactory.nitro.js +1 -0
  61. package/lib/specs/MMKVPlatformContext.nitro.d.ts +18 -0
  62. package/lib/specs/MMKVPlatformContext.nitro.js +1 -0
  63. package/lib/{typescript/src → web}/createTextEncoder.d.ts +0 -1
  64. package/lib/web/createTextEncoder.js +17 -0
  65. package/nitro.json +28 -0
  66. package/nitrogen/generated/.gitattributes +1 -0
  67. package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +80 -0
  68. package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +27 -0
  69. package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +54 -0
  70. package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +25 -0
  71. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +52 -0
  72. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +65 -0
  73. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +56 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +35 -0
  75. package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +60 -0
  76. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +32 -0
  77. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +77 -0
  78. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +45 -0
  79. package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +43 -0
  80. package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +25 -0
  81. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +11 -0
  82. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +82 -0
  83. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +50 -0
  84. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +141 -0
  85. package/nitrogen/generated/shared/c++/Configuration.hpp +86 -0
  86. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +23 -0
  87. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +69 -0
  88. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +22 -0
  89. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +64 -0
  90. package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +34 -0
  91. package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +83 -0
  92. package/nitrogen/generated/shared/c++/Listener.hpp +67 -0
  93. package/nitrogen/generated/shared/c++/Mode.hpp +76 -0
  94. package/package.json +74 -122
  95. package/react-native.config.js +2 -15
  96. package/src/__tests__/hooks.test.tsx +39 -34
  97. package/src/addMemoryWarningListener/addMemoryWarningListener.mock.ts +5 -0
  98. package/src/{MemoryWarningListener.ts → addMemoryWarningListener/addMemoryWarningListener.ts} +12 -12
  99. package/src/addMemoryWarningListener/addMemoryWarningListener.web.ts +5 -0
  100. package/src/createMMKV/createMMKV.ts +51 -0
  101. package/src/{createMMKV.web.ts → createMMKV/createMMKV.web.ts} +58 -46
  102. package/src/createMMKV/createMockMMKV.ts +78 -0
  103. package/src/createMMKV/getDefaultMMKVInstance.ts +10 -0
  104. package/src/hooks/createMMKVHook.ts +66 -0
  105. package/src/hooks/useMMKV.ts +45 -0
  106. package/src/hooks/useMMKVBoolean.ts +15 -0
  107. package/src/hooks/useMMKVBuffer.ts +15 -0
  108. package/src/hooks/useMMKVKeys.ts +36 -0
  109. package/src/hooks/useMMKVListener.ts +33 -0
  110. package/src/hooks/useMMKVNumber.ts +15 -0
  111. package/src/hooks/useMMKVObject.ts +53 -0
  112. package/src/hooks/useMMKVString.ts +15 -0
  113. package/src/index.ts +15 -3
  114. package/src/{PlatformChecker.ts → isTest.ts} +2 -2
  115. package/src/specs/MMKV.nitro.ts +93 -0
  116. package/src/specs/MMKVFactory.nitro.ts +87 -0
  117. package/src/specs/MMKVPlatformContext.nitro.ts +17 -0
  118. package/src/{createTextEncoder.ts → web/createTextEncoder.ts} +7 -7
  119. package/android/src/main/cpp/AndroidLogger.cpp +0 -16
  120. package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +0 -44
  121. package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +0 -26
  122. package/cpp/MmkvHostObject.cpp +0 -360
  123. package/cpp/MmkvHostObject.h +0 -31
  124. package/cpp/MmkvLogger.h +0 -35
  125. package/cpp/NativeMmkvModule.cpp +0 -43
  126. package/cpp/NativeMmkvModule.h +0 -31
  127. package/ios/AppleLogger.mm +0 -16
  128. package/ios/MmkvOnLoad.mm +0 -25
  129. package/ios/MmkvPlatformContext.h +0 -19
  130. package/ios/MmkvPlatformContextModule.mm +0 -55
  131. package/lib/commonjs/MMKV.js +0 -124
  132. package/lib/commonjs/MMKV.js.map +0 -1
  133. package/lib/commonjs/MemoryWarningListener.js +0 -31
  134. package/lib/commonjs/MemoryWarningListener.js.map +0 -1
  135. package/lib/commonjs/MemoryWarningListener.web.js +0 -11
  136. package/lib/commonjs/MemoryWarningListener.web.js.map +0 -1
  137. package/lib/commonjs/ModuleNotFoundError.js +0 -75
  138. package/lib/commonjs/ModuleNotFoundError.js.map +0 -1
  139. package/lib/commonjs/NativeMmkv.js +0 -47
  140. package/lib/commonjs/NativeMmkv.js.map +0 -1
  141. package/lib/commonjs/NativeMmkvPlatformContext.js +0 -22
  142. package/lib/commonjs/NativeMmkvPlatformContext.js.map +0 -1
  143. package/lib/commonjs/PlatformChecker.js +0 -14
  144. package/lib/commonjs/PlatformChecker.js.map +0 -1
  145. package/lib/commonjs/Types.js +0 -26
  146. package/lib/commonjs/Types.js.map +0 -1
  147. package/lib/commonjs/createMMKV.js +0 -43
  148. package/lib/commonjs/createMMKV.js.map +0 -1
  149. package/lib/commonjs/createMMKV.mock.js +0 -43
  150. package/lib/commonjs/createMMKV.mock.js.map +0 -1
  151. package/lib/commonjs/createMMKV.web.js +0 -110
  152. package/lib/commonjs/createMMKV.web.js.map +0 -1
  153. package/lib/commonjs/createTextEncoder.js +0 -23
  154. package/lib/commonjs/createTextEncoder.js.map +0 -1
  155. package/lib/commonjs/hooks.js +0 -198
  156. package/lib/commonjs/hooks.js.map +0 -1
  157. package/lib/commonjs/index.js +0 -40
  158. package/lib/commonjs/index.js.map +0 -1
  159. package/lib/commonjs/package.json +0 -1
  160. package/lib/module/MMKV.js +0 -119
  161. package/lib/module/MMKV.js.map +0 -1
  162. package/lib/module/MemoryWarningListener.js +0 -27
  163. package/lib/module/MemoryWarningListener.js.map +0 -1
  164. package/lib/module/MemoryWarningListener.web.js +0 -6
  165. package/lib/module/MemoryWarningListener.web.js.map +0 -1
  166. package/lib/module/ModuleNotFoundError.js +0 -70
  167. package/lib/module/ModuleNotFoundError.js.map +0 -1
  168. package/lib/module/NativeMmkv.js +0 -45
  169. package/lib/module/NativeMmkv.js.map +0 -1
  170. package/lib/module/NativeMmkvPlatformContext.js +0 -18
  171. package/lib/module/NativeMmkvPlatformContext.js.map +0 -1
  172. package/lib/module/PlatformChecker.js +0 -10
  173. package/lib/module/PlatformChecker.js.map +0 -1
  174. package/lib/module/Types.js +0 -25
  175. package/lib/module/Types.js.map +0 -1
  176. package/lib/module/createMMKV.js +0 -38
  177. package/lib/module/createMMKV.js.map +0 -1
  178. package/lib/module/createMMKV.mock.js +0 -38
  179. package/lib/module/createMMKV.mock.js.map +0 -1
  180. package/lib/module/createMMKV.web.js +0 -105
  181. package/lib/module/createMMKV.web.js.map +0 -1
  182. package/lib/module/createTextEncoder.js +0 -19
  183. package/lib/module/createTextEncoder.js.map +0 -1
  184. package/lib/module/hooks.js +0 -189
  185. package/lib/module/hooks.js.map +0 -1
  186. package/lib/module/index.js +0 -6
  187. package/lib/module/index.js.map +0 -1
  188. package/lib/module/package.json +0 -1
  189. package/lib/typescript/src/MMKV.d.ts +0 -34
  190. package/lib/typescript/src/MMKV.d.ts.map +0 -1
  191. package/lib/typescript/src/MemoryWarningListener.d.ts +0 -3
  192. package/lib/typescript/src/MemoryWarningListener.d.ts.map +0 -1
  193. package/lib/typescript/src/MemoryWarningListener.web.d.ts +0 -3
  194. package/lib/typescript/src/MemoryWarningListener.web.d.ts.map +0 -1
  195. package/lib/typescript/src/ModuleNotFoundError.d.ts +0 -7
  196. package/lib/typescript/src/ModuleNotFoundError.d.ts.map +0 -1
  197. package/lib/typescript/src/NativeMmkv.d.ts.map +0 -1
  198. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +0 -20
  199. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +0 -1
  200. package/lib/typescript/src/PlatformChecker.d.ts +0 -2
  201. package/lib/typescript/src/PlatformChecker.d.ts.map +0 -1
  202. package/lib/typescript/src/Types.d.ts +0 -172
  203. package/lib/typescript/src/Types.d.ts.map +0 -1
  204. package/lib/typescript/src/__tests__/hooks.test.d.ts +0 -2
  205. package/lib/typescript/src/__tests__/hooks.test.d.ts.map +0 -1
  206. package/lib/typescript/src/createMMKV.d.ts +0 -3
  207. package/lib/typescript/src/createMMKV.d.ts.map +0 -1
  208. package/lib/typescript/src/createMMKV.mock.d.ts +0 -3
  209. package/lib/typescript/src/createMMKV.mock.d.ts.map +0 -1
  210. package/lib/typescript/src/createMMKV.web.d.ts +0 -3
  211. package/lib/typescript/src/createMMKV.web.d.ts.map +0 -1
  212. package/lib/typescript/src/createTextEncoder.d.ts.map +0 -1
  213. package/lib/typescript/src/hooks.d.ts +0 -86
  214. package/lib/typescript/src/hooks.d.ts.map +0 -1
  215. package/lib/typescript/src/index.d.ts +0 -4
  216. package/lib/typescript/src/index.d.ts.map +0 -1
  217. package/react-native-mmkv.podspec +0 -32
  218. package/src/MMKV.ts +0 -142
  219. package/src/MemoryWarningListener.web.ts +0 -5
  220. package/src/ModuleNotFoundError.ts +0 -95
  221. package/src/NativeMmkv.ts +0 -118
  222. package/src/NativeMmkvPlatformContext.ts +0 -38
  223. package/src/Types.ts +0 -178
  224. package/src/createMMKV.mock.ts +0 -38
  225. package/src/createMMKV.ts +0 -42
  226. package/src/hooks.ts +0 -247
@@ -0,0 +1,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
- }
@@ -1,360 +0,0 @@
1
- //
2
- // MmkvHostObject.cpp
3
- // Mmkv
4
- //
5
- // Created by Marc Rousavy on 03.09.21.
6
- // Copyright © 2021 Facebook. All rights reserved.
7
- //
8
-
9
- #include "MmkvHostObject.h"
10
- #include "ManagedMMBuffer.h"
11
- #include "MmkvLogger.h"
12
- #include <string>
13
- #include <vector>
14
-
15
- using namespace mmkv;
16
- using namespace facebook;
17
-
18
- MmkvHostObject::MmkvHostObject(const facebook::react::MMKVConfig& config) {
19
- std::string path = config.path.has_value() ? config.path.value() : "";
20
- std::string encryptionKey = config.encryptionKey.has_value() ? config.encryptionKey.value() : "";
21
- bool hasEncryptionKey = encryptionKey.size() > 0;
22
- MmkvLogger::log("RNMMKV", "Creating MMKV instance \"%s\"... (Path: %s, Encrypted: %s)",
23
- config.id.c_str(), path.c_str(), hasEncryptionKey ? "true" : "false");
24
-
25
- std::string* pathPtr = path.size() > 0 ? &path : nullptr;
26
- std::string* encryptionKeyPtr = encryptionKey.size() > 0 ? &encryptionKey : nullptr;
27
- MMKVMode mode = getMMKVMode(config);
28
- if (config.readOnly.has_value() && config.readOnly.value()) {
29
- MmkvLogger::log("RNMMKV", "Instance is read-only!");
30
- mode = mode | mmkv::MMKV_READ_ONLY;
31
- }
32
-
33
- #ifdef __APPLE__
34
- instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr);
35
- #else
36
- instance = MMKV::mmkvWithID(config.id, DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
37
- #endif
38
-
39
- if (instance == nullptr) [[unlikely]] {
40
- // Check if instanceId is invalid
41
- if (config.id.empty()) [[unlikely]] {
42
- throw std::runtime_error("Failed to create MMKV instance! `id` cannot be empty!");
43
- }
44
-
45
- // Check if encryptionKey is invalid
46
- if (encryptionKey.size() > 16) [[unlikely]] {
47
- throw std::runtime_error(
48
- "Failed to create MMKV instance! `encryptionKey` cannot be longer than 16 bytes!");
49
- }
50
-
51
- throw std::runtime_error("Failed to create MMKV instance!");
52
- }
53
- }
54
-
55
- MmkvHostObject::~MmkvHostObject() {
56
- if (instance != nullptr) {
57
- std::string instanceId = instance->mmapID();
58
- MmkvLogger::log("RNMMKV", "Destroying MMKV instance \"%s\"...", instanceId.c_str());
59
- instance->sync();
60
- instance->clearMemoryCache();
61
- }
62
- instance = nullptr;
63
- }
64
-
65
- std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt) {
66
- return jsi::PropNameID::names(rt, "set", "getBoolean", "getBuffer", "getString", "getNumber",
67
- "contains", "delete", "getAllKeys", "deleteAll", "recrypt", "trim",
68
- "size", "isReadOnly");
69
- }
70
-
71
- MMKVMode MmkvHostObject::getMMKVMode(const facebook::react::MMKVConfig& config) {
72
- if (!config.mode.has_value()) {
73
- return mmkv::MMKV_SINGLE_PROCESS;
74
- }
75
- react::NativeMmkvMode mode = config.mode.value();
76
- switch (mode) {
77
- case react::NativeMmkvMode::SINGLE_PROCESS:
78
- return mmkv::MMKV_SINGLE_PROCESS;
79
- case react::NativeMmkvMode::MULTI_PROCESS:
80
- return mmkv::MMKV_MULTI_PROCESS;
81
- default:
82
- [[unlikely]] throw std::runtime_error("Invalid MMKV Mode value!");
83
- }
84
- }
85
-
86
- jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
87
- std::string propName = propNameId.utf8(runtime);
88
-
89
- if (propName == "set") {
90
- // MMKV.set(key: string, value: string | number | bool | ArrayBuffer)
91
- return jsi::Function::createFromHostFunction(
92
- runtime, jsi::PropNameID::forAscii(runtime, propName),
93
- 2, // key, value
94
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
95
- size_t count) -> jsi::Value {
96
- if (count != 2 || !arguments[0].isString()) [[unlikely]] {
97
- throw jsi::JSError(runtime,
98
- "MMKV::set: First argument ('key') has to be of type string!");
99
- }
100
-
101
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
102
-
103
- bool successful = false;
104
- if (arguments[1].isBool()) {
105
- // bool
106
- successful = instance->set(arguments[1].getBool(), keyName);
107
- } else if (arguments[1].isNumber()) {
108
- // number
109
- successful = instance->set(arguments[1].getNumber(), keyName);
110
- } else if (arguments[1].isString()) {
111
- // string
112
- std::string stringValue = arguments[1].getString(runtime).utf8(runtime);
113
- successful = instance->set(stringValue, keyName);
114
- } else if (arguments[1].isObject()) {
115
- // object
116
- jsi::Object object = arguments[1].getObject(runtime);
117
- if (object.isArrayBuffer(runtime)) {
118
- // ArrayBuffer
119
- jsi::ArrayBuffer arrayBuffer = object.getArrayBuffer(runtime);
120
- MMBuffer data(arrayBuffer.data(runtime), arrayBuffer.size(runtime), MMBufferNoCopy);
121
- successful = instance->set(data, keyName);
122
- } else [[unlikely]] {
123
- // unknown object
124
- throw jsi::JSError(
125
- runtime,
126
- "MMKV::set: 'value' argument is an object, but not of type ArrayBuffer!");
127
- }
128
- } else [[unlikely]] {
129
- // unknown type
130
- throw jsi::JSError(
131
- runtime,
132
- "MMKV::set: 'value' argument is not of type bool, number, string or buffer!");
133
- }
134
-
135
- if (!successful) [[unlikely]] {
136
- if (instance->isReadOnly()) {
137
- throw jsi::JSError(runtime,
138
- "Failed to set " + keyName + "! This instance is read-only!");
139
- } else {
140
- throw jsi::JSError(runtime, "Failed to set " + keyName + "!");
141
- }
142
- }
143
-
144
- return jsi::Value::undefined();
145
- });
146
- }
147
-
148
- if (propName == "getBoolean") {
149
- // MMKV.getBoolean(key: string)
150
- return jsi::Function::createFromHostFunction(
151
- runtime, jsi::PropNameID::forAscii(runtime, propName),
152
- 1, // key
153
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
154
- size_t count) -> jsi::Value {
155
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
156
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
157
- }
158
-
159
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
160
- bool hasValue;
161
- bool value = instance->getBool(keyName, false, &hasValue);
162
- if (!hasValue) [[unlikely]] {
163
- return jsi::Value::undefined();
164
- }
165
- return jsi::Value(value);
166
- });
167
- }
168
-
169
- if (propName == "getNumber") {
170
- // MMKV.getNumber(key: string)
171
- return jsi::Function::createFromHostFunction(
172
- runtime, jsi::PropNameID::forAscii(runtime, propName),
173
- 1, // key
174
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
175
- size_t count) -> jsi::Value {
176
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
177
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
178
- }
179
-
180
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
181
- bool hasValue;
182
- double value = instance->getDouble(keyName, 0.0, &hasValue);
183
- if (!hasValue) [[unlikely]] {
184
- return jsi::Value::undefined();
185
- }
186
- return jsi::Value(value);
187
- });
188
- }
189
-
190
- if (propName == "getString") {
191
- // MMKV.getString(key: string)
192
- return jsi::Function::createFromHostFunction(
193
- runtime, jsi::PropNameID::forAscii(runtime, propName),
194
- 1, // key
195
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
196
- size_t count) -> jsi::Value {
197
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
198
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
199
- }
200
-
201
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
202
- std::string result;
203
- bool hasValue = instance->getString(keyName, result);
204
- if (!hasValue) [[unlikely]] {
205
- return jsi::Value::undefined();
206
- }
207
- return jsi::Value(runtime, jsi::String::createFromUtf8(runtime, result));
208
- });
209
- }
210
-
211
- if (propName == "getBuffer") {
212
- // MMKV.getBuffer(key: string)
213
- return jsi::Function::createFromHostFunction(
214
- runtime, jsi::PropNameID::forAscii(runtime, propName),
215
- 1, // key
216
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
217
- size_t count) -> jsi::Value {
218
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
219
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
220
- }
221
-
222
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
223
- MMBuffer buffer;
224
- #ifdef __OBJC__
225
- // iOS: Convert std::string to NSString* for MMKVCore pod compatibility
226
- bool hasValue = instance->getBytes(@(keyName.c_str()), buffer);
227
- #else
228
- // Android/other platforms: Use std::string directly (converts to std::string_view)
229
- bool hasValue = instance->getBytes(keyName, buffer);
230
- #endif
231
- if (!hasValue) [[unlikely]] {
232
- return jsi::Value::undefined();
233
- }
234
- auto mutableData = std::make_shared<ManagedMMBuffer>(std::move(buffer));
235
- return jsi::ArrayBuffer(runtime, mutableData);
236
- });
237
- }
238
-
239
- if (propName == "contains") {
240
- // MMKV.contains(key: string)
241
- return jsi::Function::createFromHostFunction(
242
- runtime, jsi::PropNameID::forAscii(runtime, propName),
243
- 1, // key
244
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
245
- size_t count) -> jsi::Value {
246
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
247
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
248
- }
249
-
250
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
251
- bool containsKey = instance->containsKey(keyName);
252
- return jsi::Value(containsKey);
253
- });
254
- }
255
-
256
- if (propName == "delete") {
257
- // MMKV.delete(key: string)
258
- return jsi::Function::createFromHostFunction(
259
- runtime, jsi::PropNameID::forAscii(runtime, propName),
260
- 1, // key
261
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
262
- size_t count) -> jsi::Value {
263
- if (count != 1 || !arguments[0].isString()) [[unlikely]] {
264
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
265
- }
266
-
267
- std::string keyName = arguments[0].getString(runtime).utf8(runtime);
268
- instance->removeValueForKey(keyName);
269
- return jsi::Value::undefined();
270
- });
271
- }
272
-
273
- if (propName == "getAllKeys") {
274
- // MMKV.getAllKeys()
275
- return jsi::Function::createFromHostFunction(
276
- runtime, jsi::PropNameID::forAscii(runtime, propName), 0,
277
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
278
- size_t count) -> jsi::Value {
279
- std::vector<std::string> keys = instance->allKeys();
280
- jsi::Array array(runtime, keys.size());
281
- for (int i = 0; i < keys.size(); i++) {
282
- array.setValueAtIndex(runtime, i, keys[i]);
283
- }
284
- return array;
285
- });
286
- }
287
-
288
- if (propName == "clearAll") {
289
- // MMKV.clearAll()
290
- return jsi::Function::createFromHostFunction(
291
- runtime, jsi::PropNameID::forAscii(runtime, propName), 0,
292
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
293
- size_t count) -> jsi::Value {
294
- instance->clearAll();
295
- return jsi::Value::undefined();
296
- });
297
- }
298
-
299
- if (propName == "recrypt") {
300
- // MMKV.recrypt(encryptionKey)
301
- return jsi::Function::createFromHostFunction(
302
- runtime, jsi::PropNameID::forAscii(runtime, propName),
303
- 1, // encryptionKey
304
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
305
- size_t count) -> jsi::Value {
306
- if (count != 1) [[unlikely]] {
307
- throw jsi::JSError(runtime, "Expected 1 argument (encryptionKey), but received " +
308
- std::to_string(count) + "!");
309
- }
310
-
311
- bool successful = false;
312
- if (arguments[0].isUndefined()) {
313
- // reset encryption key to "no encryption"
314
- successful = instance->reKey(std::string());
315
- } else if (arguments[0].isString()) {
316
- // reKey(..) with new encryption-key
317
- std::string encryptionKey = arguments[0].getString(runtime).utf8(runtime);
318
- successful = instance->reKey(encryptionKey);
319
- } else [[unlikely]] {
320
- // Invalid argument (maybe object?)
321
- throw jsi::JSError(
322
- runtime,
323
- "First argument ('encryptionKey') has to be of type string (or undefined)!");
324
- }
325
-
326
- if (!successful) [[unlikely]] {
327
- throw jsi::JSError(runtime, "Failed to recrypt MMKV instance!");
328
- }
329
-
330
- return jsi::Value::undefined();
331
- });
332
- }
333
-
334
- if (propName == "trim") {
335
- // MMKV.trim()
336
- return jsi::Function::createFromHostFunction(
337
- runtime, jsi::PropNameID::forAscii(runtime, propName), 0,
338
- [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
339
- size_t count) -> jsi::Value {
340
- instance->clearMemoryCache();
341
- instance->trim();
342
-
343
- return jsi::Value::undefined();
344
- });
345
- }
346
-
347
- if (propName == "size") {
348
- // MMKV.size
349
- size_t size = instance->actualSize();
350
- return jsi::Value(static_cast<int>(size));
351
- }
352
-
353
- if (propName == "isReadOnly") {
354
- // MMKV.isReadOnly
355
- bool isReadOnly = instance->isReadOnly();
356
- return jsi::Value(isReadOnly);
357
- }
358
-
359
- return jsi::Value::undefined();
360
- }