diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -39,7 +39,7 @@ jsi::Value processMessageStoreOperations( jsi::Runtime &rt, const jsi::Array &operations) override; - bool processMessageStoreOperationsSync( + void processMessageStoreOperationsSync( jsi::Runtime &rt, const jsi::Array &operations) override; jsi::Value getAllThreads(jsi::Runtime &rt) override; @@ -47,7 +47,7 @@ jsi::Value processThreadStoreOperations( jsi::Runtime &rt, const jsi::Array &operations) override; - bool processThreadStoreOperationsSync( + void processThreadStoreOperationsSync( jsi::Runtime &rt, const jsi::Array &operations) override; jsi::Value diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -25,7 +25,12 @@ std::promise promise; this->databaseThread->scheduleTask([&promise, &task]() { try { - promise.set_value(task()); + if constexpr (std::is_void::value) { + task(); + promise.set_value(); + } else { + promise.set_value(task()); + } } catch (const std::exception &e) { promise.set_exception(std::make_exception_ptr(e)); } @@ -403,40 +408,29 @@ }); } -bool CommCoreModule::processMessageStoreOperationsSync( +void CommCoreModule::processMessageStoreOperationsSync( jsi::Runtime &rt, const jsi::Array &operations) { - - std::promise operationsResult; - std::future operationsResultFuture = operationsResult.get_future(); - std::vector> messageStoreOps; - std::string operationsError; try { messageStoreOps = createMessageStoreOperations(rt, operations); - } catch (std::runtime_error &e) { - return false; + } catch (const std::exception &e) { + throw jsi::JSError(rt, e.what()); } - this->databaseThread->scheduleTask( - [=, &messageStoreOps, &operationsResult, &rt]() { - std::string error = operationsError; - if (!error.size()) { - try { - DatabaseManager::getQueryExecutor().beginTransaction(); - for (const auto &operation : messageStoreOps) { - operation->execute(); - } - DatabaseManager::getQueryExecutor().commitTransaction(); - } catch (std::system_error &e) { - error = e.what(); - DatabaseManager::getQueryExecutor().rollbackTransaction(); - } - } - operationsResult.set_value(error.size() == 0); - }); - return operationsResultFuture.get(); + this->runSyncOrThrowJSError(rt, [&messageStoreOps]() { + try { + DatabaseManager::getQueryExecutor().beginTransaction(); + for (const auto &operation : messageStoreOps) { + operation->execute(); + } + DatabaseManager::getQueryExecutor().commitTransaction(); + } catch (const std::exception &e) { + DatabaseManager::getQueryExecutor().rollbackTransaction(); + throw e; + } + }); } jsi::Value CommCoreModule::getAllThreads(jsi::Runtime &rt) { @@ -730,37 +724,29 @@ }); } -bool CommCoreModule::processThreadStoreOperationsSync( +void CommCoreModule::processThreadStoreOperationsSync( jsi::Runtime &rt, const jsi::Array &operations) { - - std::promise operationsResult; - std::future operationsResultFuture = operationsResult.get_future(); std::vector> threadStoreOps; - std::string operationsError; + try { threadStoreOps = createThreadStoreOperations(rt, operations); - } catch (std::runtime_error &e) { - return false; + } catch (const std::exception &e) { + throw jsi::JSError(rt, e.what()); } - this->databaseThread->scheduleTask( - [=, &threadStoreOps, &operationsResult, &rt]() { - std::string error = operationsError; - if (!error.size()) { - try { - DatabaseManager::getQueryExecutor().beginTransaction(); - for (const auto &operation : threadStoreOps) { - operation->execute(); - } - DatabaseManager::getQueryExecutor().commitTransaction(); - } catch (std::system_error &e) { - error = e.what(); - DatabaseManager::getQueryExecutor().rollbackTransaction(); - } - } - operationsResult.set_value(error.size() == 0); - }); - return operationsResultFuture.get(); + + this->runSyncOrThrowJSError(rt, [&threadStoreOps]() { + try { + DatabaseManager::getQueryExecutor().beginTransaction(); + for (const auto &operation : threadStoreOps) { + operation->execute(); + } + DatabaseManager::getQueryExecutor().commitTransaction(); + } catch (const std::exception &e) { + DatabaseManager::getQueryExecutor().rollbackTransaction(); + throw e; + } + }); } jsi::Value CommCoreModule::initializeCryptoAccount( diff --git a/native/cpp/CommonCpp/_generated/NativeModules.h b/native/cpp/CommonCpp/_generated/NativeModules.h --- a/native/cpp/CommonCpp/_generated/NativeModules.h +++ b/native/cpp/CommonCpp/_generated/NativeModules.h @@ -26,11 +26,11 @@ virtual jsi::Value getAllMessages(jsi::Runtime &rt) = 0; virtual jsi::Array getAllMessagesSync(jsi::Runtime &rt) = 0; virtual jsi::Value processMessageStoreOperations(jsi::Runtime &rt, const jsi::Array &operations) = 0; -virtual bool processMessageStoreOperationsSync(jsi::Runtime &rt, const jsi::Array &operations) = 0; +virtual void processMessageStoreOperationsSync(jsi::Runtime &rt, const jsi::Array &operations) = 0; virtual jsi::Value getAllThreads(jsi::Runtime &rt) = 0; virtual jsi::Array getAllThreadsSync(jsi::Runtime &rt) = 0; virtual jsi::Value processThreadStoreOperations(jsi::Runtime &rt, const jsi::Array &operations) = 0; -virtual bool processThreadStoreOperationsSync(jsi::Runtime &rt, const jsi::Array &operations) = 0; +virtual void processThreadStoreOperationsSync(jsi::Runtime &rt, const jsi::Array &operations) = 0; virtual jsi::Value initializeCryptoAccount(jsi::Runtime &rt, const jsi::String &userId) = 0; virtual jsi::Value getUserPublicKey(jsi::Runtime &rt) = 0; virtual jsi::Value getUserOneTimeKeys(jsi::Runtime &rt) = 0; diff --git a/native/cpp/CommonCpp/_generated/NativeModules.cpp b/native/cpp/CommonCpp/_generated/NativeModules.cpp --- a/native/cpp/CommonCpp/_generated/NativeModules.cpp +++ b/native/cpp/CommonCpp/_generated/NativeModules.cpp @@ -37,7 +37,8 @@ return static_cast(&turboModule)->processMessageStoreOperations(rt, args[0].getObject(rt).getArray(rt)); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processMessageStoreOperationsSync(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { - return static_cast(&turboModule)->processMessageStoreOperationsSync(rt, args[0].getObject(rt).getArray(rt)); + static_cast(&turboModule)->processMessageStoreOperationsSync(rt, args[0].getObject(rt).getArray(rt)); + return jsi::Value::undefined(); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllThreads(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->getAllThreads(rt); @@ -49,7 +50,8 @@ return static_cast(&turboModule)->processThreadStoreOperations(rt, args[0].getObject(rt).getArray(rt)); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_processThreadStoreOperationsSync(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { - return static_cast(&turboModule)->processThreadStoreOperationsSync(rt, args[0].getObject(rt).getArray(rt)); + static_cast(&turboModule)->processThreadStoreOperationsSync(rt, args[0].getObject(rt).getArray(rt)); + return jsi::Value::undefined(); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeCryptoAccount(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->initializeCryptoAccount(rt, args[0].getString(rt)); diff --git a/native/redux/persist.js b/native/redux/persist.js --- a/native/redux/persist.js +++ b/native/redux/persist.js @@ -341,10 +341,12 @@ payload: { id, threadInfo: threadInfos[id] }, })), ]; - const processingResult: boolean = commCoreModule.processThreadStoreOperationsSync( - convertThreadStoreOperationsToClientDBOperations(operations), - ); - if (!processingResult) { + try { + commCoreModule.processThreadStoreOperationsSync( + convertThreadStoreOperationsToClientDBOperations(operations), + ); + } catch (exception) { + console.log(exception); return { ...state, cookie: null }; } return state; @@ -360,10 +362,10 @@ payload: translateRawMessageInfoToClientDBMessageInfo(messages[id]), })), ]; - const processingResult: boolean = commCoreModule.processMessageStoreOperationsSync( - operations, - ); - if (!processingResult) { + try { + commCoreModule.processMessageStoreOperationsSync(operations); + } catch (exception) { + console.log(exception); return { ...state, cookie: null }; } return state; diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -32,7 +32,7 @@ ) => Promise; +processMessageStoreOperationsSync: ( operations: $ReadOnlyArray, - ) => boolean; + ) => void; +getAllThreads: () => Promise<$ReadOnlyArray>; +getAllThreadsSync: () => $ReadOnlyArray; +processThreadStoreOperations: ( @@ -40,7 +40,7 @@ ) => Promise; +processThreadStoreOperationsSync: ( operations: $ReadOnlyArray, - ) => boolean; + ) => void; +initializeCryptoAccount: (userId: string) => Promise; +getUserPublicKey: () => Promise; +getUserOneTimeKeys: () => Promise;