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
@@ -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
- };
package/src/hooks.ts DELETED
@@ -1,247 +0,0 @@
1
- import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
2
- import { MMKV } from './MMKV';
3
- import type { Configuration } from './Types';
4
-
5
- function isConfigurationEqual(
6
- left?: Configuration,
7
- right?: Configuration
8
- ): boolean {
9
- if (left == null || right == null) return left == null && right == null;
10
-
11
- return (
12
- left.encryptionKey === right.encryptionKey &&
13
- left.id === right.id &&
14
- left.path === right.path &&
15
- left.mode === right.mode
16
- );
17
- }
18
-
19
- let defaultInstance: MMKV | null = null;
20
- function getDefaultInstance(): MMKV {
21
- if (defaultInstance == null) {
22
- defaultInstance = new MMKV();
23
- }
24
- return defaultInstance;
25
- }
26
-
27
- /**
28
- * Use the default, shared MMKV instance.
29
- */
30
- export function useMMKV(): MMKV;
31
- /**
32
- * Use a custom MMKV instance with the given configuration.
33
- * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
34
- */
35
- export function useMMKV(configuration: Configuration): MMKV;
36
- export function useMMKV(configuration?: Configuration): MMKV {
37
- const instance = useRef<MMKV>(undefined);
38
- const lastConfiguration = useRef<Configuration>(undefined);
39
-
40
- if (configuration == null) return getDefaultInstance();
41
-
42
- if (
43
- instance.current == null ||
44
- !isConfigurationEqual(lastConfiguration.current, configuration)
45
- ) {
46
- lastConfiguration.current = configuration;
47
- instance.current = new MMKV(configuration);
48
- }
49
-
50
- return instance.current;
51
- }
52
-
53
- function createMMKVHook<
54
- T extends (boolean | number | string | ArrayBufferLike) | undefined,
55
- TSet extends T | undefined,
56
- TSetAction extends TSet | ((current: T) => TSet),
57
- >(getter: (instance: MMKV, key: string) => T) {
58
- return (
59
- key: string,
60
- instance?: MMKV
61
- ): [value: T, setValue: (value: TSetAction) => void] => {
62
- const mmkv = instance ?? getDefaultInstance();
63
-
64
- const [bump, setBump] = useState(0);
65
- const value = useMemo(() => {
66
- // bump is here as an additional outside dependency, so this useMemo
67
- // re-computes the value each time bump changes, effectively acting as a hint
68
- // that the outside value (storage) has changed. setting bump refreshes this value.
69
- bump;
70
- return getter(mmkv, key);
71
- }, [mmkv, key, bump]);
72
-
73
- // update value by user set
74
- const set = useCallback(
75
- (v: TSetAction) => {
76
- const newValue = typeof v === 'function' ? v(getter(mmkv, key)) : v;
77
- switch (typeof newValue) {
78
- case 'number':
79
- case 'string':
80
- case 'boolean':
81
- mmkv.set(key, newValue);
82
- break;
83
- case 'undefined':
84
- mmkv.delete(key);
85
- break;
86
- case 'object':
87
- if (newValue instanceof ArrayBuffer) {
88
- mmkv.set(key, newValue);
89
- break;
90
- } else {
91
- throw new Error(
92
- `MMKV: Type object (${newValue}) is not supported!`
93
- );
94
- }
95
- default:
96
- throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
97
- }
98
- },
99
- [key, mmkv]
100
- );
101
-
102
- // update value if it changes somewhere else (second hook, same key)
103
- useEffect(() => {
104
- const listener = mmkv.addOnValueChangedListener((changedKey) => {
105
- if (changedKey === key) {
106
- setBump((b) => b + 1);
107
- }
108
- });
109
- return () => listener.remove();
110
- }, [key, mmkv]);
111
-
112
- return [value, set];
113
- };
114
- }
115
-
116
- /**
117
- * Use the string value of the given `key` from the given MMKV storage instance.
118
- *
119
- * If no instance is provided, a shared default instance will be used.
120
- *
121
- * @example
122
- * ```ts
123
- * const [username, setUsername] = useMMKVString("user.name")
124
- * ```
125
- */
126
- export const useMMKVString = createMMKVHook((instance, key) =>
127
- instance.getString(key)
128
- );
129
-
130
- /**
131
- * Use the number value of the given `key` from the given MMKV storage instance.
132
- *
133
- * If no instance is provided, a shared default instance will be used.
134
- *
135
- * @example
136
- * ```ts
137
- * const [age, setAge] = useMMKVNumber("user.age")
138
- * ```
139
- */
140
- export const useMMKVNumber = createMMKVHook((instance, key) =>
141
- instance.getNumber(key)
142
- );
143
- /**
144
- * Use the boolean value of the given `key` from the given MMKV storage instance.
145
- *
146
- * If no instance is provided, a shared default instance will be used.
147
- *
148
- * @example
149
- * ```ts
150
- * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
151
- * ```
152
- */
153
- export const useMMKVBoolean = createMMKVHook((instance, key) =>
154
- instance.getBoolean(key)
155
- );
156
- /**
157
- * Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
158
- *
159
- * If no instance is provided, a shared default instance will be used.
160
- *
161
- * @example
162
- * ```ts
163
- * const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
164
- * ```
165
- */
166
- export const useMMKVBuffer = createMMKVHook((instance, key) =>
167
- instance.getBuffer(key)
168
- );
169
- /**
170
- * Use an object value of the given `key` from the given MMKV storage instance.
171
- *
172
- * If no instance is provided, a shared default instance will be used.
173
- *
174
- * The object will be serialized using `JSON`.
175
- *
176
- * @example
177
- * ```ts
178
- * const [user, setUser] = useMMKVObject<User>("user")
179
- * ```
180
- */
181
- export function useMMKVObject<T>(
182
- key: string,
183
- instance?: MMKV
184
- ): [
185
- value: T | undefined,
186
- setValue: (
187
- value: T | undefined | ((prevValue: T | undefined) => T | undefined)
188
- ) => void,
189
- ] {
190
- const [json, setJson] = useMMKVString(key, instance);
191
-
192
- const value = useMemo(() => {
193
- if (json == null) return undefined;
194
- return JSON.parse(json) as T;
195
- }, [json]);
196
-
197
- const setValue = useCallback(
198
- (v: (T | undefined) | ((prev: T | undefined) => T | undefined)) => {
199
- if (v instanceof Function) {
200
- setJson((currentJson) => {
201
- const currentValue =
202
- currentJson != null ? (JSON.parse(currentJson) as T) : undefined;
203
- const newValue = v(currentValue);
204
- // Store the Object as a serialized Value or clear the value
205
- return newValue != null ? JSON.stringify(newValue) : undefined;
206
- });
207
- } else {
208
- // Store the Object as a serialized Value or clear the value
209
- const newValue = v != null ? JSON.stringify(v) : undefined;
210
- setJson(newValue);
211
- }
212
- },
213
- [setJson]
214
- );
215
-
216
- return [value, setValue];
217
- }
218
-
219
- /**
220
- * Listen for changes in the given MMKV storage instance.
221
- * If no instance is passed, the default instance will be used.
222
- * @param valueChangedListener The function to call whenever a value inside the storage instance changes
223
- * @param instance The instance to listen to changes to (or the default instance)
224
- *
225
- * @example
226
- * ```ts
227
- * useMMKVListener((key) => {
228
- * console.log(`Value for "${key}" changed!`)
229
- * })
230
- * ```
231
- */
232
- export function useMMKVListener(
233
- valueChangedListener: (key: string) => void,
234
- instance?: MMKV
235
- ): void {
236
- const ref = useRef(valueChangedListener);
237
- ref.current = valueChangedListener;
238
-
239
- const mmkv = instance ?? getDefaultInstance();
240
-
241
- useEffect(() => {
242
- const listener = mmkv.addOnValueChangedListener((changedKey) => {
243
- ref.current(changedKey);
244
- });
245
- return () => listener.remove();
246
- }, [mmkv]);
247
- }