Signed-off-by: ma-shaoyin <mashaoyin1@huawei.com>

Changes to be committed:
This commit is contained in:
ma-shaoyin 2023-08-04 20:17:26 +08:00
parent ad5cb03bef
commit e0a30ea34f
3 changed files with 32 additions and 1 deletions

View File

@ -27,7 +27,10 @@ config("inputmethod_js_common_config") {
config("inputmethod_js_common_public_config") {
visibility = [ "./*" ]
include_dirs = [ "./" ]
include_dirs = [
"./",
"${inputmethod_path}/frameworks/common",
]
}
ohos_static_library("inputmethod_js_common") {

View File

@ -13,10 +13,13 @@
* limitations under the License.
*/
#include <uv.h>
#include "js_callback_object.h"
namespace OHOS {
namespace MiscServices {
constexpr int32_t MAX_TIMEOUT = 2000;
JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::thread::id threadId)
: env_(env), threadId_(threadId)
{
@ -26,8 +29,31 @@ JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::threa
JSCallbackObject::~JSCallbackObject()
{
if (callback_ != nullptr) {
if (threadId_ == std::this_thread::get_id()) {
napi_delete_reference(env_, callback_);
env_ = nullptr;
return;
}
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
return;
}
isDone_ = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
work->data = this;
uv_loop_s *loop = nullptr;
napi_delete_reference(env_, callback_);
napi_get_uv_event_loop(env_, &loop);
uv_queue_work_with_qos(
loop, work, [](uv_work_t *work) {},
[](uv_work_t *work, int status) {
JSCallbackObject *jsObject = static_cast<JSCallbackObject *>(work->data);
napi_delete_reference(jsObject->env_, jsObject->callback_);
bool isFinish = true;
jsObject->isDone_->SetValue(isFinish);
},
uv_qos_user_initiated);
}
isDone_->GetValue();
env_ = nullptr;
}
} // namespace MiscServices

View File

@ -17,6 +17,7 @@
#include <thread>
#include "block_data.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
@ -29,6 +30,7 @@ public:
napi_ref callback_ = nullptr;
napi_env env_{};
std::thread::id threadId_;
std::shared_ptr<BlockData<bool>> isDone_;
};
} // namespace MiscServices
} // namespace OHOS