react-native-mmkv 2.1.1 → 2.3.0
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/MMKV/CHANGELOG.md +14 -0
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +1 -1
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +1 -1
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +1 -1
- package/MMKV/Core/MMKV.cpp +70 -7
- package/MMKV/Core/MMKV.h +9 -7
- package/MMKV/Core/MMKVPredef.h +1 -1
- package/MMKV/README.md +19 -19
- package/README.md +6 -2
- package/android/src/main/cpp/MmkvHostObject.cpp +79 -56
- package/ios/JSIUtils.mm +24 -25
- package/ios/MmkvHostObject.mm +44 -17
- package/ios/MmkvModule.mm +4 -1
- package/lib/commonjs/MMKV.js +142 -0
- package/lib/commonjs/MMKV.js.map +1 -0
- package/lib/commonjs/createMMKV.js +66 -0
- package/lib/commonjs/createMMKV.js.map +1 -0
- package/lib/commonjs/createMMKV.web.js +70 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -0
- package/lib/commonjs/hooks.js +192 -0
- package/lib/commonjs/hooks.js.map +1 -0
- package/lib/commonjs/index.js +32 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/MMKV.js +131 -0
- package/lib/module/MMKV.js.map +1 -0
- package/lib/module/createMMKV.js +55 -0
- package/lib/module/createMMKV.js.map +1 -0
- package/lib/module/createMMKV.web.js +60 -0
- package/lib/module/createMMKV.web.js.map +1 -0
- package/lib/module/hooks.js +174 -0
- package/lib/module/hooks.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/MMKV.d.ts +130 -0
- package/lib/typescript/createMMKV.d.ts +6 -0
- package/lib/typescript/createMMKV.web.d.ts +2 -0
- package/lib/typescript/hooks.d.ts +70 -0
- package/lib/typescript/index.d.ts +2 -0
- package/package.json +1 -1
- package/react-native-mmkv.podspec +1 -1
package/ios/JSIUtils.mm
CHANGED
|
@@ -29,6 +29,7 @@ jsi::String convertNSStringToJSIString(jsi::Runtime &runtime, NSString *value)
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value);
|
|
32
|
+
|
|
32
33
|
jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, NSDictionary *value)
|
|
33
34
|
{
|
|
34
35
|
jsi::Object result = jsi::Object(runtime);
|
|
@@ -75,16 +76,14 @@ jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value)
|
|
|
75
76
|
return jsi::Value::undefined();
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
id convertJSIValueToObjCObject(
|
|
79
|
-
|
|
80
|
-
const jsi::Value &value);
|
|
79
|
+
id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value);
|
|
80
|
+
|
|
81
81
|
NSString *convertJSIStringToNSString(jsi::Runtime &runtime, const jsi::String &value)
|
|
82
82
|
{
|
|
83
83
|
return [NSString stringWithUTF8String:value.utf8(runtime).c_str()];
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
NSArray *convertJSIArrayToNSArray(
|
|
87
|
-
jsi::Runtime &runtime,
|
|
86
|
+
NSArray *convertJSIArrayToNSArray(jsi::Runtime &runtime,
|
|
88
87
|
const jsi::Array &value)
|
|
89
88
|
{
|
|
90
89
|
size_t size = value.size(runtime);
|
|
@@ -97,8 +96,7 @@ NSArray *convertJSIArrayToNSArray(
|
|
|
97
96
|
return [result copy];
|
|
98
97
|
}
|
|
99
98
|
|
|
100
|
-
NSDictionary *convertJSIObjectToNSDictionary(
|
|
101
|
-
jsi::Runtime &runtime,
|
|
99
|
+
NSDictionary *convertJSIObjectToNSDictionary(jsi::Runtime &runtime,
|
|
102
100
|
const jsi::Object &value)
|
|
103
101
|
{
|
|
104
102
|
jsi::Array propertyNames = value.getPropertyNames(runtime);
|
|
@@ -115,8 +113,7 @@ NSDictionary *convertJSIObjectToNSDictionary(
|
|
|
115
113
|
return [result copy];
|
|
116
114
|
}
|
|
117
115
|
|
|
118
|
-
RCTResponseSenderBlock convertJSIFunctionToCallback(
|
|
119
|
-
jsi::Runtime &runtime,
|
|
116
|
+
RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime &runtime,
|
|
120
117
|
const jsi::Function &value)
|
|
121
118
|
{
|
|
122
119
|
__block auto cb = value.getFunction(runtime);
|
|
@@ -126,8 +123,7 @@ RCTResponseSenderBlock convertJSIFunctionToCallback(
|
|
|
126
123
|
};
|
|
127
124
|
}
|
|
128
125
|
|
|
129
|
-
id convertJSIValueToObjCObject(
|
|
130
|
-
jsi::Runtime &runtime,
|
|
126
|
+
id convertJSIValueToObjCObject(jsi::Runtime &runtime,
|
|
131
127
|
const jsi::Value &value)
|
|
132
128
|
{
|
|
133
129
|
if (value.isUndefined() || value.isNull()) {
|
|
@@ -148,7 +144,8 @@ id convertJSIValueToObjCObject(
|
|
|
148
144
|
return convertJSIArrayToNSArray(runtime, o.getArray(runtime));
|
|
149
145
|
}
|
|
150
146
|
if (o.isFunction(runtime)) {
|
|
151
|
-
return convertJSIFunctionToCallback(runtime,
|
|
147
|
+
return convertJSIFunctionToCallback(runtime,
|
|
148
|
+
std::move(o.getFunction(runtime)));
|
|
152
149
|
}
|
|
153
150
|
return convertJSIObjectToNSDictionary(runtime, o);
|
|
154
151
|
}
|
|
@@ -159,34 +156,36 @@ id convertJSIValueToObjCObject(
|
|
|
159
156
|
Promise::Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject)
|
|
160
157
|
: runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}
|
|
161
158
|
|
|
162
|
-
void Promise::resolve(const jsi::Value &result)
|
|
159
|
+
void Promise::resolve(const jsi::Value &result)
|
|
160
|
+
{
|
|
163
161
|
resolve_.call(runtime_, result);
|
|
164
162
|
}
|
|
165
163
|
|
|
166
|
-
void Promise::reject(const std::string &message)
|
|
164
|
+
void Promise::reject(const std::string &message)
|
|
165
|
+
{
|
|
167
166
|
jsi::Object error(runtime_);
|
|
168
|
-
error.setProperty(
|
|
169
|
-
|
|
167
|
+
error.setProperty(runtime_,
|
|
168
|
+
"message",
|
|
169
|
+
jsi::String::createFromUtf8(runtime_, message));
|
|
170
170
|
reject_.call(runtime_, error);
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
jsi::Value createPromiseAsJSIValue(
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
jsi::Value createPromiseAsJSIValue(jsi::Runtime &rt,
|
|
174
|
+
const PromiseSetupFunctionType func)
|
|
175
|
+
{
|
|
176
176
|
jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
|
|
177
|
-
jsi::Function fn = jsi::Function::createFromHostFunction(
|
|
178
|
-
rt,
|
|
177
|
+
jsi::Function fn = jsi::Function::createFromHostFunction(rt,
|
|
179
178
|
jsi::PropNameID::forAscii(rt, "fn"),
|
|
180
179
|
2,
|
|
181
|
-
[func](
|
|
182
|
-
jsi::Runtime &rt2,
|
|
180
|
+
[func](jsi::Runtime &rt2,
|
|
183
181
|
const jsi::Value &thisVal,
|
|
184
182
|
const jsi::Value *args,
|
|
185
183
|
size_t count) {
|
|
186
184
|
jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
|
|
187
185
|
jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
|
|
188
|
-
auto wrapper = std::make_shared<Promise>(
|
|
189
|
-
|
|
186
|
+
auto wrapper = std::make_shared<Promise>(rt2,
|
|
187
|
+
std::move(resolve),
|
|
188
|
+
std::move(reject));
|
|
190
189
|
func(rt2, wrapper);
|
|
191
190
|
return jsi::Value::undefined();
|
|
192
191
|
});
|
package/ios/MmkvHostObject.mm
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
#import "MmkvHostObject.h"
|
|
11
11
|
#import "JSIUtils.h"
|
|
12
12
|
|
|
13
|
-
MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* cryptKey)
|
|
13
|
+
MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* cryptKey)
|
|
14
|
+
{
|
|
14
15
|
NSData* cryptData = cryptKey == nil ? nil : [cryptKey dataUsingEncoding:NSUTF8StringEncoding];
|
|
15
16
|
instance = [MMKV mmkvWithID:instanceId cryptKey:cryptData rootPath:path];
|
|
16
17
|
|
|
@@ -36,7 +37,8 @@ MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* c
|
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt)
|
|
40
|
+
std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt)
|
|
41
|
+
{
|
|
40
42
|
std::vector<jsi::PropNameID> result;
|
|
41
43
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("set")));
|
|
42
44
|
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getBoolean")));
|
|
@@ -50,7 +52,8 @@ std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt)
|
|
|
50
52
|
return result;
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId)
|
|
55
|
+
jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId)
|
|
56
|
+
{
|
|
54
57
|
auto propName = propNameId.utf8(runtime);
|
|
55
58
|
auto funcName = "MMKV." + propName;
|
|
56
59
|
|
|
@@ -63,9 +66,11 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
63
66
|
const jsi::Value& thisValue,
|
|
64
67
|
const jsi::Value* arguments,
|
|
65
68
|
size_t count) -> jsi::Value {
|
|
66
|
-
if (!arguments[0].isString())
|
|
67
|
-
|
|
69
|
+
if (!arguments[0].isString()) {
|
|
70
|
+
throw jsi::JSError(runtime, "MMKV::set: First argument ('key') has to be of type string!");
|
|
71
|
+
}
|
|
68
72
|
|
|
73
|
+
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
69
74
|
if (arguments[1].isBool()) {
|
|
70
75
|
[instance setBool:arguments[1].getBool() forKey:keyName];
|
|
71
76
|
} else if (arguments[1].isNumber()) {
|
|
@@ -74,8 +79,9 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
74
79
|
auto stringValue = convertJSIStringToNSString(runtime, arguments[1].getString(runtime));
|
|
75
80
|
[instance setString:stringValue forKey:keyName];
|
|
76
81
|
} else {
|
|
77
|
-
throw jsi::JSError(runtime, "
|
|
82
|
+
throw jsi::JSError(runtime, "Second argument ('value') has to be of type bool, number or string!");
|
|
78
83
|
}
|
|
84
|
+
|
|
79
85
|
return jsi::Value::undefined();
|
|
80
86
|
});
|
|
81
87
|
}
|
|
@@ -89,11 +95,18 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
89
95
|
const jsi::Value& thisValue,
|
|
90
96
|
const jsi::Value* arguments,
|
|
91
97
|
size_t count) -> jsi::Value {
|
|
92
|
-
if (!arguments[0].isString())
|
|
98
|
+
if (!arguments[0].isString()) {
|
|
99
|
+
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
|
|
100
|
+
}
|
|
93
101
|
|
|
94
102
|
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
95
|
-
bool
|
|
96
|
-
|
|
103
|
+
bool hasValue;
|
|
104
|
+
auto value = [instance getBoolForKey:keyName defaultValue:false hasValue:&hasValue];
|
|
105
|
+
if (hasValue) {
|
|
106
|
+
return jsi::Value(value);
|
|
107
|
+
} else {
|
|
108
|
+
return jsi::Value::undefined();
|
|
109
|
+
}
|
|
97
110
|
});
|
|
98
111
|
}
|
|
99
112
|
|
|
@@ -106,14 +119,17 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
106
119
|
const jsi::Value& thisValue,
|
|
107
120
|
const jsi::Value* arguments,
|
|
108
121
|
size_t count) -> jsi::Value {
|
|
109
|
-
if (!arguments[0].isString())
|
|
122
|
+
if (!arguments[0].isString()) {
|
|
123
|
+
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
|
|
124
|
+
}
|
|
110
125
|
|
|
111
126
|
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
112
127
|
auto value = [instance getStringForKey:keyName];
|
|
113
|
-
if (value != nil)
|
|
128
|
+
if (value != nil) {
|
|
114
129
|
return convertNSStringToJSIString(runtime, value);
|
|
115
|
-
else
|
|
130
|
+
} else {
|
|
116
131
|
return jsi::Value::undefined();
|
|
132
|
+
}
|
|
117
133
|
});
|
|
118
134
|
}
|
|
119
135
|
|
|
@@ -126,11 +142,18 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
126
142
|
const jsi::Value& thisValue,
|
|
127
143
|
const jsi::Value* arguments,
|
|
128
144
|
size_t count) -> jsi::Value {
|
|
129
|
-
if (!arguments[0].isString())
|
|
145
|
+
if (!arguments[0].isString()) {
|
|
146
|
+
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
|
|
147
|
+
}
|
|
130
148
|
|
|
131
149
|
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
132
|
-
|
|
133
|
-
|
|
150
|
+
bool hasValue;
|
|
151
|
+
auto value = [instance getDoubleForKey:keyName defaultValue:0.0 hasValue:&hasValue];
|
|
152
|
+
if (hasValue) {
|
|
153
|
+
return jsi::Value(value);
|
|
154
|
+
} else {
|
|
155
|
+
return jsi::Value::undefined();
|
|
156
|
+
}
|
|
134
157
|
});
|
|
135
158
|
}
|
|
136
159
|
|
|
@@ -143,7 +166,9 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
143
166
|
const jsi::Value& thisValue,
|
|
144
167
|
const jsi::Value* arguments,
|
|
145
168
|
size_t count) -> jsi::Value {
|
|
146
|
-
if (!arguments[0].isString())
|
|
169
|
+
if (!arguments[0].isString()) {
|
|
170
|
+
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
|
|
171
|
+
}
|
|
147
172
|
|
|
148
173
|
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
149
174
|
bool containsKey = [instance containsKey:keyName];
|
|
@@ -160,7 +185,9 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
160
185
|
const jsi::Value& thisValue,
|
|
161
186
|
const jsi::Value* arguments,
|
|
162
187
|
size_t count) -> jsi::Value {
|
|
163
|
-
if (!arguments[0].isString())
|
|
188
|
+
if (!arguments[0].isString()) {
|
|
189
|
+
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
|
|
190
|
+
}
|
|
164
191
|
|
|
165
192
|
auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
|
|
166
193
|
[instance removeValueForKey:keyName];
|
package/ios/MmkvModule.mm
CHANGED
|
@@ -14,7 +14,10 @@ using namespace facebook;
|
|
|
14
14
|
|
|
15
15
|
RCT_EXPORT_MODULE(MMKV)
|
|
16
16
|
|
|
17
|
-
+ (NSString*)getPropertyAsStringOrNilFromObject:(jsi::Object&)object
|
|
17
|
+
+ (NSString*)getPropertyAsStringOrNilFromObject:(jsi::Object&)object
|
|
18
|
+
propertyName:(std::string)propertyName
|
|
19
|
+
runtime:(jsi::Runtime&)runtime
|
|
20
|
+
{
|
|
18
21
|
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
|
|
19
22
|
std::string string = value.isString() ? value.asString(runtime).utf8(runtime) : "";
|
|
20
23
|
return string.length() > 0 ? [NSString stringWithUTF8String:string.c_str()] : nil;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MMKV = void 0;
|
|
7
|
+
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
|
|
10
|
+
var _createMMKV = require("./createMMKV");
|
|
11
|
+
|
|
12
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
+
|
|
14
|
+
const onValueChangedListeners = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* A single MMKV instance.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
class MMKV {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new MMKV instance with the given Configuration.
|
|
22
|
+
* If no custom `id` is supplied, `'mmkv.default'` will be used.
|
|
23
|
+
*/
|
|
24
|
+
constructor(configuration = {
|
|
25
|
+
id: 'mmkv.default'
|
|
26
|
+
}) {
|
|
27
|
+
_defineProperty(this, "nativeInstance", void 0);
|
|
28
|
+
|
|
29
|
+
_defineProperty(this, "functionCache", void 0);
|
|
30
|
+
|
|
31
|
+
_defineProperty(this, "id", void 0);
|
|
32
|
+
|
|
33
|
+
this.id = configuration.id;
|
|
34
|
+
this.nativeInstance = (0, _createMMKV.createMMKV)(configuration);
|
|
35
|
+
this.functionCache = {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get onValueChangedListeners() {
|
|
39
|
+
if (!onValueChangedListeners.has(this.id)) {
|
|
40
|
+
onValueChangedListeners.set(this.id, []);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return onValueChangedListeners.get(this.id);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getFunctionFromCache(functionName) {
|
|
47
|
+
if (this.functionCache[functionName] == null) {
|
|
48
|
+
this.functionCache[functionName] = this.nativeInstance[functionName];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return this.functionCache[functionName];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
onValuesAboutToChange(keys) {
|
|
55
|
+
if (this.onValueChangedListeners.length === 0) return;
|
|
56
|
+
setImmediate(() => {
|
|
57
|
+
(0, _reactNative.unstable_batchedUpdates)(() => {
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
for (const listener of this.onValueChangedListeners) {
|
|
60
|
+
listener(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
set(key, value) {
|
|
68
|
+
this.onValuesAboutToChange([key]);
|
|
69
|
+
const func = this.getFunctionFromCache('set');
|
|
70
|
+
return func(key, value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getBoolean(key) {
|
|
74
|
+
const func = this.getFunctionFromCache('getBoolean');
|
|
75
|
+
return func(key);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getString(key) {
|
|
79
|
+
const func = this.getFunctionFromCache('getString');
|
|
80
|
+
return func(key);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getNumber(key) {
|
|
84
|
+
const func = this.getFunctionFromCache('getNumber');
|
|
85
|
+
return func(key);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
contains(key) {
|
|
89
|
+
const func = this.getFunctionFromCache('contains');
|
|
90
|
+
return func(key);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
delete(key) {
|
|
94
|
+
this.onValuesAboutToChange([key]);
|
|
95
|
+
const func = this.getFunctionFromCache('delete');
|
|
96
|
+
return func(key);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
getAllKeys() {
|
|
100
|
+
const func = this.getFunctionFromCache('getAllKeys');
|
|
101
|
+
return func();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
clearAll() {
|
|
105
|
+
const keys = this.getAllKeys();
|
|
106
|
+
this.onValuesAboutToChange(keys);
|
|
107
|
+
const func = this.getFunctionFromCache('clearAll');
|
|
108
|
+
return func();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
recrypt(key) {
|
|
112
|
+
const func = this.getFunctionFromCache('recrypt');
|
|
113
|
+
return func(key);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
toString() {
|
|
117
|
+
return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
toJSON() {
|
|
121
|
+
return {
|
|
122
|
+
[this.id]: this.getAllKeys()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
addOnValueChangedListener(onValueChanged) {
|
|
127
|
+
this.onValueChangedListeners.push(onValueChanged);
|
|
128
|
+
return {
|
|
129
|
+
remove: () => {
|
|
130
|
+
const index = this.onValueChangedListeners.indexOf(onValueChanged);
|
|
131
|
+
|
|
132
|
+
if (index !== -1) {
|
|
133
|
+
this.onValueChangedListeners.splice(index, 1);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
exports.MMKV = MMKV;
|
|
142
|
+
//# sourceMappingURL=MMKV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["MMKV.ts"],"names":["onValueChangedListeners","Map","MMKV","constructor","configuration","id","nativeInstance","functionCache","has","set","get","getFunctionFromCache","functionName","onValuesAboutToChange","keys","length","setImmediate","key","listener","value","func","getBoolean","getString","getNumber","contains","delete","getAllKeys","clearAll","recrypt","toString","join","toJSON","addOnValueChangedListener","onValueChanged","push","remove","index","indexOf","splice"],"mappings":"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","sourcesContent":["import { unstable_batchedUpdates } from 'react-native';\nimport { createMMKV } from './createMMKV';\n\ninterface Listener {\n remove: () => void;\n}\n\n/**\n * Used for configuration of a single MMKV instance.\n */\nexport interface MMKVConfiguration {\n /**\n * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!\n *\n * @example\n * ```ts\n * const userStorage = new MMKV({ id: `user-${userId}-storage` })\n * const globalStorage = new MMKV({ id: 'global-app-storage' })\n * ```\n *\n * @default 'mmkv.default'\n */\n id: string;\n /**\n * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:\n *\n * @example\n * ```ts\n * const temporaryStorage = new MMKV({ path: '/tmp/' })\n * ```\n */\n path?: string;\n /**\n * 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.\n *\n * Encryption keys can have a maximum length of 16 bytes.\n *\n * @example\n * ```ts\n * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })\n * ```\n */\n encryptionKey?: string;\n}\n\n/**\n * Represents a single MMKV instance.\n */\ninterface MMKVInterface {\n /**\n * Set a value for the given `key`.\n */\n set: (key: string, value: boolean | string | number) => void;\n /**\n * Get the boolean value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getBoolean: (key: string) => boolean | undefined;\n /**\n * Get the string value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getString: (key: string) => string | undefined;\n /**\n * Get the number value for the given `key`, or `undefined` if it does not exist.\n *\n * @default undefined\n */\n getNumber: (key: string) => number | undefined;\n /**\n * Checks whether the given `key` is being stored in this MMKV instance.\n */\n contains: (key: string) => boolean;\n /**\n * Delete the given `key`.\n */\n delete: (key: string) => void;\n /**\n * Get all keys.\n *\n * @default []\n */\n getAllKeys: () => string[];\n /**\n * Delete all keys.\n */\n clearAll: () => void;\n /**\n * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.\n *\n * To remove encryption, pass `undefined` as a key.\n *\n * Encryption keys can have a maximum length of 16 bytes.\n */\n recrypt: (key: string | undefined) => void;\n /**\n * Adds a value changed listener. The Listener will be called whenever any value\n * in this storage instance changes (set or delete).\n *\n * To unsubscribe from value changes, call `remove()` on the Listener.\n */\n addOnValueChangedListener: (\n onValueChanged: (key: string) => void\n ) => Listener;\n}\n\nexport type NativeMMKV = Pick<\n MMKVInterface,\n | 'clearAll'\n | 'contains'\n | 'delete'\n | 'getAllKeys'\n | 'getBoolean'\n | 'getNumber'\n | 'getString'\n | 'set'\n | 'recrypt'\n>;\n\nconst onValueChangedListeners = new Map<string, ((key: string) => void)[]>();\n\n/**\n * A single MMKV instance.\n */\nexport class MMKV implements MMKVInterface {\n private nativeInstance: NativeMMKV;\n private functionCache: Partial<NativeMMKV>;\n private id: string;\n\n /**\n * Creates a new MMKV instance with the given Configuration.\n * If no custom `id` is supplied, `'mmkv.default'` will be used.\n */\n constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {\n this.id = configuration.id;\n this.nativeInstance = createMMKV(configuration);\n this.functionCache = {};\n }\n\n private get onValueChangedListeners() {\n if (!onValueChangedListeners.has(this.id)) {\n onValueChangedListeners.set(this.id, []);\n }\n return onValueChangedListeners.get(this.id)!;\n }\n\n private getFunctionFromCache<T extends keyof NativeMMKV>(\n functionName: T\n ): NativeMMKV[T] {\n if (this.functionCache[functionName] == null) {\n this.functionCache[functionName] = this.nativeInstance[functionName];\n }\n return this.functionCache[functionName] as NativeMMKV[T];\n }\n\n private onValuesAboutToChange(keys: string[]) {\n if (this.onValueChangedListeners.length === 0) return;\n\n setImmediate(() => {\n unstable_batchedUpdates(() => {\n for (const key of keys) {\n for (const listener of this.onValueChangedListeners) {\n listener(key);\n }\n }\n });\n });\n }\n\n set(key: string, value: boolean | string | number): void {\n this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('set');\n return func(key, value);\n }\n getBoolean(key: string): boolean | undefined {\n const func = this.getFunctionFromCache('getBoolean');\n return func(key);\n }\n getString(key: string): string | undefined {\n const func = this.getFunctionFromCache('getString');\n return func(key);\n }\n getNumber(key: string): number | undefined {\n const func = this.getFunctionFromCache('getNumber');\n return func(key);\n }\n contains(key: string): boolean {\n const func = this.getFunctionFromCache('contains');\n return func(key);\n }\n delete(key: string): void {\n this.onValuesAboutToChange([key]);\n\n const func = this.getFunctionFromCache('delete');\n return func(key);\n }\n getAllKeys(): string[] {\n const func = this.getFunctionFromCache('getAllKeys');\n return func();\n }\n clearAll(): void {\n const keys = this.getAllKeys();\n this.onValuesAboutToChange(keys);\n\n const func = this.getFunctionFromCache('clearAll');\n return func();\n }\n recrypt(key: string | undefined): void {\n const func = this.getFunctionFromCache('recrypt');\n return func(key);\n }\n\n toString(): string {\n return `MMKV (${this.id}): [${this.getAllKeys().join(', ')}]`;\n }\n toJSON(): object {\n return {\n [this.id]: this.getAllKeys(),\n };\n }\n\n addOnValueChangedListener(onValueChanged: (key: string) => void): Listener {\n this.onValueChangedListeners.push(onValueChanged);\n\n return {\n remove: () => {\n const index = this.onValueChangedListeners.indexOf(onValueChanged);\n if (index !== -1) {\n this.onValueChangedListeners.splice(index, 1);\n }\n },\n };\n }\n}\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
|
|
10
|
+
// Root directory of all MMKV stores
|
|
11
|
+
const ROOT_DIRECTORY = null;
|
|
12
|
+
|
|
13
|
+
const createMMKV = config => {
|
|
14
|
+
// Check if the constructor exists. If not, try installing the JSI bindings.
|
|
15
|
+
if (global.mmkvCreateNewInstance == null) {
|
|
16
|
+
// Get the native MMKV ReactModule
|
|
17
|
+
const MMKVModule = _reactNative.NativeModules.MMKV;
|
|
18
|
+
|
|
19
|
+
if (MMKVModule == null) {
|
|
20
|
+
var _NativeModules$Native, _NativeModules$Native2;
|
|
21
|
+
|
|
22
|
+
let message = 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
|
|
23
|
+
message += '\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
|
|
24
|
+
|
|
25
|
+
if (_reactNative.Platform.OS === 'ios' || _reactNative.Platform.OS === 'macos') {
|
|
26
|
+
message += '\n* Make sure you ran `pod install` in the ios/ directory.';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (_reactNative.Platform.OS === 'android') {
|
|
30
|
+
message += '\n* Make sure gradle is synced.';
|
|
31
|
+
} // check if Expo
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const ExpoConstants = (_NativeModules$Native = _reactNative.NativeModules.NativeUnimoduleProxy) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;
|
|
35
|
+
|
|
36
|
+
if (ExpoConstants != null) {
|
|
37
|
+
if (ExpoConstants.appOwnership === 'expo') {
|
|
38
|
+
// We're running Expo Go
|
|
39
|
+
throw new Error('react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
|
|
40
|
+
} else {
|
|
41
|
+
// We're running Expo bare / standalone
|
|
42
|
+
message += '\n* Make sure you ran `expo prebuild`.';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
message += '\n* Make sure you rebuilt the app.';
|
|
47
|
+
throw new Error(message);
|
|
48
|
+
} // Check if we are running on-device (JSI)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if (global.nativeCallSyncHook == null || MMKVModule.install == null) {
|
|
52
|
+
throw new Error('Failed to create a new MMKV instance: React Native is not running on-device. MMKV can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.');
|
|
53
|
+
} // Call the synchronous blocking install() function
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
57
|
+
if (result !== true) throw new Error(`Failed to create a new MMKV instance: The native MMKV Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`); // Check again if the constructor now exists. If not, throw an error.
|
|
58
|
+
|
|
59
|
+
if (global.mmkvCreateNewInstance == null) throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Are you trying to use MMKV from different JS Runtimes?');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return global.mmkvCreateNewInstance(config);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
exports.createMMKV = createMMKV;
|
|
66
|
+
//# sourceMappingURL=createMMKV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.ts"],"names":["ROOT_DIRECTORY","createMMKV","config","global","mmkvCreateNewInstance","MMKVModule","NativeModules","MMKV","message","Platform","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result"],"mappings":";;;;;;;AAAA;;AASA;AACA,MAAMA,cAA6B,GAAG,IAAtC;;AAEO,MAAMC,UAAU,GAAIC,MAAD,IAA2C;AACnE;AACA,MAAIC,MAAM,CAACC,qBAAP,IAAgC,IAApC,EAA0C;AACxC;AACA,UAAMC,UAAU,GAAGC,2BAAcC,IAAjC;;AACA,QAAIF,UAAU,IAAI,IAAlB,EAAwB;AAAA;;AACtB,UAAIG,OAAO,GACT,kFADF;AAEAA,MAAAA,OAAO,IACL,mGADF;;AAEA,UAAIC,sBAASC,EAAT,KAAgB,KAAhB,IAAyBD,sBAASC,EAAT,KAAgB,OAA7C,EAAsD;AACpDF,QAAAA,OAAO,IAAI,4DAAX;AACD;;AACD,UAAIC,sBAASC,EAAT,KAAgB,SAApB,EAA+B;AAC7BF,QAAAA,OAAO,IAAI,iCAAX;AACD,OAVqB,CAWtB;;;AACA,YAAMG,aAAa,4BACjBL,2BAAcM,oBADG,oFACjB,sBAAoCC,gBADnB,2DACjB,uBAAsDC,iBADxD;;AAEA,UAAIH,aAAa,IAAI,IAArB,EAA2B;AACzB,YAAIA,aAAa,CAACI,YAAd,KAA+B,MAAnC,EAA2C;AACzC;AACA,gBAAM,IAAIC,KAAJ,CACJ,+GADI,CAAN;AAGD,SALD,MAKO;AACL;AACAR,UAAAA,OAAO,IAAI,wCAAX;AACD;AACF;;AAEDA,MAAAA,OAAO,IAAI,oCAAX;AACA,YAAM,IAAIQ,KAAJ,CAAUR,OAAV,CAAN;AACD,KA/BuC,CAiCxC;;;AACA,QAAIL,MAAM,CAACc,kBAAP,IAA6B,IAA7B,IAAqCZ,UAAU,CAACa,OAAX,IAAsB,IAA/D,EAAqE;AACnE,YAAM,IAAIF,KAAJ,CACJ,sQADI,CAAN;AAGD,KAtCuC,CAwCxC;;;AACA,UAAMG,MAAM,GAAGd,UAAU,CAACa,OAAX,CAAmBlB,cAAnB,CAAf;AACA,QAAImB,MAAM,KAAK,IAAf,EACE,MAAM,IAAIH,KAAJ,CACH,sJAAqJG,MAAO,EADzJ,CAAN,CA3CsC,CA+CxC;;AACA,QAAIhB,MAAM,CAACC,qBAAP,IAAgC,IAApC,EACE,MAAM,IAAIY,KAAJ,CACJ,8IADI,CAAN;AAGH;;AAED,SAAOb,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CAzDM","sourcesContent":["import { NativeModules, Platform } from 'react-native';\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\n\n// global func declaration for JSI functions\ndeclare global {\n function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;\n function nativeCallSyncHook(): unknown;\n}\n\n// Root directory of all MMKV stores\nconst ROOT_DIRECTORY: string | null = null;\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n // Check if the constructor exists. If not, try installing the JSI bindings.\n if (global.mmkvCreateNewInstance == null) {\n // Get the native MMKV ReactModule\n const MMKVModule = NativeModules.MMKV;\n if (MMKVModule == null) {\n let message =\n 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';\n message +=\n '\\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';\n if (Platform.OS === 'ios' || Platform.OS === 'macos') {\n message += '\\n* Make sure you ran `pod install` in the ios/ directory.';\n }\n if (Platform.OS === 'android') {\n message += '\\n* Make sure gradle is synced.';\n }\n // check if Expo\n const ExpoConstants =\n NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;\n if (ExpoConstants != null) {\n if (ExpoConstants.appOwnership === 'expo') {\n // We're running Expo Go\n throw new Error(\n 'react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'\n );\n } else {\n // We're running Expo bare / standalone\n message += '\\n* Make sure you ran `expo prebuild`.';\n }\n }\n\n message += '\\n* Make sure you rebuilt the app.';\n throw new Error(message);\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || MMKVModule.install == null) {\n throw new Error(\n 'Failed to create a new MMKV instance: React Native is not running on-device. MMKV can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = MMKVModule.install(ROOT_DIRECTORY);\n if (result !== true)\n throw new Error(\n `Failed to create a new MMKV instance: The native MMKV Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.mmkvCreateNewInstance == null)\n throw new Error(\n 'Failed to create a new MMKV instance, the native initializer function does not exist. Are you trying to use MMKV from different JS Runtimes?'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createMMKV = void 0;
|
|
7
|
+
|
|
8
|
+
var _window$document;
|
|
9
|
+
|
|
10
|
+
/* global localStorage */
|
|
11
|
+
const canUseDOM = typeof window !== 'undefined' && ((_window$document = window.document) === null || _window$document === void 0 ? void 0 : _window$document.createElement) != null;
|
|
12
|
+
|
|
13
|
+
const createMMKV = config => {
|
|
14
|
+
if (config.id !== 'mmkv.default') {
|
|
15
|
+
throw new Error("MMKV: 'id' is not supported on Web!");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (config.encryptionKey != null) {
|
|
19
|
+
throw new Error("MMKV: 'encryptionKey' is not supported on Web!");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (config.path != null) {
|
|
23
|
+
throw new Error("MMKV: 'path' is not supported on Web!");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const storage = () => {
|
|
27
|
+
var _ref, _global$localStorage, _global, _window;
|
|
28
|
+
|
|
29
|
+
if (!canUseDOM) {
|
|
30
|
+
throw new Error('Tried to access storage on the server. Did you forget to call this in useEffect?');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const domStorage = (_ref = (_global$localStorage = (_global = global) === null || _global === void 0 ? void 0 : _global.localStorage) !== null && _global$localStorage !== void 0 ? _global$localStorage : (_window = window) === null || _window === void 0 ? void 0 : _window.localStorage) !== null && _ref !== void 0 ? _ref : localStorage;
|
|
34
|
+
|
|
35
|
+
if (domStorage == null) {
|
|
36
|
+
throw new Error(`Could not find 'localStorage' instance!`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return domStorage;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
clearAll: () => storage().clear(),
|
|
44
|
+
delete: key => storage().removeItem(key),
|
|
45
|
+
set: (key, value) => storage().setItem(key, value.toString()),
|
|
46
|
+
getString: key => {
|
|
47
|
+
var _storage$getItem;
|
|
48
|
+
|
|
49
|
+
return (_storage$getItem = storage().getItem(key)) !== null && _storage$getItem !== void 0 ? _storage$getItem : undefined;
|
|
50
|
+
},
|
|
51
|
+
getNumber: key => {
|
|
52
|
+
var _storage$getItem2;
|
|
53
|
+
|
|
54
|
+
return Number((_storage$getItem2 = storage().getItem(key)) !== null && _storage$getItem2 !== void 0 ? _storage$getItem2 : 0);
|
|
55
|
+
},
|
|
56
|
+
getBoolean: key => {
|
|
57
|
+
var _storage$getItem3;
|
|
58
|
+
|
|
59
|
+
return Boolean((_storage$getItem3 = storage().getItem(key)) !== null && _storage$getItem3 !== void 0 ? _storage$getItem3 : false);
|
|
60
|
+
},
|
|
61
|
+
getAllKeys: () => Object.keys(storage()),
|
|
62
|
+
contains: key => storage().getItem(key) != null,
|
|
63
|
+
recrypt: () => {
|
|
64
|
+
throw new Error('`recrypt(..)` is not supported on Web!');
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
exports.createMMKV = createMMKV;
|
|
70
|
+
//# sourceMappingURL=createMMKV.web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMMKV.web.ts"],"names":["canUseDOM","window","document","createElement","createMMKV","config","id","Error","encryptionKey","path","storage","domStorage","global","localStorage","clearAll","clear","delete","key","removeItem","set","value","setItem","toString","getString","getItem","undefined","getNumber","Number","getBoolean","Boolean","getAllKeys","Object","keys","contains","recrypt"],"mappings":";;;;;;;;;AAAA;AAGA,MAAMA,SAAS,GACb,OAAOC,MAAP,KAAkB,WAAlB,IAAiC,qBAAAA,MAAM,CAACC,QAAP,sEAAiBC,aAAjB,KAAkC,IADrE;;AAGO,MAAMC,UAAU,GAAIC,MAAD,IAA2C;AACnE,MAAIA,MAAM,CAACC,EAAP,KAAc,cAAlB,EAAkC;AAChC,UAAM,IAAIC,KAAJ,CAAU,qCAAV,CAAN;AACD;;AACD,MAAIF,MAAM,CAACG,aAAP,IAAwB,IAA5B,EAAkC;AAChC,UAAM,IAAID,KAAJ,CAAU,gDAAV,CAAN;AACD;;AACD,MAAIF,MAAM,CAACI,IAAP,IAAe,IAAnB,EAAyB;AACvB,UAAM,IAAIF,KAAJ,CAAU,uCAAV,CAAN;AACD;;AAED,QAAMG,OAAO,GAAG,MAAM;AAAA;;AACpB,QAAI,CAACV,SAAL,EAAgB;AACd,YAAM,IAAIO,KAAJ,CACJ,kFADI,CAAN;AAGD;;AACD,UAAMI,UAAU,8CACdC,MADc,4CACd,QAAQC,YADM,kFACUZ,MADV,4CACU,QAAQY,YADlB,uCACkCA,YADlD;;AAEA,QAAIF,UAAU,IAAI,IAAlB,EAAwB;AACtB,YAAM,IAAIJ,KAAJ,CAAW,yCAAX,CAAN;AACD;;AACD,WAAOI,UAAP;AACD,GAZD;;AAcA,SAAO;AACLG,IAAAA,QAAQ,EAAE,MAAMJ,OAAO,GAAGK,KAAV,EADX;AAELC,IAAAA,MAAM,EAAGC,GAAD,IAASP,OAAO,GAAGQ,UAAV,CAAqBD,GAArB,CAFZ;AAGLE,IAAAA,GAAG,EAAE,CAACF,GAAD,EAAMG,KAAN,KAAgBV,OAAO,GAAGW,OAAV,CAAkBJ,GAAlB,EAAuBG,KAAK,CAACE,QAAN,EAAvB,CAHhB;AAILC,IAAAA,SAAS,EAAGN,GAAD;AAAA;;AAAA,iCAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAT,+DAAmCQ,SAAnC;AAAA,KAJN;AAKLC,IAAAA,SAAS,EAAGT,GAAD;AAAA;;AAAA,aAASU,MAAM,sBAACjB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,CAA3B,CAAf;AAAA,KALN;AAMLW,IAAAA,UAAU,EAAGX,GAAD;AAAA;;AAAA,aAASY,OAAO,sBAACnB,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,CAAD,iEAA2B,KAA3B,CAAhB;AAAA,KANP;AAOLa,IAAAA,UAAU,EAAE,MAAMC,MAAM,CAACC,IAAP,CAAYtB,OAAO,EAAnB,CAPb;AAQLuB,IAAAA,QAAQ,EAAGhB,GAAD,IAASP,OAAO,GAAGc,OAAV,CAAkBP,GAAlB,KAA0B,IARxC;AASLiB,IAAAA,OAAO,EAAE,MAAM;AACb,YAAM,IAAI3B,KAAJ,CAAU,wCAAV,CAAN;AACD;AAXI,GAAP;AAaD,CAtCM","sourcesContent":["/* global localStorage */\nimport type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';\n\nconst canUseDOM =\n typeof window !== 'undefined' && window.document?.createElement != null;\n\nexport const createMMKV = (config: MMKVConfiguration): NativeMMKV => {\n if (config.id !== 'mmkv.default') {\n throw new Error(\"MMKV: 'id' is not supported on Web!\");\n }\n if (config.encryptionKey != null) {\n throw new Error(\"MMKV: 'encryptionKey' is not supported on Web!\");\n }\n if (config.path != null) {\n throw new Error(\"MMKV: 'path' is not supported on Web!\");\n }\n\n const storage = () => {\n if (!canUseDOM) {\n throw new Error(\n 'Tried to access storage on the server. Did you forget to call this in useEffect?'\n );\n }\n const domStorage =\n global?.localStorage ?? window?.localStorage ?? localStorage;\n if (domStorage == null) {\n throw new Error(`Could not find 'localStorage' instance!`);\n }\n return domStorage;\n };\n\n return {\n clearAll: () => storage().clear(),\n delete: (key) => storage().removeItem(key),\n set: (key, value) => storage().setItem(key, value.toString()),\n getString: (key) => storage().getItem(key) ?? undefined,\n getNumber: (key) => Number(storage().getItem(key) ?? 0),\n getBoolean: (key) => Boolean(storage().getItem(key) ?? false),\n getAllKeys: () => Object.keys(storage()),\n contains: (key) => storage().getItem(key) != null,\n recrypt: () => {\n throw new Error('`recrypt(..)` is not supported on Web!');\n },\n };\n};\n"]}
|