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; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

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

HarmonyOS
2024-08-22 23:19:26
1123浏览
收藏 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"); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
分享
微博
QQ
微信
回复
2024-08-23 16:25:44


相关问题
OpenHarmony idl如何实现异步
5762浏览 • 1回复 待解决
NAPI执行上层时,如何获取env
2914浏览 • 1回复 待解决
如何为 C++ 提供函数?
3298浏览 • 1回复 待解决
arkts 关于异步问题
959浏览 • 1回复 待解决
如何在NAPI执行上层时获取env
707浏览 • 1回复 待解决
HarmonyOS 人脸识别问题
710浏览 • 1回复 待解决
HarmonyOS onAreaChange方法问题
836浏览 • 1回复 待解决
HarmonyOS Slider值问题
626浏览 • 1回复 待解决
HarmonyOS NAPI开发相关问题
1012浏览 • 1回复 待解决
HarmonyOS 关于各种异步api的问题
710浏览 • 1回复 待解决
c/c++主动调用ArkTS存在问题
1501浏览 • 1回复 待解决
Flutter - EventChannel问题
913浏览 • 1回复 待解决
HarmonyOS关于异步Promise的使用问题
1248浏览 • 1回复 待解决
NAPI开发问题
652浏览 • 1回复 待解决
HarmonyOS 活体检测问题
901浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。