react-native-mmkv 1.6.1 → 2.0.1
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 +30 -0
- package/MMKV/Core/CMakeLists.txt +2 -1
- package/MMKV/Core/CodedInputDataCrypt.cpp +1 -1
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +7 -4
- package/MMKV/Core/InterProcessLock_Win32.cpp +10 -5
- package/MMKV/Core/MMBuffer.h +1 -0
- package/MMKV/Core/MMKV.cpp +359 -17
- package/MMKV/Core/MMKV.h +29 -4
- package/MMKV/Core/MMKVLog.cpp +11 -10
- package/MMKV/Core/MMKVLog.h +1 -1
- package/MMKV/Core/MMKVPredef.h +6 -4
- package/MMKV/Core/MMKV_Android.cpp +2 -6
- package/MMKV/Core/MMKV_IO.cpp +1 -3
- package/MMKV/Core/MMKV_IO.h +3 -3
- package/MMKV/Core/MemoryFile.cpp +276 -43
- package/MMKV/Core/MemoryFile.h +85 -9
- package/MMKV/Core/MemoryFile_Android.cpp +37 -18
- package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
- package/MMKV/Core/MemoryFile_OSX.cpp +92 -2
- package/MMKV/Core/MemoryFile_Win32.cpp +254 -34
- package/MMKV/Core/aes/openssl/openssl_aes.h +2 -2
- package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +4 -4
- package/MMKV/README.md +4 -4
- package/README.md +11 -15
- package/android/CMakeLists.txt +1 -6
- package/android/build.gradle +41 -9
- 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 +50 -2
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/hooks.js +8 -2
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/createMMKV.js +48 -2
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/hooks.js +8 -2
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/createMMKV.d.ts +1 -0
- package/lib/typescript/hooks.d.ts +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
|
@@ -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,58 @@ 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 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?');
|
|
12
60
|
}
|
|
13
61
|
|
|
14
62
|
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","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"]}
|
package/lib/commonjs/hooks.js
CHANGED
|
@@ -71,7 +71,7 @@ function createMMKVHook(getter) {
|
|
|
71
71
|
});
|
|
72
72
|
return () => listener.remove();
|
|
73
73
|
}, [key, mmkv]);
|
|
74
|
-
return
|
|
74
|
+
return [value, set];
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
@@ -135,7 +135,13 @@ function useMMKVObject(key, instance) {
|
|
|
135
135
|
return JSON.parse(string);
|
|
136
136
|
}, [string]);
|
|
137
137
|
const setValue = (0, _react.useCallback)(v => {
|
|
138
|
-
|
|
138
|
+
if (v == null) {
|
|
139
|
+
// Clear the Value
|
|
140
|
+
setString(undefined);
|
|
141
|
+
} else {
|
|
142
|
+
// Store the Object as a serialized Value
|
|
143
|
+
setString(JSON.stringify(v));
|
|
144
|
+
}
|
|
139
145
|
}, [setString]);
|
|
140
146
|
return [value, setValue];
|
|
141
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["hooks.ts"],"names":["isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","MMKV","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify"],"mappings":";;;;;;;;;;AAAA;;AAOA;;AAEA,SAASA,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,SACED,IAAI,CAACE,aAAL,KAAuBD,KAAK,CAACC,aAA7B,IACAF,IAAI,CAACG,EAAL,KAAYF,KAAK,CAACE,EADlB,IAEAH,IAAI,CAACI,IAAL,KAAcH,KAAK,CAACG,IAHtB;AAKD;;AAED,IAAIC,eAA4B,GAAG,IAAnC;;AACA,SAASC,kBAAT,GAAoC;AAClC,MAAID,eAAe,IAAI,IAAvB,EAA6B;AAC3BA,IAAAA,eAAe,GAAG,IAAIE,UAAJ,EAAlB;AACD;;AACD,SAAOF,eAAP;AACD;;AAEM,SAASG,OAAT,CACLC,aADK,EAEkB;AACvB,QAAMC,QAAQ,GAAG,oBAAjB;AAEA,QAAMC,iBAAiB,GAAG,oBAA1B;;AACA,MACEA,iBAAiB,CAACC,OAAlB,IAA6B,IAA7B,IACA,CAACb,oBAAoB,CAACY,iBAAiB,CAACC,OAAnB,EAA4BH,aAA5B,CAFvB,EAGE;AACAE,IAAAA,iBAAiB,CAACC,OAAlB,GAA4BH,aAA5B;AACAC,IAAAA,QAAQ,CAACE,OAAT,GAAmB,IAAIL,UAAJ,CAASE,aAAT,CAAnB;AACD,GAVsB,CAYvB;;;AACA,SAAOC,QAAP;AACD;;AAED,SAASG,cAAT,CAIEC,MAJF,EAI8C;AAC5C,SAAO,CACLC,GADK,EAELL,QAFK,KAGiD;AACtD,UAAMM,IAAI,GAAGN,QAAH,aAAGA,QAAH,cAAGA,QAAH,GAAeJ,kBAAkB,EAA3C;AACA,UAAM,CAACW,KAAD,EAAQC,QAAR,IAAoB,qBAAS,MAAMJ,MAAM,CAACE,IAAD,EAAOD,GAAP,CAArB,CAA1B;AACA,UAAMI,QAAQ,GAAG,mBAAUF,KAAV,CAAjB;AACAE,IAAAA,QAAQ,CAACP,OAAT,GAAmBK,KAAnB;AAEA,UAAMG,GAAG,GAAG,wBACTC,CAAD,IAAmB;AACjB,YAAMC,QAAQ,GAAG,OAAOD,CAAP,KAAa,UAAb,GAA0BA,CAAC,CAACF,QAAQ,CAACP,OAAV,CAA3B,GAAgDS,CAAjE;;AACA,cAAQ,OAAOC,QAAf;AACE,aAAK,QAAL;AACA,aAAK,QAAL;AACA,aAAK,SAAL;AACEN,UAAAA,IAAI,CAACI,GAAL,CAASL,GAAT,EAAcO,QAAd;AACA;;AACF,aAAK,WAAL;AACEN,UAAAA,IAAI,CAACO,MAAL,CAAYR,GAAZ;AACA;;AACF;AACE,gBAAM,IAAIS,KAAJ,CAAW,cAAa,OAAOF,QAAS,oBAAxC,CAAN;AAVJ;AAYD,KAfS,EAgBV,CAACP,GAAD,EAAMC,IAAN,CAhBU,CAAZ;AAmBA,0BAAU,MAAM;AACd,YAAMS,QAAQ,GAAGT,IAAI,CAACU,yBAAL,CAAgCC,UAAD,IAAgB;AAC9D,YAAIA,UAAU,KAAKZ,GAAnB,EAAwB;AACtBG,UAAAA,QAAQ,CAACJ,MAAM,CAACE,IAAD,EAAOD,GAAP,CAAP,CAAR;AACD;AACF,OAJgB,CAAjB;AAKA,aAAO,MAAMU,QAAQ,CAACG,MAAT,EAAb;AACD,KAPD,EAOG,CAACb,GAAD,EAAMC,IAAN,CAPH;AASA,WAAO,
|
|
1
|
+
{"version":3,"sources":["hooks.ts"],"names":["isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","MMKV","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify"],"mappings":";;;;;;;;;;AAAA;;AAOA;;AAEA,SAASA,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,SACED,IAAI,CAACE,aAAL,KAAuBD,KAAK,CAACC,aAA7B,IACAF,IAAI,CAACG,EAAL,KAAYF,KAAK,CAACE,EADlB,IAEAH,IAAI,CAACI,IAAL,KAAcH,KAAK,CAACG,IAHtB;AAKD;;AAED,IAAIC,eAA4B,GAAG,IAAnC;;AACA,SAASC,kBAAT,GAAoC;AAClC,MAAID,eAAe,IAAI,IAAvB,EAA6B;AAC3BA,IAAAA,eAAe,GAAG,IAAIE,UAAJ,EAAlB;AACD;;AACD,SAAOF,eAAP;AACD;;AAEM,SAASG,OAAT,CACLC,aADK,EAEkB;AACvB,QAAMC,QAAQ,GAAG,oBAAjB;AAEA,QAAMC,iBAAiB,GAAG,oBAA1B;;AACA,MACEA,iBAAiB,CAACC,OAAlB,IAA6B,IAA7B,IACA,CAACb,oBAAoB,CAACY,iBAAiB,CAACC,OAAnB,EAA4BH,aAA5B,CAFvB,EAGE;AACAE,IAAAA,iBAAiB,CAACC,OAAlB,GAA4BH,aAA5B;AACAC,IAAAA,QAAQ,CAACE,OAAT,GAAmB,IAAIL,UAAJ,CAASE,aAAT,CAAnB;AACD,GAVsB,CAYvB;;;AACA,SAAOC,QAAP;AACD;;AAED,SAASG,cAAT,CAIEC,MAJF,EAI8C;AAC5C,SAAO,CACLC,GADK,EAELL,QAFK,KAGiD;AACtD,UAAMM,IAAI,GAAGN,QAAH,aAAGA,QAAH,cAAGA,QAAH,GAAeJ,kBAAkB,EAA3C;AACA,UAAM,CAACW,KAAD,EAAQC,QAAR,IAAoB,qBAAS,MAAMJ,MAAM,CAACE,IAAD,EAAOD,GAAP,CAArB,CAA1B;AACA,UAAMI,QAAQ,GAAG,mBAAUF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AAX,KAC1CL,QAAQ,CAACsB,SAAT,CAAmBjB,GAAnB,CADyC,CAApC;AAGP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMkB,cAAc,GAAGpB,cAAc,CAAC,CAACH,QAAD,EAAWK,GAAX,KAC3CL,QAAQ,CAACwB,UAAT,CAAoBnB,GAApB,CAD0C,CAArC;AAGP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;AACO,SAASoB,aAAT,CACLpB,GADK,EAELL,QAFK,EAG6D;AAClE,QAAM,CAAC0B,MAAD,EAASC,SAAT,IAAsBR,aAAa,CAACd,GAAD,EAAML,QAAN,CAAzC;AAEA,QAAMO,KAAK,GAAG,oBAAQ,MAAM;AAC1B,QAAImB,MAAM,IAAI,IAAd,EAAoB,OAAOE,SAAP;AACpB,WAAOC,IAAI,CAACC,KAAL,CAAWJ,MAAX,CAAP;AACD,GAHa,EAGX,CAACA,MAAD,CAHW,CAAd;AAIA,QAAMlB,QAAQ,GAAG,wBACdG,CAAD,IAAsB;AACpB,QAAIA,CAAC,IAAI,IAAT,EAAe;AACb;AACAgB,MAAAA,SAAS,CAACC,SAAD,CAAT;AACD,KAHD,MAGO;AACL;AACAD,MAAAA,SAAS,CAACE,IAAI,CAACE,SAAL,CAAepB,CAAf,CAAD,CAAT;AACD;AACF,GATc,EAUf,CAACgB,SAAD,CAVe,CAAjB;AAaA,SAAO,CAACpB,KAAD,EAAQC,QAAR,CAAP;AACD","sourcesContent":["import React, {\n useRef,\n useState,\n useMemo,\n useCallback,\n useEffect,\n} from 'react';\nimport { MMKV, MMKVConfiguration } from './MMKV';\n\nfunction isConfigurationEqual(\n left: MMKVConfiguration,\n right: MMKVConfiguration\n): boolean {\n return (\n left.encryptionKey === right.encryptionKey &&\n left.id === right.id &&\n left.path === right.path\n );\n}\n\nlet defaultInstance: MMKV | null = null;\nfunction getDefaultInstance(): MMKV {\n if (defaultInstance == null) {\n defaultInstance = new MMKV();\n }\n return defaultInstance;\n}\n\nexport function useMMKV(\n configuration: MMKVConfiguration\n): React.RefObject<MMKV> {\n const instance = useRef<MMKV>();\n\n const lastConfiguration = useRef<MMKVConfiguration>();\n if (\n lastConfiguration.current == null ||\n !isConfigurationEqual(lastConfiguration.current, configuration)\n ) {\n lastConfiguration.current = configuration;\n instance.current = new MMKV(configuration);\n }\n\n // @ts-expect-error it's not null, I promise.\n return instance;\n}\n\nfunction createMMKVHook<\n T extends boolean | number | (string | undefined),\n TSet extends T | undefined,\n TSetAction extends TSet | ((current: T) => TSet)\n>(getter: (instance: MMKV, key: string) => T) {\n return (\n key: string,\n instance?: MMKV\n ): [value: T, setValue: (value: TSetAction) => void] => {\n const mmkv = instance ?? getDefaultInstance();\n const [value, setValue] = useState(() => getter(mmkv, key));\n const valueRef = useRef<T>(value);\n valueRef.current = value;\n\n const set = useCallback(\n (v: TSetAction) => {\n const newValue = typeof v === 'function' ? v(valueRef.current) : v;\n switch (typeof newValue) {\n case 'number':\n case 'string':\n case 'boolean':\n mmkv.set(key, newValue);\n break;\n case 'undefined':\n mmkv.delete(key);\n break;\n default:\n throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);\n }\n },\n [key, mmkv]\n );\n\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n if (changedKey === key) {\n setValue(getter(mmkv, key));\n }\n });\n return () => listener.remove();\n }, [key, mmkv]);\n\n return [value, set];\n };\n}\n\n/**\n * Use the string value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [username, setUsername] = useMMKVString(\"user.name\")\n * ```\n */\nexport const useMMKVString = createMMKVHook((instance, key) =>\n instance.getString(key)\n);\n\n/**\n * Use the number value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [age, setAge] = useMMKVNumber(\"user.age\")\n * ```\n */\nexport const useMMKVNumber = createMMKVHook((instance, key) =>\n instance.getNumber(key)\n);\n/**\n * Use the boolean value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean(\"user.isPremium\")\n * ```\n */\nexport const useMMKVBoolean = createMMKVHook((instance, key) =>\n instance.getBoolean(key)\n);\n/**\n * Use an object value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * The object will be serialized using `JSON`.\n *\n * @example\n * ```ts\n * const [user, setUser] = useMMKVObject<User>(\"user\")\n * ```\n */\nexport function useMMKVObject<T>(\n key: string,\n instance?: MMKV\n): [value: T | undefined, setValue: (value: T | undefined) => void] {\n const [string, setString] = useMMKVString(key, instance);\n\n const value = useMemo(() => {\n if (string == null) return undefined;\n return JSON.parse(string) as T;\n }, [string]);\n const setValue = useCallback(\n (v: T | undefined) => {\n if (v == null) {\n // Clear the Value\n setString(undefined);\n } else {\n // Store the Object as a serialized Value\n setString(JSON.stringify(v));\n }\n },\n [setString]\n );\n\n return [value, setValue];\n}\n"]}
|
package/lib/module/createMMKV.js
CHANGED
|
@@ -1,7 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
import { NativeModules, Platform } 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 native MMKV ReactModule
|
|
8
|
+
const MMKVModule = NativeModules.MMKV;
|
|
9
|
+
|
|
10
|
+
if (MMKVModule == null) {
|
|
11
|
+
var _NativeModules$Native, _NativeModules$Native2;
|
|
12
|
+
|
|
13
|
+
let message = 'Failed to create a new MMKV instance: The native MMKV Module could not be found.';
|
|
14
|
+
message += '\n* Make sure react-native-mmkv is correctly autolinked (run `npx react-native config` to verify)';
|
|
15
|
+
|
|
16
|
+
if (Platform.OS === 'ios' || Platform.OS === 'macos') {
|
|
17
|
+
message += '\n* Make sure you ran `pod install` in the ios/ directory.';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (Platform.OS === 'android') {
|
|
21
|
+
message += '\n* Make sure gradle is synced.';
|
|
22
|
+
} // check if Expo
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const ExpoConstants = (_NativeModules$Native = NativeModules.NativeUnimoduleProxy) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;
|
|
26
|
+
|
|
27
|
+
if (ExpoConstants != null) {
|
|
28
|
+
if (ExpoConstants.appOwnership === 'expo') {
|
|
29
|
+
// We're running Expo Go
|
|
30
|
+
throw new Error('react-native-mmkv is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
|
|
31
|
+
} else {
|
|
32
|
+
// We're running Expo bare / standalone
|
|
33
|
+
message += '\n* Make sure you ran `expo prebuild`.';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
message += '\n* Make sure you rebuilt the app.';
|
|
38
|
+
throw new Error(message);
|
|
39
|
+
} // Check if we are running on-device (JSI)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if (global.nativeCallSyncHook == null || MMKVModule.install == null) {
|
|
43
|
+
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.');
|
|
44
|
+
} // Call the synchronous blocking install() function
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const result = MMKVModule.install(ROOT_DIRECTORY);
|
|
48
|
+
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.
|
|
49
|
+
|
|
50
|
+
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?');
|
|
5
51
|
}
|
|
6
52
|
|
|
7
53
|
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","Platform","ROOT_DIRECTORY","createMMKV","config","global","mmkvCreateNewInstance","MMKVModule","MMKV","message","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,QAAxB,QAAwC,cAAxC;AASA;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,GAAGP,aAAa,CAACQ,IAAjC;;AACA,QAAID,UAAU,IAAI,IAAlB,EAAwB;AAAA;;AACtB,UAAIE,OAAO,GACT,kFADF;AAEAA,MAAAA,OAAO,IACL,mGADF;;AAEA,UAAIR,QAAQ,CAACS,EAAT,KAAgB,KAAhB,IAAyBT,QAAQ,CAACS,EAAT,KAAgB,OAA7C,EAAsD;AACpDD,QAAAA,OAAO,IAAI,4DAAX;AACD;;AACD,UAAIR,QAAQ,CAACS,EAAT,KAAgB,SAApB,EAA+B;AAC7BD,QAAAA,OAAO,IAAI,iCAAX;AACD,OAVqB,CAWtB;;;AACA,YAAME,aAAa,4BACjBX,aAAa,CAACY,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;AACAP,UAAAA,OAAO,IAAI,wCAAX;AACD;AACF;;AAEDA,MAAAA,OAAO,IAAI,oCAAX;AACA,YAAM,IAAIO,KAAJ,CAAUP,OAAV,CAAN;AACD,KA/BuC,CAiCxC;;;AACA,QAAIJ,MAAM,CAACY,kBAAP,IAA6B,IAA7B,IAAqCV,UAAU,CAACW,OAAX,IAAsB,IAA/D,EAAqE;AACnE,YAAM,IAAIF,KAAJ,CACJ,sQADI,CAAN;AAGD,KAtCuC,CAwCxC;;;AACA,UAAMG,MAAM,GAAGZ,UAAU,CAACW,OAAX,CAAmBhB,cAAnB,CAAf;AACA,QAAIiB,MAAM,KAAK,IAAf,EACE,MAAM,IAAIH,KAAJ,CACH,sJAAqJG,MAAO,EADzJ,CAAN,CA3CsC,CA+CxC;;AACA,QAAId,MAAM,CAACC,qBAAP,IAAgC,IAApC,EACE,MAAM,IAAIU,KAAJ,CACJ,8IADI,CAAN;AAGH;;AAED,SAAOX,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"]}
|
package/lib/module/hooks.js
CHANGED
|
@@ -60,7 +60,7 @@ function createMMKVHook(getter) {
|
|
|
60
60
|
});
|
|
61
61
|
return () => listener.remove();
|
|
62
62
|
}, [key, mmkv]);
|
|
63
|
-
return
|
|
63
|
+
return [value, set];
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
@@ -120,7 +120,13 @@ export function useMMKVObject(key, instance) {
|
|
|
120
120
|
return JSON.parse(string);
|
|
121
121
|
}, [string]);
|
|
122
122
|
const setValue = useCallback(v => {
|
|
123
|
-
|
|
123
|
+
if (v == null) {
|
|
124
|
+
// Clear the Value
|
|
125
|
+
setString(undefined);
|
|
126
|
+
} else {
|
|
127
|
+
// Store the Object as a serialized Value
|
|
128
|
+
setString(JSON.stringify(v));
|
|
129
|
+
}
|
|
124
130
|
}, [setString]);
|
|
125
131
|
return [value, setValue];
|
|
126
132
|
}
|
package/lib/module/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["hooks.ts"],"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify"],"mappings":"AAAA,SACEA,MADF,EAEEC,QAFF,EAGEC,OAHF,EAIEC,WAJF,EAKEC,SALF,QAMO,OANP;AAOA,SAASC,IAAT,QAAwC,QAAxC;;AAEA,SAASC,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,SACED,IAAI,CAACE,aAAL,KAAuBD,KAAK,CAACC,aAA7B,IACAF,IAAI,CAACG,EAAL,KAAYF,KAAK,CAACE,EADlB,IAEAH,IAAI,CAACI,IAAL,KAAcH,KAAK,CAACG,IAHtB;AAKD;;AAED,IAAIC,eAA4B,GAAG,IAAnC;;AACA,SAASC,kBAAT,GAAoC;AAClC,MAAID,eAAe,IAAI,IAAvB,EAA6B;AAC3BA,IAAAA,eAAe,GAAG,IAAIP,IAAJ,EAAlB;AACD;;AACD,SAAOO,eAAP;AACD;;AAED,OAAO,SAASE,OAAT,CACLC,aADK,EAEkB;AACvB,QAAMC,QAAQ,GAAGh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IAAN,CAhBqB,CAAvB;AAmBAlB,IAAAA,SAAS,CAAC,MAAM;AACd,YAAM2B,QAAQ,GAAGT,IAAI,CAACU,yBAAL,CAAgCC,UAAD,IAAgB;AAC9D,YAAIA,UAAU,KAAKZ,GAAnB,EAAwB;AACtBG,UAAAA,QAAQ,CAACJ,MAAM,CAACE,IAAD,EAAOD,GAAP,CAAP,CAAR;AACD;AACF,OAJgB,CAAjB;AAKA,aAAO,MAAMU,QAAQ,CAACG,MAAT,EAAb;AACD,KAPQ,EAON,CAACb,GAAD,EAAMC,IAAN,CAPM,CAAT;AASA,
|
|
1
|
+
{"version":3,"sources":["hooks.ts"],"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify"],"mappings":"AAAA,SACEA,MADF,EAEEC,QAFF,EAGEC,OAHF,EAIEC,WAJF,EAKEC,SALF,QAMO,OANP;AAOA,SAASC,IAAT,QAAwC,QAAxC;;AAEA,SAASC,oBAAT,CACEC,IADF,EAEEC,KAFF,EAGW;AACT,SACED,IAAI,CAACE,aAAL,KAAuBD,KAAK,CAACC,aAA7B,IACAF,IAAI,CAACG,EAAL,KAAYF,KAAK,CAACE,EADlB,IAEAH,IAAI,CAACI,IAAL,KAAcH,KAAK,CAACG,IAHtB;AAKD;;AAED,IAAIC,eAA4B,GAAG,IAAnC;;AACA,SAASC,kBAAT,GAAoC;AAClC,MAAID,eAAe,IAAI,IAAvB,EAA6B;AAC3BA,IAAAA,eAAe,GAAG,IAAIP,IAAJ,EAAlB;AACD;;AACD,SAAOO,eAAP;AACD;;AAED,OAAO,SAASE,OAAT,CACLC,aADK,EAEkB;A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AC1B,QAAIwC,MAAM,IAAI,IAAd,EAAoB,OAAOE,SAAP;AACpB,WAAOC,IAAI,CAACC,KAAL,CAAWJ,MAAX,CAAP;AACD,GAHoB,EAGlB,CAACA,MAAD,CAHkB,CAArB;AAIA,QAAMlB,QAAQ,GAAGrB,WAAW,CACzBwB,CAAD,IAAsB;AACpB,QAAIA,CAAC,IAAI,IAAT,EAAe;AACb;AACAgB,MAAAA,SAAS,CAACC,SAAD,CAAT;AACD,KAHD,MAGO;AACL;AACAD,MAAAA,SAAS,CAACE,IAAI,CAACE,SAAL,CAAepB,CAAf,CAAD,CAAT;AACD;AACF,GATyB,EAU1B,CAACgB,SAAD,CAV0B,CAA5B;AAaA,SAAO,CAACpB,KAAD,EAAQC,QAAR,CAAP;AACD","sourcesContent":["import React, {\n useRef,\n useState,\n useMemo,\n useCallback,\n useEffect,\n} from 'react';\nimport { MMKV, MMKVConfiguration } from './MMKV';\n\nfunction isConfigurationEqual(\n left: MMKVConfiguration,\n right: MMKVConfiguration\n): boolean {\n return (\n left.encryptionKey === right.encryptionKey &&\n left.id === right.id &&\n left.path === right.path\n );\n}\n\nlet defaultInstance: MMKV | null = null;\nfunction getDefaultInstance(): MMKV {\n if (defaultInstance == null) {\n defaultInstance = new MMKV();\n }\n return defaultInstance;\n}\n\nexport function useMMKV(\n configuration: MMKVConfiguration\n): React.RefObject<MMKV> {\n const instance = useRef<MMKV>();\n\n const lastConfiguration = useRef<MMKVConfiguration>();\n if (\n lastConfiguration.current == null ||\n !isConfigurationEqual(lastConfiguration.current, configuration)\n ) {\n lastConfiguration.current = configuration;\n instance.current = new MMKV(configuration);\n }\n\n // @ts-expect-error it's not null, I promise.\n return instance;\n}\n\nfunction createMMKVHook<\n T extends boolean | number | (string | undefined),\n TSet extends T | undefined,\n TSetAction extends TSet | ((current: T) => TSet)\n>(getter: (instance: MMKV, key: string) => T) {\n return (\n key: string,\n instance?: MMKV\n ): [value: T, setValue: (value: TSetAction) => void] => {\n const mmkv = instance ?? getDefaultInstance();\n const [value, setValue] = useState(() => getter(mmkv, key));\n const valueRef = useRef<T>(value);\n valueRef.current = value;\n\n const set = useCallback(\n (v: TSetAction) => {\n const newValue = typeof v === 'function' ? v(valueRef.current) : v;\n switch (typeof newValue) {\n case 'number':\n case 'string':\n case 'boolean':\n mmkv.set(key, newValue);\n break;\n case 'undefined':\n mmkv.delete(key);\n break;\n default:\n throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);\n }\n },\n [key, mmkv]\n );\n\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n if (changedKey === key) {\n setValue(getter(mmkv, key));\n }\n });\n return () => listener.remove();\n }, [key, mmkv]);\n\n return [value, set];\n };\n}\n\n/**\n * Use the string value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [username, setUsername] = useMMKVString(\"user.name\")\n * ```\n */\nexport const useMMKVString = createMMKVHook((instance, key) =>\n instance.getString(key)\n);\n\n/**\n * Use the number value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [age, setAge] = useMMKVNumber(\"user.age\")\n * ```\n */\nexport const useMMKVNumber = createMMKVHook((instance, key) =>\n instance.getNumber(key)\n);\n/**\n * Use the boolean value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean(\"user.isPremium\")\n * ```\n */\nexport const useMMKVBoolean = createMMKVHook((instance, key) =>\n instance.getBoolean(key)\n);\n/**\n * Use an object value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * The object will be serialized using `JSON`.\n *\n * @example\n * ```ts\n * const [user, setUser] = useMMKVObject<User>(\"user\")\n * ```\n */\nexport function useMMKVObject<T>(\n key: string,\n instance?: MMKV\n): [value: T | undefined, setValue: (value: T | undefined) => void] {\n const [string, setString] = useMMKVString(key, instance);\n\n const value = useMemo(() => {\n if (string == null) return undefined;\n return JSON.parse(string) as T;\n }, [string]);\n const setValue = useCallback(\n (v: T | undefined) => {\n if (v == null) {\n // Clear the Value\n setString(undefined);\n } else {\n // Store the Object as a serialized Value\n setString(JSON.stringify(v));\n }\n },\n [setString]\n );\n\n return [value, setValue];\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
|
|
2
2
|
declare global {
|
|
3
3
|
function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;
|
|
4
|
+
function nativeCallSyncHook(): unknown;
|
|
4
5
|
}
|
|
5
6
|
export declare const createMMKV: (config: MMKVConfiguration) => NativeMMKV;
|
|
@@ -46,4 +46,4 @@ export declare const useMMKVBoolean: (key: string, instance?: MMKV | undefined)
|
|
|
46
46
|
* const [user, setUser] = useMMKVObject<User>("user")
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
export declare function useMMKVObject<T>(key: string, instance?: MMKV): [value: T | undefined, setValue: (value: T) => void];
|
|
49
|
+
export declare function useMMKVObject<T>(key: string, instance?: MMKV): [value: T | undefined, setValue: (value: T | undefined) => void];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-mmkv",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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
|