react-native-mmkv 1.6.3 → 2.0.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/README.md +8 -13
- package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +33 -8
- package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +1 -2
- package/ios/{Mmkv.h → MmkvModule.h} +1 -1
- package/ios/MmkvModule.mm +70 -0
- package/lib/commonjs/createMMKV.js +14 -2
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/module/createMMKV.js +12 -2
- package/lib/module/createMMKV.js.map +1 -1
- package/package.json +1 -1
- package/react-native-mmkv.podspec +1 -1
- package/android/src/main/java/com/reactnativemmkv/MmkvModulePackage.java +0 -16
- package/ios/Mmkv.mm +0 -76
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
</div>
|
|
44
44
|
|
|
45
45
|
react-native-mmkv is sponsored by **getstream.io**. <br/>
|
|
46
|
-
[Try the React Native Chat tutorial 💬](https://getstream.io/chat/react-native-chat/tutorial/?utm_source=
|
|
46
|
+
[Try the React Native Chat tutorial 💬](https://getstream.io/chat/react-native-chat/tutorial/?utm_source=Github&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=Github_Jan2022_ReactNative&utm_term=react-native-mmkv)
|
|
47
47
|
|
|
48
48
|
## Benchmark
|
|
49
49
|
|
|
@@ -57,25 +57,19 @@ react-native-mmkv is sponsored by **getstream.io**. <br/>
|
|
|
57
57
|
|
|
58
58
|
## Installation
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
npm install react-native-mmkv
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### iOS
|
|
65
|
-
|
|
66
|
-
iOS installation is automatic, just run:
|
|
60
|
+
### React Native
|
|
67
61
|
|
|
68
62
|
```sh
|
|
63
|
+
yarn add react-native-mmkv
|
|
69
64
|
cd ios && pod install
|
|
70
65
|
```
|
|
71
66
|
|
|
72
|
-
### Android
|
|
73
|
-
|
|
74
|
-
To correctly initialize MMKV on Android, please follow the [Installation guide](./INSTALL.md).
|
|
75
|
-
|
|
76
67
|
### Expo
|
|
77
68
|
|
|
78
|
-
|
|
69
|
+
```sh
|
|
70
|
+
expo install react-native-mmkv
|
|
71
|
+
expo prebuild
|
|
72
|
+
```
|
|
79
73
|
|
|
80
74
|
## Usage
|
|
81
75
|
|
|
@@ -168,6 +162,7 @@ const userObject = JSON.parse(jsonUser)
|
|
|
168
162
|
* [Migrate from AsyncStorage](./docs/MIGRATE_FROM_ASYNC_STORAGE.md)
|
|
169
163
|
* [Using MMKV with redux-persis](./docs/WRAPPER_REDUX.md)
|
|
170
164
|
* [Using MMKV with mobx-persist-storage](./docs/WRAPPER_MOBX.md)
|
|
165
|
+
* [Using MMKV with mobx-persist](./docs/WRAPPER_MOBXPERSIST.md)
|
|
171
166
|
|
|
172
167
|
## Limitations
|
|
173
168
|
|
|
@@ -1,24 +1,49 @@
|
|
|
1
1
|
package com.reactnativemmkv;
|
|
2
2
|
|
|
3
|
+
import android.util.Log;
|
|
4
|
+
|
|
3
5
|
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
4
7
|
|
|
5
8
|
import com.facebook.react.bridge.JavaScriptContextHolder;
|
|
6
9
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
11
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
12
|
+
import com.facebook.react.module.annotations.ReactModule;
|
|
7
13
|
|
|
14
|
+
@ReactModule(name = MmkvModule.NAME)
|
|
8
15
|
public class MmkvModule extends ReactContextBaseJavaModule {
|
|
9
|
-
static
|
|
10
|
-
System.loadLibrary("reactnativemmkv");
|
|
11
|
-
}
|
|
16
|
+
public static final String NAME = "MMKV";
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
public static void install(JavaScriptContextHolder jsContext, String storageDirectory) {
|
|
16
|
-
nativeInstall(jsContext.get(), storageDirectory);
|
|
18
|
+
public MmkvModule(ReactApplicationContext reactContext) {
|
|
19
|
+
super(reactContext);
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
@NonNull
|
|
20
23
|
@Override
|
|
21
24
|
public String getName() {
|
|
22
|
-
return
|
|
25
|
+
return NAME;
|
|
23
26
|
}
|
|
27
|
+
|
|
28
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
29
|
+
public boolean install(@Nullable String rootDirectory) {
|
|
30
|
+
try {
|
|
31
|
+
Log.i(NAME, "Loading C++ library...");
|
|
32
|
+
System.loadLibrary("reactnativemmkv");
|
|
33
|
+
|
|
34
|
+
JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
|
|
35
|
+
if (rootDirectory == null) {
|
|
36
|
+
rootDirectory = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/mmkv";
|
|
37
|
+
}
|
|
38
|
+
Log.i(NAME, "Installing MMKV JSI Bindings for MMKV root directory: " + rootDirectory);
|
|
39
|
+
nativeInstall(jsContext.get(), rootDirectory);
|
|
40
|
+
Log.i(NAME, "Successfully installed MMKV JSI Bindings!");
|
|
41
|
+
return true;
|
|
42
|
+
} catch (Exception exception) {
|
|
43
|
+
Log.e(NAME, "Failed to install MMKV JSI Bindings!", exception);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private static native void nativeInstall(long jsiPtr, String path);
|
|
24
49
|
}
|
|
@@ -15,8 +15,7 @@ public class MmkvPackage implements ReactPackage {
|
|
|
15
15
|
@NonNull
|
|
16
16
|
@Override
|
|
17
17
|
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
|
|
18
|
-
|
|
19
|
-
return Collections.singletonList(new MmkvModule());
|
|
18
|
+
return Collections.singletonList(new MmkvModule(reactContext));
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
@NonNull
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#import "MmkvModule.h"
|
|
2
|
+
#import "JSIUtils.h"
|
|
3
|
+
|
|
4
|
+
#import <React/RCTBridge+Private.h>
|
|
5
|
+
#import <React/RCTUtils.h>
|
|
6
|
+
#import <jsi/jsi.h>
|
|
7
|
+
|
|
8
|
+
#import <MMKV/MMKV.h>
|
|
9
|
+
#import "MmkvHostObject.h"
|
|
10
|
+
|
|
11
|
+
using namespace facebook;
|
|
12
|
+
|
|
13
|
+
@implementation MmkvModule
|
|
14
|
+
@synthesize bridge = _bridge;
|
|
15
|
+
@synthesize methodQueue = _methodQueue;
|
|
16
|
+
|
|
17
|
+
RCT_EXPORT_MODULE(MMKV)
|
|
18
|
+
|
|
19
|
+
+ (NSString*)getPropertyAsStringOrNilFromObject:(jsi::Object&)object propertyName:(std::string)propertyName runtime:(jsi::Runtime&)runtime {
|
|
20
|
+
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
|
|
21
|
+
std::string string = value.isString() ? value.asString(runtime).utf8(runtime) : "";
|
|
22
|
+
return string.length() > 0 ? [NSString stringWithUTF8String:string.c_str()] : nil;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install:(nullable NSString*)storageDirectory)
|
|
26
|
+
{
|
|
27
|
+
NSLog(@"Installing global.mmkvCreateNewInstance...");
|
|
28
|
+
RCTBridge* bridge = [RCTBridge currentBridge];
|
|
29
|
+
RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge;
|
|
30
|
+
if (cxxBridge == nil) {
|
|
31
|
+
return @false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
using namespace facebook;
|
|
35
|
+
|
|
36
|
+
auto jsiRuntime = (jsi::Runtime*) cxxBridge.runtime;
|
|
37
|
+
if (jsiRuntime == nil) {
|
|
38
|
+
return @false;
|
|
39
|
+
}
|
|
40
|
+
auto& runtime = *jsiRuntime;
|
|
41
|
+
|
|
42
|
+
[MMKV initializeMMKV:storageDirectory];
|
|
43
|
+
|
|
44
|
+
// MMKV.createNewInstance()
|
|
45
|
+
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(runtime,
|
|
46
|
+
jsi::PropNameID::forAscii(runtime, "mmkvCreateNewInstance"),
|
|
47
|
+
1,
|
|
48
|
+
[](jsi::Runtime& runtime,
|
|
49
|
+
const jsi::Value& thisValue,
|
|
50
|
+
const jsi::Value* arguments,
|
|
51
|
+
size_t count) -> jsi::Value {
|
|
52
|
+
if (count != 1) {
|
|
53
|
+
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
|
|
54
|
+
}
|
|
55
|
+
jsi::Object config = arguments[0].asObject(runtime);
|
|
56
|
+
|
|
57
|
+
NSString* instanceId = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"id" runtime:runtime];
|
|
58
|
+
NSString* path = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"path" runtime:runtime];
|
|
59
|
+
NSString* encryptionKey = [MmkvModule getPropertyAsStringOrNilFromObject:config propertyName:"encryptionKey" runtime:runtime];
|
|
60
|
+
|
|
61
|
+
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
|
|
62
|
+
return jsi::Object::createFromHostObject(runtime, instance);
|
|
63
|
+
});
|
|
64
|
+
runtime.global().setProperty(runtime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
|
|
65
|
+
|
|
66
|
+
NSLog(@"Installed global.mmkvCreateNewInstance!");
|
|
67
|
+
return @true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@end
|
|
@@ -5,10 +5,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.createMMKV = void 0;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
|
|
10
|
+
// Root directory of all MMKV stores
|
|
11
|
+
const ROOT_DIRECTORY = null;
|
|
12
|
+
|
|
9
13
|
const createMMKV = config => {
|
|
14
|
+
// Check if the constructor exists. If not, try installing the JSI bindings.
|
|
10
15
|
if (global.mmkvCreateNewInstance == null) {
|
|
11
|
-
|
|
16
|
+
// get the MMKV ReactModule
|
|
17
|
+
const MMKVModule = _reactNative.NativeModules.MMKV;
|
|
18
|
+
if (MMKVModule == null || typeof MMKVModule.install !== 'function') throw new Error('The native MMKV Module could not be found! Is it correctly installed and autolinked?'); // Call the synchronous blocking install() function
|
|
19
|
+
|
|
20
|
+
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
21
|
+
if (result !== true) throw new Error(`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.
|
|
22
|
+
|
|
23
|
+
if (global.mmkvCreateNewInstance == null) throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
12
24
|
}
|
|
13
25
|
|
|
14
26
|
return global.mmkvCreateNewInstance(config);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["createMMKV.ts"],"names":["createMMKV","config","global","mmkvCreateNewInstance","Error"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"sources":["createMMKV.ts"],"names":["ROOT_DIRECTORY","createMMKV","config","global","mmkvCreateNewInstance","MMKVModule","NativeModules","MMKV","install","Error","result"],"mappings":";;;;;;;AAAA;;AAQA;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,IAAd,IAAsB,OAAOA,UAAU,CAACG,OAAlB,KAA8B,UAAxD,EACE,MAAM,IAAIC,KAAJ,CACJ,sFADI,CAAN,CAJsC,CAOxC;;AACA,UAAMC,MAAM,GAAGL,UAAU,CAACG,OAAX,CAAmBR,cAAnB,CAAf;AACA,QAAIU,MAAM,KAAK,IAAf,EACE,MAAM,IAAID,KAAJ,CACH,gHAA+GC,MAAO,EADnH,CAAN,CAVsC,CAaxC;;AACA,QAAIP,MAAM,CAACC,qBAAP,IAAgC,IAApC,EACE,MAAM,IAAIK,KAAJ,CACJ,0MADI,CAAN;AAGH;;AAED,SAAON,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CAvBM","sourcesContent":["import { NativeModules } 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}\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 MMKV ReactModule\n const MMKVModule = NativeModules.MMKV;\n if (MMKVModule == null || typeof MMKVModule.install !== 'function')\n throw new Error(\n 'The native MMKV Module could not be found! Is it correctly installed and autolinked?'\n );\n // Call the synchronous blocking install() function\n const result = MMKVModule.install(ROOT_DIRECTORY);\n if (result !== true)\n throw new Error(\n `The native MMKV Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\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. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
|
package/lib/module/createMMKV.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { NativeModules } from 'react-native';
|
|
2
|
+
// Root directory of all MMKV stores
|
|
3
|
+
const ROOT_DIRECTORY = null;
|
|
2
4
|
export const createMMKV = config => {
|
|
5
|
+
// Check if the constructor exists. If not, try installing the JSI bindings.
|
|
3
6
|
if (global.mmkvCreateNewInstance == null) {
|
|
4
|
-
|
|
7
|
+
// get the MMKV ReactModule
|
|
8
|
+
const MMKVModule = NativeModules.MMKV;
|
|
9
|
+
if (MMKVModule == null || typeof MMKVModule.install !== 'function') throw new Error('The native MMKV Module could not be found! Is it correctly installed and autolinked?'); // Call the synchronous blocking install() function
|
|
10
|
+
|
|
11
|
+
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
12
|
+
if (result !== true) throw new Error(`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.
|
|
13
|
+
|
|
14
|
+
if (global.mmkvCreateNewInstance == null) throw new Error('Failed to create a new MMKV instance, the native initializer function does not exist. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!');
|
|
5
15
|
}
|
|
6
16
|
|
|
7
17
|
return global.mmkvCreateNewInstance(config);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["createMMKV.ts"],"names":["createMMKV","config","global","mmkvCreateNewInstance","Error"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["createMMKV.ts"],"names":["NativeModules","ROOT_DIRECTORY","createMMKV","config","global","mmkvCreateNewInstance","MMKVModule","MMKV","install","Error","result"],"mappings":"AAAA,SAASA,aAAT,QAA8B,cAA9B;AAQA;AACA,MAAMC,cAA6B,GAAG,IAAtC;AAEA,OAAO,MAAMC,UAAU,GAAIC,MAAD,IAA2C;AACnE;AACA,MAAIC,MAAM,CAACC,qBAAP,IAAgC,IAApC,EAA0C;AACxC;AACA,UAAMC,UAAU,GAAGN,aAAa,CAACO,IAAjC;AACA,QAAID,UAAU,IAAI,IAAd,IAAsB,OAAOA,UAAU,CAACE,OAAlB,KAA8B,UAAxD,EACE,MAAM,IAAIC,KAAJ,CACJ,sFADI,CAAN,CAJsC,CAOxC;;AACA,UAAMC,MAAM,GAAGJ,UAAU,CAACE,OAAX,CAAmBP,cAAnB,CAAf;AACA,QAAIS,MAAM,KAAK,IAAf,EACE,MAAM,IAAID,KAAJ,CACH,gHAA+GC,MAAO,EADnH,CAAN,CAVsC,CAaxC;;AACA,QAAIN,MAAM,CAACC,qBAAP,IAAgC,IAApC,EACE,MAAM,IAAII,KAAJ,CACJ,0MADI,CAAN;AAGH;;AAED,SAAOL,MAAM,CAACC,qBAAP,CAA6BF,MAA7B,CAAP;AACD,CAvBM","sourcesContent":["import { NativeModules } 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}\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 MMKV ReactModule\n const MMKVModule = NativeModules.MMKV;\n if (MMKVModule == null || typeof MMKVModule.install !== 'function')\n throw new Error(\n 'The native MMKV Module could not be found! Is it correctly installed and autolinked?'\n );\n // Call the synchronous blocking install() function\n const result = MMKVModule.install(ROOT_DIRECTORY);\n if (result !== true)\n throw new Error(\n `The native MMKV Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\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. Is the native MMKV library correctly installed? Make sure to disable any remote debugger (e.g. Chrome) to use JSI!'\n );\n }\n\n return global.mmkvCreateNewInstance(config);\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-mmkv",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "The fastest key/value storage for React Native. ~30x faster than AsyncStorage! Works on Android, iOS and Web.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -17,7 +17,7 @@ Pod::Spec.new do |s|
|
|
|
17
17
|
# Note how this does not include headers, since those can nameclash.
|
|
18
18
|
s.source_files = [
|
|
19
19
|
"ios/**/*.{m,mm}",
|
|
20
|
-
"ios/
|
|
20
|
+
"ios/MmkvModule.h"
|
|
21
21
|
]
|
|
22
22
|
# Any private headers that are not globally unique should be mentioned here.
|
|
23
23
|
# Otherwise there will be a nameclash, since CocoaPods flattens out any header directories
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
package com.reactnativemmkv;
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.bridge.JSIModulePackage;
|
|
4
|
-
import com.facebook.react.bridge.JSIModuleSpec;
|
|
5
|
-
import com.facebook.react.bridge.JavaScriptContextHolder;
|
|
6
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
-
import java.util.Collections;
|
|
8
|
-
import java.util.List;
|
|
9
|
-
|
|
10
|
-
public class MmkvModulePackage implements JSIModulePackage {
|
|
11
|
-
@Override
|
|
12
|
-
public List<JSIModuleSpec> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
|
|
13
|
-
MmkvModule.install(jsContext, reactApplicationContext.getFilesDir().getAbsolutePath() + "/mmkv");
|
|
14
|
-
return Collections.emptyList();
|
|
15
|
-
}
|
|
16
|
-
}
|
package/ios/Mmkv.mm
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
#import "Mmkv.h"
|
|
2
|
-
#import "JSIUtils.h"
|
|
3
|
-
|
|
4
|
-
#import <React/RCTBridge+Private.h>
|
|
5
|
-
#import <React/RCTUtils.h>
|
|
6
|
-
#import <jsi/jsi.h>
|
|
7
|
-
|
|
8
|
-
#import <MMKV/MMKV.h>
|
|
9
|
-
#import "MmkvHostObject.h"
|
|
10
|
-
|
|
11
|
-
using namespace facebook;
|
|
12
|
-
|
|
13
|
-
@implementation Mmkv
|
|
14
|
-
@synthesize bridge = _bridge;
|
|
15
|
-
@synthesize methodQueue = _methodQueue;
|
|
16
|
-
|
|
17
|
-
RCT_EXPORT_MODULE()
|
|
18
|
-
|
|
19
|
-
+ (BOOL)requiresMainQueueSetup {
|
|
20
|
-
return YES;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static void install(jsi::Runtime & jsiRuntime)
|
|
24
|
-
{
|
|
25
|
-
// MMKV.createNewInstance()
|
|
26
|
-
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(jsiRuntime,
|
|
27
|
-
jsi::PropNameID::forAscii(jsiRuntime, "mmkvCreateNewInstance"),
|
|
28
|
-
1,
|
|
29
|
-
[](jsi::Runtime& runtime,
|
|
30
|
-
const jsi::Value& thisValue,
|
|
31
|
-
const jsi::Value* arguments,
|
|
32
|
-
size_t count) -> jsi::Value {
|
|
33
|
-
if (count != 1) {
|
|
34
|
-
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
|
|
35
|
-
}
|
|
36
|
-
jsi::Object config = arguments[0].asObject(runtime);
|
|
37
|
-
|
|
38
|
-
NSString* instanceId = [Mmkv getPropertyAsStringOrNilFromObject:config propertyName:"id" runtime:runtime];
|
|
39
|
-
NSString* path = [Mmkv getPropertyAsStringOrNilFromObject:config propertyName:"path" runtime:runtime];
|
|
40
|
-
NSString* encryptionKey = [Mmkv getPropertyAsStringOrNilFromObject:config propertyName:"encryptionKey" runtime:runtime];
|
|
41
|
-
|
|
42
|
-
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
|
|
43
|
-
return jsi::Object::createFromHostObject(runtime, instance);
|
|
44
|
-
});
|
|
45
|
-
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
+ (NSString*)getPropertyAsStringOrNilFromObject:(jsi::Object&)object propertyName:(std::string)propertyName runtime:(jsi::Runtime&)runtime {
|
|
49
|
-
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
|
|
50
|
-
std::string string = value.isString() ? value.asString(runtime).utf8(runtime) : "";
|
|
51
|
-
return string.length() > 0 ? [NSString stringWithUTF8String:string.c_str()] : nil;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
- (void)setup
|
|
55
|
-
{
|
|
56
|
-
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
|
|
57
|
-
if (!cxxBridge.runtime) {
|
|
58
|
-
// retry 10ms later - THIS IS A WACK WORKAROUND. wait for TurboModules to land.
|
|
59
|
-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
60
|
-
[self setup];
|
|
61
|
-
});
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
[MMKV initializeMMKV:nil];
|
|
66
|
-
install(*(jsi::Runtime *)cxxBridge.runtime);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
- (void)setBridge:(RCTBridge *)bridge
|
|
70
|
-
{
|
|
71
|
-
_bridge = bridge;
|
|
72
|
-
_setBridgeOnMainQueue = RCTIsMainQueue();
|
|
73
|
-
[self setup];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
@end
|