-
-
Notifications
You must be signed in to change notification settings - Fork 498
Implement AsyncProgressWorker #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,344 @@ | ||||||
| # AsyncProgressWorker | ||||||
|
|
||||||
| `Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker` | ||||||
| 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` can be used to | ||||||
| indicate execution process, which will eventually invoke `Napi::AsyncProgressWorker::OnProgress` | ||||||
| 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. | ||||||
|
|
||||||
| For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and | ||||||
| `Napi::AsyncProgressWorker::OnProgress` method must be implemented in a subclass. | ||||||
|
|
||||||
| ## Methods | ||||||
|
|
||||||
| [`Napi::AsyncWorker`]() provides detailed descriptions for most methods. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ### Execute | ||||||
|
|
||||||
| 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 | ||||||
| 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 and/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; | ||||||
| ``` | ||||||
|
|
||||||
| ### 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. Arguments to the callback can be provided by overriding the `GetResult()` | ||||||
| method. | ||||||
|
|
||||||
| ```cpp | ||||||
| virtual void Napi::AsyncProgressWorker::OnOK(); | ||||||
| ``` | ||||||
|
|
||||||
| ### OnProgress | ||||||
|
|
||||||
| This method is invoked when the computation in the `Napi::AsyncProgressWorker::ExecutionProcess::Send` | ||||||
| method was called during worker thread execution. | ||||||
|
|
||||||
| ```cpp | ||||||
| virtual void Napi::AsyncProgressWorker::OnProgress(const T* data, size_t count) | ||||||
| ``` | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Function& callback); | ||||||
| ``` | ||||||
|
|
||||||
| - `[in] callback`: The function which will be called when an asynchronous | ||||||
| operations ends. The given function is called from the main event loop thread. | ||||||
|
|
||||||
| Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by | ||||||
| calling `Napi::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name); | ||||||
|
gabrielschulhof marked this conversation as resolved.
|
||||||
| ``` | ||||||
|
|
||||||
| - `[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 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::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name, const Napi::Object& resource); | ||||||
| ``` | ||||||
|
|
||||||
| - `[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 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 | ||||||
| will be passed to possible async_hooks. | ||||||
|
|
||||||
| Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by | ||||||
| calling `Napi::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback); | ||||||
| ``` | ||||||
|
|
||||||
| - `[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. | ||||||
|
|
||||||
| Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by | ||||||
| calling `Napi::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name); | ||||||
| ``` | ||||||
|
|
||||||
| - `[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 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::AsyncWork` instance which can later be queued for execution by | ||||||
| calling `Napi::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource); | ||||||
| ``` | ||||||
|
|
||||||
| - `[in] receiver`: The `this` object passed to the called function. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - `[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 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 | ||||||
| will be passed to possible async_hooks. | ||||||
|
|
||||||
| Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||||||
| calling `Napi::AsyncWork::Queue`. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(Napi::Env env); | ||||||
| ``` | ||||||
|
|
||||||
| - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| Returns an `Napi::AsyncProgressWorker` instance which can later be queued for execution by calling | ||||||
| `Napi::AsyncProgressWorker::Queue`. | ||||||
|
|
||||||
| Available with `NAPI_VERSION` equal to or greater than 5. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| 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 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 to or greater than 5. | ||||||
|
|
||||||
| ### Constructor | ||||||
|
|
||||||
| Creates a new `Napi::AsyncProgressWorker`. | ||||||
|
|
||||||
| ```cpp | ||||||
| explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, const Napi::Object& resource); | ||||||
| ``` | ||||||
|
|
||||||
| - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`. | ||||||
| - `[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 | ||||||
| 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 to or greater than 5. | ||||||
|
|
||||||
| ### Destructor | ||||||
|
|
||||||
| Deletes the created work object that is used to execute logic asynchronously and | ||||||
| release the internal `Napi::ThreadSafeFunction`, which will be aborted to prevent | ||||||
| unexpected upcoming thread safe calls. | ||||||
|
|
||||||
| ```cpp | ||||||
| virtual Napi::AsyncProgressWorker::~AsyncProgressWorker(); | ||||||
| ``` | ||||||
|
|
||||||
| # AsyncProgressWorker::ExecutionProcess | ||||||
|
|
||||||
| A bridge class created before the worker thread execution of `Napi::AsyncProgressWorker::Execute`. | ||||||
|
|
||||||
| ## Methods | ||||||
|
|
||||||
| ### Send | ||||||
|
|
||||||
| `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 | ||||||
| pointing to. | ||||||
|
|
||||||
| 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 | ||||||
| **eventual** invocation of `Napi::AsyncProgressWorker::OnProgress`, which means | ||||||
| multiple send might be coalesced into single invocation of `Napi::AsyncProgressWorker::OnProgress` | ||||||
|
gabrielschulhof marked this conversation as resolved.
|
||||||
| with latest data. | ||||||
|
|
||||||
| ```cpp | ||||||
| void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t count) const; | ||||||
| ``` | ||||||
|
|
||||||
| ## Example | ||||||
|
|
||||||
| 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 the class' fields generally | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| passed in through its constructor. | ||||||
|
|
||||||
| During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute` | ||||||
| can be used to report the process of the execution. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| When the `Napi::AsyncProgressWorker::Execute` method completes without errors the | ||||||
| `Napi::AsyncProgressWorker::OnOK` function callback will be invoked. In this function the | ||||||
| results of the computation will be reassembled and returned back to the initial | ||||||
| JavaScript context. | ||||||
|
|
||||||
| `Napi::AsyncProgressWorker` ensures that all the code in the `Napi::AsyncProgressWorker::Execute` | ||||||
| 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 shows a basic example of `Napi::AsyncProgressWorker` the implementation: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```cpp | ||||||
| #include<napi.h> | ||||||
|
|
||||||
| #include <chrono> | ||||||
| #include <thread> | ||||||
|
|
||||||
| use namespace Napi; | ||||||
|
|
||||||
| class EchoWorker : public AsyncProgressWorker<uint32_t> { | ||||||
| public: | ||||||
| EchoWorker(Function& callback, std::string& echo) | ||||||
| : AsyncProgressWorker(callback), echo(echo) {} | ||||||
|
|
||||||
| ~EchoWorker() {} | ||||||
| // This code will be executed on the worker thread | ||||||
| void Execute(const ExecutionProgress& progress) { | ||||||
| // Need to simulate cpu heavy task | ||||||
| for (uint32_t i = 0; i < 100; ++i) { | ||||||
| progress.Send(&i, 1) | ||||||
| std::this_thread::sleep_for(std::chrono::seconds(1)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void OnOK() { | ||||||
| HandleScope scope(Env()); | ||||||
| Callback().Call({Env().Null(), String::New(Env(), echo)}); | ||||||
| } | ||||||
|
|
||||||
| void OnProgress(const uint32_t* data, size_t /* count */) { | ||||||
| HandleScope scope(Env()); | ||||||
| Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)}); | ||||||
| } | ||||||
|
|
||||||
| private: | ||||||
| std::string echo; | ||||||
| }; | ||||||
| ``` | ||||||
|
|
||||||
| The `EchoWorker`'s contructor calls the base class' constructor to pass in the | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| callback that the `Napi::AsyncProgressWorker` base class will store persistently. When | ||||||
| the work on the `Napi::AsyncProgressWorker::Execute` method is done the | ||||||
| `Napi::AsyncProgressWorker::OnOk` method is called and the results return back to | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| JavaScript invoking the stored callback with its associated environment. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| The following code shows an example of how to create and use an `Napi::AsyncProgressWorker` | ||||||
|
|
||||||
| ```cpp | ||||||
| #include<napi.h> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| // Include EchoWorker class | ||||||
| // .. | ||||||
|
|
||||||
| use namespace Napi; | ||||||
|
|
||||||
| Value Echo(const CallbackInfo& info) { | ||||||
| // You need to validate the arguments here | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Function cb = info[1].As<Function>(); | ||||||
| std::string in = info[0].As<String>(); | ||||||
| EchoWorker* wk = new EchoWorker(cb, in); | ||||||
| wk->Queue(); | ||||||
| return info.Env().Undefined(); | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Using the implementation of a `Napi::AsyncProgressWorker` is straight forward. You only | ||||||
| need to create a new instance and pass to its constructor the callback you want to | ||||||
| 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. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally avoid using "you" in our documentation. Let's rephrase this as |
||||||
|
|
||||||
| [`Napi::AsyncWorker`]: ./async_worker.md | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.