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 @@ -14,8 +14,6 @@ class CommCoreModule : public facebook::react::CommCoreModuleSchemaCxxSpecJSI { const int codeVersion{147}; - - std::unique_ptr databaseThread; std::unique_ptr cryptoThread; CommSecureStore secureStore; 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 @@ -1,6 +1,7 @@ #include "CommCoreModule.h" #include "DatabaseManager.h" #include "GRPCStreamHostObject.h" +#include "InternalModules/GlobalDBSingleton.h" #include "InternalModules/GlobalNetworkSingleton.h" #include "MessageStoreOperations.h" #include "ThreadStoreOperations.h" @@ -17,7 +18,7 @@ jsi::Runtime &rt, std::function task) { std::promise promise; - this->databaseThread->scheduleTask([&promise, &task]() { + GlobalDBSingleton::instance.scheduleOrRun([&promise, &task]() { try { if constexpr (std::is_void::value) { task(); @@ -60,7 +61,7 @@ promise->resolve(std::move(draft)); }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -85,7 +86,7 @@ } }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -115,7 +116,7 @@ } }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -155,7 +156,7 @@ promise->resolve(std::move(jsiDrafts)); }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -177,7 +178,7 @@ promise->resolve(jsi::Value::undefined()); }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -307,7 +308,7 @@ promise->resolve(std::move(jsiMessages)); }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -398,7 +399,7 @@ } }); }; - this->databaseThread->scheduleTask(job); + GlobalDBSingleton::instance.scheduleOrRun(job); }); } @@ -430,7 +431,7 @@ jsi::Value CommCoreModule::getAllThreads(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([=, &innerRt]() { + GlobalDBSingleton::instance.scheduleOrRun([=, &innerRt]() { std::string error; std::vector threadsVector; size_t numThreads; @@ -693,7 +694,7 @@ } return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([=]() { + GlobalDBSingleton::instance.scheduleOrRun([=]() { std::string error = operationsError; if (!error.size()) { try { @@ -757,7 +758,7 @@ return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([=]() { + GlobalDBSingleton::instance.scheduleOrRun([=]() { crypto::Persist persist; std::string error; try { @@ -789,7 +790,7 @@ if (persist.isEmpty()) { crypto::Persist newPersist = this->cryptoModule->storeAsB64(storedSecretKey.value()); - this->databaseThread->scheduleTask([=]() { + GlobalDBSingleton::instance.scheduleOrRun([=]() { std::string error; try { DatabaseManager::getQueryExecutor().storeOlmPersistData( @@ -888,8 +889,8 @@ CommCoreModule::CommCoreModule( std::shared_ptr jsInvoker) : facebook::react::CommCoreModuleSchemaCxxSpecJSI(jsInvoker), - databaseThread(std::make_unique("database")), cryptoThread(std::make_unique("crypto")) { + GlobalDBSingleton::instance.enableMultithreading(); GlobalNetworkSingleton::instance.enableMultithreading(); } @@ -904,29 +905,30 @@ rt, [this, notifyToken](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([this, notifyToken, promise]() { - std::string error; - try { - DatabaseManager::getQueryExecutor().setNotifyToken(notifyToken); - } catch (std::system_error &e) { - error = e.what(); - } + GlobalDBSingleton::instance.scheduleOrRun( + [this, notifyToken, promise]() { + std::string error; + try { + DatabaseManager::getQueryExecutor().setNotifyToken(notifyToken); + } catch (std::system_error &e) { + error = e.what(); + } - this->jsInvoker_->invokeAsync([error, promise]() { - if (error.size()) { - promise->reject(error); - } else { - promise->resolve(jsi::Value::undefined()); - } - }); - }); + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }); }); } jsi::Value CommCoreModule::clearNotifyToken(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([this, promise]() { + GlobalDBSingleton::instance.scheduleOrRun([this, promise]() { std::string error; try { DatabaseManager::getQueryExecutor().clearNotifyToken(); @@ -951,7 +953,9 @@ rt, [this, currentUserID](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([this, promise, currentUserID]() { + GlobalDBSingleton::instance.scheduleOrRun([this, + promise, + currentUserID]() { std::string error; try { DatabaseManager::getQueryExecutor().setCurrentUserID(currentUserID); @@ -972,7 +976,7 @@ jsi::Value CommCoreModule::getCurrentUserID(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([this, &innerRt, promise]() { + GlobalDBSingleton::instance.scheduleOrRun([this, &innerRt, promise]() { std::string error; std::string result; try { @@ -994,7 +998,7 @@ jsi::Value CommCoreModule::clearSensitiveData(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { - this->databaseThread->scheduleTask([this, promise]() { + GlobalDBSingleton::instance.scheduleOrRun([this, promise]() { std::string error; try { DatabaseManager::getQueryExecutor().clearSensitiveData();