11# AsyncProgressWorker
22
33` 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.
66
77Like ` Napi::AsyncWorker ` , once created, execution is requested by calling
88` Napi::AsyncProgressWorker::Queue ` . When a thread is available for execution
99the ` 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
1111indicate 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 `
1313completes either ` Napi::AsyncProgressWorker::OnOK ` or ` Napi::AsyncProgressWorker::OnError `
1414will be invoked. Once the ` Napi::AsyncProgressWorker::OnOK ` or ` Napi::AsyncProgressWorker::OnError `
1515methods are complete the ` Napi::AsyncProgressWorker ` instance is destructed.
@@ -19,18 +19,19 @@ For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and
1919
2020## Methods
2121
22- Most methods could be referred to ` Napi::AsyncWorker ` to get detailed descriptions.
22+ [ ` Napi::AsyncWorker ` ] ( ) provides detailed descriptions for most methods .
2323
2424### Execute
2525
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
2727worker 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
2929running on the main event loop, it must avoid calling any methods from node-addon-api
3030or running any code that might invoke JavaScript. Instead, once this method is
3131complete 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.
3435
3536``` cpp
3637virtual void Napi::AsyncProgressWorker::Execute (const ExecutionProgress& progress) = 0;
@@ -39,9 +40,10 @@ virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progres
3940### OnOK
4041
4142This 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.
4547
4648```cpp
4749virtual void Napi::AsyncProgressWorker::OnOK();
@@ -50,7 +52,7 @@ virtual void Napi::AsyncProgressWorker::OnOK();
5052### OnProgress
5153
5254This 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.
5456
5557``` cpp
5658virtual void Napi::AsyncProgressWorker::OnProgress (const T* data, size_t count)
@@ -80,7 +82,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r
8082
8183- `[in] callback`: The function which will be called when an asynchronous
8284operations 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
8486identifier for the kind of resource that is being provided for diagnostic
8587information exposed by the async_hooks API.
8688
@@ -97,7 +99,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r
9799
98100- ` [in] callback ` : The function which will be called when an asynchronous
99101operations 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
101103identifier for the kind of resource that is being provided for diagnostic
102104information exposed by the async_hooks API.
103105- ` [in] resource ` : Object associated with the asynchronous operation that
@@ -132,7 +134,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
132134- ` [in] receiver ` : The ` this ` object passed to the called function.
133135- ` [in] callback ` : The function which will be called when an asynchronous
134136operations 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
136138identifier for the kind of resource that is being provided for diagnostic
137139information exposed by the async_hooks API.
138140
@@ -150,7 +152,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
150152- `[in] receiver`: The `this` object passed to the called function.
151153- `[in] callback`: The function which will be called when an asynchronous
152154operations 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
154156identifier for the kind of resource that is being provided for diagnostic
155157information exposed by the async_hooks API.
156158- `[in] resource`: Object associated with the asynchronous operation that
@@ -183,14 +185,14 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name);
183185```
184186
185187- `[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
187189identifier for the kind of resource that is being provided for diagnostic
188190information exposed by the async_hooks API.
189191
190192Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
191193calling `Napi::AsyncProgressWorker::Queue`.
192194
193- Available with `NAPI_VERSION` equal or greater than 5.
195+ Available with `NAPI_VERSION` equal to or greater than 5.
194196
195197### Constructor
196198
@@ -201,7 +203,7 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, con
201203```
202204
203205- ` [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
205207identifier for the kind of resource that is being provided for diagnostic
206208information exposed by the async_hooks API.
207209- ` [in] resource ` : Object associated with the asynchronous operation that
@@ -210,7 +212,7 @@ will be passed to possible async_hooks.
210212Returns a ` Napi::AsyncProgressWorker ` instance which can later be queued for execution by
211213calling ` Napi::AsyncProgressWorker::Queue ` .
212214
213- Available with ` NAPI_VERSION ` equal or greater than 5.
215+ Available with ` NAPI_VERSION ` equal to or greater than 5.
214216
215217### Destructor
216218
@@ -224,17 +226,17 @@ virtual Napi::AsyncProgressWorker::~AsyncProgressWorker();
224226
225227# AsyncProgressWorker::ExecutionProcess
226228
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 ` .
228230
229231## Methods
230232
231233### Send
232234
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.
235237
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
238240be safely released.
239241
240242Note that ` Napi::AsyncProgressWorker::ExecutionProcess::Send ` merely guarantees
@@ -250,11 +252,11 @@ void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t cou
250252
251253The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
252254inherits 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
254256passed in through its constructor.
255257
256258During 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.
258260
259261When the `Napi::AsyncProgressWorker::Execute` method completes without errors the
260262`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
266268the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
267269called and are executed as part of the event loop.
268270
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:
270272
271273```cpp
272274#include<napi.h>
@@ -298,7 +300,7 @@ class EchoWorker : public AsyncProgressWorker<uint32_t> {
298300
299301 void OnProgress(const uint32_t* data, size_t /* count */) {
300302 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)});
302304 }
303305
304306 private:
@@ -312,7 +314,7 @@ the work on the `Napi::AsyncProgressWorker::Execute` method is done the
312314` Napi::AsyncProgressWorker::OnOk ` method is called and the results return back to
313315JavaScript invoking the stored callback with its associated environment.
314316
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 `
316318
317319``` cpp
318320#include < napi.h>
@@ -323,7 +325,7 @@ The following code shows an example on how to create and use an `Napi::AsyncProg
323325use namespace Napi ;
324326
325327Value Echo (const CallbackInfo& info) {
326- // You need to check the input data here
328+ // You need to validate the arguments here
327329 Function cb = info[ 1] .As<Function >();
328330 std::string in = info[ 0] .As<String >();
329331 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
337339execute when your asynchronous task ends and other data you need for your
338340computation. Once created the only other action you have to do is to call the
339341`Napi::AsyncProgressWorker::Queue` method that will queue the created worker for execution.
342+
343+ [`Napi::AsyncWorker`]: ./async_worker.md
0 commit comments