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
@@ -1,95 +0,0 @@
1
- import { NativeModules, Platform } from 'react-native';
2
-
3
- declare global {
4
- // A react-native internal from TurboModuleRegistry.js
5
- var __turboModuleProxy: unknown | undefined;
6
- }
7
-
8
- const BULLET_POINT = '\n* ';
9
-
10
- function messageWithSuggestions(
11
- message: string,
12
- suggestions: string[]
13
- ): string {
14
- return message + BULLET_POINT + suggestions.join(BULLET_POINT);
15
- }
16
-
17
- function getFrameworkType(): 'react-native' | 'expo' | 'expo-go' {
18
- // check if Expo
19
- const ExpoConstants =
20
- NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
21
- if (ExpoConstants != null) {
22
- if (ExpoConstants.appOwnership === 'expo') {
23
- // We're running Expo Go
24
- return 'expo-go';
25
- } else {
26
- // We're running Expo bare / standalone
27
- return 'expo';
28
- }
29
- }
30
- return 'react-native';
31
- }
32
-
33
- export class ModuleNotFoundError extends Error {
34
- constructor(cause?: unknown) {
35
- // TurboModule not found, something went wrong!
36
- if (global.__turboModuleProxy == null) {
37
- // TurboModules are not available/new arch is not enabled.
38
- // react-native-mmkv 3.x.x requires new arch (react-native >0.74)
39
- // react-native-mmkv 2.x.x works on old arch (react-native <0.74)
40
- const message =
41
- 'Failed to create a new MMKV instance: react-native-mmkv 3.x.x requires TurboModules, but the new architecture is not enabled!';
42
- const suggestions: string[] = [];
43
- suggestions.push(
44
- 'Downgrade to react-native-mmkv 2.x.x if you want to stay on the old architecture.'
45
- );
46
- suggestions.push(
47
- 'Enable the new architecture in your app to use react-native-mmkv 3.x.x. (See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md)'
48
- );
49
- const error = messageWithSuggestions(message, suggestions);
50
- super(error, { cause: cause });
51
- return;
52
- }
53
-
54
- const framework = getFrameworkType();
55
- if (framework === 'expo-go') {
56
- super(
57
- 'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
58
- );
59
- return;
60
- }
61
-
62
- const message =
63
- 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
64
- const suggestions: string[] = [];
65
- suggestions.push(
66
- 'Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)'
67
- );
68
- suggestions.push(
69
- 'Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the react-native-mmkv specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md'
70
- );
71
- suggestions.push(
72
- 'Make sure you are using react-native 0.74.0 or higher, because react-native-mmkv is a C++ TurboModule.'
73
- );
74
- suggestions.push('Make sure you rebuilt the app.');
75
- if (framework === 'expo') {
76
- suggestions.push('Make sure you ran `expo prebuild`.');
77
- }
78
- switch (Platform.OS) {
79
- case 'ios':
80
- case 'macos':
81
- suggestions.push(
82
- 'Make sure you ran `pod install` in the ios/ directory.'
83
- );
84
- break;
85
- case 'android':
86
- suggestions.push('Make sure gradle is synced.');
87
- break;
88
- default:
89
- throw new Error(`MMKV is not supported on ${Platform.OS}!`);
90
- }
91
-
92
- const error = messageWithSuggestions(message, suggestions);
93
- super(error, { cause: cause });
94
- }
95
- }
package/src/NativeMmkv.ts DELETED
@@ -1,118 +0,0 @@
1
- import type { TurboModule } from 'react-native';
2
- import { TurboModuleRegistry } from 'react-native';
3
- import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
4
- import { ModuleNotFoundError } from './ModuleNotFoundError';
5
- import { getMMKVPlatformContextTurboModule } from './NativeMmkvPlatformContext';
6
-
7
- /**
8
- * IMPORTANT: These types are also in the Types.ts file.
9
- * Due to how react-native-codegen works these are required here as the spec types can not be separated from spec.
10
- * We also need the types separate to allow bypassing importing turbo module registry in web
11
- */
12
- /**
13
- * Configures the mode of the MMKV instance.
14
- */
15
- export enum Mode {
16
- /**
17
- * The MMKV instance is only used from a single process (this app).
18
- */
19
- SINGLE_PROCESS,
20
- /**
21
- * The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
22
- */
23
- MULTI_PROCESS,
24
- }
25
-
26
- /**
27
- * Used for configuration of a single MMKV instance.
28
- */
29
- export interface Configuration {
30
- /**
31
- * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
32
- *
33
- * @example
34
- * ```ts
35
- * const userStorage = new MMKV({ id: `user-${userId}-storage` })
36
- * const globalStorage = new MMKV({ id: 'global-app-storage' })
37
- * ```
38
- *
39
- * @default 'mmkv.default'
40
- */
41
- id: string;
42
- /**
43
- * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
44
-
45
- * @example
46
- * ```ts
47
- * const temporaryStorage = new MMKV({ path: '/tmp/' })
48
- * ```
49
- *
50
- * @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
51
- * App Groups allow you to share MMKV storage between apps, widgets and extensions within the same AppGroup bundle.
52
- * For more information, see [the `Configuration` section](https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration).
53
- *
54
- * @default undefined
55
- */
56
- path?: string;
57
- /**
58
- * 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.
59
- *
60
- * Encryption keys can have a maximum length of 16 bytes.
61
- *
62
- * @example
63
- * ```ts
64
- * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
65
- * ```
66
- *
67
- * @default undefined
68
- */
69
- encryptionKey?: string;
70
- /**
71
- * Configure the processing mode for MMKV.
72
- * - `SINGLE_PROCESS`: The MMKV instance is only used from a single process (this app).
73
- * - `MULTI_PROCESS`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
74
- *
75
- * @default SINGLE_PROCESS
76
- */
77
- mode?: Mode;
78
- /**
79
- * If `true`, the MMKV instance can only read from the storage, but not write to it.
80
- */
81
- readOnly?: boolean;
82
- }
83
-
84
- export interface Spec extends TurboModule {
85
- /**
86
- * Initialize MMKV with the given base storage directory.
87
- * This should be the documents directory by default.
88
- */
89
- initialize(basePath: string): boolean;
90
- /**
91
- * Create a new instance of MMKV.
92
- * The returned {@linkcode UnsafeObject} is a `jsi::HostObject`.
93
- */
94
- createMMKV(configuration: Configuration): UnsafeObject;
95
- }
96
-
97
- let mmkvModule: Spec | null;
98
-
99
- export function getMMKVTurboModule(): Spec {
100
- try {
101
- if (mmkvModule == null) {
102
- // 1. Load MMKV TurboModule
103
- mmkvModule = TurboModuleRegistry.getEnforcing<Spec>('MmkvCxx');
104
-
105
- // 2. Get the PlatformContext TurboModule as well
106
- const platformContext = getMMKVPlatformContextTurboModule();
107
-
108
- // 3. Initialize it with the documents directory from platform-specific context
109
- const basePath = platformContext.getBaseDirectory();
110
- mmkvModule.initialize(basePath);
111
- }
112
-
113
- return mmkvModule;
114
- } catch (cause) {
115
- // TurboModule could not be found!
116
- throw new ModuleNotFoundError(cause);
117
- }
118
- }
@@ -1,38 +0,0 @@
1
- import type { TurboModule } from 'react-native';
2
- import { TurboModuleRegistry } from 'react-native';
3
- import { ModuleNotFoundError } from './ModuleNotFoundError';
4
-
5
- export interface Spec extends TurboModule {
6
- /**
7
- * Gets the base directory of the documents storage
8
- */
9
- getBaseDirectory(): string;
10
- /**
11
- * Get the App Group directory if it exists, or `undefined` otherwise.
12
- *
13
- * The App Group directory will be used instead of a custom path to use the same
14
- * MMKV instance between the iOS app, Widgets, and other companion apps.
15
- *
16
- * To set an App Group, add a `AppGroup` field to `Info.plist`
17
- *
18
- * @platform ios
19
- */
20
- getAppGroupDirectory(): string | undefined;
21
- }
22
-
23
- let mmkvPlatformModule: Spec | null;
24
-
25
- export function getMMKVPlatformContextTurboModule(): Spec {
26
- try {
27
- if (mmkvPlatformModule == null) {
28
- // 1. Get the TurboModule
29
- mmkvPlatformModule = TurboModuleRegistry.getEnforcing<Spec>(
30
- 'MmkvPlatformContext'
31
- );
32
- }
33
- return mmkvPlatformModule;
34
- } catch (e) {
35
- // TurboModule could not be found!
36
- throw new ModuleNotFoundError(e);
37
- }
38
- }
package/src/Types.ts DELETED
@@ -1,178 +0,0 @@
1
- /**
2
- * IMPORTANT: Some of these types are also in the NativeMmkv.ts file.
3
- * Due to how react-native-codegen works these are required here as the spec types can not be separated from spec.
4
- * We also need the types separate to allow bypassing importing turbo module registry in web
5
- */
6
-
7
- /**
8
- * Configures the mode of the MMKV instance.
9
- */
10
- export enum Mode {
11
- /**
12
- * The MMKV instance is only used from a single process (this app).
13
- */
14
- SINGLE_PROCESS,
15
- /**
16
- * The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
17
- */
18
- MULTI_PROCESS,
19
- }
20
-
21
- /**
22
- * Used for configuration of a single MMKV instance.
23
- */
24
- export interface Configuration {
25
- /**
26
- * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
27
- *
28
- * @example
29
- * ```ts
30
- * const userStorage = new MMKV({ id: `user-${userId}-storage` })
31
- * const globalStorage = new MMKV({ id: 'global-app-storage' })
32
- * ```
33
- *
34
- * @default 'mmkv.default'
35
- */
36
- id: string;
37
- /**
38
- * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
39
-
40
- * @example
41
- * ```ts
42
- * const temporaryStorage = new MMKV({ path: '/tmp/' })
43
- * ```
44
- *
45
- * @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
46
- * App Groups allow you to share MMKV storage between apps, widgets and extensions within the same AppGroup bundle.
47
- * For more information, see [the `Configuration` section](https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration).
48
- *
49
- * @default undefined
50
- */
51
- path?: string;
52
- /**
53
- * 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.
54
- *
55
- * Encryption keys can have a maximum length of 16 bytes.
56
- *
57
- * @example
58
- * ```ts
59
- * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
60
- * ```
61
- *
62
- * @default undefined
63
- */
64
- encryptionKey?: string;
65
- /**
66
- * Configure the processing mode for MMKV.
67
- * - `SINGLE_PROCESS`: The MMKV instance is only used from a single process (this app).
68
- * - `MULTI_PROCESS`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
69
- *
70
- * @default SINGLE_PROCESS
71
- */
72
- mode?: Mode;
73
- /**
74
- * If `true`, the MMKV instance can only read from the storage, but not write to it.
75
- * This is more efficient if you do not need to write to it.
76
- * @default false
77
- */
78
- readOnly?: boolean;
79
- }
80
-
81
- /**
82
- * Represents a single MMKV instance.
83
- */
84
- export interface NativeMMKV {
85
- /**
86
- * Set a value for the given `key`.
87
- *
88
- * @throws an Error if the value cannot be set.
89
- */
90
- set: (key: string, value: boolean | string | number | ArrayBuffer) => void;
91
- /**
92
- * Get the boolean value for the given `key`, or `undefined` if it does not exist.
93
- *
94
- * @default undefined
95
- */
96
- getBoolean: (key: string) => boolean | undefined;
97
- /**
98
- * Get the string value for the given `key`, or `undefined` if it does not exist.
99
- *
100
- * @default undefined
101
- */
102
- getString: (key: string) => string | undefined;
103
- /**
104
- * Get the number value for the given `key`, or `undefined` if it does not exist.
105
- *
106
- * @default undefined
107
- */
108
- getNumber: (key: string) => number | undefined;
109
- /**
110
- * Get a raw buffer of unsigned 8-bit (0-255) data.
111
- *
112
- * @default undefined
113
- */
114
- getBuffer: (key: string) => ArrayBufferLike | undefined;
115
- /**
116
- * Checks whether the given `key` is being stored in this MMKV instance.
117
- */
118
- contains: (key: string) => boolean;
119
- /**
120
- * Delete the given `key`.
121
- */
122
- delete: (key: string) => void;
123
- /**
124
- * Get all keys.
125
- *
126
- * @default []
127
- */
128
- getAllKeys: () => string[];
129
- /**
130
- * Delete all keys.
131
- */
132
- clearAll: () => void;
133
- /**
134
- * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
135
- *
136
- * To remove encryption, pass `undefined` as a key.
137
- *
138
- * Encryption keys can have a maximum length of 16 bytes.
139
- *
140
- * @throws an Error if the instance cannot be recrypted.
141
- */
142
- recrypt: (key: string | undefined) => void;
143
- /**
144
- * Trims the storage space and clears memory cache.
145
- *
146
- * Since MMKV does not resize itself after deleting keys, you can call `trim()`
147
- * after deleting a bunch of keys to manually trim the memory- and
148
- * disk-file to reduce storage and memory usage.
149
- *
150
- * In most applications, this is not needed at all.
151
- */
152
- trim(): void;
153
- /**
154
- * Get the current total size of the storage, in bytes.
155
- */
156
- readonly size: number;
157
- /**
158
- * Returns whether this instance is in read-only mode or not.
159
- * If this is `true`, you can only use "get"-functions.
160
- */
161
- readonly isReadOnly: boolean;
162
- }
163
-
164
- export interface Listener {
165
- remove: () => void;
166
- }
167
-
168
- export interface MMKVInterface extends NativeMMKV {
169
- /**
170
- * Adds a value changed listener. The Listener will be called whenever any value
171
- * in this storage instance changes (set or delete).
172
- *
173
- * To unsubscribe from value changes, call `remove()` on the Listener.
174
- */
175
- addOnValueChangedListener: (
176
- onValueChanged: (key: string) => void
177
- ) => Listener;
178
- }
@@ -1,38 +0,0 @@
1
- import type { NativeMMKV } from './Types';
2
-
3
- /* Mock MMKV instance for use in tests */
4
- export const createMockMMKV = (): NativeMMKV => {
5
- const storage = new Map<string, string | boolean | number | ArrayBuffer>();
6
-
7
- return {
8
- clearAll: () => storage.clear(),
9
- delete: (key) => storage.delete(key),
10
- set: (key, value) => storage.set(key, value),
11
- getString: (key) => {
12
- const result = storage.get(key);
13
- return typeof result === 'string' ? result : undefined;
14
- },
15
- getNumber: (key) => {
16
- const result = storage.get(key);
17
- return typeof result === 'number' ? result : undefined;
18
- },
19
- getBoolean: (key) => {
20
- const result = storage.get(key);
21
- return typeof result === 'boolean' ? result : undefined;
22
- },
23
- getBuffer: (key) => {
24
- const result = storage.get(key);
25
- return result instanceof ArrayBuffer ? result : undefined;
26
- },
27
- getAllKeys: () => Array.from(storage.keys()),
28
- contains: (key) => storage.has(key),
29
- recrypt: () => {
30
- console.warn('Encryption is not supported in mocked MMKV instances!');
31
- },
32
- size: 0,
33
- isReadOnly: false,
34
- trim: () => {
35
- // no-op
36
- },
37
- };
38
- };
package/src/createMMKV.ts DELETED
@@ -1,42 +0,0 @@
1
- import { Platform } from 'react-native';
2
- import { getMMKVTurboModule } from './NativeMmkv';
3
- import { type Configuration, Mode, type NativeMMKV } from './Types';
4
- import { getMMKVPlatformContextTurboModule } from './NativeMmkvPlatformContext';
5
-
6
- export const createMMKV = (config: Configuration): NativeMMKV => {
7
- const module = getMMKVTurboModule();
8
-
9
- if (Platform.OS === 'ios') {
10
- if (config.path == null) {
11
- try {
12
- // If no `path` was supplied, we check if an `AppGroup` was set in Info.plist
13
- const appGroupDirectory =
14
- getMMKVPlatformContextTurboModule().getAppGroupDirectory();
15
- if (appGroupDirectory != null) {
16
- // If we have an `AppGroup` in Info.plist, use that as a path.
17
- config.path = appGroupDirectory;
18
- }
19
- } catch (e) {
20
- // We cannot throw errors here because it is a sync C++ TurboModule func. idk why.
21
- console.error(e);
22
- }
23
- }
24
- }
25
-
26
- if (typeof config.mode === 'number') {
27
- // Code-gen expects enums to be strings. In TS, they might be numbers tho.
28
- // This sucks, so we need a workaround.
29
- // @ts-expect-error the native side actually expects a string.
30
- config.mode = Mode[config.mode];
31
- }
32
-
33
- const instance = module.createMMKV(config);
34
- if (__DEV__) {
35
- if (typeof instance !== 'object' || instance == null) {
36
- throw new Error(
37
- 'Failed to create MMKV instance - an unknown object was returned by createMMKV(..)!'
38
- );
39
- }
40
- }
41
- return instance as NativeMMKV;
42
- };