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
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// NitroMmkvAutolinking.swift
|
|
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
|
public final class NitroMmkvAutolinking {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpecSwift.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 "HybridMMKVPlatformContextSpecSwift.hpp"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpecSwift.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
|
|
@@ -48,6 +48,12 @@ namespace margelo::nitro::mmkv {
|
|
|
48
48
|
inline size_t getExternalMemorySize() noexcept override {
|
|
49
49
|
return _swiftPart.getMemorySize();
|
|
50
50
|
}
|
|
51
|
+
bool equals(const std::shared_ptr<HybridObject>& other) override {
|
|
52
|
+
if (auto otherCast = std::dynamic_pointer_cast<HybridMMKVPlatformContextSpecSwift>(other)) {
|
|
53
|
+
return _swiftPart.equals(otherCast->_swiftPart);
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
51
57
|
void dispose() noexcept override {
|
|
52
58
|
_swiftPart.dispose();
|
|
53
59
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpec_cxx.swift
|
|
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
|
|
@@ -96,6 +96,14 @@ open class HybridMMKVPlatformContextSpec_cxx {
|
|
|
96
96
|
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Compares this object with the given [other] object for reference equality.
|
|
101
|
+
*/
|
|
102
|
+
@inline(__always)
|
|
103
|
+
public func equals(other: HybridMMKVPlatformContextSpec_cxx) -> Bool {
|
|
104
|
+
return self.__implementation === other.__implementation
|
|
105
|
+
}
|
|
106
|
+
|
|
99
107
|
/**
|
|
100
108
|
* Call dispose() on the Swift class.
|
|
101
109
|
* This _may_ be called manually from JS.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// Configuration.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
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
#else
|
|
23
23
|
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
24
|
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
25
30
|
|
|
26
31
|
// Forward declaration of `Mode` to properly resolve imports.
|
|
27
32
|
namespace margelo::nitro::mmkv { enum class Mode; }
|
|
@@ -35,7 +40,7 @@ namespace margelo::nitro::mmkv {
|
|
|
35
40
|
/**
|
|
36
41
|
* A struct which can be represented as a JavaScript object (Configuration).
|
|
37
42
|
*/
|
|
38
|
-
struct Configuration {
|
|
43
|
+
struct Configuration final {
|
|
39
44
|
public:
|
|
40
45
|
std::string id SWIFT_PRIVATE;
|
|
41
46
|
std::optional<std::string> path SWIFT_PRIVATE;
|
|
@@ -46,6 +51,9 @@ namespace margelo::nitro::mmkv {
|
|
|
46
51
|
public:
|
|
47
52
|
Configuration() = default;
|
|
48
53
|
explicit Configuration(std::string id, std::optional<std::string> path, std::optional<std::string> encryptionKey, std::optional<Mode> mode, std::optional<bool> readOnly): id(id), path(path), encryptionKey(encryptionKey), mode(mode), readOnly(readOnly) {}
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
friend bool operator==(const Configuration& lhs, const Configuration& rhs) = default;
|
|
49
57
|
};
|
|
50
58
|
|
|
51
59
|
} // namespace margelo::nitro::mmkv
|
|
@@ -58,20 +66,20 @@ namespace margelo::nitro {
|
|
|
58
66
|
static inline margelo::nitro::mmkv::Configuration fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
59
67
|
jsi::Object obj = arg.asObject(runtime);
|
|
60
68
|
return margelo::nitro::mmkv::Configuration(
|
|
61
|
-
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "id")),
|
|
62
|
-
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "path")),
|
|
63
|
-
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "encryptionKey")),
|
|
64
|
-
JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::fromJSI(runtime, obj.getProperty(runtime, "mode")),
|
|
65
|
-
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, "readOnly"))
|
|
69
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id"))),
|
|
70
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "path"))),
|
|
71
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "encryptionKey"))),
|
|
72
|
+
JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "mode"))),
|
|
73
|
+
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "readOnly")))
|
|
66
74
|
);
|
|
67
75
|
}
|
|
68
76
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mmkv::Configuration& arg) {
|
|
69
77
|
jsi::Object obj(runtime);
|
|
70
|
-
obj.setProperty(runtime, "id", JSIConverter<std::string>::toJSI(runtime, arg.id));
|
|
71
|
-
obj.setProperty(runtime, "path", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.path));
|
|
72
|
-
obj.setProperty(runtime, "encryptionKey", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.encryptionKey));
|
|
73
|
-
obj.setProperty(runtime, "mode", JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::toJSI(runtime, arg.mode));
|
|
74
|
-
obj.setProperty(runtime, "readOnly", JSIConverter<std::optional<bool>>::toJSI(runtime, arg.readOnly));
|
|
78
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "id"), JSIConverter<std::string>::toJSI(runtime, arg.id));
|
|
79
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "path"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.path));
|
|
80
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "encryptionKey"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.encryptionKey));
|
|
81
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "mode"), JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::toJSI(runtime, arg.mode));
|
|
82
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "readOnly"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.readOnly));
|
|
75
83
|
return obj;
|
|
76
84
|
}
|
|
77
85
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
@@ -82,11 +90,11 @@ namespace margelo::nitro {
|
|
|
82
90
|
if (!nitro::isPlainObject(runtime, obj)) {
|
|
83
91
|
return false;
|
|
84
92
|
}
|
|
85
|
-
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "id"))) return false;
|
|
86
|
-
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "path"))) return false;
|
|
87
|
-
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "encryptionKey"))) return false;
|
|
88
|
-
if (!JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::canConvert(runtime, obj.getProperty(runtime, "mode"))) return false;
|
|
89
|
-
if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, "readOnly"))) return false;
|
|
93
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id")))) return false;
|
|
94
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "path")))) return false;
|
|
95
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "encryptionKey")))) return false;
|
|
96
|
+
if (!JSIConverter<std::optional<margelo::nitro::mmkv::Mode>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "mode")))) return false;
|
|
97
|
+
if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "readOnly")))) return false;
|
|
90
98
|
return true;
|
|
91
99
|
}
|
|
92
100
|
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVFactorySpec.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 "HybridMMKVFactorySpec.hpp"
|
|
@@ -15,8 +15,10 @@ namespace margelo::nitro::mmkv {
|
|
|
15
15
|
// load custom methods/properties
|
|
16
16
|
registerHybrids(this, [](Prototype& prototype) {
|
|
17
17
|
prototype.registerHybridGetter("defaultMMKVInstanceId", &HybridMMKVFactorySpec::getDefaultMMKVInstanceId);
|
|
18
|
-
prototype.registerHybridMethod("createMMKV", &HybridMMKVFactorySpec::createMMKV);
|
|
19
18
|
prototype.registerHybridMethod("initializeMMKV", &HybridMMKVFactorySpec::initializeMMKV);
|
|
19
|
+
prototype.registerHybridMethod("createMMKV", &HybridMMKVFactorySpec::createMMKV);
|
|
20
|
+
prototype.registerHybridMethod("deleteMMKV", &HybridMMKVFactorySpec::deleteMMKV);
|
|
21
|
+
prototype.registerHybridMethod("existsMMKV", &HybridMMKVFactorySpec::existsMMKV);
|
|
20
22
|
});
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVFactorySpec.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
|
|
@@ -54,8 +54,10 @@ namespace margelo::nitro::mmkv {
|
|
|
54
54
|
|
|
55
55
|
public:
|
|
56
56
|
// Methods
|
|
57
|
-
virtual std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) = 0;
|
|
58
57
|
virtual void initializeMMKV(const std::string& rootPath) = 0;
|
|
58
|
+
virtual std::shared_ptr<HybridMMKVSpec> createMMKV(const Configuration& configuration) = 0;
|
|
59
|
+
virtual bool deleteMMKV(const std::string& id) = 0;
|
|
60
|
+
virtual bool existsMMKV(const std::string& id) = 0;
|
|
59
61
|
|
|
60
62
|
protected:
|
|
61
63
|
// Hybrid Setup
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVPlatformContextSpec.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 "HybridMMKVPlatformContextSpec.hpp"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVSpec.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 "HybridMMKVSpec.hpp"
|
|
@@ -29,6 +29,7 @@ namespace margelo::nitro::mmkv {
|
|
|
29
29
|
prototype.registerHybridMethod("recrypt", &HybridMMKVSpec::recrypt);
|
|
30
30
|
prototype.registerHybridMethod("trim", &HybridMMKVSpec::trim);
|
|
31
31
|
prototype.registerHybridMethod("addOnValueChangedListener", &HybridMMKVSpec::addOnValueChangedListener);
|
|
32
|
+
prototype.registerHybridMethod("importAllFrom", &HybridMMKVSpec::importAllFrom);
|
|
32
33
|
});
|
|
33
34
|
}
|
|
34
35
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridMMKVSpec.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
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
// Forward declaration of `Listener` to properly resolve imports.
|
|
17
17
|
namespace margelo::nitro::mmkv { struct Listener; }
|
|
18
|
+
// Forward declaration of `HybridMMKVSpec` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::mmkv { class HybridMMKVSpec; }
|
|
18
20
|
|
|
19
21
|
#include <string>
|
|
20
22
|
#include <NitroModules/ArrayBuffer.hpp>
|
|
@@ -23,6 +25,8 @@ namespace margelo::nitro::mmkv { struct Listener; }
|
|
|
23
25
|
#include <vector>
|
|
24
26
|
#include "Listener.hpp"
|
|
25
27
|
#include <functional>
|
|
28
|
+
#include <memory>
|
|
29
|
+
#include "HybridMMKVSpec.hpp"
|
|
26
30
|
|
|
27
31
|
namespace margelo::nitro::mmkv {
|
|
28
32
|
|
|
@@ -69,6 +73,7 @@ namespace margelo::nitro::mmkv {
|
|
|
69
73
|
virtual void recrypt(const std::optional<std::string>& key) = 0;
|
|
70
74
|
virtual void trim() = 0;
|
|
71
75
|
virtual Listener addOnValueChangedListener(const std::function<void(const std::string& /* key */)>& onValueChanged) = 0;
|
|
76
|
+
virtual double importAllFrom(const std::shared_ptr<HybridMMKVSpec>& other) = 0;
|
|
72
77
|
|
|
73
78
|
protected:
|
|
74
79
|
// Hybrid Setup
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// Listener.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
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
#else
|
|
23
23
|
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
24
|
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
25
30
|
|
|
26
31
|
|
|
27
32
|
|
|
@@ -32,13 +37,16 @@ namespace margelo::nitro::mmkv {
|
|
|
32
37
|
/**
|
|
33
38
|
* A struct which can be represented as a JavaScript object (Listener).
|
|
34
39
|
*/
|
|
35
|
-
struct Listener {
|
|
40
|
+
struct Listener final {
|
|
36
41
|
public:
|
|
37
42
|
std::function<void()> remove SWIFT_PRIVATE;
|
|
38
43
|
|
|
39
44
|
public:
|
|
40
45
|
Listener() = default;
|
|
41
46
|
explicit Listener(std::function<void()> remove): remove(remove) {}
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
// Listener is not equatable because these properties are not equatable: remove
|
|
42
50
|
};
|
|
43
51
|
|
|
44
52
|
} // namespace margelo::nitro::mmkv
|
|
@@ -51,12 +59,12 @@ namespace margelo::nitro {
|
|
|
51
59
|
static inline margelo::nitro::mmkv::Listener fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
52
60
|
jsi::Object obj = arg.asObject(runtime);
|
|
53
61
|
return margelo::nitro::mmkv::Listener(
|
|
54
|
-
JSIConverter<std::function<void()>>::fromJSI(runtime, obj.getProperty(runtime, "remove"))
|
|
62
|
+
JSIConverter<std::function<void()>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "remove")))
|
|
55
63
|
);
|
|
56
64
|
}
|
|
57
65
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mmkv::Listener& arg) {
|
|
58
66
|
jsi::Object obj(runtime);
|
|
59
|
-
obj.setProperty(runtime, "remove", JSIConverter<std::function<void()>>::toJSI(runtime, arg.remove));
|
|
67
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "remove"), JSIConverter<std::function<void()>>::toJSI(runtime, arg.remove));
|
|
60
68
|
return obj;
|
|
61
69
|
}
|
|
62
70
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
@@ -67,7 +75,7 @@ namespace margelo::nitro {
|
|
|
67
75
|
if (!nitro::isPlainObject(runtime, obj)) {
|
|
68
76
|
return false;
|
|
69
77
|
}
|
|
70
|
-
if (!JSIConverter<std::function<void()>>::canConvert(runtime, obj.getProperty(runtime, "remove"))) return false;
|
|
78
|
+
if (!JSIConverter<std::function<void()>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "remove")))) return false;
|
|
71
79
|
return true;
|
|
72
80
|
}
|
|
73
81
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-mmkv",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "⚡️ The fastest key/value storage for React Native.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"eslint": "^8.57.0",
|
|
68
68
|
"eslint-config-prettier": "^9.1.0",
|
|
69
69
|
"eslint-plugin-prettier": "^5.2.1",
|
|
70
|
-
"nitrogen": "0.
|
|
70
|
+
"nitrogen": "0.32.1",
|
|
71
71
|
"prettier": "^3.3.3",
|
|
72
72
|
"react": "19.1.1",
|
|
73
73
|
"react-native": "0.82.0",
|
|
74
|
-
"react-native-nitro-modules": "0.
|
|
74
|
+
"react-native-nitro-modules": "0.32.1",
|
|
75
75
|
"typescript": "^5.8.3"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
1
|
import type { MMKV } from '../specs/MMKV.nitro'
|
|
3
|
-
import type { Configuration
|
|
4
|
-
import type { MMKVPlatformContext } from '../specs/MMKVPlatformContext.nitro'
|
|
2
|
+
import type { Configuration } from '../specs/MMKVFactory.nitro'
|
|
5
3
|
import { Platform } from 'react-native'
|
|
6
4
|
import { addMemoryWarningListener } from '../addMemoryWarningListener/addMemoryWarningListener'
|
|
7
5
|
import { isTest } from '../isTest'
|
|
8
6
|
import { createMockMMKV } from './createMockMMKV'
|
|
9
|
-
|
|
10
|
-
let factory: MMKVFactory | undefined
|
|
11
|
-
let platformContext: MMKVPlatformContext | undefined
|
|
7
|
+
import { getMMKVFactory, getPlatformContext } from '../getMMKVFactory'
|
|
12
8
|
|
|
13
9
|
export function createMMKV(configuration?: Configuration): MMKV {
|
|
14
10
|
if (isTest()) {
|
|
@@ -16,18 +12,7 @@ export function createMMKV(configuration?: Configuration): MMKV {
|
|
|
16
12
|
return createMockMMKV(configuration)
|
|
17
13
|
}
|
|
18
14
|
|
|
19
|
-
|
|
20
|
-
// Lazy-init the platform-context HybridObject
|
|
21
|
-
platformContext = NitroModules.createHybridObject<MMKVPlatformContext>(
|
|
22
|
-
'MMKVPlatformContext'
|
|
23
|
-
)
|
|
24
|
-
}
|
|
25
|
-
if (factory == null) {
|
|
26
|
-
// Lazy-init the factory HybridObject
|
|
27
|
-
factory = NitroModules.createHybridObject<MMKVFactory>('MMKVFactory')
|
|
28
|
-
const baseDirectory = platformContext.getBaseDirectory()
|
|
29
|
-
factory.initializeMMKV(baseDirectory)
|
|
30
|
-
}
|
|
15
|
+
const factory = getMMKVFactory()
|
|
31
16
|
|
|
32
17
|
// Pre-parse the config
|
|
33
18
|
let config = configuration ?? { id: factory.defaultMMKVInstanceId }
|
|
@@ -36,6 +21,7 @@ export function createMMKV(configuration?: Configuration): MMKV {
|
|
|
36
21
|
if (config.path == null) {
|
|
37
22
|
// If the user set an App Group directory in Info.plist, let's use
|
|
38
23
|
// the App Group as a MMKV path:
|
|
24
|
+
const platformContext = getPlatformContext()
|
|
39
25
|
const appGroupDirectory = platformContext.getAppGroupDirectory()
|
|
40
26
|
if (appGroupDirectory != null) {
|
|
41
27
|
config.path = appGroupDirectory
|
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
import type { MMKV } from '../specs/MMKV.nitro'
|
|
2
2
|
import type { Configuration } from '../specs/MMKVFactory.nitro'
|
|
3
3
|
import { createTextEncoder } from '../web/createTextEncoder'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const hasAccessToLocalStorage = () => {
|
|
9
|
-
try {
|
|
10
|
-
// throws ACCESS_DENIED error
|
|
11
|
-
window.localStorage
|
|
12
|
-
|
|
13
|
-
return true
|
|
14
|
-
} catch {
|
|
15
|
-
return false
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const KEY_WILDCARD = '\\'
|
|
20
|
-
const inMemoryStorage = new Map<string, string>()
|
|
4
|
+
import {
|
|
5
|
+
getLocalStorage,
|
|
6
|
+
LOCAL_STORAGE_KEY_WILDCARD,
|
|
7
|
+
} from '../web/getLocalStorage'
|
|
21
8
|
|
|
22
9
|
export function createMMKV(
|
|
23
10
|
config: Configuration = { id: 'mmkv.default' }
|
|
@@ -29,48 +16,14 @@ export function createMMKV(
|
|
|
29
16
|
throw new Error("MMKV: 'path' is not supported on Web!")
|
|
30
17
|
}
|
|
31
18
|
|
|
32
|
-
// canUseDOM check prevents spam in Node server environments, such as Next.js server side props.
|
|
33
|
-
if (!hasAccessToLocalStorage() && canUseDOM) {
|
|
34
|
-
console.warn(
|
|
35
|
-
'MMKV: LocalStorage has been disabled. Your experience will be limited to in-memory storage!'
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const storage = () => {
|
|
40
|
-
if (!canUseDOM) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
'Tried to access storage on the server. Did you forget to call this in useEffect?'
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (!hasAccessToLocalStorage()) {
|
|
47
|
-
return {
|
|
48
|
-
getItem: (key: string) => inMemoryStorage.get(key) ?? null,
|
|
49
|
-
setItem: (key: string, value: string) =>
|
|
50
|
-
inMemoryStorage.set(key, value),
|
|
51
|
-
removeItem: (key: string) => inMemoryStorage.delete(key),
|
|
52
|
-
clear: () => inMemoryStorage.clear(),
|
|
53
|
-
length: inMemoryStorage.size,
|
|
54
|
-
key: (index: number) => Object.keys(inMemoryStorage).at(index) ?? null,
|
|
55
|
-
} as Storage
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const domStorage =
|
|
59
|
-
global?.localStorage ?? window?.localStorage ?? localStorage
|
|
60
|
-
if (domStorage == null) {
|
|
61
|
-
throw new Error(`Could not find 'localStorage' instance!`)
|
|
62
|
-
}
|
|
63
|
-
return domStorage
|
|
64
|
-
}
|
|
65
|
-
|
|
66
19
|
const textEncoder = createTextEncoder()
|
|
67
20
|
const listeners = new Set<(key: string) => void>()
|
|
68
21
|
|
|
69
|
-
if (config.id.includes(
|
|
22
|
+
if (config.id.includes(LOCAL_STORAGE_KEY_WILDCARD)) {
|
|
70
23
|
throw new Error('MMKV: `id` cannot contain the backslash character (`\\`)!')
|
|
71
24
|
}
|
|
72
25
|
|
|
73
|
-
const keyPrefix = `${config.id}${
|
|
26
|
+
const keyPrefix = `${config.id}${LOCAL_STORAGE_KEY_WILDCARD}` // mmkv.default\\
|
|
74
27
|
const prefixedKey = (key: string) => {
|
|
75
28
|
if (key.includes('\\')) {
|
|
76
29
|
throw new Error(
|
|
@@ -89,48 +42,61 @@ export function createMMKV(
|
|
|
89
42
|
size: 0,
|
|
90
43
|
isReadOnly: false,
|
|
91
44
|
clearAll: () => {
|
|
92
|
-
const
|
|
45
|
+
const storage = getLocalStorage()
|
|
46
|
+
const keys = Object.keys(storage)
|
|
93
47
|
for (const key of keys) {
|
|
94
48
|
if (key.startsWith(keyPrefix)) {
|
|
95
|
-
storage
|
|
49
|
+
storage.removeItem(key)
|
|
96
50
|
callListeners(key)
|
|
97
51
|
}
|
|
98
52
|
}
|
|
99
53
|
},
|
|
100
54
|
remove: (key) => {
|
|
101
|
-
storage()
|
|
102
|
-
|
|
55
|
+
const storage = getLocalStorage()
|
|
56
|
+
storage.removeItem(prefixedKey(key))
|
|
57
|
+
const wasRemoved = storage.getItem(prefixedKey(key)) === null
|
|
103
58
|
if (wasRemoved) callListeners(key)
|
|
104
59
|
return wasRemoved
|
|
105
60
|
},
|
|
106
61
|
set: (key, value) => {
|
|
62
|
+
const storage = getLocalStorage()
|
|
107
63
|
if (key === '') throw new Error('Cannot set a value for an empty key!')
|
|
108
|
-
storage
|
|
64
|
+
storage.setItem(prefixedKey(key), value.toString())
|
|
109
65
|
callListeners(key)
|
|
110
66
|
},
|
|
111
|
-
getString: (key) =>
|
|
67
|
+
getString: (key) => {
|
|
68
|
+
const storage = getLocalStorage()
|
|
69
|
+
return storage.getItem(prefixedKey(key)) ?? undefined
|
|
70
|
+
},
|
|
112
71
|
getNumber: (key) => {
|
|
113
|
-
const
|
|
72
|
+
const storage = getLocalStorage()
|
|
73
|
+
const value = storage.getItem(prefixedKey(key))
|
|
114
74
|
if (value == null) return undefined
|
|
115
75
|
return Number(value)
|
|
116
76
|
},
|
|
117
77
|
getBoolean: (key) => {
|
|
118
|
-
const
|
|
78
|
+
const storage = getLocalStorage()
|
|
79
|
+
const value = storage.getItem(prefixedKey(key))
|
|
119
80
|
if (value == null) return undefined
|
|
120
81
|
return value === 'true'
|
|
121
82
|
},
|
|
122
83
|
getBuffer: (key) => {
|
|
123
|
-
const
|
|
84
|
+
const storage = getLocalStorage()
|
|
85
|
+
const value = storage.getItem(prefixedKey(key))
|
|
124
86
|
if (value == null) return undefined
|
|
125
87
|
return textEncoder.encode(value).buffer
|
|
126
88
|
},
|
|
127
89
|
getAllKeys: () => {
|
|
128
|
-
const
|
|
90
|
+
const storage = getLocalStorage()
|
|
91
|
+
const keys = Object.keys(storage)
|
|
129
92
|
return keys
|
|
130
93
|
.filter((key) => key.startsWith(keyPrefix))
|
|
131
94
|
.map((key) => key.slice(keyPrefix.length))
|
|
132
95
|
},
|
|
133
|
-
contains: (key) =>
|
|
96
|
+
contains: (key) => {
|
|
97
|
+
const storage = getLocalStorage()
|
|
98
|
+
return storage.getItem(prefixedKey(key)) != null
|
|
99
|
+
},
|
|
134
100
|
recrypt: () => {
|
|
135
101
|
throw new Error('`recrypt(..)` is not supported on Web!')
|
|
136
102
|
},
|
|
@@ -148,5 +114,18 @@ export function createMMKV(
|
|
|
148
114
|
},
|
|
149
115
|
}
|
|
150
116
|
},
|
|
117
|
+
importAllFrom: (other) => {
|
|
118
|
+
const storage = getLocalStorage()
|
|
119
|
+
const keys = other.getAllKeys()
|
|
120
|
+
let imported = 0
|
|
121
|
+
for (const key of keys) {
|
|
122
|
+
const string = other.getString(key)
|
|
123
|
+
if (string != null) {
|
|
124
|
+
storage.set(key, string)
|
|
125
|
+
imported++
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return imported
|
|
129
|
+
},
|
|
151
130
|
}
|
|
152
131
|
}
|
|
@@ -79,5 +79,17 @@ export function createMockMMKV(
|
|
|
79
79
|
},
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
|
+
importAllFrom: (other) => {
|
|
83
|
+
const keys = other.getAllKeys()
|
|
84
|
+
let imported = 0
|
|
85
|
+
for (const key of keys) {
|
|
86
|
+
const data = other.getBuffer(key)
|
|
87
|
+
if (data != null) {
|
|
88
|
+
storage.set(key, data)
|
|
89
|
+
imported++
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return imported
|
|
93
|
+
},
|
|
82
94
|
}
|
|
83
95
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocalStorage,
|
|
3
|
+
LOCAL_STORAGE_KEY_WILDCARD,
|
|
4
|
+
} from '../web/getLocalStorage'
|
|
5
|
+
|
|
6
|
+
export function deleteMMKV(id: string): boolean {
|
|
7
|
+
const storage = getLocalStorage()
|
|
8
|
+
const prefix = id + LOCAL_STORAGE_KEY_WILDCARD
|
|
9
|
+
let wasRemoved = false
|
|
10
|
+
|
|
11
|
+
const keys = Object.keys(storage)
|
|
12
|
+
for (const key of keys) {
|
|
13
|
+
if (key.startsWith(prefix)) {
|
|
14
|
+
storage.removeItem(key)
|
|
15
|
+
wasRemoved = true
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return wasRemoved
|
|
20
|
+
}
|