1.ts接口申明
index.d.ts
export const startDownload: (cbFn: (progress: number) => void) => void;
2. C/C++ 代码
typedef struct CallbackContext {
napi_env env = nullptr;
napi_ref callbackRef = nullptr;
int progress = 0;
} CallbackContext;
// Call back the ts side function to notify the progress information to the ts side.
static void callTS(napi_env env, napi_value jsCb, void *context, void *data) {
CallbackContext *arg = (CallbackContext *)data;
napi_value progress;
//使用CallbackContext中的数据,构造progress
napi_create_int32(arg->env, arg->progress, &progress);
//调用jsCb,参数为progress,1位参数个数
napi_call_function(arg->env, nullptr, jsCb, 1, &progress, nullptr);
}
// Simulate a download task. Because the ts side function is called here, a thread-safe function must be used.
void downloadTask(CallbackContext *context) {
if (context) {
n

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



