diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 66d14d784bb7301e0c63eb5fc910100cbb32a614..6ceae196ea3e303bd79dc8388ac1f9712323de86 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -455,47 +455,44 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) napi_status ret = napi_create_string_latin1(env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName); CHKCV((ret == napi_ok), "napi_create_string_latin1 fail"); asyncCallbackInfo->IncStrongRef(nullptr); - napi_status status = napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - CALL_LOG_ENTER; - sptr asyncCallbackInfo(static_cast(data)); - /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the - * asynchronous task is created, and subtract 1 from the reference count after the naked - * pointer is converted to a pointer when the asynchronous task is executed, the reference - * count of the smart pointer is guaranteed to be 1. - */ - asyncCallbackInfo->DecStrongRef(nullptr); - napi_value callback = nullptr; - napi_value callResult = nullptr; - napi_value result[2] = {0}; - if (asyncCallbackInfo->type == SUBSCRIBE_FAIL) { - CHKCV((napi_get_reference_value(env, asyncCallbackInfo->callback[1], &callback) == napi_ok), - "napi_get_reference_value fail"); - CHKCV((napi_create_string_utf8(env, asyncCallbackInfo->error.message.c_str(), - NAPI_AUTO_LENGTH, &result[0]) == napi_ok), "napi_create_string_utf8 fail"); - CHKCV((napi_create_int32(env, asyncCallbackInfo->error.code, &result[1]) == napi_ok), - "napi_create_int32 fail"); - CHKCV((napi_call_function(env, nullptr, callback, 2, result, &callResult) == napi_ok), - "napi_call_function fail"); - return; - } - CHKCV((napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback) == napi_ok), + AsyncCallbackInfo *event = asyncCallbackInfo.GetRefPtr(); + auto task = [event, env]() { + CALL_LOG_ENTER; + sptr asyncCallbackInfo(static_cast(event)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_value result[2] = {0}; + if (asyncCallbackInfo->type == SUBSCRIBE_FAIL) { + CHKCV((napi_get_reference_value(env, asyncCallbackInfo->callback[1], &callback) == napi_ok), "napi_get_reference_value fail"); - CHKCV((g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), - "Callback type invalid in async work"); - bool ret = g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); - CHKCV(ret, "Create napi data fail in async work"); + CHKCV((napi_create_string_utf8(env, asyncCallbackInfo->error.message.c_str(), + NAPI_AUTO_LENGTH, &result[0]) == napi_ok), "napi_create_string_utf8 fail"); + CHKCV((napi_create_int32(env, asyncCallbackInfo->error.code, &result[1]) == napi_ok), + "napi_create_int32 fail"); CHKCV((napi_call_function(env, nullptr, callback, 2, result, &callResult) == napi_ok), "napi_call_function fail"); - }, - asyncCallbackInfo.GetRefPtr(), &asyncCallbackInfo->asyncWork); - if (status != napi_ok - || napi_queue_async_work_with_qos( - asyncCallbackInfo->env, asyncCallbackInfo->asyncWork, napi_qos_default) != napi_ok) { - SEN_HILOGE("Create async work fail"); + return; + } + CHKCV((napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback) == napi_ok), + "napi_get_reference_value fail"); + CHKCV((g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), + "Callback type invalid in async work"); + bool ret = g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); + CHKCV(ret, "Create napi data fail in async work"); + CHKCV((napi_call_function(env, nullptr, callback, 2, result, &callResult) == napi_ok), + "napi_call_function fail"); + }; + ret = napi_send_event(asyncCallbackInfo->env, task, napi_eprio_immediate); + if (ret != napi_ok) { + SEN_HILOGE("Failed to SendEvent, ret:%{public}d", ret); asyncCallbackInfo->DecStrongRef(nullptr); } } @@ -510,17 +507,10 @@ void DeleteWork(uv_work_t *work) void EmitUvEventLoop(sptr asyncCallbackInfo) { CHKPV(asyncCallbackInfo); - uv_loop_s *loop(nullptr); - CHKCV((napi_get_uv_event_loop(asyncCallbackInfo->env, &loop) == napi_ok), "napi_get_uv_event_loop fail"); - CHKPV(loop); - uv_work_t *work = new(std::nothrow) uv_work_t; - CHKPV(work); asyncCallbackInfo->IncStrongRef(nullptr); - work->data = asyncCallbackInfo.GetRefPtr(); - int32_t ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { - CHKPV(work); - sptr asyncCallbackInfo(static_cast(work->data)); - DeleteWork(work); + AsyncCallbackInfo *event = asyncCallbackInfo.GetRefPtr(); + auto task = [event]() { + sptr asyncCallbackInfo(static_cast(event)); /** * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the @@ -564,11 +554,11 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) } ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); - }, uv_qos_default); - if (ret != 0) { - SEN_HILOGE("uv_queue_work_with_qos fail"); + }; + auto ret = napi_send_event(asyncCallbackInfo->env, task, napi_eprio_immediate); + if (ret != napi_ok) { + SEN_HILOGE("Failed to SendEvent, ret:%{public}d", ret); asyncCallbackInfo->DecStrongRef(nullptr); - DeleteWork(work); } } @@ -581,37 +571,34 @@ void EmitPromiseWork(sptr asyncCallbackInfo) napi_status ret = napi_create_string_latin1(env, "Promise", NAPI_AUTO_LENGTH, &resourceName); CHKCV((ret == napi_ok), "napi_create_string_latin1 fail"); asyncCallbackInfo->IncStrongRef(nullptr); - napi_status status = napi_create_async_work(env, nullptr, resourceName, - [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - CALL_LOG_ENTER; - sptr asyncCallbackInfo(static_cast(data)); - /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the - * asynchronous task is created, and subtract 1 from the reference count after the naked - * pointer is converted to a pointer when the asynchronous task is executed, the reference - * count of the smart pointer is guaranteed to be 1. - */ - asyncCallbackInfo->DecStrongRef(nullptr); - napi_value result[2] = {0}; - CHKCV((g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), - "Callback type invalid in promise"); - bool ret = g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); - CHKCV(ret, "Callback type invalid in promise"); - if (asyncCallbackInfo->type == FAIL) { - CHKCV((napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]) == napi_ok), - "napi_reject_deferred fail"); - } else { - CHKCV((napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[1]) == napi_ok), - "napi_resolve_deferred fail"); - } - }, - asyncCallbackInfo.GetRefPtr(), &asyncCallbackInfo->asyncWork); - if (status != napi_ok - || napi_queue_async_work_with_qos( - asyncCallbackInfo->env, asyncCallbackInfo->asyncWork, napi_qos_default) != napi_ok) { - SEN_HILOGE("Create async work fail"); + AsyncCallbackInfo *event = asyncCallbackInfo.GetRefPtr(); + auto task = [event, env]() { + CALL_LOG_ENTER; + sptr asyncCallbackInfo(static_cast(event)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); + napi_value result[2] = {0}; + CHKCV((g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), + "Callback type invalid in promise"); + bool ret = g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); + CHKCV(ret, "Callback type invalid in promise"); + if (asyncCallbackInfo->type == FAIL) { + CHKCV((napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]) == napi_ok), + "napi_reject_deferred fail"); + } else { + CHKCV((napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[1]) == napi_ok), + "napi_resolve_deferred fail"); + } + }; + ret = napi_send_event(asyncCallbackInfo->env, task, napi_eprio_immediate); + if (ret != napi_ok) { + SEN_HILOGE("Failed to SendEvent, ret:%{public}d", ret); asyncCallbackInfo->DecStrongRef(nullptr); } }