From 12b9bd9980d43a732d4fed2e94a0c35e636943f2 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Thu, 27 Feb 2025 20:34:24 +0800 Subject: [PATCH] Modify the async function Signed-off-by: wuzhihuitmac Change-Id: I7920f9f29f5311cc4e2449e06490fb342731de2c --- .../napi/vibrator/src/vibrator_napi_utils.cpp | 139 +++++++++--------- 1 file changed, 67 insertions(+), 72 deletions(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 6e3c122..ed9af87 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -318,42 +318,39 @@ 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); - if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { - EmitSystemCallback(env, asyncCallbackInfo); - return; - } - CHKPV(asyncCallbackInfo->callback[0]); - napi_value callback = nullptr; - napi_status ret = napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - CHKCV((ret == napi_ok), "napi_get_reference_value fail"); - napi_value result[RESULT_LENGTH] = { 0 }; - CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), - "Callback type invalid in async work"); - bool state = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, - sizeof(result) / sizeof(napi_value)); - CHKCV(state, "Create napi data fail in async work"); - napi_value callResult = nullptr; - 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) { - MISC_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); + if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { + EmitSystemCallback(env, asyncCallbackInfo); + return; + } + CHKPV(asyncCallbackInfo->callback[0]); + napi_value callback = nullptr; + napi_status ret = napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + CHKCV((ret == napi_ok), "napi_get_reference_value fail"); + napi_value result[RESULT_LENGTH] = { 0 }; + CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), + "Callback type invalid in async work"); + bool state = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, + sizeof(result) / sizeof(napi_value)); + CHKCV(state, "Create napi data fail in async work"); + napi_value callResult = nullptr; + 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) { + MISC_HILOGE("Failed to SendEvent, ret:%{public}d", ret); asyncCallbackInfo->DecStrongRef(nullptr); } } @@ -369,42 +366,40 @@ void EmitPromiseWork(sptr asyncCallbackInfo) CHKCV((ret == napi_ok), "napi_create_string_latin1 fail"); // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor 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); - CHKPV(asyncCallbackInfo->deferred); - if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { - EmitSystemCallback(env, asyncCallbackInfo); - return; - } - napi_value result[RESULT_LENGTH] = { 0 }; - CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), - "Callback type invalid in promise"); - bool ret = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, - sizeof(result) / sizeof(napi_value)); - CHKCV(ret, "Callback type invalid in promise"); - if (asyncCallbackInfo->error.code != SUCCESS) { - 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) { - MISC_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); + CHKPV(asyncCallbackInfo->deferred); + if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { + EmitSystemCallback(env, asyncCallbackInfo); + return; + } + napi_value result[RESULT_LENGTH] = { 0 }; + CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), + "Callback type invalid in promise"); + bool ret = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, + sizeof(result) / sizeof(napi_value)); + CHKCV(ret, "Callback type invalid in promise"); + if (asyncCallbackInfo->error.code != SUCCESS) { + 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) { + MISC_HILOGE("Failed to SendEvent, ret:%{public}d", ret); asyncCallbackInfo->DecStrongRef(nullptr); } } -- Gitee