|
1 | 1 | ## Miscellaneous Node Helpers |
2 | 2 |
|
3 | | - - <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a> |
4 | 3 | - <a href="#api_nan_async_init"><b><code>Nan::AsyncInit()</code></b></a> |
5 | 4 | - <a href="#api_nan_async_destory"><b><code>Nan::AsyncDestory()</code></b></a> |
| 5 | + - <a href="#api_nan_asyncresource"><b><code>Nan::AsyncResource</code></b></a> |
| 6 | + - <a href="#api_nan_make_callback"><b><code>Nan::MakeCallback()</code></b></a> |
6 | 7 | - <a href="#api_nan_module_init"><b><code>NAN_MODULE_INIT()</code></b></a> |
7 | 8 | - <a href="#api_nan_export"><b><code>Nan::Export()</code></b></a> |
8 | 9 |
|
9 | 10 |
|
10 | | -<a name="api_async_init"></a> |
| 11 | +<a name="api_nan_async_init"></a> |
11 | 12 | ### Nan::AsyncInit() |
12 | 13 |
|
13 | 14 | When calling back into JavaScript asynchornously, special care must be taken to ensure that the runtime can properly track |
@@ -35,36 +36,85 @@ Nan::async_context AsyncInit(v8::MaybeLocal<v8::Object> maybe_resource, |
35 | 36 | For more details, see the Node [async_hooks][] documentation. You might also want to take a look at the documentation for the |
36 | 37 | [N-API counterpart][napi]. For example usage, see the `makecallbackcontext.cpp` example in the `test/cpp` directory. |
37 | 38 |
|
38 | | -<a name="api_async_destory"></a> |
| 39 | +Note: It might be more convenient to use `Nan::AsyncResource` instead of using this directly. |
| 40 | +
|
| 41 | +<a name="api_nan_async_destory"></a> |
39 | 42 | ### Nan::AsyncDestroy() |
40 | 43 |
|
41 | 44 | Wrapper around `node::EmitAsyncDestroy`. |
42 | 45 |
|
| 46 | +Note: It might be more convenient to use `Nan::AsyncResource` instead of using this directly. |
| 47 | +
|
| 48 | +<a name="api_nan_asyncresource"></a> |
| 49 | +### Nan::AsyncResource |
| 50 | +
|
| 51 | +`Nan::AsyncResouce` is a convenience class that provides RAII wrapper around `Nan::AsyncInit`, `Nan::AsyncDestroy` and `Nan::MakeCallback`. It is analogous to the `AsyncResource` JavaScript class exposed by Node's [async_hooks][] API. |
| 52 | +
|
| 53 | +Definition: |
| 54 | +
|
| 55 | +```c++ |
| 56 | +class AsyncResource { |
| 57 | + public: |
| 58 | + AsyncResource(MaybeLocal<v8::Object> maybe_resource, v8::Local<v8::String> name); |
| 59 | + AsyncResource(MaybeLocal<v8::Object> maybe_resource, const char* name); |
| 60 | + ~AsyncResource(); |
| 61 | +
|
| 62 | + v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target, |
| 63 | + v8::Local<v8::Function> func, |
| 64 | + int argc, |
| 65 | + v8::Local<v8::Value>* argv, |
| 66 | + Nan::async_context async_context); |
| 67 | + v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target, |
| 68 | + v8::Local<v8::String> symbol, |
| 69 | + int argc, |
| 70 | + v8::Local<v8::Value>* argv, |
| 71 | + Nan::async_context async_context); |
| 72 | + v8::MaybeLocal<v8::Value> runInAsyncScope(v8::Local<v8::Object> target, |
| 73 | + const char* method, |
| 74 | + int argc, |
| 75 | + v8::Local<v8::Value>* argv, |
| 76 | + Nan::async_context async_context); |
| 77 | +}; |
| 78 | +``` |
| 79 | +* `maybe_resource`: An optional object associated with the async work that will be passed to the possible [async_hooks][] |
| 80 | + `init` hook. |
| 81 | +* `name`: Identified for the kind of resource that is being provided for diagnostics information exposed by the [async_hooks][] |
| 82 | + API. This will be passed to the possible `init` hook as the `type`. To avoid name collisions with other modules we recommend |
| 83 | + that the name include the name of the owning module as a prefix. For example `mysql` module could use something like |
| 84 | + `mysql:batch-db-query-resource`. |
| 85 | +* When calling JS on behalf of this resource, one can use `runInAsyncScope`. This will ensure that the callback runs in the |
| 86 | + correct async execution context. |
| 87 | +* `AsyncDestroy` is automatically called when an AsyncResource object is destroyed. |
| 88 | + |
| 89 | +For example usage, see the `asyncresource.cpp` example in the `test/cpp` directory. |
| 90 | + |
43 | 91 | <a name="api_nan_make_callback"></a> |
44 | 92 | ### Nan::MakeCallback() |
45 | 93 |
|
| 94 | +Note: It might be more convenient to use `Nan::AsyncResource` instead of using this directly. |
| 95 | + |
46 | 96 | Wrappers around `node::MakeCallback()` providing a consistent API across all supported versions of Node. |
47 | 97 |
|
48 | 98 | Use `MakeCallback()` rather than using `v8::Function#Call()` directly in order to properly process internal Node functionality including domains, async hooks, the microtask queue, and other debugging functionality. |
49 | 99 |
|
50 | 100 | Signatures: |
51 | 101 |
|
52 | 102 | ```c++ |
53 | | -v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
54 | | - v8::Local<v8::Function> func, |
55 | | - int argc, |
56 | | - v8::Local<v8::Value>* argv, |
57 | | - Nan::async_context async_context); |
58 | | -v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
59 | | - v8::Local<v8::String> symbol, |
60 | | - int argc, |
61 | | - v8::Local<v8::Value>* argv, |
62 | | - Nan::async_context async_context); |
63 | | -v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
64 | | - const char* method, |
65 | | - int argc, |
66 | | - v8::Local<v8::Value>* argv, |
67 | | - Nan::async_context async_context); |
| 103 | +v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
| 104 | + v8::Local<v8::Function> func, |
| 105 | + int argc, |
| 106 | + v8::Local<v8::Value>* argv, |
| 107 | + Nan::async_context async_context); |
| 108 | +v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
| 109 | + v8::Local<v8::String> symbol, |
| 110 | + int argc, |
| 111 | + v8::Local<v8::Value>* argv, |
| 112 | + Nan::async_context async_context); |
| 113 | +v8::MaybeLocal<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
| 114 | + const char* method, |
| 115 | + int argc, |
| 116 | + v8::Local<v8::Value>* argv, |
| 117 | + Nan::async_context async_context); |
68 | 118 |
|
69 | 119 | // Legacy versions. We recommend the async context preserving versions above. |
70 | 120 | v8::Local<v8::Value> Nan::MakeCallback(v8::Local<v8::Object> target, |
|
0 commit comments