1 |
3a515b92
|
cagy
|
## Nan::Callback
|
2 |
|
|
|
3 |
|
|
`Nan::Callback` makes it easier to use `v8::Function` handles as callbacks. A class that wraps a `v8::Function` handle, protecting it from garbage collection and making it particularly useful for storage and use across asynchronous execution.
|
4 |
|
|
|
5 |
|
|
- <a href="#api_nan_callback"><b><code>Nan::Callback</code></b></a>
|
6 |
|
|
|
7 |
|
|
<a name="api_nan_callback"></a>
|
8 |
|
|
### Nan::Callback
|
9 |
|
|
|
10 |
|
|
```c++
|
11 |
|
|
class Callback {
|
12 |
|
|
public:
|
13 |
|
|
Callback();
|
14 |
|
|
|
15 |
|
|
explicit Callback(const v8::Local<v8::Function> &fn);
|
16 |
|
|
|
17 |
|
|
~Callback();
|
18 |
|
|
|
19 |
|
|
bool operator==(const Callback &other) const;
|
20 |
|
|
|
21 |
|
|
bool operator!=(const Callback &other) const;
|
22 |
|
|
|
23 |
|
|
v8::Local<v8::Function> operator*() const;
|
24 |
|
|
|
25 |
|
|
MaybeLocal<v8::Value> operator()(AsyncResource* async_resource,
|
26 |
|
|
v8::Local<v8::Object> target,
|
27 |
|
|
int argc = 0,
|
28 |
|
|
v8::Local<v8::Value> argv[] = 0) const;
|
29 |
|
|
|
30 |
|
|
MaybeLocal<v8::Value> operator()(AsyncResource* async_resource,
|
31 |
|
|
int argc = 0,
|
32 |
|
|
v8::Local<v8::Value> argv[] = 0) const;
|
33 |
|
|
|
34 |
|
|
void SetFunction(const v8::Local<v8::Function> &fn);
|
35 |
|
|
|
36 |
|
|
v8::Local<v8::Function> GetFunction() const;
|
37 |
|
|
|
38 |
|
|
bool IsEmpty() const;
|
39 |
|
|
|
40 |
|
|
void Reset(const v8::Local<v8::Function> &fn);
|
41 |
|
|
|
42 |
|
|
void Reset();
|
43 |
|
|
|
44 |
|
|
MaybeLocal<v8::Value> Call(v8::Local<v8::Object> target,
|
45 |
|
|
int argc,
|
46 |
|
|
v8::Local<v8::Value> argv[],
|
47 |
|
|
AsyncResource* async_resource) const;
|
48 |
|
|
MaybeLocal<v8::Value> Call(int argc,
|
49 |
|
|
v8::Local<v8::Value> argv[],
|
50 |
|
|
AsyncResource* async_resource) const;
|
51 |
|
|
|
52 |
|
|
// Deprecated versions. Use the versions that accept an async_resource instead
|
53 |
|
|
// as they run the callback in the correct async context as specified by the
|
54 |
|
|
// resource. If you want to call a synchronous JS function (i.e. on a
|
55 |
|
|
// non-empty JS stack), you can use Nan::Call instead.
|
56 |
|
|
v8::Local<v8::Value> operator()(v8::Local<v8::Object> target,
|
57 |
|
|
int argc = 0,
|
58 |
|
|
v8::Local<v8::Value> argv[] = 0) const;
|
59 |
|
|
|
60 |
|
|
v8::Local<v8::Value> operator()(int argc = 0,
|
61 |
|
|
v8::Local<v8::Value> argv[] = 0) const;
|
62 |
|
|
v8::Local<v8::Value> Call(v8::Local<v8::Object> target,
|
63 |
|
|
int argc,
|
64 |
|
|
v8::Local<v8::Value> argv[]) const;
|
65 |
|
|
|
66 |
|
|
v8::Local<v8::Value> Call(int argc, v8::Local<v8::Value> argv[]) const;
|
67 |
|
|
};
|
68 |
|
|
```
|
69 |
|
|
|
70 |
|
|
Example usage:
|
71 |
|
|
|
72 |
|
|
```c++
|
73 |
|
|
v8::Local<v8::Function> function;
|
74 |
|
|
Nan::Callback callback(function);
|
75 |
|
|
callback.Call(0, 0);
|
76 |
|
|
```
|