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.
- package/NitroMmkv.podspec +0 -1
- package/android/build.gradle +1 -1
- package/cpp/HybridMMKV.cpp +10 -0
- package/cpp/HybridMMKV.hpp +1 -0
- package/cpp/HybridMMKVFactory.cpp +12 -4
- package/cpp/HybridMMKVFactory.hpp +4 -1
- package/lib/createMMKV/createMMKV.js +3 -13
- package/lib/createMMKV/createMMKV.web.js +40 -50
- package/lib/createMMKV/createMockMMKV.js +12 -0
- package/lib/deleteMMKV/deleteMMKV.d.ts +1 -0
- package/lib/deleteMMKV/deleteMMKV.js +9 -0
- package/lib/deleteMMKV/deleteMMKV.web.d.ts +1 -0
- package/lib/deleteMMKV/deleteMMKV.web.js +14 -0
- package/lib/existsMMKV/existsMMKV.d.ts +1 -0
- package/lib/existsMMKV/existsMMKV.js +9 -0
- package/lib/existsMMKV/existsMMKV.web.d.ts +1 -0
- package/lib/existsMMKV/existsMMKV.web.js +7 -0
- package/lib/getMMKVFactory.d.ts +4 -0
- package/lib/getMMKVFactory.js +20 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -0
- package/lib/specs/MMKV.nitro.d.ts +6 -0
- package/lib/specs/MMKVFactory.nitro.d.ts +13 -3
- package/lib/web/getLocalStorage.d.ts +2 -0
- package/lib/web/getLocalStorage.js +33 -0
- package/nitrogen/generated/android/NitroMmkv+autolinking.cmake +1 -1
- package/nitrogen/generated/android/NitroMmkv+autolinking.gradle +1 -1
- package/nitrogen/generated/android/NitroMmkvOnLoad.cpp +1 -1
- package/nitrogen/generated/android/NitroMmkvOnLoad.hpp +1 -1
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.cpp +8 -1
- package/nitrogen/generated/android/c++/JHybridMMKVPlatformContextSpec.hpp +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/NitroMmkvOnLoad.kt +1 -1
- package/nitrogen/generated/ios/NitroMmkv+autolinking.rb +2 -2
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.cpp +1 -1
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/NitroMmkv-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/NitroMmkvAutolinking.mm +1 -1
- package/nitrogen/generated/ios/NitroMmkvAutolinking.swift +1 -1
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridMMKVPlatformContextSpecSwift.hpp +7 -1
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridMMKVPlatformContextSpec_cxx.swift +9 -1
- package/nitrogen/generated/shared/c++/Configuration.hpp +25 -17
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.cpp +4 -2
- package/nitrogen/generated/shared/c++/HybridMMKVFactorySpec.hpp +4 -2
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridMMKVPlatformContextSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.cpp +2 -1
- package/nitrogen/generated/shared/c++/HybridMMKVSpec.hpp +6 -1
- package/nitrogen/generated/shared/c++/Listener.hpp +13 -5
- package/nitrogen/generated/shared/c++/Mode.hpp +1 -1
- package/package.json +3 -3
- package/src/createMMKV/createMMKV.ts +4 -18
- package/src/createMMKV/createMMKV.web.ts +43 -64
- package/src/createMMKV/createMockMMKV.ts +12 -0
- package/src/deleteMMKV/deleteMMKV.ts +11 -0
- package/src/deleteMMKV/deleteMMKV.web.ts +20 -0
- package/src/existsMMKV/existsMMKV.ts +11 -0
- package/src/existsMMKV/existsMMKV.web.ts +11 -0
- package/src/getMMKVFactory.ts +28 -0
- package/src/index.ts +4 -0
- package/src/specs/MMKV.nitro.ts +7 -0
- package/src/specs/MMKVFactory.nitro.ts +15 -3
- package/src/web/getLocalStorage.ts +42 -0
package/NitroMmkv.podspec
CHANGED
package/android/build.gradle
CHANGED
package/cpp/HybridMMKV.cpp
CHANGED
|
@@ -212,4 +212,14 @@ MMKVMode HybridMMKV::getMMKVMode(const Configuration& config) {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
+
double HybridMMKV::importAllFrom(const std::shared_ptr<HybridMMKVSpec>& other) {
|
|
216
|
+
auto hybridMMKV = std::dynamic_pointer_cast<HybridMMKV>(other);
|
|
217
|
+
if (hybridMMKV == nullptr) {
|
|
218
|
+
throw std::runtime_error("The given `MMKV` instance is not of type `HybridMMKV`!");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
size_t importedCount = instance->importFrom(hybridMMKV->instance);
|
|
222
|
+
return static_cast<double>(importedCount);
|
|
223
|
+
}
|
|
224
|
+
|
|
215
225
|
} // namespace margelo::nitro::mmkv
|
package/cpp/HybridMMKV.hpp
CHANGED
|
@@ -37,6 +37,7 @@ public:
|
|
|
37
37
|
void recrypt(const std::optional<std::string>& key) override;
|
|
38
38
|
void trim() override;
|
|
39
39
|
Listener addOnValueChangedListener(const std::function<void(const std::string& /* key */)>& onValueChanged) override;
|
|
40
|
+
double importAllFrom(const std::shared_ptr<HybridMMKVSpec>& other) override;
|
|
40
41
|
|
|
41
42
|
private:
|
|
42
43
|
static MMKVMode getMMKVMode(const Configuration& config);
|
|
@@ -15,10 +15,6 @@ std::string HybridMMKVFactory::getDefaultMMKVInstanceId() {
|
|
|
15
15
|
return DEFAULT_MMAP_ID;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
std::shared_ptr<HybridMMKVSpec> HybridMMKVFactory::createMMKV(const Configuration& configuration) {
|
|
19
|
-
return std::make_shared<HybridMMKV>(configuration);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
18
|
void HybridMMKVFactory::initializeMMKV(const std::string& rootPath) {
|
|
23
19
|
Logger::log(LogLevel::Info, TAG, "Initializing MMKV with rootPath=%s", rootPath.c_str());
|
|
24
20
|
|
|
@@ -30,4 +26,16 @@ void HybridMMKVFactory::initializeMMKV(const std::string& rootPath) {
|
|
|
30
26
|
MMKV::initializeMMKV(rootPath, logLevel);
|
|
31
27
|
}
|
|
32
28
|
|
|
29
|
+
std::shared_ptr<HybridMMKVSpec> HybridMMKVFactory::createMMKV(const Configuration& configuration) {
|
|
30
|
+
return std::make_shared<HybridMMKV>(configuration);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
bool HybridMMKVFactory::deleteMMKV(const std::string& id) {
|
|
34
|
+
return MMKV::removeStorage(id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
bool HybridMMKVFactory::existsMMKV(const std::string& id) {
|
|
38
|
+
return MMKV::checkExist(id);
|
|
39
|
+
}
|
|
40
|
+
|
|
33
41
|
} // namespace margelo::nitro::mmkv
|
|
@@ -17,8 +17,11 @@ public:
|
|
|
17
17
|
|
|
18
18
|
public:
|
|
19
19
|
std::string getDefaultMMKVInstanceId() override;
|
|
20
|
-
std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) override;
|
|
21
20
|
void initializeMMKV(const std::string& rootPath) override;
|
|
21
|
+
|
|
22
|
+
std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) override;
|
|
23
|
+
bool deleteMMKV(const std::string& id) override;
|
|
24
|
+
bool existsMMKV(const std::string& id) override;
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
} // namespace margelo::nitro::mmkv
|
|
@@ -1,31 +1,21 @@
|
|
|
1
|
-
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
1
|
import { Platform } from 'react-native';
|
|
3
2
|
import { addMemoryWarningListener } from '../addMemoryWarningListener/addMemoryWarningListener';
|
|
4
3
|
import { isTest } from '../isTest';
|
|
5
4
|
import { createMockMMKV } from './createMockMMKV';
|
|
6
|
-
|
|
7
|
-
let platformContext;
|
|
5
|
+
import { getMMKVFactory, getPlatformContext } from '../getMMKVFactory';
|
|
8
6
|
export function createMMKV(configuration) {
|
|
9
7
|
if (isTest()) {
|
|
10
8
|
// In a test environment, we mock the MMKV instance.
|
|
11
9
|
return createMockMMKV(configuration);
|
|
12
10
|
}
|
|
13
|
-
|
|
14
|
-
// Lazy-init the platform-context HybridObject
|
|
15
|
-
platformContext = NitroModules.createHybridObject('MMKVPlatformContext');
|
|
16
|
-
}
|
|
17
|
-
if (factory == null) {
|
|
18
|
-
// Lazy-init the factory HybridObject
|
|
19
|
-
factory = NitroModules.createHybridObject('MMKVFactory');
|
|
20
|
-
const baseDirectory = platformContext.getBaseDirectory();
|
|
21
|
-
factory.initializeMMKV(baseDirectory);
|
|
22
|
-
}
|
|
11
|
+
const factory = getMMKVFactory();
|
|
23
12
|
// Pre-parse the config
|
|
24
13
|
let config = configuration ?? { id: factory.defaultMMKVInstanceId };
|
|
25
14
|
if (Platform.OS === 'ios') {
|
|
26
15
|
if (config.path == null) {
|
|
27
16
|
// If the user set an App Group directory in Info.plist, let's use
|
|
28
17
|
// the App Group as a MMKV path:
|
|
18
|
+
const platformContext = getPlatformContext();
|
|
29
19
|
const appGroupDirectory = platformContext.getAppGroupDirectory();
|
|
30
20
|
if (appGroupDirectory != null) {
|
|
31
21
|
config.path = appGroupDirectory;
|
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
import { createTextEncoder } from '../web/createTextEncoder';
|
|
2
|
-
|
|
3
|
-
const hasAccessToLocalStorage = () => {
|
|
4
|
-
try {
|
|
5
|
-
// throws ACCESS_DENIED error
|
|
6
|
-
window.localStorage;
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
const KEY_WILDCARD = '\\';
|
|
14
|
-
const inMemoryStorage = new Map();
|
|
2
|
+
import { getLocalStorage, LOCAL_STORAGE_KEY_WILDCARD, } from '../web/getLocalStorage';
|
|
15
3
|
export function createMMKV(config = { id: 'mmkv.default' }) {
|
|
16
4
|
if (config.encryptionKey != null) {
|
|
17
5
|
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
@@ -19,36 +7,12 @@ export function createMMKV(config = { id: 'mmkv.default' }) {
|
|
|
19
7
|
if (config.path != null) {
|
|
20
8
|
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
21
9
|
}
|
|
22
|
-
// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
|
|
23
|
-
if (!hasAccessToLocalStorage() && canUseDOM) {
|
|
24
|
-
console.warn('MMKV: LocalStorage has been disabled. Your experience will be limited to in-memory storage!');
|
|
25
|
-
}
|
|
26
|
-
const storage = () => {
|
|
27
|
-
if (!canUseDOM) {
|
|
28
|
-
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
29
|
-
}
|
|
30
|
-
if (!hasAccessToLocalStorage()) {
|
|
31
|
-
return {
|
|
32
|
-
getItem: (key) => inMemoryStorage.get(key) ?? null,
|
|
33
|
-
setItem: (key, value) => inMemoryStorage.set(key, value),
|
|
34
|
-
removeItem: (key) => inMemoryStorage.delete(key),
|
|
35
|
-
clear: () => inMemoryStorage.clear(),
|
|
36
|
-
length: inMemoryStorage.size,
|
|
37
|
-
key: (index) => Object.keys(inMemoryStorage).at(index) ?? null,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const domStorage = global?.localStorage ?? window?.localStorage ?? localStorage;
|
|
41
|
-
if (domStorage == null) {
|
|
42
|
-
throw new Error(`Could not find 'localStorage' instance!`);
|
|
43
|
-
}
|
|
44
|
-
return domStorage;
|
|
45
|
-
};
|
|
46
10
|
const textEncoder = createTextEncoder();
|
|
47
11
|
const listeners = new Set();
|
|
48
|
-
if (config.id.includes(
|
|
12
|
+
if (config.id.includes(LOCAL_STORAGE_KEY_WILDCARD)) {
|
|
49
13
|
throw new Error('MMKV: `id` cannot contain the backslash character (`\\`)!');
|
|
50
14
|
}
|
|
51
|
-
const keyPrefix = `${config.id}${
|
|
15
|
+
const keyPrefix = `${config.id}${LOCAL_STORAGE_KEY_WILDCARD}`; // mmkv.default\\
|
|
52
16
|
const prefixedKey = (key) => {
|
|
53
17
|
if (key.includes('\\')) {
|
|
54
18
|
throw new Error('MMKV: `key` cannot contain the backslash character (`\\`)!');
|
|
@@ -63,53 +27,66 @@ export function createMMKV(config = { id: 'mmkv.default' }) {
|
|
|
63
27
|
size: 0,
|
|
64
28
|
isReadOnly: false,
|
|
65
29
|
clearAll: () => {
|
|
66
|
-
const
|
|
30
|
+
const storage = getLocalStorage();
|
|
31
|
+
const keys = Object.keys(storage);
|
|
67
32
|
for (const key of keys) {
|
|
68
33
|
if (key.startsWith(keyPrefix)) {
|
|
69
|
-
storage
|
|
34
|
+
storage.removeItem(key);
|
|
70
35
|
callListeners(key);
|
|
71
36
|
}
|
|
72
37
|
}
|
|
73
38
|
},
|
|
74
39
|
remove: (key) => {
|
|
75
|
-
storage()
|
|
76
|
-
|
|
40
|
+
const storage = getLocalStorage();
|
|
41
|
+
storage.removeItem(prefixedKey(key));
|
|
42
|
+
const wasRemoved = storage.getItem(prefixedKey(key)) === null;
|
|
77
43
|
if (wasRemoved)
|
|
78
44
|
callListeners(key);
|
|
79
45
|
return wasRemoved;
|
|
80
46
|
},
|
|
81
47
|
set: (key, value) => {
|
|
48
|
+
const storage = getLocalStorage();
|
|
82
49
|
if (key === '')
|
|
83
50
|
throw new Error('Cannot set a value for an empty key!');
|
|
84
|
-
storage
|
|
51
|
+
storage.setItem(prefixedKey(key), value.toString());
|
|
85
52
|
callListeners(key);
|
|
86
53
|
},
|
|
87
|
-
getString: (key) =>
|
|
54
|
+
getString: (key) => {
|
|
55
|
+
const storage = getLocalStorage();
|
|
56
|
+
return storage.getItem(prefixedKey(key)) ?? undefined;
|
|
57
|
+
},
|
|
88
58
|
getNumber: (key) => {
|
|
89
|
-
const
|
|
59
|
+
const storage = getLocalStorage();
|
|
60
|
+
const value = storage.getItem(prefixedKey(key));
|
|
90
61
|
if (value == null)
|
|
91
62
|
return undefined;
|
|
92
63
|
return Number(value);
|
|
93
64
|
},
|
|
94
65
|
getBoolean: (key) => {
|
|
95
|
-
const
|
|
66
|
+
const storage = getLocalStorage();
|
|
67
|
+
const value = storage.getItem(prefixedKey(key));
|
|
96
68
|
if (value == null)
|
|
97
69
|
return undefined;
|
|
98
70
|
return value === 'true';
|
|
99
71
|
},
|
|
100
72
|
getBuffer: (key) => {
|
|
101
|
-
const
|
|
73
|
+
const storage = getLocalStorage();
|
|
74
|
+
const value = storage.getItem(prefixedKey(key));
|
|
102
75
|
if (value == null)
|
|
103
76
|
return undefined;
|
|
104
77
|
return textEncoder.encode(value).buffer;
|
|
105
78
|
},
|
|
106
79
|
getAllKeys: () => {
|
|
107
|
-
const
|
|
80
|
+
const storage = getLocalStorage();
|
|
81
|
+
const keys = Object.keys(storage);
|
|
108
82
|
return keys
|
|
109
83
|
.filter((key) => key.startsWith(keyPrefix))
|
|
110
84
|
.map((key) => key.slice(keyPrefix.length));
|
|
111
85
|
},
|
|
112
|
-
contains: (key) =>
|
|
86
|
+
contains: (key) => {
|
|
87
|
+
const storage = getLocalStorage();
|
|
88
|
+
return storage.getItem(prefixedKey(key)) != null;
|
|
89
|
+
},
|
|
113
90
|
recrypt: () => {
|
|
114
91
|
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
115
92
|
},
|
|
@@ -127,5 +104,18 @@ export function createMMKV(config = { id: 'mmkv.default' }) {
|
|
|
127
104
|
},
|
|
128
105
|
};
|
|
129
106
|
},
|
|
107
|
+
importAllFrom: (other) => {
|
|
108
|
+
const storage = getLocalStorage();
|
|
109
|
+
const keys = other.getAllKeys();
|
|
110
|
+
let imported = 0;
|
|
111
|
+
for (const key of keys) {
|
|
112
|
+
const string = other.getString(key);
|
|
113
|
+
if (string != null) {
|
|
114
|
+
storage.set(key, string);
|
|
115
|
+
imported++;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return imported;
|
|
119
|
+
},
|
|
130
120
|
};
|
|
131
121
|
}
|
|
@@ -73,5 +73,17 @@ export function createMockMMKV(config = { id: 'mmkv.default' }) {
|
|
|
73
73
|
},
|
|
74
74
|
};
|
|
75
75
|
},
|
|
76
|
+
importAllFrom: (other) => {
|
|
77
|
+
const keys = other.getAllKeys();
|
|
78
|
+
let imported = 0;
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
const data = other.getBuffer(key);
|
|
81
|
+
if (data != null) {
|
|
82
|
+
storage.set(key, data);
|
|
83
|
+
imported++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return imported;
|
|
87
|
+
},
|
|
76
88
|
};
|
|
77
89
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deleteMMKV(id: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deleteMMKV(id: string): boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getLocalStorage, LOCAL_STORAGE_KEY_WILDCARD, } from '../web/getLocalStorage';
|
|
2
|
+
export function deleteMMKV(id) {
|
|
3
|
+
const storage = getLocalStorage();
|
|
4
|
+
const prefix = id + LOCAL_STORAGE_KEY_WILDCARD;
|
|
5
|
+
let wasRemoved = false;
|
|
6
|
+
const keys = Object.keys(storage);
|
|
7
|
+
for (const key of keys) {
|
|
8
|
+
if (key.startsWith(prefix)) {
|
|
9
|
+
storage.removeItem(key);
|
|
10
|
+
wasRemoved = true;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return wasRemoved;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function existsMMKV(id: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function existsMMKV(id: string): boolean;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { getLocalStorage, LOCAL_STORAGE_KEY_WILDCARD, } from '../web/getLocalStorage';
|
|
2
|
+
export function existsMMKV(id) {
|
|
3
|
+
const storage = getLocalStorage();
|
|
4
|
+
const prefix = id + LOCAL_STORAGE_KEY_WILDCARD;
|
|
5
|
+
const keys = Object.keys(storage);
|
|
6
|
+
return keys.some((k) => k.startsWith(prefix));
|
|
7
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
let factory;
|
|
3
|
+
let platformContext;
|
|
4
|
+
export function getPlatformContext() {
|
|
5
|
+
if (platformContext == null) {
|
|
6
|
+
// Lazy-init the platform-context HybridObject
|
|
7
|
+
platformContext = NitroModules.createHybridObject('MMKVPlatformContext');
|
|
8
|
+
}
|
|
9
|
+
return platformContext;
|
|
10
|
+
}
|
|
11
|
+
export function getMMKVFactory() {
|
|
12
|
+
if (factory == null) {
|
|
13
|
+
// Lazy-init the factory HybridObject
|
|
14
|
+
factory = NitroModules.createHybridObject('MMKVFactory');
|
|
15
|
+
const context = getPlatformContext();
|
|
16
|
+
const baseDirectory = context.getBaseDirectory();
|
|
17
|
+
factory.initializeMMKV(baseDirectory);
|
|
18
|
+
}
|
|
19
|
+
return factory;
|
|
20
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type { MMKV } from './specs/MMKV.nitro';
|
|
2
2
|
export type { Configuration, Mode } from './specs/MMKVFactory.nitro';
|
|
3
3
|
export { createMMKV } from './createMMKV/createMMKV';
|
|
4
|
+
export { existsMMKV } from './existsMMKV/existsMMKV';
|
|
5
|
+
export { deleteMMKV } from './deleteMMKV/deleteMMKV';
|
|
4
6
|
export { useMMKV } from './hooks/useMMKV';
|
|
5
7
|
export { useMMKVBoolean } from './hooks/useMMKVBoolean';
|
|
6
8
|
export { useMMKVBuffer } from './hooks/useMMKVBuffer';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// The create function
|
|
2
2
|
export { createMMKV } from './createMMKV/createMMKV';
|
|
3
|
+
// Exists + Delete
|
|
4
|
+
export { existsMMKV } from './existsMMKV/existsMMKV';
|
|
5
|
+
export { deleteMMKV } from './deleteMMKV/deleteMMKV';
|
|
3
6
|
// All the hooks
|
|
4
7
|
export { useMMKV } from './hooks/useMMKV';
|
|
5
8
|
export { useMMKVBoolean } from './hooks/useMMKVBoolean';
|
|
@@ -96,4 +96,10 @@ export interface MMKV extends HybridObject<{
|
|
|
96
96
|
* To unsubscribe from value changes, call `remove()` on the Listener.
|
|
97
97
|
*/
|
|
98
98
|
addOnValueChangedListener(onValueChanged: (key: string) => void): Listener;
|
|
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;
|
|
99
105
|
}
|
|
@@ -67,15 +67,25 @@ export interface MMKVFactory extends HybridObject<{
|
|
|
67
67
|
ios: 'c++';
|
|
68
68
|
android: 'c++';
|
|
69
69
|
}> {
|
|
70
|
+
/**
|
|
71
|
+
* Initialize the MMKV library with the given root path.
|
|
72
|
+
* This has to be called once, before using {@linkcode createMMKV}.
|
|
73
|
+
*/
|
|
74
|
+
initializeMMKV(rootPath: string): void;
|
|
70
75
|
/**
|
|
71
76
|
* Create a new {@linkcode MMKV} instance with the given {@linkcode Configuration}
|
|
72
77
|
*/
|
|
73
78
|
createMMKV(configuration: Configuration): MMKV;
|
|
74
79
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
80
|
+
* Deletes the MMKV instance with the
|
|
81
|
+
* given {@linkcode id}.
|
|
77
82
|
*/
|
|
78
|
-
|
|
83
|
+
deleteMMKV(id: string): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Returns `true` if an MMKV instance with the
|
|
86
|
+
* given {@linkcode id} exists, `false` otherwise.
|
|
87
|
+
*/
|
|
88
|
+
existsMMKV(id: string): boolean;
|
|
79
89
|
/**
|
|
80
90
|
* Get the default MMKV instance's ID.
|
|
81
91
|
* @default 'mmkv.default'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export const LOCAL_STORAGE_KEY_WILDCARD = '\\';
|
|
2
|
+
const canUseDOM = typeof window !== 'undefined' && window.document?.createElement != null;
|
|
3
|
+
const hasAccessToLocalStorage = () => {
|
|
4
|
+
try {
|
|
5
|
+
// throws ACCESS_DENIED error
|
|
6
|
+
window.localStorage;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const inMemoryStorage = new Map();
|
|
14
|
+
export function getLocalStorage() {
|
|
15
|
+
if (!canUseDOM) {
|
|
16
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
17
|
+
}
|
|
18
|
+
if (!hasAccessToLocalStorage()) {
|
|
19
|
+
return {
|
|
20
|
+
getItem: (key) => inMemoryStorage.get(key) ?? null,
|
|
21
|
+
setItem: (key, value) => inMemoryStorage.set(key, value),
|
|
22
|
+
removeItem: (key) => inMemoryStorage.delete(key),
|
|
23
|
+
clear: () => inMemoryStorage.clear(),
|
|
24
|
+
length: inMemoryStorage.size,
|
|
25
|
+
key: (index) => Object.keys(inMemoryStorage).at(index) ?? null,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const domStorage = global?.localStorage ?? window?.localStorage ?? localStorage;
|
|
29
|
+
if (domStorage == null) {
|
|
30
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
31
|
+
}
|
|
32
|
+
return domStorage;
|
|
33
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# NitroMmkv+autolinking.cmake
|
|
3
3
|
# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
# https://github.com/mrousavy/nitro
|
|
5
|
-
# Copyright ©
|
|
5
|
+
# Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
#
|
|
7
7
|
|
|
8
8
|
# This is a CMake file that adds all files generated by Nitrogen
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// NitroMmkv+autolinking.gradle
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
/// This is a Gradle file that adds all files generated by Nitrogen
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// NitroMmkvOnLoad.cpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#ifndef BUILDING_NITROMMKV_WITH_GENERATED_CMAKE_PROJECT
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// JHybridMMKVPlatformContextSpec.cpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#include "JHybridMMKVPlatformContextSpec.hpp"
|
|
@@ -29,6 +29,13 @@ namespace margelo::nitro::mmkv {
|
|
|
29
29
|
return method(_javaPart);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
bool JHybridMMKVPlatformContextSpec::equals(const std::shared_ptr<HybridObject>& other) {
|
|
33
|
+
if (auto otherCast = std::dynamic_pointer_cast<JHybridMMKVPlatformContextSpec>(other)) {
|
|
34
|
+
return _javaPart == otherCast->_javaPart;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
32
39
|
void JHybridMMKVPlatformContextSpec::dispose() noexcept {
|
|
33
40
|
static const auto method = javaClassStatic()->getMethod<void()>("dispose");
|
|
34
41
|
method(_javaPart);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpec.hpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#pragma once
|
|
@@ -40,6 +40,7 @@ namespace margelo::nitro::mmkv {
|
|
|
40
40
|
|
|
41
41
|
public:
|
|
42
42
|
size_t getExternalMemorySize() noexcept override;
|
|
43
|
+
bool equals(const std::shared_ptr<HybridObject>& other) override;
|
|
43
44
|
void dispose() noexcept override;
|
|
44
45
|
std::string toString() override;
|
|
45
46
|
|
package/nitrogen/generated/android/kotlin/com/margelo/nitro/mmkv/HybridMMKVPlatformContextSpec.kt
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpec.kt
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
package com.margelo.nitro.mmkv
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# NitroMmkv+autolinking.rb
|
|
3
3
|
# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
# https://github.com/mrousavy/nitro
|
|
5
|
-
# Copyright ©
|
|
5
|
+
# Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
#
|
|
7
7
|
|
|
8
8
|
# This is a Ruby script that adds all files generated by Nitrogen
|
|
@@ -52,7 +52,7 @@ def add_nitrogen_files(spec)
|
|
|
52
52
|
spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({
|
|
53
53
|
# Use C++ 20
|
|
54
54
|
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
|
|
55
|
-
# Enables C++ <-> Swift interop (by default it's only
|
|
55
|
+
# Enables C++ <-> Swift interop (by default it's only ObjC)
|
|
56
56
|
"SWIFT_OBJC_INTEROP_MODE" => "objcxx",
|
|
57
57
|
# Enables stricter modular headers
|
|
58
58
|
"DEFINES_MODULE" => "YES",
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// NitroMmkv-Swift-Cxx-Bridge.cpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#include "NitroMmkv-Swift-Cxx-Bridge.hpp"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// NitroMmkvAutolinking.mm
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#import <Foundation/Foundation.h>
|