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 @@ -37,7 +37,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; @@ -45,7 +45,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 @@ -387,40 +387,36 @@ }); } -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(); + std::promise operationsResult; + this->databaseThread->scheduleTask([&messageStoreOps, &operationsResult]() { + try { + DatabaseManager::getQueryExecutor().beginTransaction(); + for (const auto &operation : messageStoreOps) { + operation->execute(); + } + DatabaseManager::getQueryExecutor().commitTransaction(); + } catch (const std::exception &e) { + DatabaseManager::getQueryExecutor().rollbackTransaction(); + operationsResult.set_exception(std::make_exception_ptr(e)); + } + }); + + try { + operationsResult.get_future().get(); + } catch (const std::exception &e) { + throw jsi::JSError(rt, e.what()); + } } jsi::Value CommCoreModule::getAllThreads(jsi::Runtime &rt) { @@ -721,37 +717,36 @@ }); } -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()); + } + + std::promise operationsResult; + this->databaseThread->scheduleTask([&threadStoreOps, &operationsResult]() { + try { + DatabaseManager::getQueryExecutor().beginTransaction(); + for (const auto &operation : threadStoreOps) { + operation->execute(); + } + DatabaseManager::getQueryExecutor().commitTransaction(); + } catch (const std::exception &e) { + DatabaseManager::getQueryExecutor().rollbackTransaction(); + operationsResult.set_exception(std::make_exception_ptr(e)); + } + }); + + try { + operationsResult.get_future().get(); + } 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(); } 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 @@ -340,10 +340,12 @@ payload: { id, threadInfo: threadInfos[id] }, })), ]; - const processingResult: boolean = global.CommCoreModule.processThreadStoreOperationsSync( - convertThreadStoreOperationsToClientDBOperations(operations), - ); - if (!processingResult) { + try { + global.CommCoreModule.processThreadStoreOperationsSync( + convertThreadStoreOperationsToClientDBOperations(operations), + ); + } catch (exception) { + console.log(exception); return { ...state, cookie: null }; } return state; @@ -359,10 +361,10 @@ payload: translateRawMessageInfoToClientDBMessageInfo(messages[id]), })), ]; - const processingResult: boolean = global.CommCoreModule.processMessageStoreOperationsSync( - operations, - ); - if (!processingResult) { + try { + global.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;