Skip to content

Commit

Permalink
update todo comment
Browse files Browse the repository at this point in the history
doc
  • Loading branch information
legendecas committed Oct 23, 2019
1 parent 2540375 commit 7e0e636
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
68 changes: 36 additions & 32 deletions doc/async_progress_worker.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# AsyncProgressWorker

`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker`
while extends `Napi::AsyncWorker` internally with `Napi::ThreadSafeFunction` to
moving work progress reports from worker to event loop threads.
while extending `Napi::AsyncWorker` internally with `Napi::ThreadSafeFunction` for
moving work progress reports from worker thread(s) to event loop threads.

Like `Napi::AsyncWorker`, once created, execution is requested by calling
`Napi::AsyncProgressWorker::Queue`. When a thread is available for execution
the `Napi::AsyncProgressWorker::Execute` method will be invoked. During the
execution, `Napi::AsyncProgressWorker::ExecutionProgress::Send` could be used to
execution, `Napi::AsyncProgressWorker::ExecutionProgress::Send` can be used to
indicate execution process, which would eventually invoke `Napi::AsyncProgressWorker::OnProgress`
on JavaScript thread to safely call into JavaScript lands. Once `Napi::AsyncProgressWorker::Execute`
on the JavaScript thread to safely call into JavaScript. Once `Napi::AsyncProgressWorker::Execute`
completes either `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
will be invoked. Once the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
methods are complete the `Napi::AsyncProgressWorker` instance is destructed.
Expand All @@ -19,18 +19,19 @@ For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and

## Methods

Most methods could be referred to `Napi::AsyncWorker` to get detailed descriptions.
[`Napi::AsyncWorker`]() provides detailed descriptions for most methods.

### Execute

This method is used to execute some tasks out of the **event loop** on a libuv
This method is used to execute some tasks outside of the **event loop** on a libuv
worker thread. Subclasses must implement this method and the method is run on
a thread other than that running the main event loop. As the method is not
a thread other than that running the main event loop. As the method is not
running on the main event loop, it must avoid calling any methods from node-addon-api
or running any code that might invoke JavaScript. Instead, once this method is
complete any interaction through node-addon-api with JavaScript should be implemented
in the `Napi::AsyncProgressWorker::OnOK` method which runs on the main thread and is
invoked when the `Napi::AsyncProgressWorker::Execute` method completes.
in the `Napi::AsyncProgressWorker::OnOK` method or `Napi::AsyncProgressWorker::OnError`
which run on the main thread and are invoked when the `Napi::AsyncProgressWorker::Execute`
method completes.

```cpp
virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progress) = 0;
Expand All @@ -39,9 +40,10 @@ virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progres
### OnOK
This method is invoked when the computation in the `Execute` method ends.
The default implementation runs the Callback optionally provided when the
AsyncProgressWorker class was created. The callback will by default receive no
arguments. To provide arguments, override the `GetResult()` method.
The default implementation runs the `Callback` optionally provided when the
`AsyncProgressWorker` class was created. The `Callback` will by default receive no
arguments. Arguments to the callback can be provided by overriding the `GetResult()`
method.
```cpp
virtual void Napi::AsyncProgressWorker::OnOK();
Expand All @@ -50,7 +52,7 @@ virtual void Napi::AsyncProgressWorker::OnOK();
### OnProgress

This method is invoked when the computation in the `Napi::AsyncProgressWorker::ExecutionProcess::Send`
method was called on worker thread execution.
method was called during worker thread execution.

```cpp
virtual void Napi::AsyncProgressWorker::OnProgress(const T* data, size_t count)
Expand Down Expand Up @@ -80,7 +82,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r
- `[in] callback`: The function which will be called when an asynchronous
operations ends. The given function is called from the main event loop thread.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.
Expand All @@ -97,7 +99,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* r

- `[in] callback`: The function which will be called when an asynchronous
operations ends. The given function is called from the main event loop thread.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.
- `[in] resource`: Object associated with the asynchronous operation that
Expand Down Expand Up @@ -132,7 +134,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
- `[in] receiver`: The `this` object passed to the called function.
- `[in] callback`: The function which will be called when an asynchronous
operations ends. The given function is called from the main event loop thread.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.

Expand All @@ -150,7 +152,7 @@ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Fun
- `[in] receiver`: The `this` object passed to the called function.
- `[in] callback`: The function which will be called when an asynchronous
operations ends. The given function is called from the main event loop thread.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.
- `[in] resource`: Object associated with the asynchronous operation that
Expand Down Expand Up @@ -183,14 +185,14 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name);
```
- `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.
Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
calling `Napi::AsyncProgressWorker::Queue`.
Available with `NAPI_VERSION` equal or greater than 5.
Available with `NAPI_VERSION` equal to or greater than 5.
### Constructor
Expand All @@ -201,7 +203,7 @@ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, con
```

- `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
- `[in] resource_name`: Null-terminated strings that represents the
- `[in] resource_name`: Null-terminated string that represents the
identifier for the kind of resource that is being provided for diagnostic
information exposed by the async_hooks API.
- `[in] resource`: Object associated with the asynchronous operation that
Expand All @@ -210,7 +212,7 @@ will be passed to possible async_hooks.
Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
calling `Napi::AsyncProgressWorker::Queue`.

Available with `NAPI_VERSION` equal or greater than 5.
Available with `NAPI_VERSION` equal to or greater than 5.

### Destructor

Expand All @@ -224,17 +226,17 @@ virtual Napi::AsyncProgressWorker::~AsyncProgressWorker();

# AsyncProgressWorker::ExecutionProcess

A bridge class created hereby before the worker thread execution of `Napi::AsyncProgressWorker::Execute`.
A bridge class created before the worker thread execution of `Napi::AsyncProgressWorker::Execute`.

## Methods

### Send

`Napi::AsyncProgressWorker::ExecutionProcess::Send` takes two argument, a pointer
to generic type of data, and a `size_t` indicates how many items the pointer has pointed to.
`Napi::AsyncProgressWorker::ExecutionProcess::Send` takes two arguments, a pointer
to a generic type of data, and a `size_t` to indicate how many items the pointer is pointed to.

Pointed data would be copied to internal slots of `Napi::AsyncProgressWorker` so
after call of `Napi::AsyncProgressWorker::ExecutionProcess::Send` the data could
The data pointed to will be copied to internal slots of `Napi::AsyncProgressWorker` so
after the call to `Napi::AsyncProgressWorker::ExecutionProcess::Send` the data can
be safely released.

Note that `Napi::AsyncProgressWorker::ExecutionProcess::Send` merely guarantees
Expand All @@ -250,11 +252,11 @@ void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t cou
The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method.
Typically input to your worker will be saved within class' fields generally
Typically input to your worker will be saved within the class' fields generally
passed in through its constructor.
During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute`
could be used to report process of the execution.
can be used to report the process of the execution.
When the `Napi::AsyncProgressWorker::Execute` method completes without errors the
`Napi::AsyncProgressWorker::OnOK` function callback will be invoked. In this function the
Expand All @@ -266,7 +268,7 @@ function runs in the background out of the **event loop** thread and at the end
the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
called and are executed as part of the event loop.
The code below show a basic example of `Napi::AsyncProgressWorker` the implementation:
The code below shows a basic example of `Napi::AsyncProgressWorker` the implementation:
```cpp
#include<napi.h>
Expand Down Expand Up @@ -298,7 +300,7 @@ class EchoWorker : public AsyncProgressWorker<uint32_t> {
void OnProgress(const uint32_t* data, size_t /* count */) {
HandleScope scope(Env());
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), data)});
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
}
private:
Expand All @@ -312,7 +314,7 @@ the work on the `Napi::AsyncProgressWorker::Execute` method is done the
`Napi::AsyncProgressWorker::OnOk` method is called and the results return back to
JavaScript invoking the stored callback with its associated environment.

The following code shows an example on how to create and use an `Napi::AsyncProgressWorker`
The following code shows an example of how to create and use an `Napi::AsyncProgressWorker`

```cpp
#include<napi.h>
Expand All @@ -323,7 +325,7 @@ The following code shows an example on how to create and use an `Napi::AsyncProg
use namespace Napi;

Value Echo(const CallbackInfo& info) {
// You need to check the input data here
// You need to validate the arguments here
Function cb = info[1].As<Function>();
std::string in = info[0].As<String>();
EchoWorker* wk = new EchoWorker(cb, in);
Expand All @@ -337,3 +339,5 @@ need to create a new instance and pass to its constructor the callback you want
execute when your asynchronous task ends and other data you need for your
computation. Once created the only other action you have to do is to call the
`Napi::AsyncProgressWorker::Queue` method that will queue the created worker for execution.
[`Napi::AsyncWorker`]: ./async_worker.md
3 changes: 2 additions & 1 deletion napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4186,7 +4186,8 @@ inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
: AsyncWorker(env, resource_name, resource),
_asyncdata(nullptr),
_asyncsize(0) {
// FIXME: Napi::ThreadSafeFunction nullptr callback support?
// TODO: Once the changes to make the callback optional for threadsafe
// functions are no longer optional we can remove the dummy Function here.
Function callback;
_tsfn = ThreadSafeFunction::New(env, callback, resource_name, 1, 1);
}
Expand Down

0 comments on commit 7e0e636

Please sign in to comment.