react-native-mmkv 2.10.1 → 2.11.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/ios/JSIUtils.mm CHANGED
@@ -7,189 +7,161 @@
7
7
  //
8
8
 
9
9
  #include "JSIUtils.h"
10
- #import <React/RCTConvert.h>
11
10
  #import <React/RCTBridgeModule.h>
11
+ #import <React/RCTConvert.h>
12
12
 
13
13
  /**
14
14
  * All helper functions are ObjC++ specific.
15
15
  */
16
- jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime &runtime, NSNumber *value)
17
- {
18
- return jsi::Value((bool)[value boolValue]);
16
+ jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime& runtime, NSNumber* value) {
17
+ return jsi::Value((bool)[value boolValue]);
19
18
  }
20
19
 
21
- jsi::Value convertNSNumberToJSINumber(jsi::Runtime &runtime, NSNumber *value)
22
- {
23
- return jsi::Value([value doubleValue]);
20
+ jsi::Value convertNSNumberToJSINumber(jsi::Runtime& runtime, NSNumber* value) {
21
+ return jsi::Value([value doubleValue]);
24
22
  }
25
23
 
26
- jsi::String convertNSStringToJSIString(jsi::Runtime &runtime, NSString *value)
27
- {
28
- return jsi::String::createFromUtf8(runtime, [value UTF8String] ?: "");
24
+ jsi::String convertNSStringToJSIString(jsi::Runtime& runtime, NSString* value) {
25
+ return jsi::String::createFromUtf8(runtime, [value UTF8String] ?: "");
29
26
  }
30
27
 
31
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value);
28
+ jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value);
32
29
 
33
- jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, NSDictionary *value)
34
- {
35
- jsi::Object result = jsi::Object(runtime);
36
- for (NSString *k in value) {
37
- result.setProperty(runtime, [k UTF8String], convertObjCObjectToJSIValue(runtime, value[k]));
38
- }
39
- return result;
30
+ jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime& runtime, NSDictionary* value) {
31
+ jsi::Object result = jsi::Object(runtime);
32
+ for (NSString* k in value) {
33
+ result.setProperty(runtime, [k UTF8String], convertObjCObjectToJSIValue(runtime, value[k]));
34
+ }
35
+ return result;
40
36
  }
41
37
 
42
- jsi::Array convertNSArrayToJSIArray(jsi::Runtime &runtime, NSArray *value)
43
- {
44
- jsi::Array result = jsi::Array(runtime, value.count);
45
- for (size_t i = 0; i < value.count; i++) {
46
- result.setValueAtIndex(runtime, i, convertObjCObjectToJSIValue(runtime, value[i]));
47
- }
48
- return result;
38
+ jsi::Array convertNSArrayToJSIArray(jsi::Runtime& runtime, NSArray* value) {
39
+ jsi::Array result = jsi::Array(runtime, value.count);
40
+ for (size_t i = 0; i < value.count; i++) {
41
+ result.setValueAtIndex(runtime, i, convertObjCObjectToJSIValue(runtime, value[i]));
42
+ }
43
+ return result;
49
44
  }
50
45
 
51
- std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime &runtime, NSArray *value)
52
- {
53
- std::vector<jsi::Value> result;
54
- for (size_t i = 0; i < value.count; i++) {
55
- result.emplace_back(convertObjCObjectToJSIValue(runtime, value[i]));
56
- }
57
- return result;
46
+ std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime& runtime, NSArray* value) {
47
+ std::vector<jsi::Value> result;
48
+ for (size_t i = 0; i < value.count; i++) {
49
+ result.emplace_back(convertObjCObjectToJSIValue(runtime, value[i]));
50
+ }
51
+ return result;
58
52
  }
59
53
 
60
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value)
61
- {
62
- if ([value isKindOfClass:[NSString class]]) {
63
- return convertNSStringToJSIString(runtime, (NSString *)value);
64
- } else if ([value isKindOfClass:[NSNumber class]]) {
65
- if ([value isKindOfClass:[@YES class]]) {
66
- return convertNSNumberToJSIBoolean(runtime, (NSNumber *)value);
67
- }
68
- return convertNSNumberToJSINumber(runtime, (NSNumber *)value);
69
- } else if ([value isKindOfClass:[NSDictionary class]]) {
70
- return convertNSDictionaryToJSIObject(runtime, (NSDictionary *)value);
71
- } else if ([value isKindOfClass:[NSArray class]]) {
72
- return convertNSArrayToJSIArray(runtime, (NSArray *)value);
73
- } else if (value == (id)kCFNull) {
74
- return jsi::Value::null();
54
+ jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value) {
55
+ if ([value isKindOfClass:[NSString class]]) {
56
+ return convertNSStringToJSIString(runtime, (NSString*)value);
57
+ } else if ([value isKindOfClass:[NSNumber class]]) {
58
+ if ([value isKindOfClass:[@YES class]]) {
59
+ return convertNSNumberToJSIBoolean(runtime, (NSNumber*)value);
75
60
  }
76
- return jsi::Value::undefined();
61
+ return convertNSNumberToJSINumber(runtime, (NSNumber*)value);
62
+ } else if ([value isKindOfClass:[NSDictionary class]]) {
63
+ return convertNSDictionaryToJSIObject(runtime, (NSDictionary*)value);
64
+ } else if ([value isKindOfClass:[NSArray class]]) {
65
+ return convertNSArrayToJSIArray(runtime, (NSArray*)value);
66
+ } else if (value == (id)kCFNull) {
67
+ return jsi::Value::null();
68
+ }
69
+ return jsi::Value::undefined();
77
70
  }
78
71
 
79
- id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value);
72
+ id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value);
80
73
 
81
- NSString *convertJSIStringToNSString(jsi::Runtime &runtime, const jsi::String &value)
82
- {
83
- auto string = value.utf8(runtime);
84
- return [NSString stringWithUTF8String:string.c_str()];
74
+ NSString* convertJSIStringToNSString(jsi::Runtime& runtime, const jsi::String& value) {
75
+ auto string = value.utf8(runtime);
76
+ return [NSString stringWithUTF8String:string.c_str()];
85
77
  }
86
78
 
87
- NSArray *convertJSIArrayToNSArray(jsi::Runtime &runtime,
88
- const jsi::Array &value)
89
- {
90
- size_t size = value.size(runtime);
91
- NSMutableArray *result = [NSMutableArray new];
92
- for (size_t i = 0; i < size; i++) {
93
- // Insert kCFNull when it's `undefined` value to preserve the indices.
94
- [result
95
- addObject:convertJSIValueToObjCObject(runtime, value.getValueAtIndex(runtime, i)) ?: (id)kCFNull];
96
- }
97
- return [result copy];
79
+ NSArray* convertJSIArrayToNSArray(jsi::Runtime& runtime, const jsi::Array& value) {
80
+ size_t size = value.size(runtime);
81
+ NSMutableArray* result = [NSMutableArray new];
82
+ for (size_t i = 0; i < size; i++) {
83
+ // Insert kCFNull when it's `undefined` value to preserve the indices.
84
+ [result addObject:convertJSIValueToObjCObject(runtime, value.getValueAtIndex(runtime, i))
85
+ ?: (id)kCFNull];
86
+ }
87
+ return [result copy];
98
88
  }
99
89
 
100
- NSDictionary *convertJSIObjectToNSDictionary(jsi::Runtime &runtime,
101
- const jsi::Object &value)
102
- {
103
- jsi::Array propertyNames = value.getPropertyNames(runtime);
104
- size_t size = propertyNames.size(runtime);
105
- NSMutableDictionary *result = [NSMutableDictionary new];
106
- for (size_t i = 0; i < size; i++) {
107
- jsi::String name = propertyNames.getValueAtIndex(runtime, i).getString(runtime);
108
- NSString *k = convertJSIStringToNSString(runtime, name);
109
- id v = convertJSIValueToObjCObject(runtime, value.getProperty(runtime, name));
110
- if (v) {
111
- result[k] = v;
112
- }
90
+ NSDictionary* convertJSIObjectToNSDictionary(jsi::Runtime& runtime, const jsi::Object& value) {
91
+ jsi::Array propertyNames = value.getPropertyNames(runtime);
92
+ size_t size = propertyNames.size(runtime);
93
+ NSMutableDictionary* result = [NSMutableDictionary new];
94
+ for (size_t i = 0; i < size; i++) {
95
+ jsi::String name = propertyNames.getValueAtIndex(runtime, i).getString(runtime);
96
+ NSString* k = convertJSIStringToNSString(runtime, name);
97
+ id v = convertJSIValueToObjCObject(runtime, value.getProperty(runtime, name));
98
+ if (v) {
99
+ result[k] = v;
113
100
  }
114
- return [result copy];
101
+ }
102
+ return [result copy];
115
103
  }
116
104
 
117
- RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime &runtime,
118
- const jsi::Function &value)
119
- {
120
- __block auto cb = value.getFunction(runtime);
105
+ RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime& runtime,
106
+ const jsi::Function& value) {
107
+ __block auto cb = value.getFunction(runtime);
121
108
 
122
- return ^(NSArray *responses) {
123
- cb.call(runtime, convertNSArrayToJSIArray(runtime, responses), 2);
124
- };
109
+ return ^(NSArray* responses) {
110
+ cb.call(runtime, convertNSArrayToJSIArray(runtime, responses), 2);
111
+ };
125
112
  }
126
113
 
127
- id convertJSIValueToObjCObject(jsi::Runtime &runtime,
128
- const jsi::Value &value)
129
- {
130
- if (value.isUndefined() || value.isNull()) {
131
- return nil;
132
- }
133
- if (value.isBool()) {
134
- return @(value.getBool());
135
- }
136
- if (value.isNumber()) {
137
- return @(value.getNumber());
138
- }
139
- if (value.isString()) {
140
- return convertJSIStringToNSString(runtime, value.getString(runtime));
114
+ id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value) {
115
+ if (value.isUndefined() || value.isNull()) {
116
+ return nil;
117
+ }
118
+ if (value.isBool()) {
119
+ return @(value.getBool());
120
+ }
121
+ if (value.isNumber()) {
122
+ return @(value.getNumber());
123
+ }
124
+ if (value.isString()) {
125
+ return convertJSIStringToNSString(runtime, value.getString(runtime));
126
+ }
127
+ if (value.isObject()) {
128
+ jsi::Object o = value.getObject(runtime);
129
+ if (o.isArray(runtime)) {
130
+ return convertJSIArrayToNSArray(runtime, o.getArray(runtime));
141
131
  }
142
- if (value.isObject()) {
143
- jsi::Object o = value.getObject(runtime);
144
- if (o.isArray(runtime)) {
145
- return convertJSIArrayToNSArray(runtime, o.getArray(runtime));
146
- }
147
- if (o.isFunction(runtime)) {
148
- return convertJSIFunctionToCallback(runtime,
149
- std::move(o.getFunction(runtime)));
150
- }
151
- return convertJSIObjectToNSDictionary(runtime, o);
132
+ if (o.isFunction(runtime)) {
133
+ return convertJSIFunctionToCallback(runtime, std::move(o.getFunction(runtime)));
152
134
  }
135
+ return convertJSIObjectToNSDictionary(runtime, o);
136
+ }
153
137
 
154
- throw std::runtime_error("Unsupported jsi::jsi::Value kind");
138
+ throw std::runtime_error("Unsupported jsi::jsi::Value kind");
155
139
  }
156
140
 
157
- Promise::Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject)
158
- : runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}
141
+ Promise::Promise(jsi::Runtime& rt, jsi::Function resolve, jsi::Function reject)
142
+ : runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}
159
143
 
160
- void Promise::resolve(const jsi::Value &result)
161
- {
162
- resolve_.call(runtime_, result);
144
+ void Promise::resolve(const jsi::Value& result) {
145
+ resolve_.call(runtime_, result);
163
146
  }
164
147
 
165
- void Promise::reject(const std::string &message)
166
- {
167
- jsi::Object error(runtime_);
168
- error.setProperty(runtime_,
169
- "message",
170
- jsi::String::createFromUtf8(runtime_, message));
171
- reject_.call(runtime_, error);
148
+ void Promise::reject(const std::string& message) {
149
+ jsi::Object error(runtime_);
150
+ error.setProperty(runtime_, "message", jsi::String::createFromUtf8(runtime_, message));
151
+ reject_.call(runtime_, error);
172
152
  }
173
153
 
174
- jsi::Value createPromiseAsJSIValue(jsi::Runtime &rt,
175
- const PromiseSetupFunctionType func)
176
- {
177
- jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
178
- jsi::Function fn = jsi::Function::createFromHostFunction(rt,
179
- jsi::PropNameID::forAscii(rt, "fn"),
180
- 2,
181
- [func](jsi::Runtime &rt2,
182
- const jsi::Value &thisVal,
183
- const jsi::Value *args,
184
- size_t count) {
154
+ jsi::Value createPromiseAsJSIValue(jsi::Runtime& rt, const PromiseSetupFunctionType func) {
155
+ jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
156
+ jsi::Function fn = jsi::Function::createFromHostFunction(
157
+ rt, jsi::PropNameID::forAscii(rt, "fn"), 2,
158
+ [func](jsi::Runtime& rt2, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
185
159
  jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
186
160
  jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
187
- auto wrapper = std::make_shared<Promise>(rt2,
188
- std::move(resolve),
189
- std::move(reject));
161
+ auto wrapper = std::make_shared<Promise>(rt2, std::move(resolve), std::move(reject));
190
162
  func(rt2, wrapper);
191
163
  return jsi::Value::undefined();
192
- });
164
+ });
193
165
 
194
- return JSPromise.callAsConstructor(rt, fn);
166
+ return JSPromise.callAsConstructor(rt, fn);
195
167
  }
@@ -9,12 +9,12 @@
9
9
  #pragma once
10
10
 
11
11
  #import <Foundation/Foundation.h>
12
- #import <jsi/jsi.h>
13
12
  #import <MMKV/MMKV.h>
13
+ #import <jsi/jsi.h>
14
14
 
15
15
  using namespace facebook;
16
16
 
17
- class JSI_EXPORT MmkvHostObject: public jsi::HostObject {
17
+ class JSI_EXPORT MmkvHostObject : public jsi::HostObject {
18
18
  public:
19
19
  MmkvHostObject(NSString* instanceId, NSString* path, NSString* cryptKey);
20
20