Page MenuHomePhabricator

D10613.id35658.diff
No OneTemporary

D10613.id35658.diff

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
@@ -1276,7 +1276,7 @@
if (!error.size()) {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
::createBackup(
rust::string(backupID),
rust::string(backupSecretStr),
@@ -1298,7 +1298,7 @@
return createPromiseAsJSIValue(
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
::restoreBackup(rust::string(backupSecretStr), currentID);
});
}
diff --git a/native/cpp/CommonCpp/NativeModules/CommRustModule.cpp b/native/cpp/CommonCpp/NativeModules/CommRustModule.cpp
--- a/native/cpp/CommonCpp/NativeModules/CommRustModule.cpp
+++ b/native/cpp/CommonCpp/NativeModules/CommRustModule.cpp
@@ -19,7 +19,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityGenerateNonce(currentID);
} catch (const std::exception &e) {
error = e.what();
@@ -61,7 +61,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityRegisterUser(
usernameRust,
passwordRust,
@@ -114,7 +114,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityLoginPasswordUser(
usernameRust,
passwordRust,
@@ -169,7 +169,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityLoginWalletUser(
siweMessageRust,
siweSignatureRust,
@@ -209,7 +209,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityUpdateUserPassword(
userIDRust,
deviceIDRust,
@@ -240,7 +240,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityDeleteUser(
userIDRust, deviceIDRust, accessTokenRust, currentID);
} catch (const std::exception &e) {
@@ -269,7 +269,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityGetOutboundKeysForUser(
authUserIDRust,
authDeviceIDRust,
@@ -302,7 +302,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityGetInboundKeysForUser(
authUserIDRust,
authDeviceIDRust,
@@ -325,7 +325,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityVersionSupported(currentID);
} catch (const std::exception &e) {
error = e.what();
@@ -357,7 +357,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityUploadOneTimeKeys(
authUserIDRust,
authDeviceIDRust,
@@ -391,7 +391,7 @@
std::string error;
try {
auto currentID = RustPromiseManager::instance.addPromise(
- promise, this->jsInvoker_, innerRt);
+ {promise, this->jsInvoker_, innerRt});
identityGetKeyserverKeys(
authUserIDRust,
authDeviceIDRust,
diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.h b/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.h
--- a/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.h
+++ b/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.h
@@ -5,7 +5,9 @@
#include <jsi/JSIDynamic.h>
#include <atomic>
+#include <future>
#include <shared_mutex>
+#include <variant>
namespace comm {
@@ -20,19 +22,26 @@
public:
static RustPromiseManager instance;
- uint32_t addPromise(
- std::shared_ptr<facebook::react::Promise> promise,
- std::shared_ptr<facebook::react::CallInvoker> jsInvoker,
- facebook::jsi::Runtime &rt);
- void removePromise(uint32_t id);
- void resolvePromise(uint32_t id, folly::dynamic ret);
- void rejectPromise(uint32_t id, const std::string &error);
- struct PromiseInfo {
+ struct JSIPromiseInfo {
std::shared_ptr<facebook::react::Promise> promise;
std::shared_ptr<facebook::react::CallInvoker> jsInvoker;
facebook::jsi::Runtime &rt;
};
+
+ struct CPPPromiseInfo {
+ std::promise<folly::dynamic> promise;
+ };
+
+ using PromiseInfo = std::variant<JSIPromiseInfo, CPPPromiseInfo>;
+
+ uint32_t addPromise(JSIPromiseInfo info);
+ uint32_t addPromise(CPPPromiseInfo info);
+
+ void removePromise(uint32_t id);
+ void resolvePromise(uint32_t id, folly::dynamic ret);
+ void rejectPromise(uint32_t id, const std::string &error);
+
std::unordered_map<uint32_t, PromiseInfo> promises;
std::shared_mutex mutex;
};
diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.cpp b/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.cpp
--- a/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.cpp
+++ b/native/cpp/CommonCpp/NativeModules/InternalModules/RustPromiseManager.cpp
@@ -1,20 +1,29 @@
#include "RustPromiseManager.h"
+#include <exception>
namespace comm {
RustPromiseManager RustPromiseManager::instance;
-RustPromiseManager::RustPromiseManager(){};
+RustPromiseManager::RustPromiseManager() {
+}
+
+uint32_t RustPromiseManager::addPromise(JSIPromiseInfo info) {
+ if (!info.jsInvoker) {
+ throw std::invalid_argument("jsInvoker is missing");
+ }
-uint32_t RustPromiseManager::addPromise(
- std::shared_ptr<facebook::react::Promise> promise,
- std::shared_ptr<facebook::react::CallInvoker> jsInvoker,
- facebook::jsi::Runtime &rt) {
uint32_t id = getNextID();
- PromiseInfo info = {promise, jsInvoker, rt};
// Acquire a lock for writing
std::unique_lock<std::shared_mutex> lock(mutex);
- promises.insert({id, info});
+ promises.insert({id, std::move(info)});
+ return id;
+}
+
+uint32_t RustPromiseManager::addPromise(CPPPromiseInfo info) {
+ auto id = getNextID();
+ std::unique_lock<std::shared_mutex> lock(mutex);
+ promises.insert({id, std::move(info)});
return id;
}
@@ -33,13 +42,14 @@
}
// Release the shared lock
lock.unlock();
- auto promiseInfo = it->second;
- if (promiseInfo.jsInvoker) {
- promiseInfo.jsInvoker->invokeAsync([promiseInfo, ret]() {
+
+ if (auto jsiPtr = std::get_if<JSIPromiseInfo>(&it->second)) {
+ jsiPtr->jsInvoker->invokeAsync([promiseInfo = *jsiPtr, ret]() {
promiseInfo.promise->resolve(valueFromDynamic(promiseInfo.rt, ret));
});
- } else {
- promiseInfo.promise->resolve(valueFromDynamic(promiseInfo.rt, ret));
+
+ } else if (auto cppPtr = std::get_if<CPPPromiseInfo>(&it->second)) {
+ cppPtr->promise.set_value(ret);
}
removePromise(id);
}
@@ -53,11 +63,14 @@
}
// Release the shared lock
lock.unlock();
- if (it->second.jsInvoker) {
- it->second.jsInvoker->invokeAsync(
- [promise = it->second.promise, error]() { promise->reject(error); });
- } else {
- it->second.promise->reject(error);
+
+ if (auto jsiPtr = std::get_if<JSIPromiseInfo>(&it->second)) {
+ jsiPtr->jsInvoker->invokeAsync(
+ [promise = jsiPtr->promise, error]() { promise->reject(error); });
+
+ } else if (auto cppPtr = std::get_if<CPPPromiseInfo>(&it->second)) {
+ cppPtr->promise.set_exception(
+ std::make_exception_ptr(std::runtime_error(error)));
}
removePromise(id);
}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 3, 9:05 AM (19 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2610943
Default Alt Text
D10613.id35658.diff (9 KB)

Event Timeline