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 @@ -31,6 +31,7 @@ jsi::Runtime &rt, const jsi::String &oldKey, const jsi::String &newKey) override; + jsi::Value getClientDBStore(jsi::Runtime &rt) override; jsi::Value getAllDrafts(jsi::Runtime &rt) override; jsi::Value removeAllDrafts(jsi::Runtime &rt) override; jsi::Value getAllMessages(jsi::Runtime &rt) override; 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 @@ -255,6 +255,57 @@ return jsiThreads; } +jsi::Value CommCoreModule::getClientDBStore(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::vector draftsVector; + std::vector threadsVector; + std::vector>> messagesVector; + try { + draftsVector = DatabaseManager::getQueryExecutor().getAllDrafts(); + messagesVector = + DatabaseManager::getQueryExecutor().getAllMessages(); + threadsVector = DatabaseManager::getQueryExecutor().getAllThreads(); + } catch (std::system_error &e) { + error = e.what(); + } + auto draftsVectorPtr = + std::make_shared>(std::move(draftsVector)); + auto messagesVectorPtr = std::make_shared< + std::vector>>>( + std::move(messagesVector)); + auto threadsVectorPtr = + std::make_shared>(std::move(threadsVector)); + this->jsInvoker_->invokeAsync([&innerRt, + draftsVectorPtr, + messagesVectorPtr, + threadsVectorPtr, + error, + promise]() { + if (error.size()) { + promise->reject(error); + return; + } + jsi::Array jsiDrafts = parseDBDrafts(innerRt, draftsVectorPtr); + jsi::Array jsiMessages = + parseDBMessages(innerRt, messagesVectorPtr); + jsi::Array jsiThreads = parseDBThreads(innerRt, threadsVectorPtr); + + auto jsiClientDBStore = jsi::Object(innerRt); + jsiClientDBStore.setProperty(innerRt, "messages", jsiMessages); + jsiClientDBStore.setProperty(innerRt, "threads", jsiThreads); + jsiClientDBStore.setProperty(innerRt, "drafts", jsiDrafts); + + promise->resolve(std::move(jsiClientDBStore)); + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + jsi::Value CommCoreModule::getAllDrafts(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { 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 @@ -21,6 +21,7 @@ virtual jsi::Value getDraft(jsi::Runtime &rt, const jsi::String &key) = 0; virtual jsi::Value updateDraft(jsi::Runtime &rt, const jsi::String &key, const jsi::String &text) = 0; virtual jsi::Value moveDraft(jsi::Runtime &rt, const jsi::String &oldKey, const jsi::String &newKey) = 0; +virtual jsi::Value getClientDBStore(jsi::Runtime &rt) = 0; virtual jsi::Value getAllDrafts(jsi::Runtime &rt) = 0; virtual jsi::Value removeAllDrafts(jsi::Runtime &rt) = 0; virtual jsi::Value getAllMessages(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 @@ -21,6 +21,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_moveDraft(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->moveDraft(rt, args[0].getString(rt), args[1].getString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getClientDBStore(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getClientDBStore(rt); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllDrafts(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->getAllDrafts(rt); } @@ -95,6 +98,7 @@ methodMap_["getDraft"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getDraft}; methodMap_["updateDraft"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_updateDraft}; methodMap_["moveDraft"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_moveDraft}; + methodMap_["getClientDBStore"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getClientDBStore}; methodMap_["getAllDrafts"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllDrafts}; methodMap_["removeAllDrafts"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_removeAllDrafts}; methodMap_["getAllMessages"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getAllMessages}; diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -18,10 +18,17 @@ ClientDBThreadStoreOperation, } from 'lib/types/thread-types'; +type ClientDBStore = { + +messages: $ReadOnlyArray, + +drafts: $ReadOnlyArray, + +threads: $ReadOnlyArray, +}; + export interface Spec extends TurboModule { +getDraft: (key: string) => Promise; +updateDraft: (key: string, text: string) => Promise; +moveDraft: (oldKey: string, newKey: string) => Promise; + +getClientDBStore: () => Promise; +getAllDrafts: () => Promise<$ReadOnlyArray>; +removeAllDrafts: () => Promise; +getAllMessages: () => Promise<$ReadOnlyArray>;