forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_progress_queue_worker.cc
More file actions
83 lines (66 loc) · 2.04 KB
/
async_progress_queue_worker.cc
File metadata and controls
83 lines (66 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "napi.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#if (NAPI_VERSION > 3)
using namespace Napi;
namespace {
struct ProgressData {
int32_t progress;
};
class TestWorker : public AsyncProgressQueueWorker<ProgressData> {
public:
static Napi::Value CreateWork(const CallbackInfo& info) {
int32_t times = info[0].As<Number>().Int32Value();
Function cb = info[1].As<Function>();
Function progress = info[2].As<Function>();
TestWorker* worker = new TestWorker(
cb, progress, "TestResource", Object::New(info.Env()), times);
return Napi::External<TestWorker>::New(info.Env(), worker);
}
static void QueueWork(const CallbackInfo& info) {
auto wrap = info[0].As<Napi::External<TestWorker>>();
auto worker = wrap.Data();
worker->Queue();
}
protected:
void Execute(const ExecutionProgress& progress) override {
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
if (_times < 0) {
SetError("test error");
}
ProgressData data{0};
for (int32_t idx = 0; idx < _times; idx++) {
data.progress = idx;
progress.Send(&data, 1);
}
}
void OnProgress(const ProgressData* data, size_t /* count */) override {
Napi::Env env = Env();
if (!_js_progress_cb.IsEmpty()) {
Number progress = Number::New(env, data->progress);
_js_progress_cb.Call(Receiver().Value(), {progress});
}
}
private:
TestWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource,
int32_t times)
: AsyncProgressQueueWorker(cb, resource_name, resource), _times(times) {
_js_progress_cb.Reset(progress, 1);
}
int32_t _times;
FunctionReference _js_progress_cb;
};
} // namespace
Object InitAsyncProgressQueueWorker(Env env) {
Object exports = Object::New(env);
exports["createWork"] = Function::New(env, TestWorker::CreateWork);
exports["queueWork"] = Function::New(env, TestWorker::QueueWork);
return exports;
}
#endif