File: threadsafe_function_unref.cc

package info (click to toggle)
node-addon-api 5.0.0-6%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,920 kB
  • sloc: cpp: 7,531; ansic: 5,297; javascript: 4,654; makefile: 7
file content (42 lines) | stat: -rw-r--r-- 949 bytes parent folder | download
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
#include "napi.h"
#include "test_helper.h"

#if (NAPI_VERSION > 3)

using namespace Napi;

namespace {

static Value TestUnref(const CallbackInfo& info) {
  Napi::Env env = info.Env();
  Object global = env.Global();
  Object resource = info[0].As<Object>();
  Function cb = info[1].As<Function>();
  Function setTimeout = MaybeUnwrap(global.Get("setTimeout")).As<Function>();
  ThreadSafeFunction* tsfn = new ThreadSafeFunction;

  *tsfn = ThreadSafeFunction::New(info.Env(), cb, resource, "Test", 1, 1, [tsfn](Napi::Env /* env */) {
    delete tsfn;
  });

  tsfn->BlockingCall();

  setTimeout.Call( global, {
    Function::New(env, [tsfn](const CallbackInfo& info) {
      tsfn->Unref(info.Env());
    }),
    Number::New(env, 100)
  });

  return info.Env().Undefined();
}

}

Object InitThreadSafeFunctionUnref(Env env) {
  Object exports = Object::New(env);
  exports["testUnref"] = Function::New(env, TestUnref);
  return exports;
}

#endif