.cpp代码
struct CallbackContext {
napi_env env = nullptr;
napi_ref recvCallbackRef = nullptr;
napi_async_work work;
double value0 = 0;
double value1 = 0;
double sum = 0;
};
void callbackDone(napi_env env, napi_status status, void *data)
{
// OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "callbackDone is called");
CallbackContext* context = (CallbackContext*)data;
napi_value recvCallback= nullptr;
napi_get_reference_value(context->env, context->recvCallbackRef, &recvCallback);
napi_value argv[1];
//argv[0] = thisArg;
napi_value ret;
napi_create_double(env, context->sum, &argv[0]);
napi_call_function(env, nullptr, recvCallback, 1, argv, &ret);
napi_delete_reference(context->env, context->recvCallbackRef);
napi_delete_async_work(context->env, context->work);
delete context;
}
void callbackPreDo(napi_env env, void *data)
{
// OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "callbackPreDo is called");
CallbackContext* context = (CallbackContext*)data;
context->sum = context->value0 + context->value1;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
void callbackExec(CallbackContext* context)
{
// OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "callbackExec is called");
napi_value resource;
napi_create_string_latin1(context->env, "mqttAPI", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(context->env, nullptr, resource, callbackPreDo, callbackDone, context, &context->work);
napi_queue_async_work(context->env, context->work);//实现在UI主线程调用
}
static napi_value AsyncWork(napi_env env, napi_callback_info info)
{
// OH_LOG_Print(LOG_APP, LOG_INFO, 0, TAG, "AsyncWork is called");
size_t requireArgc = 3;
size_t argc = 3;
napi_value args[3] = {nullptr};
napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
napi_ref recvCallbackRef;
napi_create_reference(env, args[2], 1, &recvCallbackRef);
CallbackContext *context = new CallbackContext;
context->env = env;
context->recvCallbackRef = recvCallbackRef;
context->value0 = value0;
context->value1 = value1;
callbackExec(context);
return nullptr;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "asyncWork", nullptr, AsyncWork, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version =1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
}
index.d.ts
export const asyncWork: (a: number, b: number, c: (number) => void) => number;
index.ets
import hilog from '@ohos.hilog';
import entry from 'libentry.so'
@Entry
@Component
struct Index {
@State message: string = 'Async Test'
build() {
Row() {
Column() {
Button("Async")
.onClick(() => {
entry.asyncWork(2, 3, (value) => {
hilog.info(0x0000, 'testTag', 'Async Test 2 + 3 = %{public}d', value);
})
})
}
.width('100%')
}
.height('100%')
}
}
事实证明,鸿蒙开发确实是一块香饽饽。作为华为自家的操作系统,正在逐步扩张市场份额。想要转行或者入行的朋友可以下手了。在这里,为大家提供一份我整理的鸿蒙开发学习资料,涵盖了UI开发、web、应用模型多个知识点,有需要的朋友可以扫描下方二维码,免费获取更多相关资料。
一、鸿蒙进阶开发学习之UI开发
二、鸿蒙开发进阶学习之web
三、鸿蒙开发进阶学习之应用模型
四、鸿蒙零基础入门学习指南