1 ## Asynchronous work helpers
3 `Nan::AsyncWorker`, `Nan::AsyncProgressWorker` and `Nan::AsyncProgressQueueWorker` are helper classes that make working with asynchronous code easier.
5 - <a href="#api_nan_async_worker"><b><code>Nan::AsyncWorker</code></b></a>
6 - <a href="#api_nan_async_progress_worker"><b><code>Nan::AsyncProgressWorkerBase & Nan::AsyncProgressWorker</code></b></a>
7 - <a href="#api_nan_async_progress_queue_worker"><b><code>Nan::AsyncProgressQueueWorker</code></b></a>
8 - <a href="#api_nan_async_queue_worker"><b><code>Nan::AsyncQueueWorker</code></b></a>
10 <a name="api_nan_async_worker"></a>
13 `Nan::AsyncWorker` is an _abstract_ class that you can subclass to have much of the annoying asynchronous queuing and handling taken care of for you. It can even store arbitrary V8 objects for you and have them persist while the asynchronous work is in progress.
15 This class internally handles the details of creating an [`AsyncResource`][AsyncResource], and running the callback in the
16 correct async context. To be able to identify the async resources created by this class in async-hooks, provide a
17 `resource_name` to the constructor. It is recommended that the module name be used as a prefix to the `resource_name` to avoid
18 collisions in the names. For more details see [`AsyncResource`][AsyncResource] documentation. The `resource_name` needs to stay valid for the lifetime of the worker instance.
25 explicit AsyncWorker(Callback *callback_, const char* resource_name = "nan:AsyncWorker");
27 virtual ~AsyncWorker();
29 virtual void WorkComplete();
31 void SaveToPersistent(const char *key, const v8::Local<v8::Value> &value);
33 void SaveToPersistent(const v8::Local<v8::String> &key,
34 const v8::Local<v8::Value> &value);
36 void SaveToPersistent(uint32_t index,
37 const v8::Local<v8::Value> &value);
39 v8::Local<v8::Value> GetFromPersistent(const char *key) const;
41 v8::Local<v8::Value> GetFromPersistent(const v8::Local<v8::String> &key) const;
43 v8::Local<v8::Value> GetFromPersistent(uint32_t index) const;
45 virtual void Execute() = 0;
49 virtual void Destroy();
52 Persistent<v8::Object> persistentHandle;
56 virtual void HandleOKCallback();
58 virtual void HandleErrorCallback();
60 void SetErrorMessage(const char *msg);
62 const char* ErrorMessage();
66 <a name="api_nan_async_progress_worker"></a>
67 ### Nan::AsyncProgressWorkerBase & Nan::AsyncProgressWorker
69 `Nan::AsyncProgressWorkerBase` is an _abstract_ class template that extends `Nan::AsyncWorker` and adds additional progress reporting callbacks that can be used during the asynchronous work execution to provide progress data back to JavaScript.
71 Previously the definition of `Nan::AsyncProgressWorker` only allowed sending `const char` data. Now extending `Nan::AsyncProgressWorker` will yield an instance of the implicit `Nan::AsyncProgressWorkerBase` template with type `<char>` for compatibility.
73 `Nan::AsyncProgressWorkerBase` & `Nan::AsyncProgressWorker` is intended for best-effort delivery of nonessential progress messages, e.g. a progress bar. The last event sent before the main thread is woken will be delivered.
79 class AsyncProgressWorkerBase<T> : public AsyncWorker {
81 explicit AsyncProgressWorkerBase(Callback *callback_, const char* resource_name = ...);
83 virtual ~AsyncProgressWorkerBase();
87 class ExecutionProgress {
90 void Send(const T* data, size_t count) const;
93 virtual void Execute(const ExecutionProgress& progress) = 0;
95 virtual void HandleProgressCallback(const T *data, size_t count) = 0;
97 virtual void Destroy();
100 typedef AsyncProgressWorkerBase<T> AsyncProgressWorker;
103 <a name="api_nan_async_progress_queue_worker"></a>
104 ### Nan::AsyncProgressQueueWorker
106 `Nan::AsyncProgressQueueWorker` is an _abstract_ class template that extends `Nan::AsyncWorker` and adds additional progress reporting callbacks that can be used during the asynchronous work execution to provide progress data back to JavaScript.
108 `Nan::AsyncProgressQueueWorker` behaves exactly the same as `Nan::AsyncProgressWorker`, except all events are queued and delivered to the main thread.
114 class AsyncProgressQueueWorker<T> : public AsyncWorker {
116 explicit AsyncProgressQueueWorker(Callback *callback_, const char* resource_name = "nan:AsyncProgressQueueWorker");
118 virtual ~AsyncProgressQueueWorker();
122 class ExecutionProgress {
124 void Send(const T* data, size_t count) const;
127 virtual void Execute(const ExecutionProgress& progress) = 0;
129 virtual void HandleProgressCallback(const T *data, size_t count) = 0;
131 virtual void Destroy();
135 <a name="api_nan_async_queue_worker"></a>
136 ### Nan::AsyncQueueWorker
138 `Nan::AsyncQueueWorker` will run a `Nan::AsyncWorker` asynchronously via libuv. Both the `execute` and `after_work` steps are taken care of for you. Most of the logic for this is embedded in `Nan::AsyncWorker`.
143 void AsyncQueueWorker(AsyncWorker *);
146 [AsyncResource]: node_misc.md#api_nan_asyncresource