1
1
# AsyncProgressWorker
2
2
3
3
` Napi::AsyncProgressWorker ` is an abstract class, which implements ` Napi::AsyncWorker `
4
- while extends ` Napi::AsyncWorker ` internally with ` Napi::ThreadSafeFunction ` to
5
- moving work progress reports from worker to event loop threads.
4
+ while extending ` Napi::AsyncWorker ` internally with ` Napi::ThreadSafeFunction ` for
5
+ moving work progress reports from worker thread(s) to event loop threads.
6
6
7
7
Like ` Napi::AsyncWorker ` , once created, execution is requested by calling
8
8
` Napi::AsyncProgressWorker::Queue ` . When a thread is available for execution
9
9
the ` Napi::AsyncProgressWorker::Execute ` method will be invoked. During the
10
- execution, ` Napi::AsyncProgressWorker::ExecutionProgress::Send ` could be used to
10
+ execution, ` Napi::AsyncProgressWorker::ExecutionProgress::Send ` can be used to
11
11
indicate execution process, which would eventually invoke ` Napi::AsyncProgressWorker::OnProgress `
12
- on JavaScript thread to safely call into JavaScript lands . Once ` Napi::AsyncProgressWorker::Execute `
12
+ on the JavaScript thread to safely call into JavaScript. Once ` Napi::AsyncProgressWorker::Execute `
13
13
completes either ` Napi::AsyncProgressWorker::OnOK ` or ` Napi::AsyncProgressWorker::OnError `
14
14
will be invoked. Once the ` Napi::AsyncProgressWorker::OnOK ` or ` Napi::AsyncProgressWorker::OnError `
15
15
methods are complete the ` Napi::AsyncProgressWorker ` instance is destructed.
@@ -19,18 +19,19 @@ For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and
19
19
20
20
## Methods
21
21
22
- Most methods could be referred to ` Napi::AsyncWorker ` to get detailed descriptions.
22
+ [ ` Napi::AsyncWorker ` ] ( ) provides detailed descriptions for most methods .
23
23
24
24
### Execute
25
25
26
- This method is used to execute some tasks out of the ** event loop** on a libuv
26
+ This method is used to execute some tasks outside of the ** event loop** on a libuv
27
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
28
+ a thread other than that running the main event loop. As the method is not
29
29
running on the main event loop, it must avoid calling any methods from node-addon-api
30
30
or running any code that might invoke JavaScript. Instead, once this method is
31
31
complete any interaction through node-addon-api with JavaScript should be implemented
32
- in the ` Napi::AsyncProgressWorker::OnOK ` method which runs on the main thread and is
33
- invoked when the ` Napi::AsyncProgressWorker::Execute ` method completes.
32
+ in the ` Napi::AsyncProgressWorker::OnOK ` method or ` Napi::AsyncProgressWorker::OnError `
33
+ which run on the main thread and are invoked when the ` Napi::AsyncProgressWorker::Execute `
34
+ method completes.
34
35
35
36
``` cpp
36
37
virtual void Napi::AsyncProgressWorker::Execute (const ExecutionProgress& progress) = 0;
@@ -39,9 +40,10 @@ virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progres
39
40
### OnOK
40
41
41
42
This method is invoked when the computation in the `Execute` method ends.
42
- The default implementation runs the Callback optionally provided when the
43
- AsyncProgressWorker class was created. The callback will by default receive no
44
- arguments. To provide arguments, override the `GetResult()` method.
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.
45
47
46
48
```cpp
47
49
virtual void Napi::AsyncProgressWorker::OnOK();
@@ -50,7 +52,7 @@ virtual void Napi::AsyncProgressWorker::OnOK();
50
52
### OnProgress
51
53
52
54
This method is invoked when the computation in the ` Napi::AsyncProgressWorker::ExecutionProcess::Send `
53
- method was called on worker thread execution.
55
+ method was called during worker thread execution.
54
56
55
57
``` cpp
56
58
virtual void Napi::AsyncProgressWorker::OnProgress (const T* data, size_t count)
@@ -80,7 +82,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r
80
82
81
83
- `[in] callback`: The function which will be called when an asynchronous
82
84
operations ends. The given function is called from the main event loop thread.
83
- - `[in] resource_name`: Null-terminated strings that represents the
85
+ - `[in] resource_name`: Null-terminated string that represents the
84
86
identifier for the kind of resource that is being provided for diagnostic
85
87
information exposed by the async_hooks API.
86
88
@@ -97,7 +99,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r
97
99
98
100
- ` [in] callback ` : The function which will be called when an asynchronous
99
101
operations ends. The given function is called from the main event loop thread.
100
- - ` [in] resource_name ` : Null-terminated strings that represents the
102
+ - ` [in] resource_name ` : Null-terminated string that represents the
101
103
identifier for the kind of resource that is being provided for diagnostic
102
104
information exposed by the async_hooks API.
103
105
- ` [in] resource ` : Object associated with the asynchronous operation that
@@ -132,7 +134,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
132
134
- ` [in] receiver ` : The ` this ` object passed to the called function.
133
135
- ` [in] callback ` : The function which will be called when an asynchronous
134
136
operations ends. The given function is called from the main event loop thread.
135
- - ` [in] resource_name ` : Null-terminated strings that represents the
137
+ - ` [in] resource_name ` : Null-terminated string that represents the
136
138
identifier for the kind of resource that is being provided for diagnostic
137
139
information exposed by the async_hooks API.
138
140
@@ -150,7 +152,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
150
152
- `[in] receiver`: The `this` object passed to the called function.
151
153
- `[in] callback`: The function which will be called when an asynchronous
152
154
operations ends. The given function is called from the main event loop thread.
153
- - `[in] resource_name`: Null-terminated strings that represents the
155
+ - `[in] resource_name`: Null-terminated string that represents the
154
156
identifier for the kind of resource that is being provided for diagnostic
155
157
information exposed by the async_hooks API.
156
158
- `[in] resource`: Object associated with the asynchronous operation that
@@ -183,14 +185,14 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name);
183
185
```
184
186
185
187
- `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
186
- - `[in] resource_name`: Null-terminated strings that represents the
188
+ - `[in] resource_name`: Null-terminated string that represents the
187
189
identifier for the kind of resource that is being provided for diagnostic
188
190
information exposed by the async_hooks API.
189
191
190
192
Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
191
193
calling `Napi::AsyncProgressWorker::Queue`.
192
194
193
- Available with `NAPI_VERSION` equal or greater than 5.
195
+ Available with `NAPI_VERSION` equal to or greater than 5.
194
196
195
197
### Constructor
196
198
@@ -201,7 +203,7 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, con
201
203
```
202
204
203
205
- ` [in] env ` : The environment in which to create the ` Napi::AsyncProgressWorker ` .
204
- - ` [in] resource_name ` : Null-terminated strings that represents the
206
+ - ` [in] resource_name ` : Null-terminated string that represents the
205
207
identifier for the kind of resource that is being provided for diagnostic
206
208
information exposed by the async_hooks API.
207
209
- ` [in] resource ` : Object associated with the asynchronous operation that
@@ -210,7 +212,7 @@ will be passed to possible async_hooks.
210
212
Returns a ` Napi::AsyncProgressWorker ` instance which can later be queued for execution by
211
213
calling ` Napi::AsyncProgressWorker::Queue ` .
212
214
213
- Available with ` NAPI_VERSION ` equal or greater than 5.
215
+ Available with ` NAPI_VERSION ` equal to or greater than 5.
214
216
215
217
### Destructor
216
218
@@ -224,17 +226,17 @@ virtual Napi::AsyncProgressWorker::~AsyncProgressWorker();
224
226
225
227
# AsyncProgressWorker::ExecutionProcess
226
228
227
- A bridge class created hereby before the worker thread execution of ` Napi::AsyncProgressWorker::Execute ` .
229
+ A bridge class created before the worker thread execution of ` Napi::AsyncProgressWorker::Execute ` .
228
230
229
231
## Methods
230
232
231
233
### Send
232
234
233
- ` Napi::AsyncProgressWorker::ExecutionProcess::Send ` takes two argument , a pointer
234
- to generic type of data, and a ` size_t ` indicates how many items the pointer has pointed to.
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 pointed to.
235
237
236
- Pointed data would be copied to internal slots of ` Napi::AsyncProgressWorker ` so
237
- after call of ` Napi::AsyncProgressWorker::ExecutionProcess::Send ` the data could
238
+ The data pointed to will be copied to internal slots of ` Napi::AsyncProgressWorker ` so
239
+ after the call to ` Napi::AsyncProgressWorker::ExecutionProcess::Send ` the data can
238
240
be safely released.
239
241
240
242
Note that ` Napi::AsyncProgressWorker::ExecutionProcess::Send ` merely guarantees
@@ -250,11 +252,11 @@ void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t cou
250
252
251
253
The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
252
254
inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method.
253
- Typically input to your worker will be saved within class' fields generally
255
+ Typically input to your worker will be saved within the class' fields generally
254
256
passed in through its constructor.
255
257
256
258
During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute`
257
- could be used to report process of the execution.
259
+ can be used to report the process of the execution.
258
260
259
261
When the `Napi::AsyncProgressWorker::Execute` method completes without errors the
260
262
`Napi::AsyncProgressWorker::OnOK` function callback will be invoked. In this function the
@@ -266,7 +268,7 @@ function runs in the background out of the **event loop** thread and at the end
266
268
the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
267
269
called and are executed as part of the event loop.
268
270
269
- The code below show a basic example of `Napi::AsyncProgressWorker` the implementation:
271
+ The code below shows a basic example of `Napi::AsyncProgressWorker` the implementation:
270
272
271
273
```cpp
272
274
#include<napi.h>
@@ -298,7 +300,7 @@ class EchoWorker : public AsyncProgressWorker<uint32_t> {
298
300
299
301
void OnProgress(const uint32_t* data, size_t /* count */) {
300
302
HandleScope scope(Env());
301
- Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), data)});
303
+ Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), * data)});
302
304
}
303
305
304
306
private:
@@ -312,7 +314,7 @@ the work on the `Napi::AsyncProgressWorker::Execute` method is done the
312
314
` Napi::AsyncProgressWorker::OnOk ` method is called and the results return back to
313
315
JavaScript invoking the stored callback with its associated environment.
314
316
315
- The following code shows an example on how to create and use an ` Napi::AsyncProgressWorker `
317
+ The following code shows an example of how to create and use an ` Napi::AsyncProgressWorker `
316
318
317
319
``` cpp
318
320
#include < napi.h>
@@ -323,7 +325,7 @@ The following code shows an example on how to create and use an `Napi::AsyncProg
323
325
use namespace Napi ;
324
326
325
327
Value Echo (const CallbackInfo& info) {
326
- // You need to check the input data here
328
+ // You need to validate the arguments here
327
329
Function cb = info[ 1] .As<Function >();
328
330
std::string in = info[ 0] .As<String >();
329
331
EchoWorker* wk = new EchoWorker(cb, in);
@@ -337,3 +339,5 @@ need to create a new instance and pass to its constructor the callback you want
337
339
execute when your asynchronous task ends and other data you need for your
338
340
computation. Once created the only other action you have to do is to call the
339
341
`Napi::AsyncProgressWorker::Queue` method that will queue the created worker for execution.
342
+
343
+ [`Napi::AsyncWorker`]: ./async_worker.md
0 commit comments