react-native-mmkv 2.12.2 → 3.0.0-beta.2
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/LICENSE +1 -200
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +4 -2
- package/MMKV/Core/MMKVPredef.h +1 -1
- package/MMKV/Core/MMKV_IO.cpp +5 -1
- package/MMKV/Core/MemoryFile.cpp +1 -0
- package/MMKV/Core/MemoryFile_OSX.cpp +2 -1
- package/MMKV/README.md +8 -8
- package/README.md +35 -4
- package/android/CMakeLists.txt +20 -28
- package/android/build.gradle +59 -89
- package/android/gradle.properties +4 -5
- package/android/src/main/AndroidManifest.xml +2 -2
- package/android/src/{hasNamespace/AndroidManifest.xml → main/AndroidManifestNew.xml} +0 -1
- package/android/src/main/cpp/AndroidLogger.cpp +16 -0
- package/android/src/main/cpp/cpp-adapter.cpp +4 -80
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +44 -0
- package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +17 -0
- package/cpp/Logger.h +35 -0
- package/cpp/MMKVManagedBuffer.h +32 -0
- package/{android/src/main/cpp → cpp}/MmkvHostObject.cpp +126 -63
- package/{android/src/main/cpp → cpp}/MmkvHostObject.h +9 -3
- package/cpp/NativeMmkvModule.cpp +46 -0
- package/cpp/NativeMmkvModule.h +37 -0
- package/ios/AppleLogger.mm +16 -0
- package/ios/MmkvOnLoad.mm +25 -0
- package/ios/MmkvPlatformContext.h +19 -0
- package/ios/MmkvPlatformContextModule.mm +37 -0
- package/lib/commonjs/LazyTurboModule.js +49 -0
- package/lib/commonjs/LazyTurboModule.js.map +1 -0
- package/lib/commonjs/MMKV.js +28 -4
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/NativeMmkv.js +35 -0
- package/lib/commonjs/NativeMmkv.js.map +1 -0
- package/lib/commonjs/NativeMmkvPlatformContext.js +13 -0
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -0
- package/lib/commonjs/PlatformChecker.js.map +1 -1
- package/lib/commonjs/Types.js +2 -0
- package/lib/commonjs/Types.js.map +1 -0
- package/lib/commonjs/createMMKV.js +6 -43
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.mock.js +5 -1
- package/lib/commonjs/createMMKV.mock.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +8 -4
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/commonjs/createTextEncoder.js.map +1 -1
- package/lib/commonjs/hooks.js +11 -10
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/LazyTurboModule.js +43 -0
- package/lib/module/LazyTurboModule.js.map +1 -0
- package/lib/module/MMKV.js +16 -4
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/NativeMmkv.js +32 -0
- package/lib/module/NativeMmkv.js.map +1 -0
- package/lib/module/NativeMmkvPlatformContext.js +7 -0
- package/lib/module/NativeMmkvPlatformContext.js.map +1 -0
- package/lib/module/PlatformChecker.js.map +1 -1
- package/lib/module/Types.js +2 -0
- package/lib/module/Types.js.map +1 -0
- package/lib/module/createMMKV.js +6 -43
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/createMMKV.mock.js +5 -1
- package/lib/module/createMMKV.mock.js.map +1 -1
- package/lib/module/createMMKV.web.js +6 -4
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/module/createTextEncoder.js.map +1 -1
- package/lib/module/hooks.js +7 -2
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/LazyTurboModule.d.ts +6 -0
- package/lib/typescript/src/LazyTurboModule.d.ts.map +1 -0
- package/lib/typescript/src/MMKV.d.ts +35 -0
- package/lib/typescript/src/MMKV.d.ts.map +1 -0
- package/lib/typescript/src/NativeMmkv.d.ts +83 -0
- package/lib/typescript/src/NativeMmkv.d.ts.map +1 -0
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +9 -0
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -0
- package/lib/typescript/src/PlatformChecker.d.ts.map +1 -0
- package/lib/typescript/src/Types.d.ts +86 -0
- package/lib/typescript/src/Types.d.ts.map +1 -0
- package/lib/typescript/src/__tests__/hooks.test.d.ts +2 -0
- package/lib/typescript/src/__tests__/hooks.test.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.d.ts +4 -0
- package/lib/typescript/src/createMMKV.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.mock.d.ts +3 -0
- package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -0
- package/lib/typescript/src/createMMKV.web.d.ts +4 -0
- package/lib/typescript/src/createMMKV.web.d.ts.map +1 -0
- package/lib/typescript/src/createTextEncoder.d.ts.map +1 -0
- package/lib/typescript/{hooks.d.ts → src/hooks.d.ts} +7 -4
- package/lib/typescript/src/hooks.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +52 -40
- package/react-native-mmkv.podspec +17 -18
- package/react-native.config.js +20 -0
- package/src/LazyTurboModule.ts +62 -0
- package/src/MMKV.ts +19 -132
- package/src/NativeMmkv.ts +105 -0
- package/src/NativeMmkvPlatformContext.ts +14 -0
- package/src/Types.ts +89 -0
- package/src/__tests__/hooks.test.tsx +89 -0
- package/src/createMMKV.mock.ts +7 -3
- package/src/createMMKV.ts +10 -65
- package/src/createMMKV.web.ts +7 -2
- package/src/hooks.ts +17 -11
- package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +0 -49
- package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +0 -26
- package/cpp/TypedArray.cpp +0 -322
- package/cpp/TypedArray.h +0 -153
- package/ios/JSIUtils.h +0 -38
- package/ios/JSIUtils.mm +0 -167
- package/ios/Mmkv.xcodeproj/project.pbxproj +0 -291
- package/ios/MmkvHostObject.h +0 -27
- package/ios/MmkvHostObject.mm +0 -295
- package/ios/MmkvModule.h +0 -5
- package/ios/MmkvModule.mm +0 -101
- package/lib/typescript/MMKV.d.ts +0 -142
- package/lib/typescript/MMKV.d.ts.map +0 -1
- package/lib/typescript/PlatformChecker.d.ts.map +0 -1
- package/lib/typescript/createMMKV.d.ts +0 -7
- package/lib/typescript/createMMKV.d.ts.map +0 -1
- package/lib/typescript/createMMKV.mock.d.ts +0 -3
- package/lib/typescript/createMMKV.mock.d.ts.map +0 -1
- package/lib/typescript/createMMKV.web.d.ts +0 -3
- package/lib/typescript/createMMKV.web.d.ts.map +0 -1
- package/lib/typescript/createTextEncoder.d.ts.map +0 -1
- package/lib/typescript/hooks.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- /package/lib/typescript/{PlatformChecker.d.ts → src/PlatformChecker.d.ts} +0 -0
- /package/lib/typescript/{createTextEncoder.d.ts → src/createTextEncoder.d.ts} +0 -0
- /package/lib/typescript/{index.d.ts → src/index.d.ts} +0 -0
package/android/build.gradle
CHANGED
|
@@ -1,139 +1,109 @@
|
|
|
1
|
-
import com.android.Version
|
|
2
|
-
|
|
3
|
-
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
|
|
4
|
-
def agpVersionMajor = agpVersion.tokenize('.')[0].toInteger()
|
|
5
|
-
def androidManifestPath = agpVersionMajor >= 7 ? 'src/main/AndroidManifest.xml' : 'src/hasNamespace/AndroidManifest.xml'
|
|
6
|
-
|
|
7
1
|
buildscript {
|
|
8
2
|
repositories {
|
|
9
|
-
maven {
|
|
10
|
-
url "https://plugins.gradle.org/m2/"
|
|
11
|
-
}
|
|
12
|
-
mavenCentral()
|
|
13
3
|
google()
|
|
4
|
+
mavenCentral()
|
|
14
5
|
}
|
|
15
6
|
|
|
16
7
|
dependencies {
|
|
17
|
-
classpath
|
|
8
|
+
classpath "com.android.tools.build:gradle:7.2.1"
|
|
18
9
|
}
|
|
19
10
|
}
|
|
20
11
|
|
|
21
|
-
def resolveBuildType() {
|
|
22
|
-
Gradle gradle = getGradle()
|
|
23
|
-
String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()
|
|
24
|
-
|
|
25
|
-
return tskReqStr.contains('Release') ? 'release' : 'debug'
|
|
26
|
-
}
|
|
27
|
-
|
|
28
12
|
def isNewArchitectureEnabled() {
|
|
29
|
-
|
|
30
|
-
// - Set `newArchEnabled` to true inside the `gradle.properties` file
|
|
31
|
-
// - Invoke gradle with `-newArchEnabled=true`
|
|
32
|
-
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
|
|
33
|
-
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
|
|
13
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
34
14
|
}
|
|
35
15
|
|
|
16
|
+
apply plugin: "com.android.library"
|
|
17
|
+
|
|
36
18
|
if (isNewArchitectureEnabled()) {
|
|
37
|
-
apply plugin:
|
|
19
|
+
apply plugin: "com.facebook.react"
|
|
38
20
|
}
|
|
39
|
-
apply plugin: 'com.android.library'
|
|
40
21
|
|
|
41
|
-
def
|
|
42
|
-
rootProject.ext.has(
|
|
22
|
+
def getExtOrDefault(name) {
|
|
23
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Mmkv_" + name]
|
|
43
24
|
}
|
|
44
25
|
|
|
45
|
-
def
|
|
46
|
-
|
|
47
|
-
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
26
|
+
def getExtOrIntegerDefault(name) {
|
|
27
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Mmkv_" + name]).toInteger()
|
|
48
28
|
}
|
|
49
29
|
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
def supportsNamespace() {
|
|
31
|
+
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
32
|
+
def major = parsed[0].toInteger()
|
|
33
|
+
def minor = parsed[1].toInteger()
|
|
34
|
+
|
|
35
|
+
// Namespace support was added in 7.3.0
|
|
36
|
+
return (major == 7 && minor >= 3) || major >= 8
|
|
52
37
|
}
|
|
53
38
|
|
|
54
39
|
android {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
namespace = "com.reactnativemmkv"
|
|
58
|
-
}
|
|
40
|
+
if (supportsNamespace()) {
|
|
41
|
+
namespace "com.mrousavy.mmkv"
|
|
59
42
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
sourceSets {
|
|
44
|
+
main {
|
|
45
|
+
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
63
46
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
sourceSets {
|
|
67
|
-
main {
|
|
68
|
-
manifest.srcFile androidManifestPath
|
|
69
47
|
}
|
|
70
48
|
}
|
|
71
49
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
50
|
+
ndkVersion getExtOrDefault("ndkVersion")
|
|
51
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
52
|
+
|
|
53
|
+
defaultConfig {
|
|
54
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
55
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
56
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
79
57
|
}
|
|
80
58
|
|
|
81
59
|
buildFeatures {
|
|
82
|
-
|
|
60
|
+
buildConfig true
|
|
83
61
|
}
|
|
84
62
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
versionCode 1
|
|
89
|
-
versionName "1.0"
|
|
90
|
-
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
91
|
-
externalNativeBuild {
|
|
92
|
-
cmake {
|
|
93
|
-
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
|
|
94
|
-
arguments "-DANDROID_STL=c++_shared"
|
|
95
|
-
abiFilters (*reactNativeArchitectures())
|
|
96
|
-
}
|
|
63
|
+
buildTypes {
|
|
64
|
+
release {
|
|
65
|
+
minifyEnabled false
|
|
97
66
|
}
|
|
98
67
|
}
|
|
99
68
|
|
|
69
|
+
lintOptions {
|
|
70
|
+
disable "GradleCompatible"
|
|
71
|
+
}
|
|
72
|
+
|
|
100
73
|
compileOptions {
|
|
101
74
|
sourceCompatibility JavaVersion.VERSION_1_8
|
|
102
75
|
targetCompatibility JavaVersion.VERSION_1_8
|
|
103
76
|
}
|
|
104
77
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
78
|
+
sourceSets {
|
|
79
|
+
main {
|
|
80
|
+
if (isNewArchitectureEnabled()) {
|
|
81
|
+
java.srcDirs += [
|
|
82
|
+
// This is needed to build Kotlin project with NewArch enabled
|
|
83
|
+
"${project.buildDir}/generated/source/codegen/java"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
108
86
|
}
|
|
109
87
|
}
|
|
110
|
-
packagingOptions {
|
|
111
|
-
doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
|
|
112
|
-
excludes = [
|
|
113
|
-
"META-INF",
|
|
114
|
-
"META-INF/**",
|
|
115
|
-
"**/libjsi.so",
|
|
116
|
-
"**/libc++_shared.so"
|
|
117
|
-
]
|
|
118
|
-
}
|
|
119
88
|
}
|
|
120
89
|
|
|
90
|
+
repositories {
|
|
91
|
+
mavenCentral()
|
|
92
|
+
google()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
121
96
|
dependencies {
|
|
97
|
+
// For < 0.71, this will be from the local maven repo
|
|
98
|
+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
|
122
99
|
//noinspection GradleDynamicVersion
|
|
123
|
-
implementation
|
|
100
|
+
implementation "com.facebook.react:react-native:+"
|
|
124
101
|
}
|
|
125
102
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
|
|
134
|
-
if (task.name.contains("configureCMakeRel")) {
|
|
135
|
-
rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
|
|
136
|
-
task.dependsOn(it)
|
|
137
|
-
}
|
|
103
|
+
if (isNewArchitectureEnabled()) {
|
|
104
|
+
react {
|
|
105
|
+
jsRootDir = file("../src/")
|
|
106
|
+
libraryName = "Mmkv"
|
|
107
|
+
codegenJavaPackageName = "com.mrousavy.mmkv"
|
|
138
108
|
}
|
|
139
109
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
Mmkv_kotlinVersion=1.7.0
|
|
2
|
+
Mmkv_minSdkVersion=21
|
|
3
3
|
Mmkv_targetSdkVersion=31
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
|
|
4
|
+
Mmkv_compileSdkVersion=31
|
|
5
|
+
Mmkv_ndkversion=21.4.7075529
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
<manifest
|
|
2
|
-
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
2
|
+
package="com.mrousavy.mmkv">
|
|
3
3
|
</manifest>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// AndroidLogger.cpp
|
|
3
|
+
// react-native-mmkv
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 05.03.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#include "Logger.h"
|
|
9
|
+
#include <android/log.h>
|
|
10
|
+
|
|
11
|
+
void Logger::log(const std::string& tag, const std::string& message) {
|
|
12
|
+
#pragma clang diagnostic push
|
|
13
|
+
#pragma clang diagnostic ignored "-Wformat-security"
|
|
14
|
+
__android_log_print(ANDROID_LOG_INFO, tag.c_str(), message.c_str());
|
|
15
|
+
#pragma clang diagnostic pop
|
|
16
|
+
}
|
|
@@ -1,83 +1,7 @@
|
|
|
1
|
-
#include "MmkvHostObject.h"
|
|
2
|
-
#include "TypedArray.h"
|
|
3
|
-
#include <MMKV.h>
|
|
4
1
|
#include <jni.h>
|
|
5
|
-
#include <jsi/jsi.h>
|
|
6
2
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
jsi::Runtime& runtime) {
|
|
12
|
-
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
|
|
13
|
-
return value.isString() ? value.asString(runtime).utf8(runtime) : "";
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
void install(jsi::Runtime& jsiRuntime) {
|
|
17
|
-
// MMKV.createNewInstance()
|
|
18
|
-
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(
|
|
19
|
-
jsiRuntime, jsi::PropNameID::forAscii(jsiRuntime, "mmkvCreateNewInstance"), 1,
|
|
20
|
-
[](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
|
|
21
|
-
size_t count) -> jsi::Value {
|
|
22
|
-
if (count != 1) {
|
|
23
|
-
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
|
|
24
|
-
}
|
|
25
|
-
jsi::Object config = arguments[0].asObject(runtime);
|
|
26
|
-
|
|
27
|
-
std::string instanceId = getPropertyAsStringOrEmptyFromObject(config, "id", runtime);
|
|
28
|
-
std::string path = getPropertyAsStringOrEmptyFromObject(config, "path", runtime);
|
|
29
|
-
std::string encryptionKey =
|
|
30
|
-
getPropertyAsStringOrEmptyFromObject(config, "encryptionKey", runtime);
|
|
31
|
-
|
|
32
|
-
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
|
|
33
|
-
return jsi::Object::createFromHostObject(runtime, instance);
|
|
34
|
-
});
|
|
35
|
-
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance",
|
|
36
|
-
std::move(mmkvCreateNewInstance));
|
|
37
|
-
|
|
38
|
-
// Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets
|
|
39
|
-
// destroyed and the cache gets invalidated.
|
|
40
|
-
auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(jsiRuntime);
|
|
41
|
-
jsiRuntime.global().setProperty(jsiRuntime, "mmkvArrayBufferPropNameIdCache",
|
|
42
|
-
jsi::Object::createFromHostObject(jsiRuntime, propNameIdCache));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
std::string jstringToStdString(JNIEnv* env, jstring jStr) {
|
|
46
|
-
if (!jStr)
|
|
47
|
-
return "";
|
|
48
|
-
|
|
49
|
-
const auto stringClass = env->GetObjectClass(jStr);
|
|
50
|
-
const auto getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
|
|
51
|
-
const auto stringJbytes =
|
|
52
|
-
(jbyteArray)env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
|
|
53
|
-
|
|
54
|
-
auto length = (size_t)env->GetArrayLength(stringJbytes);
|
|
55
|
-
auto pBytes = env->GetByteArrayElements(stringJbytes, nullptr);
|
|
56
|
-
|
|
57
|
-
std::string ret = std::string((char*)pBytes, length);
|
|
58
|
-
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
|
|
59
|
-
|
|
60
|
-
env->DeleteLocalRef(stringJbytes);
|
|
61
|
-
env->DeleteLocalRef(stringClass);
|
|
62
|
-
return ret;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
extern "C" JNIEXPORT void JNICALL Java_com_reactnativemmkv_MmkvModule_nativeInstall(JNIEnv* env,
|
|
66
|
-
jobject clazz,
|
|
67
|
-
jlong jsiPtr,
|
|
68
|
-
jstring path) {
|
|
69
|
-
#if DEBUG
|
|
70
|
-
MMKVLogLevel logLevel = MMKVLogDebug;
|
|
71
|
-
#else
|
|
72
|
-
MMKVLogLevel logLevel = MMKVLogError;
|
|
73
|
-
#endif
|
|
74
|
-
std::string storageDirectory = jstringToStdString(env, path);
|
|
75
|
-
MMKV::initializeMMKV(storageDirectory, logLevel);
|
|
76
|
-
|
|
77
|
-
auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
|
|
78
|
-
if (runtime) {
|
|
79
|
-
install(*runtime);
|
|
80
|
-
}
|
|
81
|
-
// if runtime was nullptr, MMKV will not be installed. This should only happen while Remote
|
|
82
|
-
// Debugging (Chrome), but will be weird either way.
|
|
3
|
+
extern "C" JNIEXPORT jdouble JNICALL Java_com_mmkv_MmkvModule_nativeMultiply(JNIEnv* env,
|
|
4
|
+
jclass type, jdouble a,
|
|
5
|
+
jdouble b) {
|
|
6
|
+
return 5.0;
|
|
83
7
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
package com.mrousavy.mmkv;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
import com.facebook.react.bridge.NativeModule;
|
|
7
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
|
+
import com.facebook.react.module.model.ReactModuleInfo;
|
|
9
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider;
|
|
10
|
+
import com.facebook.react.TurboReactPackage;
|
|
11
|
+
|
|
12
|
+
import java.util.HashMap;
|
|
13
|
+
import java.util.Map;
|
|
14
|
+
|
|
15
|
+
public class MmkvPackage extends TurboReactPackage {
|
|
16
|
+
@Nullable
|
|
17
|
+
@Override
|
|
18
|
+
public NativeModule getModule(String name, @NonNull ReactApplicationContext reactContext) {
|
|
19
|
+
if (name.equals(MmkvPlatformContextModule.NAME)) {
|
|
20
|
+
return new MmkvPlatformContextModule(reactContext);
|
|
21
|
+
} else {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Override
|
|
27
|
+
public ReactModuleInfoProvider getReactModuleInfoProvider() {
|
|
28
|
+
return () -> {
|
|
29
|
+
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
|
|
30
|
+
moduleInfos.put(
|
|
31
|
+
MmkvPlatformContextModule.NAME,
|
|
32
|
+
new ReactModuleInfo(
|
|
33
|
+
MmkvPlatformContextModule.NAME,
|
|
34
|
+
MmkvPlatformContextModule.NAME,
|
|
35
|
+
false, // canOverrideExistingModule
|
|
36
|
+
false, // needsEagerInit
|
|
37
|
+
true, // hasConstants
|
|
38
|
+
false, // isCxxModule
|
|
39
|
+
true // isTurboModule
|
|
40
|
+
));
|
|
41
|
+
return moduleInfos;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package com.mrousavy.mmkv;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
4
|
+
|
|
5
|
+
public class MmkvPlatformContextModule extends NativeMmkvPlatformContextSpec {
|
|
6
|
+
private final ReactApplicationContext context;
|
|
7
|
+
|
|
8
|
+
public MmkvPlatformContextModule(ReactApplicationContext reactContext) {
|
|
9
|
+
super(reactContext);
|
|
10
|
+
context = reactContext;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@Override
|
|
14
|
+
public String getBaseDirectory() {
|
|
15
|
+
return context.getFilesDir().getAbsolutePath() + "/mmkv";
|
|
16
|
+
}
|
|
17
|
+
}
|
package/cpp/Logger.h
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Logger.h
|
|
3
|
+
// react-native-mmkv
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 25.03.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
class Logger {
|
|
11
|
+
private:
|
|
12
|
+
Logger() = delete;
|
|
13
|
+
|
|
14
|
+
private:
|
|
15
|
+
template <typename... Args>
|
|
16
|
+
static std::string string_format(const std::string& format, Args... args) {
|
|
17
|
+
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
|
18
|
+
if (size_s <= 0) {
|
|
19
|
+
throw std::runtime_error("Failed to format string!");
|
|
20
|
+
}
|
|
21
|
+
auto size = static_cast<size_t>(size_s);
|
|
22
|
+
std::unique_ptr<char[]> buf(new char[size]);
|
|
23
|
+
std::snprintf(buf.get(), size, format.c_str(), args...);
|
|
24
|
+
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public:
|
|
28
|
+
static void log(const std::string& tag, const std::string& message);
|
|
29
|
+
|
|
30
|
+
template <typename... Args>
|
|
31
|
+
inline static void log(const std::string& tag, const std::string& formatString, Args&&... args) {
|
|
32
|
+
std::string formattedString = string_format(formatString, std::forward<Args>(args)...);
|
|
33
|
+
log(tag, formattedString);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//
|
|
2
|
+
// ManagedBuffer.h
|
|
3
|
+
// react-native-mmkv
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 25.03.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "MMKVManagedBuffer.h"
|
|
11
|
+
#include <jsi/jsi.h>
|
|
12
|
+
|
|
13
|
+
using namespace facebook;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
A jsi::MutableBuffer that manages mmkv::MMBuffer memory (by ownership).
|
|
17
|
+
*/
|
|
18
|
+
class MMKVManagedBuffer : public jsi::MutableBuffer {
|
|
19
|
+
public:
|
|
20
|
+
explicit MMKVManagedBuffer(mmkv::MMBuffer&& buffer) : _buffer(std::move(buffer)) {}
|
|
21
|
+
|
|
22
|
+
uint8_t* data() override {
|
|
23
|
+
return static_cast<uint8_t*>(_buffer.getPtr());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
size_t size() const override {
|
|
27
|
+
return _buffer.length();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private:
|
|
31
|
+
mmkv::MMBuffer _buffer;
|
|
32
|
+
};
|