HarmonyOS 关于NAPI开发C层实现异步化回调问题

目前常规的NAPI调用示例为同步方法调用,例如:

napi_value MyObject::GetValue(napi_env env, napi_callback_info info) 
{ 
  napi_status status; 
 
  napi_value jsthis; 
  ...... 
  status = napi_create_double(env, obj->value_, &num); 
  assert(status == napi_ok); 
 
  return num; 
}

对于一种场景,ts层调用native的方法A,A进入C层进行异步化(线程池调用)后需要回调ts层设置的callback方法这种场景下,本地利用napi的示例代码未找到比较好的实现方案。该场景能否提供一套napi层在子线程回调ts层callback的示例代码

HarmonyOS
2024-08-22 23:19:26
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple
void NAPITakePhotoManger::Js_Takephoto_Code_Callback( 
  napi_env env, napi_ref callbackRef, uint32_t code) 
{ 
    CFWK_HILOGI(CALLBACK, "enter"); 
    if (callbackRef == nullptr) { 
        CFWK_HILOGE(CALLBACK, "callbackRef IS NULL"); 
        return; 
    } 
    CameraAbilityEventContext* asyncContext = new (std::nothrow) CameraAbilityEventContext(); 
    uv_work_t* work = new (std::nothrow) uv_work_t; 
    uv_loop_s* loop = nullptr; 
    napi_get_uv_event_loop(env, &loop); 
    if (asyncContext == nullptr || work == nullptr || loop == nullptr) { 
        delete asyncContext; 
        delete work; 
        CFWK_HILOGE(CALLBACK, "asyncContext is null or work is null"); 
        return; 
    } 
    asyncContext->env = env; 
    asyncContext->jsCallback = callbackRef; 
    asyncContext->code = code; 
    work->data = (void *)asyncContext; 
 
    int32_t res = uv_queue_work(loop, work, [](uv_work_t* work) {}, [](uv_work_t* work, int status) { 
        CameraAbilityEventContext* context = (CameraAbilityEventContext*)work->data; 
        napi_handle_scope scope = nullptr; 
        napi_open_handle_scope(context->env, &scope); 
        if (scope == nullptr) { 
            delete context; 
            delete work; 
            return; 
        } 
        napi_value callBack = nullptr; 
        napi_get_reference_value(context->env, context->jsCallback, &callBack); 
        if (callBack == nullptr) { 
            CFWK_HILOGE(CALLBACK, "callBack IS NULL"); 
            delete context; 
            delete work; 
            return; 
        } 
        napi_value resultObj = nullptr; 
        napi_create_object(context->env, &resultObj); 
        napi_value code; 
        napi_create_uint32(context->env, context->code, &code); 
        CFWK_HILOGI(CALLBACK, "code =%{public}d", context->code); 
        napi_create_string_utf8(context->env, context->event.c_str(), NAPI_AUTO_LENGTH, &category_); 
        napi_set_named_property(context->env, resultObj, "code", code); 
        napi_set_named_property(context->env, resultObj, "name", "张三"); 
        napi_value ret; 
void NAPITakePhotoManger::Js_Code_Callback( 
    napi_env env, napi_ref callbackRef, uint32_t code) 
{ 
    CFWK_HILOGI(CALLBACK, "enter"); 
    if (callbackRef == nullptr) { 
        CFWK_HILOGE(CALLBACK, "callbackRef IS NULL"); 
        return; 
    } 
    CameraAbilityEventContext* asyncContext = new (std::nothrow) CameraAbilityEventContext(); 
    uv_work_t* work = new (std::nothrow) uv_work_t; 
    uv_loop_s* loop = nullptr; 
    napi_get_uv_event_loop(env, &loop); 
    if (asyncContext == nullptr || work == nullptr || loop == nullptr) { 
        delete asyncContext; 
        delete work; 
        CFWK_HILOGE(CALLBACK, "asyncContext is null or work is null"); 
        return; 
    } 
    asyncContext->env = env; 
    asyncContext->jsCallback = callbackRef; 
    asyncContext->code = code; 
    work->data = (void *)asyncContext; 
 
    int32_t res = uv_queue_work(loop, work, [](uv_work_t* work) {}, [](uv_work_t* work, int status) { 
        CameraAbilityEventContext* context = (CameraAbilityEventContext*)work->data; 
        napi_handle_scope scope = nullptr; 
        napi_open_handle_scope(context->env, &scope); 
        if (scope == nullptr) { 
            delete context; 
            delete work; 
            return; 
        } 
        napi_value callBack = nullptr; 
        napi_get_reference_value(context->env, context->jsCallback, &callBack); 
        if (callBack == nullptr) { 
            CFWK_HILOGE(CALLBACK, "callBack IS NULL"); 
            delete context; 
            delete work; 
            return; 
        } 
        napi_value resultObj = nullptr; 
        napi_create_object(context->env, &resultObj); 
        napi_value code; 
        napi_create_uint32(context->env, context->code, &code); 
        CFWK_HILOGI(CALLBACK, "code =%{public}d", context->code); 
        napi_create_string_utf8(context->env, context->event.c_str(), NAPI_AUTO_LENGTH, &category_); 
        napi_set_named_property(context->env, resultObj, "code", code); 
        napi_set_named_property(context->env, resultObj, "name", "张三"); 
        napi_value ret; 
napi_call_function(context->env, nullptr, callBack, 1, &resultObj, &ret); 
        if (context->code == 0) { 
            napi_delete_reference(context->env, context->jsCallback); 
        } 
        napi_close_handle_scope(context->env, scope); 
        delete context; 
        delete work; 
    }); 
    if (res != RES_SUCCESS) { 
        CFWK_HILOGE(CALLBACK, "add uv queue fail"); 
        delete (CameraAbilityEventContext*)work->data; 
        delete work; 
    } 
    CFWK_HILOGI(CALLBACK, "leave"); 
}
分享
微博
QQ
微信
回复
2024-08-23 16:25:44
相关问题
OpenHarmony idl如何实现异步
4486浏览 • 1回复 待解决
如何为 C++ 提供函数?
2282浏览 • 1回复 待解决
HarmonyOS NAPI开发相关问题
259浏览 • 1回复 待解决
HarmonyOS关于异步Promise的使用问题
293浏览 • 1回复 待解决
NAPI执行上层时,如何获取env
2095浏览 • 1回复 待解决
arkts 关于异步问题
50浏览 • 1回复 待解决
如何在NAPI执行上层时获取env
211浏览 • 1回复 待解决
HarmonyOS C++自定义组件如何开发
192浏览 • 1回复 待解决
HarmonyOS ArkTS接口的案例问题
85浏览 • 1回复 待解决
HarmonyOS onScrollyoffset不准确问题
272浏览 • 1回复 待解决
c/c++主动调用ArkTS存在问题
230浏览 • 1回复 待解决
HarmonyOS C++异步操作
205浏览 • 1回复 待解决
ts开发如何实现异步编程
1032浏览 • 1回复 待解决
如何实现拍照预览onPreviewFrame
491浏览 • 1回复 待解决