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,172 +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
- * Configures the mode of the MMKV instance.
8
- */
9
- export declare enum Mode {
10
- /**
11
- * The MMKV instance is only used from a single process (this app).
12
- */
13
- SINGLE_PROCESS = 0,
14
- /**
15
- * The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
16
- */
17
- MULTI_PROCESS = 1
18
- }
19
- /**
20
- * Used for configuration of a single MMKV instance.
21
- */
22
- export interface Configuration {
23
- /**
24
- * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
25
- *
26
- * @example
27
- * ```ts
28
- * const userStorage = new MMKV({ id: `user-${userId}-storage` })
29
- * const globalStorage = new MMKV({ id: 'global-app-storage' })
30
- * ```
31
- *
32
- * @default 'mmkv.default'
33
- */
34
- id: string;
35
- /**
36
- * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
37
-
38
- * @example
39
- * ```ts
40
- * const temporaryStorage = new MMKV({ path: '/tmp/' })
41
- * ```
42
- *
43
- * @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
44
- * App Groups allow you to share MMKV storage between apps, widgets and extensions within the same AppGroup bundle.
45
- * For more information, see [the `Configuration` section](https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration).
46
- *
47
- * @default undefined
48
- */
49
- path?: string;
50
- /**
51
- * 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.
52
- *
53
- * Encryption keys can have a maximum length of 16 bytes.
54
- *
55
- * @example
56
- * ```ts
57
- * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
58
- * ```
59
- *
60
- * @default undefined
61
- */
62
- encryptionKey?: string;
63
- /**
64
- * Configure the processing mode for MMKV.
65
- * - `SINGLE_PROCESS`: The MMKV instance is only used from a single process (this app).
66
- * - `MULTI_PROCESS`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
67
- *
68
- * @default SINGLE_PROCESS
69
- */
70
- mode?: Mode;
71
- /**
72
- * If `true`, the MMKV instance can only read from the storage, but not write to it.
73
- * This is more efficient if you do not need to write to it.
74
- * @default false
75
- */
76
- readOnly?: boolean;
77
- }
78
- /**
79
- * Represents a single MMKV instance.
80
- */
81
- export interface NativeMMKV {
82
- /**
83
- * Set a value for the given `key`.
84
- *
85
- * @throws an Error if the value cannot be set.
86
- */
87
- set: (key: string, value: boolean | string | number | ArrayBuffer) => void;
88
- /**
89
- * Get the boolean value for the given `key`, or `undefined` if it does not exist.
90
- *
91
- * @default undefined
92
- */
93
- getBoolean: (key: string) => boolean | undefined;
94
- /**
95
- * Get the string value for the given `key`, or `undefined` if it does not exist.
96
- *
97
- * @default undefined
98
- */
99
- getString: (key: string) => string | undefined;
100
- /**
101
- * Get the number value for the given `key`, or `undefined` if it does not exist.
102
- *
103
- * @default undefined
104
- */
105
- getNumber: (key: string) => number | undefined;
106
- /**
107
- * Get a raw buffer of unsigned 8-bit (0-255) data.
108
- *
109
- * @default undefined
110
- */
111
- getBuffer: (key: string) => ArrayBufferLike | undefined;
112
- /**
113
- * Checks whether the given `key` is being stored in this MMKV instance.
114
- */
115
- contains: (key: string) => boolean;
116
- /**
117
- * Delete the given `key`.
118
- */
119
- delete: (key: string) => void;
120
- /**
121
- * Get all keys.
122
- *
123
- * @default []
124
- */
125
- getAllKeys: () => string[];
126
- /**
127
- * Delete all keys.
128
- */
129
- clearAll: () => void;
130
- /**
131
- * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
132
- *
133
- * To remove encryption, pass `undefined` as a key.
134
- *
135
- * Encryption keys can have a maximum length of 16 bytes.
136
- *
137
- * @throws an Error if the instance cannot be recrypted.
138
- */
139
- recrypt: (key: string | undefined) => void;
140
- /**
141
- * Trims the storage space and clears memory cache.
142
- *
143
- * Since MMKV does not resize itself after deleting keys, you can call `trim()`
144
- * after deleting a bunch of keys to manually trim the memory- and
145
- * disk-file to reduce storage and memory usage.
146
- *
147
- * In most applications, this is not needed at all.
148
- */
149
- trim(): void;
150
- /**
151
- * Get the current total size of the storage, in bytes.
152
- */
153
- readonly size: number;
154
- /**
155
- * Returns whether this instance is in read-only mode or not.
156
- * If this is `true`, you can only use "get"-functions.
157
- */
158
- readonly isReadOnly: boolean;
159
- }
160
- export interface Listener {
161
- remove: () => void;
162
- }
163
- export interface MMKVInterface extends NativeMMKV {
164
- /**
165
- * Adds a value changed listener. The Listener will be called whenever any value
166
- * in this storage instance changes (set or delete).
167
- *
168
- * To unsubscribe from value changes, call `remove()` on the Listener.
169
- */
170
- addOnValueChangedListener: (onValueChanged: (key: string) => void) => Listener;
171
- }
172
- //# sourceMappingURL=Types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Types.d.ts","sourceRoot":"","sources":["../../../src/Types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,oBAAY,IAAI;IACd;;OAEG;IACH,cAAc,IAAA;IACd;;OAEG;IACH,aAAa,IAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;;OAUG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;;;;;;;;;;;;OAaG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;OAWG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;;;OAIG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,KAAK,IAAI,CAAC;IAC3E;;;;OAIG;IACH,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,GAAG,SAAS,CAAC;IACjD;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAC/C;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IAC/C;;;;OAIG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,eAAe,GAAG,SAAS,CAAC;IACxD;;OAEG;IACH,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IACnC;;OAEG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B;;;;OAIG;IACH,UAAU,EAAE,MAAM,MAAM,EAAE,CAAC;IAC3B;;OAEG;IACH,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB;;;;;;;;OAQG;IACH,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAC;IAC3C;;;;;;;;OAQG;IACH,IAAI,IAAI,IAAI,CAAC;IACb;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED,MAAM,WAAW,aAAc,SAAQ,UAAU;IAC/C;;;;;OAKG;IACH,yBAAyB,EAAE,CACzB,cAAc,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,KAClC,QAAQ,CAAC;CACf"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=hooks.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.test.d.ts","sourceRoot":"","sources":["../../../../src/__tests__/hooks.test.tsx"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- import { type Configuration, type NativeMMKV } from './Types';
2
- export declare const createMMKV: (config: Configuration) => NativeMMKV;
3
- //# sourceMappingURL=createMMKV.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMMKV.d.ts","sourceRoot":"","sources":["../../../src/createMMKV.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,aAAa,EAAQ,KAAK,UAAU,EAAE,MAAM,SAAS,CAAC;AAGpE,eAAO,MAAM,UAAU,GAAI,QAAQ,aAAa,KAAG,UAoClD,CAAC"}
@@ -1,3 +0,0 @@
1
- import type { NativeMMKV } from './Types';
2
- export declare const createMockMMKV: () => NativeMMKV;
3
- //# sourceMappingURL=createMMKV.mock.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMMKV.mock.d.ts","sourceRoot":"","sources":["../../../src/createMMKV.mock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C,eAAO,MAAM,cAAc,QAAO,UAkCjC,CAAC"}
@@ -1,3 +0,0 @@
1
- import type { Configuration, NativeMMKV } from './Types';
2
- export declare const createMMKV: (config: Configuration) => NativeMMKV;
3
- //# sourceMappingURL=createMMKV.web.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMMKV.web.d.ts","sourceRoot":"","sources":["../../../src/createMMKV.web.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAoBzD,eAAO,MAAM,UAAU,GAAI,QAAQ,aAAa,KAAG,UAyGlD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTextEncoder.d.ts","sourceRoot":"","sources":["../../../src/createTextEncoder.ts"],"names":[],"mappings":"AACA,wBAAgB,iBAAiB,IAAI,WAAW,CAc/C"}
@@ -1,86 +0,0 @@
1
- import { MMKV } from './MMKV';
2
- import type { Configuration } from './Types';
3
- /**
4
- * Use the default, shared MMKV instance.
5
- */
6
- export declare function useMMKV(): MMKV;
7
- /**
8
- * Use a custom MMKV instance with the given configuration.
9
- * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
10
- */
11
- export declare function useMMKV(configuration: Configuration): MMKV;
12
- /**
13
- * Use the string value of the given `key` from the given MMKV storage instance.
14
- *
15
- * If no instance is provided, a shared default instance will be used.
16
- *
17
- * @example
18
- * ```ts
19
- * const [username, setUsername] = useMMKVString("user.name")
20
- * ```
21
- */
22
- export declare const useMMKVString: (key: string, instance?: MMKV) => [value: string | undefined, setValue: (value: string | ((current: string | undefined) => string | undefined) | undefined) => void];
23
- /**
24
- * Use the number value of the given `key` from the given MMKV storage instance.
25
- *
26
- * If no instance is provided, a shared default instance will be used.
27
- *
28
- * @example
29
- * ```ts
30
- * const [age, setAge] = useMMKVNumber("user.age")
31
- * ```
32
- */
33
- export declare const useMMKVNumber: (key: string, instance?: MMKV) => [value: number | undefined, setValue: (value: number | ((current: number | undefined) => number | undefined) | undefined) => void];
34
- /**
35
- * Use the boolean value of the given `key` from the given MMKV storage instance.
36
- *
37
- * If no instance is provided, a shared default instance will be used.
38
- *
39
- * @example
40
- * ```ts
41
- * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
42
- * ```
43
- */
44
- export declare const useMMKVBoolean: (key: string, instance?: MMKV) => [value: boolean | undefined, setValue: (value: boolean | ((current: boolean | undefined) => boolean | undefined) | undefined) => void];
45
- /**
46
- * Use the buffer value (unsigned 8-bit (0-255)) of the given `key` from the given MMKV storage instance.
47
- *
48
- * If no instance is provided, a shared default instance will be used.
49
- *
50
- * @example
51
- * ```ts
52
- * const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
53
- * ```
54
- */
55
- export declare const useMMKVBuffer: (key: string, instance?: MMKV) => [value: ArrayBufferLike | undefined, setValue: (value: ArrayBufferLike | ((current: ArrayBufferLike | undefined) => ArrayBufferLike | undefined) | undefined) => void];
56
- /**
57
- * Use an object value of the given `key` from the given MMKV storage instance.
58
- *
59
- * If no instance is provided, a shared default instance will be used.
60
- *
61
- * The object will be serialized using `JSON`.
62
- *
63
- * @example
64
- * ```ts
65
- * const [user, setUser] = useMMKVObject<User>("user")
66
- * ```
67
- */
68
- export declare function useMMKVObject<T>(key: string, instance?: MMKV): [
69
- value: T | undefined,
70
- setValue: (value: T | undefined | ((prevValue: T | undefined) => T | undefined)) => void
71
- ];
72
- /**
73
- * Listen for changes in the given MMKV storage instance.
74
- * If no instance is passed, the default instance will be used.
75
- * @param valueChangedListener The function to call whenever a value inside the storage instance changes
76
- * @param instance The instance to listen to changes to (or the default instance)
77
- *
78
- * @example
79
- * ```ts
80
- * useMMKVListener((key) => {
81
- * console.log(`Value for "${key}" changed!`)
82
- * })
83
- * ```
84
- */
85
- export declare function useMMKVListener(valueChangedListener: (key: string) => void, instance?: MMKV): void;
86
- //# sourceMappingURL=hooks.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAwB7C;;GAEG;AACH,wBAAgB,OAAO,IAAI,IAAI,CAAC;AAChC;;;GAGG;AACH,wBAAgB,OAAO,CAAC,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;AAiF5D;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QAnEjB,MAAM,aACA,IAAI,kIAC8B,IAAI,CAmEpD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QAjFjB,MAAM,aACA,IAAI,kIAC8B,IAAI,CAiFpD,CAAC;AACF;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,QA9FlB,MAAM,aACA,IAAI,sIAC8B,IAAI,CA8FpD,CAAC;AACF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,QA3GjB,MAAM,aACA,IAAI,sKAC8B,IAAI,CA2GpD,CAAC;AACF;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,IAAI,GACd;IACD,KAAK,EAAE,CAAC,GAAG,SAAS;IACpB,QAAQ,EAAE,CACR,KAAK,EAAE,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,GAAG,SAAS,KAAK,CAAC,GAAG,SAAS,CAAC,KACjE,IAAI;CACV,CA4BA;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAC7B,oBAAoB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAC3C,QAAQ,CAAC,EAAE,IAAI,GACd,IAAI,CAYN"}
@@ -1,4 +0,0 @@
1
- export * from './MMKV';
2
- export * from './hooks';
3
- export { Mode, type Configuration } from './Types';
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,IAAI,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC"}
package/src/MMKV.ts DELETED
@@ -1,142 +0,0 @@
1
- import { createMMKV } from './createMMKV';
2
- import { createMockMMKV } from './createMMKV.mock';
3
- import { isTest } from './PlatformChecker';
4
- import type {
5
- Configuration,
6
- Listener,
7
- MMKVInterface,
8
- NativeMMKV,
9
- } from './Types';
10
- import { addMemoryWarningListener } from './MemoryWarningListener';
11
-
12
- const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
13
-
14
- /**
15
- * A single MMKV instance.
16
- */
17
- export class MMKV implements MMKVInterface {
18
- private nativeInstance: NativeMMKV;
19
- private functionCache: Partial<NativeMMKV>;
20
- private id: string;
21
-
22
- /**
23
- * Creates a new MMKV instance with the given Configuration.
24
- * If no custom `id` is supplied, `'mmkv.default'` will be used.
25
- */
26
- constructor(configuration: Configuration = { id: 'mmkv.default' }) {
27
- this.id = configuration.id;
28
- this.nativeInstance = isTest()
29
- ? createMockMMKV()
30
- : createMMKV(configuration);
31
- this.functionCache = {};
32
-
33
- addMemoryWarningListener(this);
34
- }
35
-
36
- private get onValueChangedListeners() {
37
- if (!onValueChangedListeners.has(this.id)) {
38
- onValueChangedListeners.set(this.id, []);
39
- }
40
- return onValueChangedListeners.get(this.id)!;
41
- }
42
-
43
- private getFunctionFromCache<T extends keyof NativeMMKV>(
44
- functionName: T
45
- ): NativeMMKV[T] {
46
- if (this.functionCache[functionName] == null) {
47
- this.functionCache[functionName] = this.nativeInstance[functionName];
48
- }
49
- return this.functionCache[functionName] as NativeMMKV[T];
50
- }
51
-
52
- private onValuesChanged(keys: string[]) {
53
- if (this.onValueChangedListeners.length === 0) return;
54
-
55
- for (const key of keys) {
56
- for (const listener of this.onValueChangedListeners) {
57
- listener(key);
58
- }
59
- }
60
- }
61
-
62
- get size(): number {
63
- return this.nativeInstance.size;
64
- }
65
- get isReadOnly(): boolean {
66
- return this.nativeInstance.isReadOnly;
67
- }
68
- set(key: string, value: boolean | string | number | ArrayBuffer): void {
69
- const func = this.getFunctionFromCache('set');
70
- func(key, value);
71
-
72
- this.onValuesChanged([key]);
73
- }
74
- getBoolean(key: string): boolean | undefined {
75
- const func = this.getFunctionFromCache('getBoolean');
76
- return func(key);
77
- }
78
- getString(key: string): string | undefined {
79
- const func = this.getFunctionFromCache('getString');
80
- return func(key);
81
- }
82
- getNumber(key: string): number | undefined {
83
- const func = this.getFunctionFromCache('getNumber');
84
- return func(key);
85
- }
86
- getBuffer(key: string): ArrayBufferLike | undefined {
87
- const func = this.getFunctionFromCache('getBuffer');
88
- return func(key);
89
- }
90
- contains(key: string): boolean {
91
- const func = this.getFunctionFromCache('contains');
92
- return func(key);
93
- }
94
- delete(key: string): void {
95
- const func = this.getFunctionFromCache('delete');
96
- func(key);
97
-
98
- this.onValuesChanged([key]);
99
- }
100
- getAllKeys(): string[] {
101
- const func = this.getFunctionFromCache('getAllKeys');
102
- return func();
103
- }
104
- clearAll(): void {
105
- const keys = this.getAllKeys();
106
-
107
- const func = this.getFunctionFromCache('clearAll');
108
- func();
109
-
110
- this.onValuesChanged(keys);
111
- }
112
- recrypt(key: string | undefined): void {
113
- const func = this.getFunctionFromCache('recrypt');
114
- return func(key);
115
- }
116
- trim(): void {
117
- const func = this.getFunctionFromCache('trim');
118
- func();
119
- }
120
-
121
- toString(): string {
122
- return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
123
- }
124
- toJSON(): object {
125
- return {
126
- [this.id]: this.getAllKeys(),
127
- };
128
- }
129
-
130
- addOnValueChangedListener(onValueChanged: (key: string) => void): Listener {
131
- this.onValueChangedListeners.push(onValueChanged);
132
-
133
- return {
134
- remove: () => {
135
- const index = this.onValueChangedListeners.indexOf(onValueChanged);
136
- if (index !== -1) {
137
- this.onValueChangedListeners.splice(index, 1);
138
- }
139
- },
140
- };
141
- }
142
- }
@@ -1,5 +0,0 @@
1
- import { MMKVInterface } from './Types';
2
-
3
- export const addMemoryWarningListener = (_mmkv: MMKVInterface): void => {
4
- //no-op function, there is not a web equivalent to memory warning
5
- };
@@ -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
- }