Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3500681
D5771.id19094.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D5771.id19094.diff
View Options
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
@@ -125,41 +125,50 @@
});
}
+jsi::Array parseDBDrafts(
+ jsi::Runtime &rt,
+ std::shared_ptr<std::vector<Draft>> draftsVectorPtr) {
+ size_t numDrafts = count_if(
+ draftsVectorPtr->begin(), draftsVectorPtr->end(), [](Draft draft) {
+ return !draft.text.empty();
+ });
+ jsi::Array jsiDrafts = jsi::Array(rt, numDrafts);
+
+ size_t writeIndex = 0;
+ for (Draft draft : *draftsVectorPtr) {
+ if (draft.text.empty()) {
+ continue;
+ }
+ auto jsiDraft = jsi::Object(rt);
+ jsiDraft.setProperty(rt, "key", draft.key);
+ jsiDraft.setProperty(rt, "text", draft.text);
+ jsiDrafts.setValueAtIndex(rt, writeIndex++, jsiDraft);
+ }
+ return jsiDrafts;
+}
+
jsi::Value CommCoreModule::getAllDrafts(jsi::Runtime &rt) {
return createPromiseAsJSIValue(
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) {
taskType job = [=, &innerRt]() {
std::string error;
std::vector<Draft> draftsVector;
- size_t numDrafts;
try {
draftsVector = DatabaseManager::getQueryExecutor().getAllDrafts();
- numDrafts = count_if(
- draftsVector.begin(), draftsVector.end(), [](Draft draft) {
- return !draft.text.empty();
- });
} catch (std::system_error &e) {
error = e.what();
}
- this->jsInvoker_->invokeAsync([=, &innerRt]() {
- if (error.size()) {
- promise->reject(error);
- return;
- }
- jsi::Array jsiDrafts = jsi::Array(innerRt, numDrafts);
-
- size_t writeIndex = 0;
- for (Draft draft : draftsVector) {
- if (draft.text.empty()) {
- continue;
- }
- auto jsiDraft = jsi::Object(innerRt);
- jsiDraft.setProperty(innerRt, "key", draft.key);
- jsiDraft.setProperty(innerRt, "text", draft.text);
- jsiDrafts.setValueAtIndex(innerRt, writeIndex++, jsiDraft);
- }
- promise->resolve(std::move(jsiDrafts));
- });
+ auto draftsVectorPtr =
+ std::make_shared<std::vector<Draft>>(std::move(draftsVector));
+ this->jsInvoker_->invokeAsync(
+ [&innerRt, draftsVectorPtr, error, promise]() {
+ if (error.size()) {
+ promise->reject(error);
+ return;
+ }
+ jsi::Array jsiDrafts = parseDBDrafts(innerRt, draftsVectorPtr);
+ promise->resolve(std::move(jsiDrafts));
+ });
};
GlobalDBSingleton::instance.scheduleOrRunCancellable(
job, promise, this->jsInvoker_);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 21, 2:12 AM (18 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2682841
Default Alt Text
D5771.id19094.diff (2 KB)
Attached To
Mode
D5771: Refactor getAllDrafts. Bring db drafts parsing to separate function
Attached
Detach File
Event Timeline
Log In to Comment