k2hash 1.1.34 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/k2h_queue.h CHANGED
@@ -27,47 +27,55 @@
27
27
  //---------------------------------------------------------
28
28
  // K2hQueue Class
29
29
  //---------------------------------------------------------
30
- class K2hQueue : public Nan::ObjectWrap
30
+ class K2hQueue : public Napi::ObjectWrap<K2hQueue>
31
31
  {
32
32
  friend class K2hNode;
33
33
 
34
34
  public:
35
- static void Init(void);
36
- static NAN_METHOD(NewInstance);
37
- static v8::Local<v8::Object> GetInstance(Nan::NAN_METHOD_ARGS_TYPE info);
35
+ static void Init(Napi::Env env, Napi::Object exports);
36
+ static Napi::Object NewInstance(Napi::Env env);
37
+ static Napi::Object NewInstance(Napi::Env env, const Napi::Value& arg);
38
38
 
39
- private:
40
- explicit K2hQueue();
39
+ static Napi::Object GetInstance(const Napi::CallbackInfo& info);
40
+
41
+ // Constructor / Destructor
42
+ explicit K2hQueue(const Napi::CallbackInfo& info);
41
43
  ~K2hQueue();
42
44
 
43
- static NAN_METHOD(New);
45
+ private:
46
+ Napi::Value New(const Napi::CallbackInfo& info);
47
+
48
+ Napi::Value On(const Napi::CallbackInfo& info);
49
+ Napi::Value OnPush(const Napi::CallbackInfo& info);
50
+ Napi::Value OnPop(const Napi::CallbackInfo& info);
51
+ Napi::Value OnCount(const Napi::CallbackInfo& info);
52
+ Napi::Value OnRead(const Napi::CallbackInfo& info);
53
+ Napi::Value OnRemove(const Napi::CallbackInfo& info);
44
54
 
45
- static NAN_METHOD(On);
46
- static NAN_METHOD(OnPush);
47
- static NAN_METHOD(OnPop);
48
- static NAN_METHOD(OnCount);
49
- static NAN_METHOD(OnRead);
50
- static NAN_METHOD(OnRemove);
51
- static NAN_METHOD(Off);
52
- static NAN_METHOD(OffPush);
53
- static NAN_METHOD(OffPop);
54
- static NAN_METHOD(OffCount);
55
- static NAN_METHOD(OffRead);
56
- static NAN_METHOD(OffRemove);
55
+ Napi::Value Off(const Napi::CallbackInfo& info);
56
+ Napi::Value OffPush(const Napi::CallbackInfo& info);
57
+ Napi::Value OffPop(const Napi::CallbackInfo& info);
58
+ Napi::Value OffCount(const Napi::CallbackInfo& info);
59
+ Napi::Value OffRead(const Napi::CallbackInfo& info);
60
+ Napi::Value OffRemove(const Napi::CallbackInfo& info);
61
+
62
+ Napi::Value Initialize(const Napi::CallbackInfo& info);
63
+ Napi::Value IsEmpty(const Napi::CallbackInfo& info);
64
+ Napi::Value Count(const Napi::CallbackInfo& info);
65
+ Napi::Value Read(const Napi::CallbackInfo& info);
66
+ Napi::Value Push(const Napi::CallbackInfo& info);
67
+ Napi::Value Pop(const Napi::CallbackInfo& info);
68
+ Napi::Value Remove(const Napi::CallbackInfo& info);
69
+ Napi::Value Dump(const Napi::CallbackInfo& info);
70
+
71
+ public:
72
+ // constructor reference
73
+ static Napi::FunctionReference constructor;
57
74
 
58
- static NAN_METHOD(Init);
59
- static NAN_METHOD(IsEmpty);
60
- static NAN_METHOD(Count);
61
- static NAN_METHOD(Read);
62
- static NAN_METHOD(Push);
63
- static NAN_METHOD(Pop);
64
- static NAN_METHOD(Remove);
65
- static NAN_METHOD(Dump);
75
+ StackEmitCB _cbs;
66
76
 
67
77
  private:
68
- static Nan::Persistent<v8::Function> constructor;
69
- K2HQueue _k2hqueue;
70
- StackEmitCB _cbs;
78
+ K2HQueue _k2hqueue;
71
79
  };
72
80
 
73
81
  #endif
@@ -34,69 +34,76 @@
34
34
  // Callback function: function(string error)
35
35
  //
36
36
  //---------------------------------------------------------
37
- class K2hqPushWorker : public Nan::AsyncWorker
37
+ class K2hqPushAsyncWorker : public Napi::AsyncWorker
38
38
  {
39
39
  public:
40
- K2hqPushWorker(Nan::Callback* callback, K2HQueue* pobj, const char* pdata, const char* ppass, const time_t* pexpire) : Nan::AsyncWorker(callback), pk2hqobj(pobj), is_data_set(false), strdata(pdata ? pdata : ""), is_pass_set(false), strpass(ppass ? ppass : ""), expire(pexpire ? *pexpire : 0)
40
+ K2hqPushAsyncWorker(const Napi::Function& callback, K2HQueue* pobj, const char* pdata, const char* ppass, const time_t* pexpire) :
41
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hqobj(pobj), _is_data_set(nullptr != pdata), _strdata(pdata ? pdata : ""), _is_pass_set(nullptr != ppass), _strpass(ppass ? ppass : ""), _expire(pexpire ? *pexpire : 0)
41
42
  {
42
- is_data_set = (NULL != pdata);
43
- is_pass_set = (NULL != ppass);
43
+ _callbackRef.Ref();
44
44
  }
45
- ~K2hqPushWorker() {}
46
45
 
47
- void Execute()
46
+ ~K2hqPushAsyncWorker()
48
47
  {
49
- if(!pk2hqobj){
50
- Nan::ReferenceError("No object is associated to async worker");
48
+ if(_callbackRef){
49
+ _callbackRef.Unref();
50
+ _callbackRef.Reset();
51
+ }
52
+ }
53
+
54
+ // Runs on worker thread
55
+ void Execute() override
56
+ {
57
+ if(!_pk2hqobj){
58
+ SetError("No object is associated to async worker");
51
59
  return;
52
60
  }
53
- if(!is_data_set){
54
- Nan::ReferenceError("Specified data is empty(null)");
61
+ if(!_is_data_set){
62
+ SetError("Specified data is empty(null)");
55
63
  return;
56
64
  }
57
65
 
58
66
  // push
59
- if(!pk2hqobj->Push(reinterpret_cast<const unsigned char*>(strdata.c_str()), (strdata.length() + 1), NULL, (is_pass_set ? strpass.c_str() : NULL), (expire > 0 ? &expire : NULL))){
60
- // set error
61
- this->SetErrorMessage("Failed to push data for k2hqueue.");
67
+ if(!_pk2hqobj->Push(reinterpret_cast<const unsigned char*>(_strdata.c_str()), (_strdata.length() + 1), NULL, (_is_pass_set ? _strpass.c_str() : NULL), (_expire > 0 ? &_expire : NULL))){
68
+ SetError("Failed to push data for k2hqueue.");
69
+ return;
62
70
  }
63
71
  }
64
72
 
65
- void HandleOKCallback()
73
+ // handler for success
74
+ void OnOK() override
66
75
  {
67
- Nan::HandleScope scope;
68
- const int argc = 1;
69
- v8::Local<v8::Value> argv[argc] = { Nan::Null() };
76
+ Napi::Env env = Env();
77
+ Napi::HandleScope scope(env);
70
78
 
71
- if(callback){
72
- callback->Call(argc, argv);
79
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
80
+ _callbackRef.Call({ env.Null() });
73
81
  }else{
74
- Nan::ThrowSyntaxError("Internal error in async worker");
75
- return;
82
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
76
83
  }
77
84
  }
78
85
 
79
- void HandleErrorCallback()
80
- {
81
- Nan::HandleScope scope;
82
- const int argc = 1;
83
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
86
+ // handler for failure (by calling SetError)
87
+ void OnError(const Napi::Error& err) override
88
+ {
89
+ Napi::Env env = Env();
90
+ Napi::HandleScope scope(env);
84
91
 
85
- if(callback){
86
- callback->Call(argc, argv);
92
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
93
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
87
94
  }else{
88
- Nan::ThrowSyntaxError("Internal error in async worker");
89
- return;
95
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
90
96
  }
91
- }
97
+ }
92
98
 
93
99
  private:
94
- K2HQueue* pk2hqobj;
95
- bool is_data_set;
96
- std::string strdata;
97
- bool is_pass_set;
98
- std::string strpass;
99
- time_t expire;
100
+ Napi::FunctionReference _callbackRef;
101
+ K2HQueue* _pk2hqobj;
102
+ bool _is_data_set;
103
+ std::string _strdata;
104
+ bool _is_pass_set;
105
+ std::string _strpass;
106
+ time_t _expire;
100
107
  };
101
108
 
102
109
  //---------------------------------------------------------
@@ -106,59 +113,66 @@ class K2hqPushWorker : public Nan::AsyncWorker
106
113
  // Callback function: function(string error, int count)
107
114
  //
108
115
  //---------------------------------------------------------
109
- class K2hqCountWorker : public Nan::AsyncWorker
116
+ class K2hqCountAsyncWorker : public Napi::AsyncWorker
110
117
  {
111
118
  public:
112
- K2hqCountWorker(Nan::Callback* callback, K2HQueue* pobj) : Nan::AsyncWorker(callback), pk2hqobj(pobj), rescount(0) {}
113
- ~K2hqCountWorker() {}
119
+ K2hqCountAsyncWorker(const Napi::Function& callback, K2HQueue* pobj) :
120
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hqobj(pobj), _rescount(0)
121
+ {
122
+ _callbackRef.Ref();
123
+ }
124
+
125
+ ~K2hqCountAsyncWorker()
126
+ {
127
+ if(_callbackRef){
128
+ _callbackRef.Unref();
129
+ _callbackRef.Reset();
130
+ }
131
+ }
114
132
 
115
- void Execute()
133
+ // Runs on worker thread
134
+ void Execute() override
116
135
  {
117
- if(!pk2hqobj){
118
- Nan::ReferenceError("No object is associated to async worker");
136
+ if(NULL == _pk2hqobj){
137
+ // Report an error so OnError will be invoked.
138
+ SetError("No object is associated to async worker");
119
139
  return;
120
140
  }
121
141
 
122
142
  // count
123
- rescount = pk2hqobj->GetCount();
124
-
125
- // [NOTE]
126
- // This execution does not return error at any case.
143
+ _rescount = static_cast<int>(_pk2hqobj->GetCount());
127
144
  }
128
145
 
129
- void HandleOKCallback()
146
+ // handler for success
147
+ void OnOK() override
130
148
  {
131
- Nan::HandleScope scope;
132
- const int argc = 2;
133
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(rescount) };
149
+ Napi::Env env = Env();
150
+ Napi::HandleScope scope(env);
134
151
 
135
- if(callback){
136
- callback->Call(argc, argv);
152
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
153
+ _callbackRef.Call({ env.Null(), Napi::Number::New(env, _rescount) });
137
154
  }else{
138
- Nan::ThrowSyntaxError("Internal error in async worker");
139
- return;
155
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
140
156
  }
141
157
  }
142
158
 
143
- void HandleErrorCallback()
144
- {
145
- // This method does not call anytime.
146
- //
147
- Nan::HandleScope scope;
148
- const int argc = 1;
149
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
159
+ // handler for failure (by calling SetError)
160
+ void OnError(const Napi::Error& err) override
161
+ {
162
+ Napi::Env env = Env();
163
+ Napi::HandleScope scope(env);
150
164
 
151
- if(callback){
152
- callback->Call(argc, argv);
165
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
166
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
153
167
  }else{
154
- Nan::ThrowSyntaxError("Internal error in async worker");
155
- return;
168
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
156
169
  }
157
- }
170
+ }
158
171
 
159
172
  private:
160
- K2HQueue* pk2hqobj;
161
- int rescount;
173
+ Napi::FunctionReference _callbackRef;
174
+ K2HQueue* _pk2hqobj;
175
+ int _rescount;
162
176
  };
163
177
 
164
178
  //---------------------------------------------------------
@@ -168,71 +182,81 @@ class K2hqCountWorker : public Nan::AsyncWorker
168
182
  // Callback function: function(string error[, string data])
169
183
  //
170
184
  //---------------------------------------------------------
171
- class K2hqReadWorker : public Nan::AsyncWorker
185
+ class K2hqReadAsyncWorker : public Napi::AsyncWorker
172
186
  {
173
187
  public:
174
- K2hqReadWorker(Nan::Callback* callback, K2HQueue* pobj, int pos, const char* ppass) : Nan::AsyncWorker(callback), pk2hqobj(pobj), getpos(pos), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), resdata("") {}
175
- ~K2hqReadWorker() {}
188
+ K2hqReadAsyncWorker(const Napi::Function& callback, K2HQueue* pobj, int pos, const char* ppass) :
189
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hqobj(pobj), _getpos(pos), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _resdata()
190
+ {
191
+ _callbackRef.Ref();
192
+ }
193
+
194
+ ~K2hqReadAsyncWorker()
195
+ {
196
+ if(_callbackRef){
197
+ _callbackRef.Unref();
198
+ _callbackRef.Reset();
199
+ }
200
+ }
176
201
 
177
- void Execute()
202
+ // Runs on worker thread
203
+ void Execute() override
178
204
  {
179
- if(!pk2hqobj){
180
- Nan::ReferenceError("No object is associated to async worker");
205
+ if(NULL == _pk2hqobj){
206
+ SetError("No object is associated to async worker");
181
207
  return;
182
208
  }
183
- if(0 > getpos){
184
- Nan::ReferenceError("Specified position is under zero.");
209
+ if(_getpos < 0){
210
+ SetError("Specified position is under zero.");
185
211
  return;
186
212
  }
187
213
 
188
214
  // read
189
215
  unsigned char* pdata = NULL;
190
216
  size_t datalen = 0;
191
- bool result = pk2hqobj->Read(&pdata, datalen, getpos, (is_pass_set ? strpass.c_str() : NULL));
217
+ bool result = _pk2hqobj->Read(&pdata, datalen, _getpos, (_is_pass_set ? _strpass.c_str() : NULL));
192
218
  if(!result || !pdata || 0 == datalen){
193
- // set error
194
- this->SetErrorMessage("Failed to read data for k2hqueue.");
219
+ SetError("Failed to read data for k2hqueue.");
195
220
  }else{
196
- // copy result
197
- resdata = std::string(reinterpret_cast<const char*>(pdata), datalen);
221
+ _resdata.assign(reinterpret_cast<const char*>(pdata), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pdata))));
198
222
  }
199
223
  K2H_Free(pdata);
200
224
  }
201
225
 
202
- void HandleOKCallback()
226
+ // handler for success
227
+ void OnOK() override
203
228
  {
204
- Nan::HandleScope scope;
205
- const int argc = 2;
206
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(resdata.c_str()).ToLocalChecked() };
229
+ Napi::Env env = Env();
230
+ Napi::HandleScope scope(env);
207
231
 
208
- if(callback){
209
- callback->Call(argc, argv);
232
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
233
+ Napi::String val = Napi::String::New(env, _resdata.c_str(), static_cast<size_t>(strlen(_resdata.c_str())));
234
+ _callbackRef.Call({ env.Null(), val });
210
235
  }else{
211
- Nan::ThrowSyntaxError("Internal error in async worker");
212
- return;
236
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
213
237
  }
214
238
  }
215
239
 
216
- void HandleErrorCallback()
217
- {
218
- Nan::HandleScope scope;
219
- const int argc = 1;
220
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
240
+ // handler for failure (by calling SetError)
241
+ void OnError(const Napi::Error& err) override
242
+ {
243
+ Napi::Env env = Env();
244
+ Napi::HandleScope scope(env);
221
245
 
222
- if(callback){
223
- callback->Call(argc, argv);
246
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
247
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
224
248
  }else{
225
- Nan::ThrowSyntaxError("Internal error in async worker");
226
- return;
249
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
227
250
  }
228
- }
251
+ }
229
252
 
230
253
  private:
231
- K2HQueue* pk2hqobj;
232
- int getpos;
233
- bool is_pass_set;
234
- std::string strpass;
235
- std::string resdata;
254
+ Napi::FunctionReference _callbackRef;
255
+ K2HQueue* _pk2hqobj;
256
+ int _getpos;
257
+ bool _is_pass_set;
258
+ std::string _strpass;
259
+ std::string _resdata;
236
260
  };
237
261
 
238
262
  //---------------------------------------------------------
@@ -242,66 +266,76 @@ class K2hqReadWorker : public Nan::AsyncWorker
242
266
  // Callback function: function(string error[, string data])
243
267
  //
244
268
  //---------------------------------------------------------
245
- class K2hqPopWorker : public Nan::AsyncWorker
269
+ class K2hqPopAsyncWorker : public Napi::AsyncWorker
246
270
  {
247
271
  public:
248
- K2hqPopWorker(Nan::Callback* callback, K2HQueue* pobj, const char* ppass) : Nan::AsyncWorker(callback), pk2hqobj(pobj), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), resdata("") {}
249
- ~K2hqPopWorker() {}
272
+ K2hqPopAsyncWorker(const Napi::Function& callback, K2HQueue* pobj, const char* ppass) :
273
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hqobj(pobj), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _resdata()
274
+ {
275
+ _callbackRef.Ref();
276
+ }
250
277
 
251
- void Execute()
278
+ ~K2hqPopAsyncWorker()
252
279
  {
253
- if(!pk2hqobj){
254
- Nan::ReferenceError("No object is associated to async worker");
280
+ if(_callbackRef){
281
+ _callbackRef.Unref();
282
+ _callbackRef.Reset();
283
+ }
284
+ }
285
+
286
+ // Runs on worker thread
287
+ void Execute() override
288
+ {
289
+ if(!_pk2hqobj){
290
+ SetError("No object is associated to async worker");
255
291
  return;
256
292
  }
257
293
 
258
294
  // pop
259
295
  unsigned char* pdata = NULL;
260
296
  size_t datalen = 0;
261
- bool result = pk2hqobj->Pop(&pdata, datalen, NULL, (is_pass_set ? strpass.c_str() : NULL));
297
+ bool result = _pk2hqobj->Pop(&pdata, datalen, NULL, (_is_pass_set ? _strpass.c_str() : NULL));
262
298
  if(!result || !pdata || 0 == datalen){
263
- // set error
264
- this->SetErrorMessage("Failed to pop data for k2hqueue.");
299
+ SetError("Failed to pop data for k2hqueue.");
265
300
  }else{
266
- // copy result
267
- resdata = std::string(reinterpret_cast<const char*>(pdata), datalen);
301
+ _resdata.assign(reinterpret_cast<const char*>(pdata), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pdata))));
268
302
  }
269
303
  K2H_Free(pdata);
270
304
  }
271
305
 
272
- void HandleOKCallback()
306
+ // handler for success
307
+ void OnOK() override
273
308
  {
274
- Nan::HandleScope scope;
275
- const int argc = 2;
276
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(resdata.c_str()).ToLocalChecked() };
309
+ Napi::Env env = Env();
310
+ Napi::HandleScope scope(env);
277
311
 
278
- if(callback){
279
- callback->Call(argc, argv);
312
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
313
+ Napi::String val = Napi::String::New(env, _resdata.c_str(), static_cast<size_t>(strlen(_resdata.c_str())));
314
+ _callbackRef.Call({ env.Null(), val });
280
315
  }else{
281
- Nan::ThrowSyntaxError("Internal error in async worker");
282
- return;
316
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
283
317
  }
284
318
  }
285
319
 
286
- void HandleErrorCallback()
287
- {
288
- Nan::HandleScope scope;
289
- const int argc = 1;
290
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
320
+ // handler for failure (by calling SetError)
321
+ void OnError(const Napi::Error& err) override
322
+ {
323
+ Napi::Env env = Env();
324
+ Napi::HandleScope scope(env);
291
325
 
292
- if(callback){
293
- callback->Call(argc, argv);
326
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
327
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
294
328
  }else{
295
- Nan::ThrowSyntaxError("Internal error in async worker");
296
- return;
329
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
297
330
  }
298
- }
331
+ }
299
332
 
300
333
  private:
301
- K2HQueue* pk2hqobj;
302
- bool is_pass_set;
303
- std::string strpass;
304
- std::string resdata;
334
+ Napi::FunctionReference _callbackRef;
335
+ K2HQueue* _pk2hqobj;
336
+ bool _is_pass_set;
337
+ std::string _strpass;
338
+ std::string _resdata;
305
339
  };
306
340
 
307
341
  //---------------------------------------------------------
@@ -311,65 +345,76 @@ class K2hqPopWorker : public Nan::AsyncWorker
311
345
  // Callback function: function(string error[, int removed_queue_count])
312
346
  //
313
347
  //---------------------------------------------------------
314
- class K2hqRemoveWorker : public Nan::AsyncWorker
348
+ class K2hqRemoveAsyncWorker : public Napi::AsyncWorker
315
349
  {
316
350
  public:
317
- K2hqRemoveWorker(Nan::Callback* callback, K2HQueue* pobj, int count, const char* ppass) : Nan::AsyncWorker(callback), pk2hqobj(pobj), rmcount(count), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), removed_count(0) {}
318
- ~K2hqRemoveWorker() {}
351
+ K2hqRemoveAsyncWorker(const Napi::Function& callback, K2HQueue* pobj, int count, const char* ppass) :
352
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hqobj(pobj), _rmcount(count), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _removed_count(0)
353
+ {
354
+ _callbackRef.Ref();
355
+ }
356
+
357
+ ~K2hqRemoveAsyncWorker()
358
+ {
359
+ if(_callbackRef){
360
+ _callbackRef.Unref();
361
+ _callbackRef.Reset();
362
+ }
363
+ }
319
364
 
320
- void Execute()
365
+ // Runs on worker thread
366
+ void Execute() override
321
367
  {
322
- if(!pk2hqobj){
323
- Nan::ReferenceError("No object is associated to async worker");
368
+ if(!_pk2hqobj){
369
+ SetError("No object is associated to async worker");
324
370
  return;
325
371
  }
326
- if(0 >= rmcount){
327
- Nan::ReferenceError("Specified remove count is under 1.");
372
+ if(_rmcount <= 0){
373
+ SetError("Specified remove count is under 1.");
328
374
  return;
329
375
  }
330
376
 
331
377
  // remove
332
- removed_count = pk2hqobj->Remove(rmcount, NULL, NULL, (is_pass_set ? strpass.c_str() : NULL));
333
- if(-1 == removed_count){
334
- // set error
335
- this->SetErrorMessage("Failed to remove queue for k2hqueue.");
378
+ _removed_count = _pk2hqobj->Remove(_rmcount, NULL, NULL, (_is_pass_set ? _strpass.c_str() : NULL));
379
+ if(-1 == _removed_count){
380
+ SetError("Failed to remove queue for k2hqueue.");
381
+ return;
336
382
  }
337
383
  }
338
384
 
339
- void HandleOKCallback()
385
+ // handler for success
386
+ void OnOK() override
340
387
  {
341
- Nan::HandleScope scope;
342
- const int argc = 2;
343
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(removed_count) };
388
+ Napi::Env env = Env();
389
+ Napi::HandleScope scope(env);
344
390
 
345
- if(callback){
346
- callback->Call(argc, argv);
391
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
392
+ _callbackRef.Call({ env.Null(), Napi::Number::New(env, static_cast<double>(_removed_count)) });
347
393
  }else{
348
- Nan::ThrowSyntaxError("Internal error in async worker");
349
- return;
394
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
350
395
  }
351
396
  }
352
397
 
353
- void HandleErrorCallback()
354
- {
355
- Nan::HandleScope scope;
356
- const int argc = 1;
357
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
398
+ // handler for failure (by calling SetError)
399
+ void OnError(const Napi::Error& err) override
400
+ {
401
+ Napi::Env env = Env();
402
+ Napi::HandleScope scope(env);
358
403
 
359
- if(callback){
360
- callback->Call(argc, argv);
404
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
405
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
361
406
  }else{
362
- Nan::ThrowSyntaxError("Internal error in async worker");
363
- return;
407
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
364
408
  }
365
- }
409
+ }
366
410
 
367
411
  private:
368
- K2HQueue* pk2hqobj;
369
- int rmcount;
370
- bool is_pass_set;
371
- std::string strpass;
372
- int removed_count;
412
+ Napi::FunctionReference _callbackRef;
413
+ K2HQueue* _pk2hqobj;
414
+ int _rmcount;
415
+ bool _is_pass_set;
416
+ std::string _strpass;
417
+ int _removed_count;
373
418
  };
374
419
 
375
420
  #endif