react-native-mmkv 2.10.2 → 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.
@@ -6,25 +6,24 @@
6
6
  // Copyright © 2021 Facebook. All rights reserved.
7
7
  //
8
8
 
9
- #import <Foundation/Foundation.h>
10
9
  #import "MmkvHostObject.h"
11
- #import "JSIUtils.h"
12
10
  #import "../cpp/TypedArray.h"
11
+ #import "JSIUtils.h"
12
+ #import <Foundation/Foundation.h>
13
13
  #import <vector>
14
14
 
15
- MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* cryptKey)
16
- {
15
+ MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* cryptKey) {
17
16
  NSData* cryptData = cryptKey == nil ? nil : [cryptKey dataUsingEncoding:NSUTF8StringEncoding];
18
-
17
+
19
18
  // Get appGroup value from info.plist using key "AppGroup"
20
- NSString *appGroup = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroup"];
19
+ NSString* appGroup = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroup"];
21
20
  if (appGroup == nil) {
22
- instance = [MMKV mmkvWithID:instanceId cryptKey:cryptData rootPath:path];
21
+ instance = [MMKV mmkvWithID:instanceId cryptKey:cryptData rootPath:path];
23
22
  } else {
24
- if (path != nil) {
25
- NSLog(@"Warning: `path` is ignored when `appGroup` is set!");
26
- }
27
- instance = [MMKV mmkvWithID:instanceId cryptKey:cryptData mode:MMKVMultiProcess];
23
+ if (path != nil) {
24
+ NSLog(@"Warning: `path` is ignored when `appGroup` is set!");
25
+ }
26
+ instance = [MMKV mmkvWithID:instanceId cryptKey:cryptData mode:MMKVMultiProcess];
28
27
  }
29
28
 
30
29
  if (instance == nil) {
@@ -35,7 +34,8 @@ MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* c
35
34
 
36
35
  // Check if encryptionKey is invalid
37
36
  if (cryptData != nil && [cryptData length] > 16) {
38
- throw std::runtime_error("Failed to create MMKV instance! `encryptionKey` cannot be longer than 16 bytes!");
37
+ throw std::runtime_error(
38
+ "Failed to create MMKV instance! `encryptionKey` cannot be longer than 16 bytes!");
39
39
  }
40
40
 
41
41
  if (appGroup == nil) {
@@ -43,7 +43,8 @@ MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* c
43
43
  NSFileManager* fileManager = [[NSFileManager alloc] init];
44
44
  bool pathExists = [fileManager fileExistsAtPath:path isDirectory:nil];
45
45
  if (!pathExists) {
46
- throw std::runtime_error("Failed to create MMKV instance! The given Storage Path does not exist!");
46
+ throw std::runtime_error(
47
+ "Failed to create MMKV instance! The given Storage Path does not exist!");
47
48
  }
48
49
  }
49
50
 
@@ -51,8 +52,7 @@ MmkvHostObject::MmkvHostObject(NSString* instanceId, NSString* path, NSString* c
51
52
  }
52
53
  }
53
54
 
54
- std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt)
55
- {
55
+ std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt) {
56
56
  std::vector<jsi::PropNameID> result;
57
57
  result.push_back(jsi::PropNameID::forUtf8(rt, std::string("set")));
58
58
  result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getBoolean")));
@@ -67,245 +67,228 @@ std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt)
67
67
  return result;
68
68
  }
69
69
 
70
- jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId)
71
- {
70
+ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
72
71
  auto propName = propNameId.utf8(runtime);
73
72
  auto funcName = "MMKV." + propName;
74
73
 
75
74
  if (propName == "set") {
76
75
  // MMKV.set(key: string, value: string | number | bool)
77
- return jsi::Function::createFromHostFunction(runtime,
78
- jsi::PropNameID::forAscii(runtime, funcName),
79
- 2, // key, value
80
- [this](jsi::Runtime& runtime,
81
- const jsi::Value& thisValue,
82
- const jsi::Value* arguments,
83
- size_t count) -> jsi::Value {
84
- if (!arguments[0].isString()) {
85
- throw jsi::JSError(runtime, "MMKV::set: First argument ('key') has to be of type string!");
86
- }
76
+ return jsi::Function::createFromHostFunction(
77
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
78
+ 2, // key, value
79
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
80
+ size_t count) -> jsi::Value {
81
+ if (!arguments[0].isString()) {
82
+ throw jsi::JSError(runtime,
83
+ "MMKV::set: First argument ('key') has to be of type string!");
84
+ }
87
85
 
88
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
89
- if (arguments[1].isBool()) {
90
- // Set as boolean
91
- [instance setBool:arguments[1].getBool() forKey:keyName];
92
- } else if (arguments[1].isNumber()) {
93
- // Set as number (double in JS)
94
- [instance setDouble:arguments[1].getNumber() forKey:keyName];
95
- } else if (arguments[1].isString()) {
96
- // Set as UTF-8 string
97
- auto stringValue = convertJSIStringToNSString(runtime, arguments[1].getString(runtime));
98
- [instance setString:stringValue forKey:keyName];
99
- } else if (arguments[1].isObject()) {
100
- // object
101
- auto object = arguments[1].asObject(runtime);
102
- if (isTypedArray(runtime, object)) {
103
- // Uint8Array
104
- auto typedArray = getTypedArray(runtime, object);
105
- auto bufferValue = typedArray.getBuffer(runtime);
106
- auto data = [[NSData alloc] initWithBytes:bufferValue.data(runtime)
107
- length:bufferValue.length(runtime)];
108
- [instance setData:data forKey:keyName];
109
- } else {
110
- // unknown object
111
- throw jsi::JSError(runtime, "MMKV::set: 'value' argument is an object, but not of type Uint8Array!");
112
- }
113
- } else {
114
- // Invalid type
115
- throw jsi::JSError(runtime, "Second argument ('value') has to be of type bool, number or string!");
116
- }
86
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
87
+ if (arguments[1].isBool()) {
88
+ // Set as boolean
89
+ [instance setBool:arguments[1].getBool() forKey:keyName];
90
+ } else if (arguments[1].isNumber()) {
91
+ // Set as number (double in JS)
92
+ [instance setDouble:arguments[1].getNumber() forKey:keyName];
93
+ } else if (arguments[1].isString()) {
94
+ // Set as UTF-8 string
95
+ auto stringValue = convertJSIStringToNSString(runtime, arguments[1].getString(runtime));
96
+ [instance setString:stringValue forKey:keyName];
97
+ } else if (arguments[1].isObject()) {
98
+ // object
99
+ auto object = arguments[1].asObject(runtime);
100
+ if (isTypedArray(runtime, object)) {
101
+ // Uint8Array
102
+ auto typedArray = getTypedArray(runtime, object);
103
+ auto bufferValue = typedArray.getBuffer(runtime);
104
+ auto data = [[NSData alloc] initWithBytes:bufferValue.data(runtime)
105
+ length:bufferValue.length(runtime)];
106
+ [instance setData:data forKey:keyName];
107
+ } else {
108
+ // unknown object
109
+ throw jsi::JSError(
110
+ runtime, "MMKV::set: 'value' argument is an object, but not of type Uint8Array!");
111
+ }
112
+ } else {
113
+ // Invalid type
114
+ throw jsi::JSError(
115
+ runtime, "Second argument ('value') has to be of type bool, number or string!");
116
+ }
117
117
 
118
- return jsi::Value::undefined();
119
- });
118
+ return jsi::Value::undefined();
119
+ });
120
120
  }
121
121
 
122
122
  if (propName == "getBoolean") {
123
123
  // MMKV.getBoolean(key: string)
124
- return jsi::Function::createFromHostFunction(runtime,
125
- jsi::PropNameID::forAscii(runtime, funcName),
126
- 1, // key
127
- [this](jsi::Runtime& runtime,
128
- const jsi::Value& thisValue,
129
- const jsi::Value* arguments,
130
- size_t count) -> jsi::Value {
131
- if (!arguments[0].isString()) {
132
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
133
- }
124
+ return jsi::Function::createFromHostFunction(
125
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
126
+ 1, // key
127
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
128
+ size_t count) -> jsi::Value {
129
+ if (!arguments[0].isString()) {
130
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
131
+ }
134
132
 
135
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
136
- BOOL hasValue;
137
- auto value = [instance getBoolForKey:keyName defaultValue:false hasValue:&hasValue];
138
- if (hasValue) {
139
- return jsi::Value(value == true);
140
- } else {
141
- return jsi::Value::undefined();
142
- }
143
- });
133
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
134
+ BOOL hasValue;
135
+ auto value = [instance getBoolForKey:keyName defaultValue:false hasValue:&hasValue];
136
+ if (hasValue) {
137
+ return jsi::Value(value == true);
138
+ } else {
139
+ return jsi::Value::undefined();
140
+ }
141
+ });
144
142
  }
145
143
 
146
144
  if (propName == "getString") {
147
145
  // MMKV.getString(key: string)
148
- return jsi::Function::createFromHostFunction(runtime,
149
- jsi::PropNameID::forAscii(runtime, funcName),
150
- 1, // key
151
- [this](jsi::Runtime& runtime,
152
- const jsi::Value& thisValue,
153
- const jsi::Value* arguments,
154
- size_t count) -> jsi::Value {
155
- if (!arguments[0].isString()) {
156
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
157
- }
146
+ return jsi::Function::createFromHostFunction(
147
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
148
+ 1, // key
149
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
150
+ size_t count) -> jsi::Value {
151
+ if (!arguments[0].isString()) {
152
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
153
+ }
158
154
 
159
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
160
- auto value = [instance getStringForKey:keyName];
161
- if (value != nil) {
162
- return convertNSStringToJSIString(runtime, value);
163
- } else {
164
- return jsi::Value::undefined();
165
- }
166
- });
155
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
156
+ auto value = [instance getStringForKey:keyName];
157
+ if (value != nil) {
158
+ return convertNSStringToJSIString(runtime, value);
159
+ } else {
160
+ return jsi::Value::undefined();
161
+ }
162
+ });
167
163
  }
168
164
 
169
165
  if (propName == "getNumber") {
170
166
  // MMKV.getNumber(key: string)
171
- return jsi::Function::createFromHostFunction(runtime,
172
- jsi::PropNameID::forAscii(runtime, funcName),
173
- 1, // key
174
- [this](jsi::Runtime& runtime,
175
- const jsi::Value& thisValue,
176
- const jsi::Value* arguments,
177
- size_t count) -> jsi::Value {
178
- if (!arguments[0].isString()) {
179
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
180
- }
167
+ return jsi::Function::createFromHostFunction(
168
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
169
+ 1, // key
170
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
171
+ size_t count) -> jsi::Value {
172
+ if (!arguments[0].isString()) {
173
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
174
+ }
181
175
 
182
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
183
- BOOL hasValue;
184
- auto value = [instance getDoubleForKey:keyName defaultValue:0.0 hasValue:&hasValue];
185
- if (hasValue) {
186
- return jsi::Value(value);
187
- } else {
188
- return jsi::Value::undefined();
189
- }
190
- });
176
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
177
+ BOOL hasValue;
178
+ auto value = [instance getDoubleForKey:keyName defaultValue:0.0 hasValue:&hasValue];
179
+ if (hasValue) {
180
+ return jsi::Value(value);
181
+ } else {
182
+ return jsi::Value::undefined();
183
+ }
184
+ });
191
185
  }
192
186
 
193
187
  if (propName == "getBuffer") {
194
188
  // MMKV.getBuffer(key: string)
195
- return jsi::Function::createFromHostFunction(runtime,
196
- jsi::PropNameID::forAscii(runtime, funcName),
197
- 1, // key
198
- [this](jsi::Runtime& runtime,
199
- const jsi::Value& thisValue,
200
- const jsi::Value* arguments,
201
- size_t count) -> jsi::Value {
202
- if (!arguments[0].isString()) {
203
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
204
- }
189
+ return jsi::Function::createFromHostFunction(
190
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
191
+ 1, // key
192
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
193
+ size_t count) -> jsi::Value {
194
+ if (!arguments[0].isString()) {
195
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
196
+ }
205
197
 
206
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
207
- auto data = [instance getDataForKey:keyName];
208
- if (data != nil) {
209
- TypedArray<TypedArrayKind::Uint8Array> array(runtime, data.length);
210
- auto charArray = static_cast<const unsigned char*>([data bytes]);
211
- std::vector<unsigned char> vector(data.length);
212
- vector.assign(charArray, charArray + data.length);
213
- array.update(runtime, vector);
214
- return array;
215
- } else {
216
- return jsi::Value::undefined();
217
- }
218
- });
198
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
199
+ auto data = [instance getDataForKey:keyName];
200
+ if (data != nil) {
201
+ TypedArray<TypedArrayKind::Uint8Array> array(runtime, data.length);
202
+ auto charArray = static_cast<const unsigned char*>([data bytes]);
203
+ std::vector<unsigned char> vector(data.length);
204
+ vector.assign(charArray, charArray + data.length);
205
+ array.update(runtime, vector);
206
+ return array;
207
+ } else {
208
+ return jsi::Value::undefined();
209
+ }
210
+ });
219
211
  }
220
212
 
221
213
  if (propName == "contains") {
222
214
  // MMKV.contains(key: string)
223
- return jsi::Function::createFromHostFunction(runtime,
224
- jsi::PropNameID::forAscii(runtime, funcName),
225
- 1, // key
226
- [this](jsi::Runtime& runtime,
227
- const jsi::Value& thisValue,
228
- const jsi::Value* arguments,
229
- size_t count) -> jsi::Value {
230
- if (!arguments[0].isString()) {
231
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
232
- }
215
+ return jsi::Function::createFromHostFunction(
216
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
217
+ 1, // key
218
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
219
+ size_t count) -> jsi::Value {
220
+ if (!arguments[0].isString()) {
221
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
222
+ }
233
223
 
234
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
235
- bool containsKey = [instance containsKey:keyName];
236
- return jsi::Value(containsKey);
237
- });
224
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
225
+ bool containsKey = [instance containsKey:keyName];
226
+ return jsi::Value(containsKey);
227
+ });
238
228
  }
239
229
 
240
230
  if (propName == "delete") {
241
231
  // MMKV.delete(key: string)
242
- return jsi::Function::createFromHostFunction(runtime,
243
- jsi::PropNameID::forAscii(runtime, funcName),
244
- 1, // key
245
- [this](jsi::Runtime& runtime,
246
- const jsi::Value& thisValue,
247
- const jsi::Value* arguments,
248
- size_t count) -> jsi::Value {
249
- if (!arguments[0].isString()) {
250
- throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
251
- }
232
+ return jsi::Function::createFromHostFunction(
233
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
234
+ 1, // key
235
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
236
+ size_t count) -> jsi::Value {
237
+ if (!arguments[0].isString()) {
238
+ throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
239
+ }
252
240
 
253
- auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
254
- [instance removeValueForKey:keyName];
255
- return jsi::Value::undefined();
256
- });
241
+ auto keyName = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
242
+ [instance removeValueForKey:keyName];
243
+ return jsi::Value::undefined();
244
+ });
257
245
  }
258
246
 
259
247
  if (propName == "getAllKeys") {
260
248
  // MMKV.getAllKeys()
261
- return jsi::Function::createFromHostFunction(runtime,
262
- jsi::PropNameID::forAscii(runtime, funcName),
263
- 0,
264
- [this](jsi::Runtime& runtime,
265
- const jsi::Value& thisValue,
266
- const jsi::Value* arguments,
267
- size_t count) -> jsi::Value {
268
- auto keys = [instance allKeys];
269
- return convertNSArrayToJSIArray(runtime, keys);
270
- });
249
+ return jsi::Function::createFromHostFunction(
250
+ runtime, jsi::PropNameID::forAscii(runtime, funcName), 0,
251
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
252
+ size_t count) -> jsi::Value {
253
+ auto keys = [instance allKeys];
254
+ return convertNSArrayToJSIArray(runtime, keys);
255
+ });
271
256
  }
272
257
 
273
258
  if (propName == "clearAll") {
274
259
  // MMKV.clearAll()
275
- return jsi::Function::createFromHostFunction(runtime,
276
- jsi::PropNameID::forAscii(runtime, funcName),
277
- 0,
278
- [this](jsi::Runtime& runtime,
279
- const jsi::Value& thisValue,
280
- const jsi::Value* arguments,
281
- size_t count) -> jsi::Value {
282
- [instance clearAll];
283
- return jsi::Value::undefined();
284
- });
260
+ return jsi::Function::createFromHostFunction(
261
+ runtime, jsi::PropNameID::forAscii(runtime, funcName), 0,
262
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
263
+ size_t count) -> jsi::Value {
264
+ [instance clearAll];
265
+ return jsi::Value::undefined();
266
+ });
285
267
  }
286
268
 
287
269
  if (propName == "recrypt") {
288
270
  // MMKV.recrypt(encryptionKey)
289
- return jsi::Function::createFromHostFunction(runtime,
290
- jsi::PropNameID::forAscii(runtime, funcName),
291
- 1, // encryptionKey
292
- [this](jsi::Runtime& runtime,
293
- const jsi::Value& thisValue,
294
- const jsi::Value* arguments,
295
- size_t count) -> jsi::Value {
296
- if (arguments[0].isUndefined()) {
297
- // reset encryption key to "no encryption"
298
- [instance reKey:nil];
299
- } else if (arguments[0].isString()) {
300
- // reKey(..) with new encryption-key
301
- NSString* encryptionKey = convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
302
- NSData* encryptionKeyBytes = [encryptionKey dataUsingEncoding:NSUTF8StringEncoding];
303
- [instance reKey:encryptionKeyBytes];
304
- } else {
305
- throw jsi::JSError(runtime, "First argument ('encryptionKey') has to be of type string (or undefined)!");
306
- }
307
- return jsi::Value::undefined();
308
- });
271
+ return jsi::Function::createFromHostFunction(
272
+ runtime, jsi::PropNameID::forAscii(runtime, funcName),
273
+ 1, // encryptionKey
274
+ [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
275
+ size_t count) -> jsi::Value {
276
+ if (arguments[0].isUndefined()) {
277
+ // reset encryption key to "no encryption"
278
+ [instance reKey:nil];
279
+ } else if (arguments[0].isString()) {
280
+ // reKey(..) with new encryption-key
281
+ NSString* encryptionKey =
282
+ convertJSIStringToNSString(runtime, arguments[0].getString(runtime));
283
+ NSData* encryptionKeyBytes = [encryptionKey dataUsingEncoding:NSUTF8StringEncoding];
284
+ [instance reKey:encryptionKeyBytes];
285
+ } else {
286
+ throw jsi::JSError(
287
+ runtime,
288
+ "First argument ('encryptionKey') has to be of type string (or undefined)!");
289
+ }
290
+ return jsi::Value::undefined();
291
+ });
309
292
  }
310
293
 
311
294
  return jsi::Value::undefined();
package/ios/MmkvModule.mm CHANGED
@@ -5,81 +5,91 @@
5
5
  #import <React/RCTUtils.h>
6
6
  #import <jsi/jsi.h>
7
7
 
8
- #import <MMKV/MMKV.h>
9
- #import "MmkvHostObject.h"
10
8
  #import "../cpp/TypedArray.h"
9
+ #import "MmkvHostObject.h"
10
+ #import <MMKV/MMKV.h>
11
11
 
12
12
  using namespace facebook;
13
13
 
14
14
  @implementation MmkvModule
15
15
 
16
+ @synthesize bridge=_bridge;
17
+
16
18
  RCT_EXPORT_MODULE(MMKV)
17
19
 
20
+ - (void)setBridge:(RCTBridge *)bridge {
21
+ _bridge = bridge;
22
+ }
23
+
18
24
  + (NSString*)getPropertyAsStringOrNilFromObject:(jsi::Object&)object
19
25
  propertyName:(std::string)propertyName
20
- runtime:(jsi::Runtime&)runtime
21
- {
22
- jsi::Value value = object.getProperty(runtime, propertyName.c_str());
23
- std::string string = value.isString() ? value.asString(runtime).utf8(runtime) : "";
24
- return string.length() > 0 ? [NSString stringWithUTF8String:string.c_str()] : nil;
26
+ runtime:(jsi::Runtime&)runtime {
27
+ jsi::Value value = object.getProperty(runtime, propertyName.c_str());
28
+ std::string string = value.isString() ? value.asString(runtime).utf8(runtime) : "";
29
+ return string.length() > 0 ? [NSString stringWithUTF8String:string.c_str()] : nil;
25
30
  }
26
31
 
27
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install:(nullable NSString*)storageDirectory)
28
- {
29
- NSLog(@"Installing global.mmkvCreateNewInstance...");
30
- RCTBridge* bridge = [RCTBridge currentBridge];
31
- RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge;
32
- if (cxxBridge == nil) {
33
- return @false;
32
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install : (nullable NSString*)storageDirectory) {
33
+ NSLog(@"Installing global.mmkvCreateNewInstance...");
34
+ RCTCxxBridge* cxxBridge = (RCTCxxBridge*)_bridge;
35
+ if (cxxBridge == nil) {
36
+ return @false;
37
+ }
38
+
39
+ using namespace facebook;
40
+
41
+ auto jsiRuntime = (jsi::Runtime*)cxxBridge.runtime;
42
+ if (jsiRuntime == nil) {
43
+ return @false;
44
+ }
45
+ auto& runtime = *jsiRuntime;
46
+
47
+ RCTUnsafeExecuteOnMainQueueSync(^{
48
+ // Get appGroup value from info.plist using key "AppGroup"
49
+ NSString* appGroup = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroup"];
50
+ if (appGroup == nil) {
51
+ [MMKV initializeMMKV:storageDirectory];
52
+ } else {
53
+ NSString* groupDir = [[NSFileManager defaultManager]
54
+ containerURLForSecurityApplicationGroupIdentifier:appGroup]
55
+ .path;
56
+ [MMKV initializeMMKV:nil groupDir:groupDir logLevel:MMKVLogNone];
34
57
  }
58
+ });
35
59
 
36
- using namespace facebook;
37
-
38
- auto jsiRuntime = (jsi::Runtime*) cxxBridge.runtime;
39
- if (jsiRuntime == nil) {
40
- return @false;
41
- }
42
- auto& runtime = *jsiRuntime;
43
-
44
- RCTUnsafeExecuteOnMainQueueSync(^{
45
- // Get appGroup value from info.plist using key "AppGroup"
46
- NSString *appGroup = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroup"];
47
- if (appGroup == nil) {
48
- [MMKV initializeMMKV:storageDirectory];
49
- } else {
50
- NSString *groupDir = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroup].path;
51
- [MMKV initializeMMKV:nil groupDir:groupDir logLevel:MMKVLogNone];
52
- }
53
- });
54
-
55
- // MMKV.createNewInstance()
56
- auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(runtime,
57
- jsi::PropNameID::forAscii(runtime, "mmkvCreateNewInstance"),
58
- 1,
59
- [](jsi::Runtime& runtime,
60
- const jsi::Value& thisValue,
61
- const jsi::Value* arguments,
62
- size_t count) -> jsi::Value {
60
+ // MMKV.createNewInstance()
61
+ auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(
62
+ runtime, jsi::PropNameID::forAscii(runtime, "mmkvCreateNewInstance"), 1,
63
+ [](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
64
+ size_t count) -> jsi::Value {
63
65
  if (count != 1) {
64
- throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
66
+ throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
65
67
  }
66
68
  jsi::Object config = arguments[0].asObject(runtime);
67
69
 
68
- NSString* instanceId = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"id" runtime:runtime];
69
- NSString* path = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"path" runtime:runtime];
70
- NSString* encryptionKey = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"encryptionKey" runtime:runtime];
70
+ NSString* instanceId = [MmkvModule getPropertyAsStringOrNilFromObject:config
71
+ propertyName:"id"
72
+ runtime:runtime];
73
+ NSString* path = [MmkvModule getPropertyAsStringOrNilFromObject:config
74
+ propertyName:"path"
75
+ runtime:runtime];
76
+ NSString* encryptionKey = [MmkvModule getPropertyAsStringOrNilFromObject:config
77
+ propertyName:"encryptionKey"
78
+ runtime:runtime];
71
79
 
72
80
  auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
73
81
  return jsi::Object::createFromHostObject(runtime, instance);
74
- });
75
- runtime.global().setProperty(runtime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
82
+ });
83
+ runtime.global().setProperty(runtime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
76
84
 
77
- // Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets destroyed and the cache gets invalidated.
78
- auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(runtime);
79
- runtime.global().setProperty(runtime, "mmkvArrayBufferPropNameIdCache", jsi::Object::createFromHostObject(runtime, propNameIdCache));
85
+ // Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets
86
+ // destroyed and the cache gets invalidated.
87
+ auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(runtime);
88
+ runtime.global().setProperty(runtime, "mmkvArrayBufferPropNameIdCache",
89
+ jsi::Object::createFromHostObject(runtime, propNameIdCache));
80
90
 
81
- NSLog(@"Installed global.mmkvCreateNewInstance!");
82
- return @true;
91
+ NSLog(@"Installed global.mmkvCreateNewInstance!");
92
+ return @true;
83
93
  }
84
94
 
85
95
  @end