From a1572f47b08ba1ce3ceb1c0fa995a691f5484fd8 Mon Sep 17 00:00:00 2001 From: Anton Dalgren Date: Tue, 26 Sep 2023 10:23:38 +0200 Subject: [PATCH] feat: return outcome of set and recrypt. This commits adds a boolean return value from the underlying MMKV library to the JS environment for the set and recrypt calls. --- android/src/main/cpp/MmkvHostObject.cpp | 18 ++++++++++-------- example/src/App.tsx | 7 ++++++- ios/MmkvHostObject.mm | 19 ++++++++++--------- src/MMKV.ts | 15 ++++++++------- src/createMMKV.mock.ts | 6 +++++- src/createMMKV.web.ts | 3 ++- 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/android/src/main/cpp/MmkvHostObject.cpp b/android/src/main/cpp/MmkvHostObject.cpp index 3d715d02..c46d62ec 100644 --- a/android/src/main/cpp/MmkvHostObject.cpp +++ b/android/src/main/cpp/MmkvHostObject.cpp @@ -70,18 +70,19 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro "MMKV::set: First argument ('key') has to be of type string!"); } + bool result = false; auto keyName = arguments[0].getString(runtime).utf8(runtime); if (arguments[1].isBool()) { // bool - instance->set(arguments[1].getBool(), keyName); + result = instance->set(arguments[1].getBool(), keyName); } else if (arguments[1].isNumber()) { // number - instance->set(arguments[1].getNumber(), keyName); + result = instance->set(arguments[1].getNumber(), keyName); } else if (arguments[1].isString()) { // string auto stringValue = arguments[1].getString(runtime).utf8(runtime); - instance->set(stringValue, keyName); + result = instance->set(stringValue, keyName); } else if (arguments[1].isObject()) { // object auto object = arguments[1].asObject(runtime); @@ -91,7 +92,7 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro auto bufferValue = typedArray.getBuffer(runtime); mmkv::MMBuffer buffer(bufferValue.data(runtime), bufferValue.size(runtime), mmkv::MMBufferCopyFlag::MMBufferNoCopy); - instance->set(buffer, keyName); + result = instance->set(buffer, keyName); } else { // unknown object throw jsi::JSError( @@ -104,7 +105,7 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro "MMKV::set: 'value' argument is not of type bool, number, string or buffer!"); } - return jsi::Value::undefined(); + return jsi::Value(result); }); } @@ -269,19 +270,20 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro 1, // encryptionKey [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value { + bool result; if (arguments[0].isUndefined()) { // reset encryption key to "no encryption" - instance->reKey(std::string()); + result = instance->reKey(std::string()); } else if (arguments[0].isString()) { // reKey(..) with new encryption-key auto encryptionKey = arguments[0].getString(runtime).utf8(runtime); - instance->reKey(encryptionKey); + result = instance->reKey(encryptionKey); } else { throw jsi::JSError( runtime, "First argument ('encryptionKey') has to be of type string (or undefined)!"); } - return jsi::Value::undefined(); + return jsi::Value(result); }); } diff --git a/example/src/App.tsx b/example/src/App.tsx index 4fe88126..eaa41356 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -9,6 +9,7 @@ export default function App() { const [text, setText] = React.useState(''); const [key, setKey] = React.useState(''); const [keys, setKeys] = React.useState([]); + const [success, setSuccess] = React.useState(); const [example, setExample] = useMMKVString('yeeeet'); @@ -19,7 +20,8 @@ export default function App() { } try { console.log('setting...'); - storage.set(key, text); + const result = storage.set(key, text); + setSuccess(result); console.log('set.'); } catch (e) { console.error('Error:', e); @@ -86,6 +88,9 @@ export default function App() { onChangeText={setText} /> + + {success && Saved: {success ? 'successfully' : 'failed'}} +