react-native-mmkv 4.0.1 → 4.1.1

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 (65) hide show
  1. package/NitroMmkv.podspec +0 -1
  2. package/android/build.gradle +1 -1
  3. package/cpp/HybridMMKV.cpp +10 -0
  4. package/cpp/HybridMMKV.hpp +1 -0
  5. package/cpp/HybridMMKVFactory.cpp +12 -4
  6. package/cpp/HybridMMKVFactory.hpp +4 -1
  7. package/lib/createMMKV/createMMKV.js +3 -13
  8. package/lib/createMMKV/createMMKV.web.js +40 -50
  9. package/lib/createMMKV/createMockMMKV.js +12 -0
  10. package/lib/deleteMMKV/deleteMMKV.d.ts +1 -0
  11. package/lib/deleteMMKV/deleteMMKV.js +9 -0
  12. package/lib/deleteMMKV/deleteMMKV.web.d.ts +1 -0
  13. package/lib/deleteMMKV/deleteMMKV.web.js +14 -0
  14. package/lib/existsMMKV/existsMMKV.d.ts +1 -0
  15. package/lib/existsMMKV/existsMMKV.js +9 -0
  16. package/lib/existsMMKV/existsMMKV.web.d.ts +1 -0
  17. package/lib/existsMMKV/existsMMKV.web.js +7 -0
  18. package/lib/getMMKVFactory.d.ts +4 -0
  19. package/lib/getMMKVFactory.js +20 -0
  20. package/lib/index.d.ts +2 -0
  21. package/lib/index.js +3 -0
  22. package/lib/specs/MMKV.nitro.d.ts +6 -0
  23. package/lib/specs/MMKVFactory.nitro.d.ts +13 -3
  24. package/lib/web/getLocalStorage.d.ts +2 -0
  25. package/lib/web/getLocalStorage.js +33 -0
  26. package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +1 -1
  27. package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +1 -1
  28. package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +1 -1
  29. package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +1 -1
  30. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +8 -1
  31. package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +2 -1
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +1 -1
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +1 -1
  34. package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +2 -2
  35. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +1 -1
  36. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +1 -1
  37. package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +1 -1
  38. package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +1 -1
  39. package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +1 -1
  40. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +1 -1
  41. package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +7 -1
  42. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +1 -1
  43. package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +9 -1
  44. package/nitrogen/generated/shared/c++/Configuration.hpp +25 -17
  45. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +4 -2
  46. package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +4 -2
  47. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +1 -1
  48. package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +1 -1
  49. package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +2 -1
  50. package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +6 -1
  51. package/nitrogen/generated/shared/c++/Listener.hpp +13 -5
  52. package/nitrogen/generated/shared/c++/Mode.hpp +1 -1
  53. package/package.json +3 -3
  54. package/src/createMMKV/createMMKV.ts +4 -18
  55. package/src/createMMKV/createMMKV.web.ts +43 -64
  56. package/src/createMMKV/createMockMMKV.ts +12 -0
  57. package/src/deleteMMKV/deleteMMKV.ts +11 -0
  58. package/src/deleteMMKV/deleteMMKV.web.ts +20 -0
  59. package/src/existsMMKV/existsMMKV.ts +11 -0
  60. package/src/existsMMKV/existsMMKV.web.ts +11 -0
  61. package/src/getMMKVFactory.ts +28 -0
  62. package/src/index.ts +4 -0
  63. package/src/specs/MMKV.nitro.ts +7 -0
  64. package/src/specs/MMKVFactory.nitro.ts +15 -3
  65. package/src/web/getLocalStorage.ts +42 -0
@@ -0,0 +1,11 @@
1
+ import {
2
+ getLocalStorage,
3
+ LOCAL_STORAGE_KEY_WILDCARD,
4
+ } from '../web/getLocalStorage'
5
+
6
+ export function existsMMKV(id: string): boolean {
7
+ const storage = getLocalStorage()
8
+ const prefix = id + LOCAL_STORAGE_KEY_WILDCARD
9
+ const keys = Object.keys(storage)
10
+ return keys.some((k) => k.startsWith(prefix))
11
+ }
@@ -0,0 +1,28 @@
1
+ import { NitroModules } from 'react-native-nitro-modules'
2
+ import type { MMKVFactory } from './specs/MMKVFactory.nitro'
3
+ import type { MMKVPlatformContext } from './specs/MMKVPlatformContext.nitro'
4
+
5
+ let factory: MMKVFactory | undefined
6
+ let platformContext: MMKVPlatformContext | undefined
7
+
8
+ export function getPlatformContext(): MMKVPlatformContext {
9
+ if (platformContext == null) {
10
+ // Lazy-init the platform-context HybridObject
11
+ platformContext = NitroModules.createHybridObject<MMKVPlatformContext>(
12
+ 'MMKVPlatformContext'
13
+ )
14
+ }
15
+ return platformContext
16
+ }
17
+
18
+ export function getMMKVFactory(): MMKVFactory {
19
+ if (factory == null) {
20
+ // Lazy-init the factory HybridObject
21
+ factory = NitroModules.createHybridObject<MMKVFactory>('MMKVFactory')
22
+ const context = getPlatformContext()
23
+ const baseDirectory = context.getBaseDirectory()
24
+ factory.initializeMMKV(baseDirectory)
25
+ }
26
+
27
+ return factory
28
+ }
package/src/index.ts CHANGED
@@ -5,6 +5,10 @@ export type { Configuration, Mode } from './specs/MMKVFactory.nitro'
5
5
  // The create function
6
6
  export { createMMKV } from './createMMKV/createMMKV'
7
7
 
8
+ // Exists + Delete
9
+ export { existsMMKV } from './existsMMKV/existsMMKV'
10
+ export { deleteMMKV } from './deleteMMKV/deleteMMKV'
11
+
8
12
  // All the hooks
9
13
  export { useMMKV } from './hooks/useMMKV'
10
14
  export { useMMKVBoolean } from './hooks/useMMKVBoolean'
@@ -95,4 +95,11 @@ export interface MMKV extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
95
95
  * To unsubscribe from value changes, call `remove()` on the Listener.
96
96
  */
97
97
  addOnValueChangedListener(onValueChanged: (key: string) => void): Listener
98
+
99
+ /**
100
+ * Imports all keys and values from the
101
+ * given other {@linkcode MMKV} instance.
102
+ * @returns the number of imported keys/values.
103
+ */
104
+ importAllFrom(other: MMKV): number
98
105
  }
@@ -68,16 +68,28 @@ export interface Configuration {
68
68
 
69
69
  export interface MMKVFactory
70
70
  extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
71
+ /**
72
+ * Initialize the MMKV library with the given root path.
73
+ * This has to be called once, before using {@linkcode createMMKV}.
74
+ */
75
+ initializeMMKV(rootPath: string): void
76
+
71
77
  /**
72
78
  * Create a new {@linkcode MMKV} instance with the given {@linkcode Configuration}
73
79
  */
74
80
  createMMKV(configuration: Configuration): MMKV
75
81
 
76
82
  /**
77
- * Initialize the MMKV library with the given root path.
78
- * This has to be called once, before using {@linkcode createMMKV}.
83
+ * Deletes the MMKV instance with the
84
+ * given {@linkcode id}.
79
85
  */
80
- initializeMMKV(rootPath: string): void
86
+ deleteMMKV(id: string): boolean
87
+
88
+ /**
89
+ * Returns `true` if an MMKV instance with the
90
+ * given {@linkcode id} exists, `false` otherwise.
91
+ */
92
+ existsMMKV(id: string): boolean
81
93
 
82
94
  /**
83
95
  * Get the default MMKV instance's ID.
@@ -0,0 +1,42 @@
1
+ export const LOCAL_STORAGE_KEY_WILDCARD = '\\'
2
+
3
+ const canUseDOM =
4
+ typeof window !== 'undefined' && window.document?.createElement != null
5
+
6
+ const hasAccessToLocalStorage = () => {
7
+ try {
8
+ // throws ACCESS_DENIED error
9
+ window.localStorage
10
+
11
+ return true
12
+ } catch {
13
+ return false
14
+ }
15
+ }
16
+ const inMemoryStorage = new Map<string, string>()
17
+
18
+ export function getLocalStorage(): Storage {
19
+ if (!canUseDOM) {
20
+ throw new Error(
21
+ 'Tried to access storage on the server. Did you forget to call this in useEffect?'
22
+ )
23
+ }
24
+
25
+ if (!hasAccessToLocalStorage()) {
26
+ return {
27
+ getItem: (key: string) => inMemoryStorage.get(key) ?? null,
28
+ setItem: (key: string, value: string) => inMemoryStorage.set(key, value),
29
+ removeItem: (key: string) => inMemoryStorage.delete(key),
30
+ clear: () => inMemoryStorage.clear(),
31
+ length: inMemoryStorage.size,
32
+ key: (index: number) => Object.keys(inMemoryStorage).at(index) ?? null,
33
+ } as Storage
34
+ }
35
+
36
+ const domStorage =
37
+ global?.localStorage ?? window?.localStorage ?? localStorage
38
+ if (domStorage == null) {
39
+ throw new Error(`Could not find 'localStorage' instance!`)
40
+ }
41
+ return domStorage
42
+ }