• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{
2  "Napi async thread": {
3    "prefix": "napiasyncthreadsafefunc",
4    "body": [
5      "struct MyContext {",
6      "    std::string importantString;",
7      "};",
8      "// Thread-safe JavaScript function object.",
9      "napi_threadsafe_function g_threadsafeFunction;",
10      "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)",
11      "{",
12      "    size_t argc = 1;",
13      "    napi_value argv[1];",
14      "    MyContext* myContext = static_cast<MyContext*>(context);",
15      "    napi_create_string_utf8(env, myContext->importantString.c_str(), NAPI_AUTO_LENGTH, &argv[0]);",
16      "    // Execute the callback function in a JavaScript environment. After the data is callbacked to JS, it will not wait for JS to finish executing.",
17      "    napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);",
18      "}",
19      "static void FinalizeFunction(napi_env env, void* data, void* hint)",
20      "{",
21      "    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"FinalizeFunction called.\");",
22      "    MyContext* myContext = static_cast<MyContext*>(data);",
23      "    delete myContext;",
24      "}",
25      "static void ThreadFunction(void *data)",
26      "{",
27      "    // Asynchronously call a JavaScript function in another thread.",
28      "    std::this_thread::sleep_for(std::chrono::seconds(1));",
29      "    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Another thread sleep 1s.\");",
30      "    // Invoke a JavaScript function asynchronously from a native thread. After this, it will execute CallbackFunction to callback data to js.",
31      "    napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);",
32      "    // Decrement the reference count of a threadsafe function, potentially destroying it. After this, it will execute FinalizeFunction to release data.",
33      "    napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);",
34      "}",
35      "napi_value StartThreadsafefunc(napi_env env, napi_callback_info info)",
36      "{",
37      "    size_t argc = 1;",
38      "    napi_value args[1];",
39      "    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);",
40      "    MyContext* context = new MyContext{\"Hello from C++\"};",
41      "    napi_value name;",
42      "    napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);",
43      "    // Create a thread-safe function that can be called from worker threads.",
44      "    napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, context, FinalizeFunction, context, CallbackFunction, &g_threadsafeFunction);",
45      "    // Start a new thread.",
46      "    std::thread newThread(ThreadFunction, nullptr);",
47      "    newThread.join();",
48      "    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Main thread.\");",
49      "    return nullptr;",
50      "}"
51    ]
52  }
53}