node-addon-api 1.6.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,344 @@
1
+ # AsyncProgressWorker
2
+
3
+ `Napi::AsyncProgressWorker` is an abstract class which implements `Napi::AsyncWorker`
4
+ while extending `Napi::AsyncWorker` internally with `Napi::ThreadSafeFunction` for
5
+ moving work progress reports from worker thread(s) to event loop threads.
6
+
7
+ Like `Napi::AsyncWorker`, once created, execution is requested by calling
8
+ `Napi::AsyncProgressWorker::Queue`. When a thread is available for execution
9
+ the `Napi::AsyncProgressWorker::Execute` method will be invoked. During the
10
+ execution, `Napi::AsyncProgressWorker::ExecutionProgress::Send` can be used to
11
+ indicate execution process, which will eventually invoke `Napi::AsyncProgressWorker::OnProgress`
12
+ on the JavaScript thread to safely call into JavaScript. Once `Napi::AsyncProgressWorker::Execute`
13
+ completes either `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
14
+ will be invoked. Once the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
15
+ methods are complete the `Napi::AsyncProgressWorker` instance is destructed.
16
+
17
+ For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and
18
+ `Napi::AsyncProgressWorker::OnProgress` method must be implemented in a subclass.
19
+
20
+ ## Methods
21
+
22
+ [`Napi::AsyncWorker`][] provides detailed descriptions for most methods.
23
+
24
+ ### Execute
25
+
26
+ This method is used to execute some tasks outside of the **event loop** on a libuv
27
+ worker thread. Subclasses must implement this method and the method is run on
28
+ a thread other than that running the main event loop. As the method is not
29
+ running on the main event loop, it must avoid calling any methods from node-addon-api
30
+ or running any code that might invoke JavaScript. Instead, once this method is
31
+ complete any interaction through node-addon-api with JavaScript should be implemented
32
+ in the `Napi::AsyncProgressWorker::OnOK` method and/or `Napi::AsyncProgressWorker::OnError`
33
+ which run on the main thread and are invoked when the `Napi::AsyncProgressWorker::Execute`
34
+ method completes.
35
+
36
+ ```cpp
37
+ virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progress) = 0;
38
+ ```
39
+
40
+ ### OnOK
41
+
42
+ This method is invoked when the computation in the `Execute` method ends.
43
+ The default implementation runs the `Callback` optionally provided when the
44
+ `AsyncProgressWorker` class was created. The `Callback` will by default receive no
45
+ arguments. Arguments to the callback can be provided by overriding the `GetResult()`
46
+ method.
47
+
48
+ ```cpp
49
+ virtual void Napi::AsyncProgressWorker::OnOK();
50
+ ```
51
+
52
+ ### OnProgress
53
+
54
+ This method is invoked when the computation in the `Napi::AsyncProgressWorker::ExecutionProcess::Send`
55
+ method was called during worker thread execution.
56
+
57
+ ```cpp
58
+ virtual void Napi::AsyncProgressWorker::OnProgress(const T* data, size_t count)
59
+ ```
60
+
61
+ ### Constructor
62
+
63
+ Creates a new `Napi::AsyncProgressWorker`.
64
+
65
+ ```cpp
66
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback);
67
+ ```
68
+
69
+ - `[in] callback`: The function which will be called when an asynchronous
70
+ operations ends. The given function is called from the main event loop thread.
71
+
72
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
73
+ calling `Napi::AsyncWork::Queue`.
74
+
75
+ ### Constructor
76
+
77
+ Creates a new `Napi::AsyncProgressWorker`.
78
+
79
+ ```cpp
80
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name);
81
+ ```
82
+
83
+ - `[in] callback`: The function which will be called when an asynchronous
84
+ operations ends. The given function is called from the main event loop thread.
85
+ - `[in] resource_name`: Null-terminated string that represents the
86
+ identifier for the kind of resource that is being provided for diagnostic
87
+ information exposed by the async_hooks API.
88
+
89
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
90
+ calling `Napi::AsyncWork::Queue`.
91
+
92
+ ### Constructor
93
+
94
+ Creates a new `Napi::AsyncProgressWorker`.
95
+
96
+ ```cpp
97
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name, const Napi::Object& resource);
98
+ ```
99
+
100
+ - `[in] callback`: The function which will be called when an asynchronous
101
+ operations ends. The given function is called from the main event loop thread.
102
+ - `[in] resource_name`: Null-terminated string that represents the
103
+ identifier for the kind of resource that is being provided for diagnostic
104
+ information exposed by the async_hooks API.
105
+ - `[in] resource`: Object associated with the asynchronous operation that
106
+ will be passed to possible async_hooks.
107
+
108
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
109
+ calling `Napi::AsyncWork::Queue`.
110
+
111
+ ### Constructor
112
+
113
+ Creates a new `Napi::AsyncProgressWorker`.
114
+
115
+ ```cpp
116
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback);
117
+ ```
118
+
119
+ - `[in] receiver`: The `this` object passed to the called function.
120
+ - `[in] callback`: The function which will be called when an asynchronous
121
+ operations ends. The given function is called from the main event loop thread.
122
+
123
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
124
+ calling `Napi::AsyncWork::Queue`.
125
+
126
+ ### Constructor
127
+
128
+ Creates a new `Napi::AsyncProgressWorker`.
129
+
130
+ ```cpp
131
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name);
132
+ ```
133
+
134
+ - `[in] receiver`: The `this` object passed to the called function.
135
+ - `[in] callback`: The function which will be called when an asynchronous
136
+ operations ends. The given function is called from the main event loop thread.
137
+ - `[in] resource_name`: Null-terminated string that represents the
138
+ identifier for the kind of resource that is being provided for diagnostic
139
+ information exposed by the async_hooks API.
140
+
141
+ Returns a `Napi::AsyncWork` instance which can later be queued for execution by
142
+ calling `Napi::AsyncWork::Queue`.
143
+
144
+ ### Constructor
145
+
146
+ Creates a new `Napi::AsyncProgressWorker`.
147
+
148
+ ```cpp
149
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource);
150
+ ```
151
+
152
+ - `[in] receiver`: The `this` object to be passed to the called function.
153
+ - `[in] callback`: The function which will be called when an asynchronous
154
+ operations ends. The given function is called from the main event loop thread.
155
+ - `[in] resource_name`: Null-terminated string that represents the
156
+ identifier for the kind of resource that is being provided for diagnostic
157
+ information exposed by the async_hooks API.
158
+ - `[in] resource`: Object associated with the asynchronous operation that
159
+ will be passed to possible async_hooks.
160
+
161
+ Returns a `Napi::AsyncWork` instance which can later be queued for execution by
162
+ calling `Napi::AsyncWork::Queue`.
163
+
164
+ ### Constructor
165
+
166
+ Creates a new `Napi::AsyncProgressWorker`.
167
+
168
+ ```cpp
169
+ explicit Napi::AsyncProgressWorker(Napi::Env env);
170
+ ```
171
+
172
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
173
+
174
+ Returns an `Napi::AsyncProgressWorker` instance which can later be queued for execution by calling
175
+ `Napi::AsyncProgressWorker::Queue`.
176
+
177
+ Available with `NAPI_VERSION` equal to or greater than 5.
178
+
179
+ ### Constructor
180
+
181
+ Creates a new `Napi::AsyncProgressWorker`.
182
+
183
+ ```cpp
184
+ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name);
185
+ ```
186
+
187
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
188
+ - `[in] resource_name`: Null-terminated string that represents the
189
+ identifier for the kind of resource that is being provided for diagnostic
190
+ information exposed by the async_hooks API.
191
+
192
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
193
+ calling `Napi::AsyncProgressWorker::Queue`.
194
+
195
+ Available with `NAPI_VERSION` equal to or greater than 5.
196
+
197
+ ### Constructor
198
+
199
+ Creates a new `Napi::AsyncProgressWorker`.
200
+
201
+ ```cpp
202
+ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, const Napi::Object& resource);
203
+ ```
204
+
205
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
206
+ - `[in] resource_name`: Null-terminated string that represents the
207
+ identifier for the kind of resource that is being provided for diagnostic
208
+ information exposed by the async_hooks API.
209
+ - `[in] resource`: Object associated with the asynchronous operation that
210
+ will be passed to possible async_hooks.
211
+
212
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
213
+ calling `Napi::AsyncProgressWorker::Queue`.
214
+
215
+ Available with `NAPI_VERSION` equal to or greater than 5.
216
+
217
+ ### Destructor
218
+
219
+ Deletes the created work object that is used to execute logic asynchronously and
220
+ release the internal `Napi::ThreadSafeFunction`, which will be aborted to prevent
221
+ unexpected upcoming thread safe calls.
222
+
223
+ ```cpp
224
+ virtual Napi::AsyncProgressWorker::~AsyncProgressWorker();
225
+ ```
226
+
227
+ # AsyncProgressWorker::ExecutionProcess
228
+
229
+ A bridge class created before the worker thread execution of `Napi::AsyncProgressWorker::Execute`.
230
+
231
+ ## Methods
232
+
233
+ ### Send
234
+
235
+ `Napi::AsyncProgressWorker::ExecutionProcess::Send` takes two arguments, a pointer
236
+ to a generic type of data, and a `size_t` to indicate how many items the pointer is
237
+ pointing to.
238
+
239
+ The data pointed to will be copied to internal slots of `Napi::AsyncProgressWorker` so
240
+ after the call to `Napi::AsyncProgressWorker::ExecutionProcess::Send` the data can
241
+ be safely released.
242
+
243
+ Note that `Napi::AsyncProgressWorker::ExecutionProcess::Send` merely guarantees
244
+ **eventual** invocation of `Napi::AsyncProgressWorker::OnProgress`, which means
245
+ multiple send might be coalesced into single invocation of `Napi::AsyncProgressWorker::OnProgress`
246
+ with latest data.
247
+
248
+ ```cpp
249
+ void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t count) const;
250
+ ```
251
+
252
+ ## Example
253
+
254
+ The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
255
+ inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method.
256
+ Typically input to the worker will be saved within the class' fields generally
257
+ passed in through its constructor.
258
+
259
+ During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute`
260
+ can be used to report the progress of the execution.
261
+
262
+ When the `Napi::AsyncProgressWorker::Execute` method completes without errors the
263
+ `Napi::AsyncProgressWorker::OnOK` function callback will be invoked. In this function the
264
+ results of the computation will be reassembled and returned back to the initial
265
+ JavaScript context.
266
+
267
+ `Napi::AsyncProgressWorker` ensures that all the code in the `Napi::AsyncProgressWorker::Execute`
268
+ function runs in the background out of the **event loop** thread and at the end
269
+ the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
270
+ called and are executed as part of the event loop.
271
+
272
+ The code below shows a basic example of the `Napi::AsyncProgressWorker` implementation:
273
+
274
+ ```cpp
275
+ #include<napi.h>
276
+
277
+ #include <chrono>
278
+ #include <thread>
279
+
280
+ use namespace Napi;
281
+
282
+ class EchoWorker : public AsyncProgressWorker<uint32_t> {
283
+ public:
284
+ EchoWorker(Function& callback, std::string& echo)
285
+ : AsyncProgressWorker(callback), echo(echo) {}
286
+
287
+ ~EchoWorker() {}
288
+ // This code will be executed on the worker thread
289
+ void Execute(const ExecutionProgress& progress) {
290
+ // Need to simulate cpu heavy task
291
+ for (uint32_t i = 0; i < 100; ++i) {
292
+ progress.Send(&i, 1)
293
+ std::this_thread::sleep_for(std::chrono::seconds(1));
294
+ }
295
+ }
296
+
297
+ void OnOK() {
298
+ HandleScope scope(Env());
299
+ Callback().Call({Env().Null(), String::New(Env(), echo)});
300
+ }
301
+
302
+ void OnProgress(const uint32_t* data, size_t /* count */) {
303
+ HandleScope scope(Env());
304
+ Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
305
+ }
306
+
307
+ private:
308
+ std::string echo;
309
+ };
310
+ ```
311
+
312
+ The `EchoWorker`'s constructor calls the base class' constructor to pass in the
313
+ callback that the `Napi::AsyncProgressWorker` base class will store persistently. When
314
+ the work on the `Napi::AsyncProgressWorker::Execute` method is done the
315
+ `Napi::AsyncProgressWorker::OnOk` method is called and the results are return back to
316
+ JavaScript when the stored callback is invoked with its associated environment.
317
+
318
+ The following code shows an example of how to create and use an `Napi::AsyncProgressWorker`
319
+
320
+ ```cpp
321
+ #include <napi.h>
322
+
323
+ // Include EchoWorker class
324
+ // ..
325
+
326
+ use namespace Napi;
327
+
328
+ Value Echo(const CallbackInfo& info) {
329
+ // We need to validate the arguments here
330
+ Function cb = info[1].As<Function>();
331
+ std::string in = info[0].As<String>();
332
+ EchoWorker* wk = new EchoWorker(cb, in);
333
+ wk->Queue();
334
+ return info.Env().Undefined();
335
+ }
336
+ ```
337
+
338
+ The implementation of a `Napi::AsyncProgressWorker` can be used by creating a
339
+ new instance and passing to its constructor the callback to execute when the
340
+ asynchronous task ends and other data needed for the computation. Once created,
341
+ the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
342
+ method that will queue the created worker for execution.
343
+
344
+ [`Napi::AsyncWorker`]: ./async_worker.md
@@ -7,8 +7,8 @@ operation.
7
7
 
8
8
  Once created, execution is requested by calling `Napi::AsyncWorker::Queue`. When
9
9
  a thread is available for execution the `Napi::AsyncWorker::Execute` method will
10
- be invoked. Once `Napi::AsyncWorker::Execute` completes either
11
- `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` will be invoked. Once
10
+ be invoked. Once `Napi::AsyncWorker::Execute` completes either
11
+ `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` will be invoked. Once
12
12
  the `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` methods are
13
13
  complete the `Napi::AsyncWorker` instance is destructed.
14
14
 
@@ -38,7 +38,7 @@ void Napi::AsyncWorker::Queue();
38
38
  ### Cancel
39
39
 
40
40
  Cancels queued work if it has not yet been started. If it has already started
41
- executing, it cannot be cancelled. If cancelled successfully neither
41
+ executing, it cannot be cancelled. If cancelled successfully neither
42
42
  `OnOK` nor `OnError` will be called.
43
43
 
44
44
  ```cpp
@@ -79,7 +79,7 @@ the `Napi::AsyncWorker::OnOK` callback.
79
79
 
80
80
  Sets the error message for the error that happened during the execution. Setting
81
81
  an error message will cause the `Napi::AsyncWorker::OnError` method to be
82
- invoked instead of `Napi::AsyncWorker::OnOKOnOK` once the
82
+ invoked instead of `Napi::AsyncWorker::OnOK` once the
83
83
  `Napi::AsyncWorker::Execute` method completes.
84
84
 
85
85
  ```cpp
@@ -90,14 +90,14 @@ void Napi::AsyncWorker::SetError(const std::string& error);
90
90
 
91
91
  ### Execute
92
92
 
93
- This method is used to execute some tasks out of the **event loop** on a libuv
93
+ This method is used to execute some tasks outside of the **event loop** on a libuv
94
94
  worker thread. Subclasses must implement this method and the method is run on
95
- a thread other than that running the main event loop. As the method is not
95
+ a thread other than that running the main event loop. As the method is not
96
96
  running on the main event loop, it must avoid calling any methods from node-addon-api
97
97
  or running any code that might invoke JavaScript. Instead, once this method is
98
98
  complete any interaction through node-addon-api with JavaScript should be implemented
99
- in the `Napi::AsyncWorker::OnOK` method which runs on the main thread and is
100
- invoked when the `Napi::AsyncWorker::Execute` method completes.
99
+ in the `Napi::AsyncWorker::OnOK` method and `Napi::AsyncWorker::OnError` which run
100
+ on the main thread and are invoked when the `Napi::AsyncWorker::Execute` method completes.
101
101
 
102
102
  ```cpp
103
103
  virtual void Napi::AsyncWorker::Execute() = 0;
@@ -105,26 +105,50 @@ virtual void Napi::AsyncWorker::Execute() = 0;
105
105
 
106
106
  ### OnOK
107
107
 
108
- This method is invoked when the computation in the `Excecute` method ends.
109
- The default implementation runs the Callback provided when the AsyncWorker class
110
- was created.
108
+ This method is invoked when the computation in the `Execute` method ends.
109
+ The default implementation runs the `Callback` optionally provided when the
110
+ `AsyncWorker` class was created. The `Callback` will by default receive no
111
+ arguments. The arguments to the `Callback` can be provided by overriding the
112
+ `GetResult()` method.
111
113
 
112
114
  ```cpp
113
115
  virtual void Napi::AsyncWorker::OnOK();
114
116
  ```
117
+ ### GetResult
118
+
119
+ This method returns the arguments passed to the `Callback` invoked by the default
120
+ `OnOK()` implementation. The default implementation returns an empty vector,
121
+ providing no arguments to the `Callback`.
122
+
123
+ ```cpp
124
+ virtual std::vector<napi_value> Napi::AsyncWorker::GetResult(Napi::Env env);
125
+ ```
115
126
 
116
127
  ### OnError
117
128
 
118
- This method is invoked afer `Napi::AsyncWorker::Execute` completes if an error
129
+ This method is invoked after `Napi::AsyncWorker::Execute` completes if an error
119
130
  occurs while `Napi::AsyncWorker::Execute` is running and C++ exceptions are
120
131
  enabled or if an error was set through a call to `Napi::AsyncWorker::SetError`.
121
- The default implementation calls the callback provided when the `Napi::AsyncWorker`
132
+ The default implementation calls the `Callback` provided when the `Napi::AsyncWorker`
122
133
  class was created, passing in the error as the first parameter.
123
134
 
124
135
  ```cpp
125
136
  virtual void Napi::AsyncWorker::OnError(const Napi::Error& e);
126
137
  ```
127
138
 
139
+ ### Destroy
140
+
141
+ This method is invoked when the instance must be deallocated. If
142
+ `SuppressDestruct()` was not called then this method will be called after either
143
+ `OnError()` or `OnOK()` complete. The default implementation of this method
144
+ causes the instance to delete itself using the `delete` operator. The method is
145
+ provided so as to ensure that instances allocated by means other than the `new`
146
+ operator can be deallocated upon work completion.
147
+
148
+ ```cpp
149
+ virtual void Napi::AsyncWorker::Destroy();
150
+ ```
151
+
128
152
  ### Constructor
129
153
 
130
154
  Creates a new `Napi::AsyncWorker`.
@@ -136,7 +160,7 @@ explicit Napi::AsyncWorker(const Napi::Function& callback);
136
160
  - `[in] callback`: The function which will be called when an asynchronous
137
161
  operations ends. The given function is called from the main event loop thread.
138
162
 
139
- Returns a`Napi::AsyncWork` instance which can later be queued for execution by calling
163
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by calling
140
164
  `Queue`.
141
165
 
142
166
  ### Constructor
@@ -149,11 +173,11 @@ explicit Napi::AsyncWorker(const Napi::Function& callback, const char* resource_
149
173
 
150
174
  - `[in] callback`: The function which will be called when an asynchronous
151
175
  operations ends. The given function is called from the main event loop thread.
152
- - `[in] resource_name`: Null-terminated strings that represents the
176
+ - `[in] resource_name`: Null-terminated string that represents the
153
177
  identifier for the kind of resource that is being provided for diagnostic
154
178
  information exposed by the async_hooks API.
155
179
 
156
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
180
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
157
181
  calling `Napi::AsyncWork::Queue`.
158
182
 
159
183
  ### Constructor
@@ -166,13 +190,13 @@ explicit Napi::AsyncWorker(const Napi::Function& callback, const char* resource_
166
190
 
167
191
  - `[in] callback`: The function which will be called when an asynchronous
168
192
  operations ends. The given function is called from the main event loop thread.
169
- - `[in] resource_name`: Null-terminated strings that represents the
193
+ - `[in] resource_name`: Null-terminated string that represents the
170
194
  identifier for the kind of resource that is being provided for diagnostic
171
195
  information exposed by the async_hooks API.
172
196
  - `[in] resource`: Object associated with the asynchronous operation that
173
197
  will be passed to possible async_hooks.
174
198
 
175
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
199
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
176
200
  calling `Napi::AsyncWork::Queue`.
177
201
 
178
202
  ### Constructor
@@ -187,7 +211,7 @@ explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& c
187
211
  - `[in] callback`: The function which will be called when an asynchronous
188
212
  operations ends. The given function is called from the main event loop thread.
189
213
 
190
- Returns a `Napi::AsyncWork` instance which can later be queued for execution by
214
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
191
215
  calling `Napi::AsyncWork::Queue`.
192
216
 
193
217
  ### Constructor
@@ -195,13 +219,13 @@ calling `Napi::AsyncWork::Queue`.
195
219
  Creates a new `Napi::AsyncWorker`.
196
220
 
197
221
  ```cpp
198
- explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback,const char* resource_name);
222
+ explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name);
199
223
  ```
200
224
 
201
225
  - `[in] receiver`: The `this` object passed to the called function.
202
226
  - `[in] callback`: The function which will be called when an asynchronous
203
227
  operations ends. The given function is called from the main event loop thread.
204
- - `[in] resource_name`: Null-terminated strings that represents the
228
+ - `[in] resource_name`: Null-terminated string that represents the
205
229
  identifier for the kind of resource that is being provided for diagnostic
206
230
  information exposed by the async_hooks API.
207
231
 
@@ -219,7 +243,7 @@ explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& c
219
243
  - `[in] receiver`: The `this` object passed to the called function.
220
244
  - `[in] callback`: The function which will be called when an asynchronous
221
245
  operations ends. The given function is called from the main event loop thread.
222
- - `[in] resource_name`: Null-terminated strings that represents the
246
+ - `[in] resource_name`: Null-terminated string that represents the
223
247
  identifier for the kind of resource that is being provided for diagnostic
224
248
  information exposed by the async_hooks API.
225
249
  - `[in] resource`: Object associated with the asynchronous operation that
@@ -228,6 +252,54 @@ will be passed to possible async_hooks.
228
252
  Returns a `Napi::AsyncWork` instance which can later be queued for execution by
229
253
  calling `Napi::AsyncWork::Queue`.
230
254
 
255
+
256
+ ### Constructor
257
+
258
+ Creates a new `Napi::AsyncWorker`.
259
+
260
+ ```cpp
261
+ explicit Napi::AsyncWorker(Napi::Env env);
262
+ ```
263
+
264
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
265
+
266
+ Returns an `Napi::AsyncWorker` instance which can later be queued for execution by calling
267
+ `Napi::AsyncWorker::Queue`.
268
+
269
+ ### Constructor
270
+
271
+ Creates a new `Napi::AsyncWorker`.
272
+
273
+ ```cpp
274
+ explicit Napi::AsyncWorker(Napi::Env env, const char* resource_name);
275
+ ```
276
+
277
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
278
+ - `[in] resource_name`: Null-terminated string that represents the
279
+ identifier for the kind of resource that is being provided for diagnostic
280
+ information exposed by the async_hooks API.
281
+
282
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
283
+ calling `Napi::AsyncWorker::Queue`.
284
+
285
+ ### Constructor
286
+
287
+ Creates a new `Napi::AsyncWorker`.
288
+
289
+ ```cpp
290
+ explicit Napi::AsyncWorker(Napi::Env env, const char* resource_name, const Napi::Object& resource);
291
+ ```
292
+
293
+ - `[in] env`: The environment in which to create the `Napi::AsyncWorker`.
294
+ - `[in] resource_name`: Null-terminated string that represents the
295
+ identifier for the kind of resource that is being provided for diagnostic
296
+ information exposed by the async_hooks API.
297
+ - `[in] resource`: Object associated with the asynchronous operation that
298
+ will be passed to possible async_hooks.
299
+
300
+ Returns a `Napi::AsyncWorker` instance which can later be queued for execution by
301
+ calling `Napi::AsyncWorker::Queue`.
302
+
231
303
  ### Destructor
232
304
 
233
305
  Deletes the created work object that is used to execute logic asynchronously.
@@ -262,7 +334,7 @@ function runs in the background out of the **event loop** thread and at the end
262
334
  the `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` function will be
263
335
  called and are executed as part of the event loop.
264
336
 
265
- The code below show a basic example of `Napi::AsyncWorker` the implementation:
337
+ The code below shows a basic example of `Napi::AsyncWorker` the implementation:
266
338
 
267
339
  ```cpp
268
340
  #include<napi.h>
@@ -300,7 +372,7 @@ the work on the `Napi::AsyncWorker::Execute` method is done the
300
372
  `Napi::AsyncWorker::OnOk` method is called and the results return back to
301
373
  JavaScript invoking the stored callback with its associated environment.
302
374
 
303
- The following code shows an example on how to create and and use an `Napi::AsyncWorker`
375
+ The following code shows an example of how to create and use an `Napi::AsyncWorker`.
304
376
 
305
377
  ```cpp
306
378
  #include<napi.h>
@@ -311,7 +383,7 @@ The following code shows an example on how to create and and use an `Napi::Async
311
383
  use namespace Napi;
312
384
 
313
385
  Value Echo(const CallbackInfo& info) {
314
- // You need to check the input data here
386
+ // You need to validate the arguments here.
315
387
  Function cb = info[1].As<Function>();
316
388
  std::string in = info[0].As<String>();
317
389
  EchoWorker* wk = new EchoWorker(cb, in);
@@ -257,6 +257,14 @@ bool Napi::Value::IsExternal() const;
257
257
  Returns `true` if the underlying value is a N-API external object or `false`
258
258
  otherwise.
259
259
 
260
+ #### IsDate
261
+ ```cpp
262
+ bool Napi::Value::IsDate() const;
263
+ ```
264
+
265
+ Returns `true` if the underlying value is a JavaScript `Date` or `false`
266
+ otherwise.
267
+
260
268
  #### ToBoolean
261
269
  ```cpp
262
270
  Napi::Boolean Napi::Value::ToBoolean() const;
@@ -339,6 +347,12 @@ The value is not coerced to a string.
339
347
  Arrays are native representations of JavaScript Arrays. `Napi::Array` is a wrapper
340
348
  around `napi_value` representing a JavaScript Array.
341
349
 
350
+ [`Napi::TypedArray`][] and [`Napi::ArrayBuffer`][] correspond to JavaScript data
351
+ types such as [`Int32Array`][] and [`ArrayBuffer`][], respectively, that can be
352
+ used for transferring large amounts of data from JavaScript to the native side.
353
+ An example illustrating the use of a JavaScript-provided `ArrayBuffer` in native
354
+ code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
355
+
342
356
  ### Constructor
343
357
  ```cpp
344
358
  Napi::Array::Array();
@@ -402,3 +416,8 @@ This can execute JavaScript code implicitly according to JavaScript semantics.
402
416
  If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
403
417
  being used, callers should check the result of `Env::IsExceptionPending` before
404
418
  attempting to use the returned value.
419
+
420
+ [`Napi::TypedArray`]: ./typed_array.md
421
+ [`Napi::ArrayBuffer`]: ./array_buffer.md
422
+ [`Int32Array`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
423
+ [`ArrayBuffer`]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer