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 @@ -66,6 +66,12 @@ setDeviceID(jsi::Runtime &rt, const jsi::String &deviceType) override; jsi::Value getDeviceID(jsi::Runtime &rt) override; jsi::Value clearSensitiveData(jsi::Runtime &rt) override; + jsi::Value cancelTasks(jsi::Runtime &rt) override; + jsi::Value runTasks(jsi::Runtime &rt) override; + jsi::Value + printMessageAndWaitAsync(jsi::Runtime &rt, const jsi::String &token) override; + void + printMessageAndWaitSync(jsi::Runtime &rt, const jsi::String &token) override; public: CommCoreModule(std::shared_ptr jsInvoker); 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 @@ -10,6 +10,8 @@ #include #include +#include "Logger.h" + namespace comm { using namespace facebook::react; @@ -1103,4 +1105,83 @@ }); } +jsi::Value CommCoreModule::cancelTasks(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { + GlobalDBSingleton::instance.setTasksCancelled(true); + taskType job = [this, promise]() { + this->jsInvoker_->invokeAsync( + [promise]() { promise->resolve(jsi::Value::undefined()); }); + }; + GlobalDBSingleton::instance.scheduleOrRun(job); + }); +} + +jsi::Value CommCoreModule::runTasks(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [this, promise]() { + GlobalDBSingleton::instance.setTasksCancelled(false); + this->jsInvoker_->invokeAsync( + [promise]() { promise->resolve(jsi::Value::undefined()); }); + }; + GlobalDBSingleton::instance.scheduleOrRun(job); + }); +} + +void CommCoreModule::printMessageAndWaitSync( + jsi::Runtime &rt, + const jsi::String &message) { + std::string msg = message.utf8(rt); + this->runSyncOrThrowJSError(rt, [msg]() { + std::ostringstream startMessage; + startMessage << "Starting sync task: " << msg; + Logger::log(startMessage.str()); + + // some dummy operations here + + std::ostringstream endMessage; + endMessage << "Ending sync task: " << msg; + Logger::log(endMessage.str()); + }); + + return; +} + +jsi::Value CommCoreModule::printMessageAndWaitAsync( + jsi::Runtime &rt, + const jsi::String &message) { + std::string msg = message.utf8(rt); + return createPromiseAsJSIValue( + rt, [this, msg](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=]() { + std::string error; + try { + std::ostringstream startMessage; + startMessage << "Starting async task: " << msg; + Logger::log(startMessage.str()); + + // some dummy operations here + + std::ostringstream endMessage; + endMessage << "Ending async task: " << msg; + Logger::log(endMessage.str()); + } catch (const std::exception &e) { + error = e.what(); + } + + this->jsInvoker_->invokeAsync([promise, error]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }; + + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + } // namespace comm 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 @@ -43,6 +43,10 @@ virtual jsi::Value setDeviceID(jsi::Runtime &rt, const jsi::String &deviceType) = 0; virtual jsi::Value getDeviceID(jsi::Runtime &rt) = 0; virtual jsi::Value clearSensitiveData(jsi::Runtime &rt) = 0; +virtual jsi::Value cancelTasks(jsi::Runtime &rt) = 0; +virtual jsi::Value runTasks(jsi::Runtime &rt) = 0; +virtual jsi::Value printMessageAndWaitAsync(jsi::Runtime &rt, const jsi::String &message) = 0; +virtual void printMessageAndWaitSync(jsi::Runtime &rt, const jsi::String &message) = 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 @@ -89,6 +89,19 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearSensitiveData(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->clearSensitiveData(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_cancelTasks(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->cancelTasks(rt); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_runTasks(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->runTasks(rt); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_printMessageAndWaitAsync(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->printMessageAndWaitAsync(rt, args[0].getString(rt)); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_printMessageAndWaitSync(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + static_cast(&turboModule)->printMessageAndWaitSync(rt, args[0].getString(rt)); + return jsi::Value::undefined(); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -117,6 +130,10 @@ methodMap_["setDeviceID"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setDeviceID}; methodMap_["getDeviceID"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getDeviceID}; methodMap_["clearSensitiveData"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearSensitiveData}; + methodMap_["cancelTasks"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_cancelTasks}; + methodMap_["runTasks"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_runTasks}; + methodMap_["printMessageAndWaitAsync"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_printMessageAndWaitAsync}; + methodMap_["printMessageAndWaitSync"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_printMessageAndWaitSync}; } diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -53,6 +53,10 @@ +setDeviceID: (deviceType: string) => Promise; +getDeviceID: () => Promise; +clearSensitiveData: () => Promise; + +cancelTasks: () => Promise; + +runTasks: () => Promise; + +printMessageAndWaitAsync: (message: string) => Promise; + +printMessageAndWaitSync: (message: string) => void; } export default (TurboModuleRegistry.getEnforcing(