@@ -15,6 +15,158 @@ struct ProgressData {
1515 int32_t progress;
1616};
1717
18+ class TestWorkerWithNoCb : public AsyncProgressQueueWorker <ProgressData> {
19+ public:
20+ static void DoWork (const CallbackInfo& info) {
21+ switch (info.Length ()) {
22+ case 1 : {
23+ Function cb = info[0 ].As <Function>();
24+ TestWorkerWithNoCb* worker = new TestWorkerWithNoCb (info.Env (), cb);
25+ worker->Queue ();
26+ } break ;
27+
28+ case 2 : {
29+ std::string resName = info[0 ].As <String>();
30+ Function cb = info[1 ].As <Function>();
31+ TestWorkerWithNoCb* worker =
32+ new TestWorkerWithNoCb (info.Env (), resName.c_str (), cb);
33+ worker->Queue ();
34+ } break ;
35+
36+ case 3 : {
37+ std::string resName = info[0 ].As <String>();
38+ Object resObject = info[1 ].As <Object>();
39+ Function cb = info[2 ].As <Function>();
40+ TestWorkerWithNoCb* worker =
41+ new TestWorkerWithNoCb (info.Env (), resName.c_str (), resObject, cb);
42+ worker->Queue ();
43+ } break ;
44+
45+ default :
46+
47+ break ;
48+ }
49+ }
50+
51+ protected:
52+ void Execute (const ExecutionProgress& progress) override {
53+ ProgressData data{1 };
54+ progress.Send (&data, 1 );
55+ }
56+
57+ void OnProgress (const ProgressData*, size_t /* count */ ) override {
58+ _cb.Call ({});
59+ }
60+
61+ private:
62+ TestWorkerWithNoCb (Napi::Env env, Function cb)
63+ : AsyncProgressQueueWorker(env) {
64+ _cb.Reset (cb, 1 );
65+ }
66+ TestWorkerWithNoCb (Napi::Env env, const char * resourceName, Function cb)
67+ : AsyncProgressQueueWorker(env, resourceName) {
68+ _cb.Reset (cb, 1 );
69+ }
70+ TestWorkerWithNoCb (Napi::Env env,
71+ const char * resourceName,
72+ const Object& resourceObject,
73+ Function cb)
74+ : AsyncProgressQueueWorker(env, resourceName, resourceObject) {
75+ _cb.Reset (cb, 1 );
76+ }
77+ FunctionReference _cb;
78+ };
79+
80+ class TestWorkerWithRecv : public AsyncProgressQueueWorker <ProgressData> {
81+ public:
82+ static void DoWork (const CallbackInfo& info) {
83+ switch (info.Length ()) {
84+ case 2 : {
85+ Object recv = info[0 ].As <Object>();
86+ Function cb = info[1 ].As <Function>();
87+ TestWorkerWithRecv* worker = new TestWorkerWithRecv (recv, cb);
88+ worker->Queue ();
89+ } break ;
90+
91+ case 3 : {
92+ Object recv = info[0 ].As <Object>();
93+ Function cb = info[1 ].As <Function>();
94+ std::string resName = info[2 ].As <String>();
95+ TestWorkerWithRecv* worker =
96+ new TestWorkerWithRecv (recv, cb, resName.c_str ());
97+ worker->Queue ();
98+ } break ;
99+
100+ case 4 : {
101+ Object recv = info[0 ].As <Object>();
102+ Function cb = info[1 ].As <Function>();
103+ std::string resName = info[2 ].As <String>();
104+ Object resObject = info[3 ].As <Object>();
105+ TestWorkerWithRecv* worker =
106+ new TestWorkerWithRecv (recv, cb, resName.c_str (), resObject);
107+ worker->Queue ();
108+ } break ;
109+
110+ default :
111+
112+ break ;
113+ }
114+ }
115+
116+ protected:
117+ void Execute (const ExecutionProgress&) override {}
118+
119+ void OnProgress (const ProgressData*, size_t /* count */ ) override {}
120+
121+ private:
122+ TestWorkerWithRecv (const Object& recv, const Function& cb)
123+ : AsyncProgressQueueWorker(recv, cb) {}
124+ TestWorkerWithRecv (const Object& recv,
125+ const Function& cb,
126+ const char * resourceName)
127+ : AsyncProgressQueueWorker(recv, cb, resourceName) {}
128+ TestWorkerWithRecv (const Object& recv,
129+ const Function& cb,
130+ const char * resourceName,
131+ const Object& resourceObject)
132+ : AsyncProgressQueueWorker(recv, cb, resourceName, resourceObject) {}
133+ };
134+
135+ class TestWorkerWithCb : public AsyncProgressQueueWorker <ProgressData> {
136+ public:
137+ static void DoWork (const CallbackInfo& info) {
138+ switch (info.Length ()) {
139+ case 1 : {
140+ Function cb = info[0 ].As <Function>();
141+ TestWorkerWithCb* worker = new TestWorkerWithCb (cb);
142+ worker->Queue ();
143+ } break ;
144+
145+ case 2 : {
146+ Function cb = info[0 ].As <Function>();
147+ std::string asyncResName = info[1 ].As <String>();
148+ TestWorkerWithCb* worker =
149+ new TestWorkerWithCb (cb, asyncResName.c_str ());
150+ worker->Queue ();
151+ } break ;
152+
153+ default :
154+
155+ break ;
156+ }
157+ }
158+
159+ protected:
160+ void Execute (const ExecutionProgress&) override {}
161+
162+ void OnProgress (const ProgressData*, size_t /* count */ ) override {}
163+
164+ private:
165+ TestWorkerWithCb (Function cb) : AsyncProgressQueueWorker(cb) {}
166+ TestWorkerWithCb (Function cb, const char * res_name)
167+ : AsyncProgressQueueWorker(cb, res_name) {}
168+ };
169+
18170class TestWorker : public AsyncProgressQueueWorker <ProgressData> {
19171 public:
20172 static Napi::Value CreateWork (const CallbackInfo& info) {
@@ -87,6 +239,9 @@ Object InitAsyncProgressQueueWorker(Env env) {
87239 Object exports = Object::New (env);
88240 exports[" createWork" ] = Function::New (env, TestWorker::CreateWork);
89241 exports[" queueWork" ] = Function::New (env, TestWorker::QueueWork);
242+ exports[" runWorkerNoCb" ] = Function::New (env, TestWorkerWithNoCb::DoWork);
243+ exports[" runWorkerWithRecv" ] = Function::New (env, TestWorkerWithRecv::DoWork);
244+ exports[" runWorkerWithCb" ] = Function::New (env, TestWorkerWithCb::DoWork);
90245 return exports;
91246}
92247
0 commit comments