diff --git a/native/android/app/src/cpp/GlobalDBSingleton.cpp b/native/android/app/src/cpp/GlobalDBSingleton.cpp --- a/native/android/app/src/cpp/GlobalDBSingleton.cpp +++ b/native/android/app/src/cpp/GlobalDBSingleton.cpp @@ -18,6 +18,13 @@ this->scheduleOrRunCancellableCommonImpl(task); } +void GlobalDBSingleton::scheduleOrRunCancellable( + const taskType task, + const std::shared_ptr promise, + const std::shared_ptr jsInvoker) { + this->scheduleOrRunCancellableCommonImpl(task, promise, jsInvoker); +} + void GlobalDBSingleton::enableMultithreading() { this->enableMultithreadingCommonImpl(); } 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 @@ -5,6 +5,7 @@ #include "../Tools/WorkerThread.h" #include "../_generated/NativeModules.h" #include "../grpc/Client.h" +#include #include #include diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h --- a/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h +++ b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h @@ -1,6 +1,7 @@ #pragma once #include "../../Tools/WorkerThread.h" +#include #include @@ -36,6 +37,24 @@ }); } + void scheduleOrRunCancellableCommonImpl( + const taskType task, + const std::shared_ptr promise, + const std::shared_ptr jsInvoker) { + if (this->tasksCancelled.load()) { + jsInvoker->invokeAsync([=]() { promise->reject(TASK_CANCELLED_FLAG); }); + return; + } + + scheduleOrRunCommonImpl([this, task, promise, jsInvoker]() { + if (this->tasksCancelled.load()) { + jsInvoker->invokeAsync([=]() { promise->reject(TASK_CANCELLED_FLAG); }); + return; + } + task(); + }); + } + void enableMultithreadingCommonImpl() { if (this->databaseThread == nullptr) { this->databaseThread = std::make_unique("database"); @@ -47,6 +66,10 @@ static GlobalDBSingleton instance; void scheduleOrRun(const taskType task); void scheduleOrRunCancellable(const taskType task); + void scheduleOrRunCancellable( + const taskType task, + const std::shared_ptr promise, + const std::shared_ptr jsInvoker); void enableMultithreading(); void setTasksCancelled(bool tasksCancelled) { this->tasksCancelled.store(tasksCancelled); diff --git a/native/ios/Comm/GlobalDBSingleton.mm b/native/ios/Comm/GlobalDBSingleton.mm --- a/native/ios/Comm/GlobalDBSingleton.mm +++ b/native/ios/Comm/GlobalDBSingleton.mm @@ -1,5 +1,6 @@ #import "GlobalDBSingleton.h" #import +#include namespace comm { GlobalDBSingleton GlobalDBSingleton::instance; @@ -32,6 +33,20 @@ }); } +void GlobalDBSingleton::scheduleOrRunCancellable( + const taskType task, + const std::shared_ptr promise, + const std::shared_ptr jsInvoker) { + if (NSThread.isMainThread || this->multithreadingEnabled.load()) { + this->scheduleOrRunCancellableCommonImpl(task, promise, jsInvoker); + return; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + this->scheduleOrRunCancellableCommonImpl(task, promise, jsInvoker); + }); +} + void GlobalDBSingleton::enableMultithreading() { if (NSThread.isMainThread) { this->enableMultithreadingCommonImpl();