k2hash 1.1.34 → 2.0.0

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.
@@ -27,47 +27,55 @@
27
27
  //---------------------------------------------------------
28
28
  // K2hKeyQueue Class
29
29
  //---------------------------------------------------------
30
- class K2hKeyQueue : public Nan::ObjectWrap
30
+ class K2hKeyQueue : public Napi::ObjectWrap<K2hKeyQueue>
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 K2hKeyQueue();
39
+ static Napi::Object GetInstance(const Napi::CallbackInfo& info);
40
+
41
+ // Constructor / Destructor
42
+ explicit K2hKeyQueue(const Napi::CallbackInfo& info);
41
43
  ~K2hKeyQueue();
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
- K2HKeyQueue _k2hkeyqueue;
70
- StackEmitCB _cbs;
78
+ K2HKeyQueue _k2hkeyqueue;
71
79
  };
72
80
 
73
81
  #endif
@@ -34,74 +34,79 @@
34
34
  // Callback function: function(string error)
35
35
  //
36
36
  //---------------------------------------------------------
37
- class K2hkqPushWorker : public Nan::AsyncWorker
37
+ class K2hkqPushAsyncWorker : public Napi::AsyncWorker
38
38
  {
39
39
  public:
40
- K2hkqPushWorker(Nan::Callback* callback, K2HKeyQueue* pobj, const char* pkey, const char* pval, const char* ppass, const time_t* pexpire) :
41
- Nan::AsyncWorker(callback), pk2hkqobj(pobj),
42
- is_key_set(false), strkey(pkey ? pkey : ""), is_val_set(false), strval(pval ? pval : ""), is_pass_set(false), strpass(ppass ? ppass : ""), expire(pexpire ? *pexpire : 0)
40
+ K2hkqPushAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, const char* pkey, const char* pval, const char* ppass, const time_t* pexpire) :
41
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj),
42
+ _is_key_set(NULL != pkey), _strkey(pkey ? pkey : ""), _is_val_set(NULL != pval), _strval(pval ? pval : ""), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _expire(pexpire ? *pexpire : 0)
43
43
  {
44
- is_key_set = (NULL != pkey);
45
- is_val_set = (NULL != pval);
46
- is_pass_set = (NULL != ppass);
44
+ _callbackRef.Ref();
47
45
  }
48
- ~K2hkqPushWorker() {}
49
46
 
50
- void Execute()
47
+ ~K2hkqPushAsyncWorker()
51
48
  {
52
- if(!pk2hkqobj){
53
- Nan::ReferenceError("No object is associated to async worker");
49
+ if(_callbackRef){
50
+ _callbackRef.Unref();
51
+ _callbackRef.Reset();
52
+ }
53
+ }
54
+
55
+ // Runs on worker thread
56
+ void Execute() override
57
+ {
58
+ if(!_pk2hkqobj){
59
+ SetError("No object is associated to async worker");
54
60
  return;
55
61
  }
56
- if(!is_key_set || !is_val_set){
57
- Nan::ReferenceError("Specified key or value is empty(null)");
62
+ if(!_is_key_set || !_is_val_set){
63
+ SetError("Specified key or value is empty(null)");
58
64
  return;
59
65
  }
60
66
 
61
67
  // push
62
- if(!pk2hkqobj->Push(reinterpret_cast<const unsigned char*>(strkey.c_str()), (strkey.length() + 1), reinterpret_cast<const unsigned char*>(strval.c_str()), (strval.length() + 1), (is_pass_set ? strpass.c_str() : NULL), (expire > 0 ? &expire : NULL))){
63
- // set error
64
- this->SetErrorMessage("Failed to push data for k2hkeyqueue.");
68
+ if(!_pk2hkqobj->Push(reinterpret_cast<const unsigned char*>(_strkey.c_str()), (_strkey.length() + 1), reinterpret_cast<const unsigned char*>(_strval.c_str()), (_strval.length() + 1), (_is_pass_set ? _strpass.c_str() : NULL), (_expire > 0 ? &_expire : NULL))){
69
+ SetError("Failed to push data for k2hkeyqueue.");
70
+ return;
65
71
  }
66
72
  }
67
73
 
68
- void HandleOKCallback()
74
+ // handler for success
75
+ void OnOK() override
69
76
  {
70
- Nan::HandleScope scope;
71
- const int argc = 1;
72
- v8::Local<v8::Value> argv[argc] = { Nan::Null() };
77
+ Napi::Env env = Env();
78
+ Napi::HandleScope scope(env);
73
79
 
74
- if(callback){
75
- callback->Call(argc, argv);
80
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
81
+ _callbackRef.Call({ env.Null() });
76
82
  }else{
77
- Nan::ThrowSyntaxError("Internal error in async worker");
78
- return;
83
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
79
84
  }
80
85
  }
81
86
 
82
- void HandleErrorCallback()
83
- {
84
- Nan::HandleScope scope;
85
- const int argc = 1;
86
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
87
+ // handler for failure (by calling SetError)
88
+ void OnError(const Napi::Error& err) override
89
+ {
90
+ Napi::Env env = Env();
91
+ Napi::HandleScope scope(env);
87
92
 
88
- if(callback){
89
- callback->Call(argc, argv);
93
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
94
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
90
95
  }else{
91
- Nan::ThrowSyntaxError("Internal error in async worker");
92
- return;
96
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
93
97
  }
94
- }
98
+ }
95
99
 
96
100
  private:
97
- K2HKeyQueue* pk2hkqobj;
98
- bool is_key_set;
99
- std::string strkey;
100
- bool is_val_set;
101
- std::string strval;
102
- bool is_pass_set;
103
- std::string strpass;
104
- time_t expire;
101
+ Napi::FunctionReference _callbackRef;
102
+ K2HKeyQueue* _pk2hkqobj;
103
+ bool _is_key_set;
104
+ std::string _strkey;
105
+ bool _is_val_set;
106
+ std::string _strval;
107
+ bool _is_pass_set;
108
+ std::string _strpass;
109
+ time_t _expire;
105
110
  };
106
111
 
107
112
  //---------------------------------------------------------
@@ -111,59 +116,65 @@ class K2hkqPushWorker : public Nan::AsyncWorker
111
116
  // Callback function: function(string error[, int count])
112
117
  //
113
118
  //---------------------------------------------------------
114
- class K2hkqCountWorker : public Nan::AsyncWorker
119
+ class K2hkqCountAsyncWorker : public Napi::AsyncWorker
115
120
  {
116
121
  public:
117
- K2hkqCountWorker(Nan::Callback* callback, K2HKeyQueue* pobj) : Nan::AsyncWorker(callback), pk2hkqobj(pobj), rescount(0) {}
118
- ~K2hkqCountWorker() {}
122
+ K2hkqCountAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj) :
123
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _rescount(0)
124
+ {
125
+ _callbackRef.Ref();
126
+ }
127
+
128
+ ~K2hkqCountAsyncWorker()
129
+ {
130
+ if(_callbackRef){
131
+ _callbackRef.Unref();
132
+ _callbackRef.Reset();
133
+ }
134
+ }
119
135
 
120
- void Execute()
136
+ // Runs on worker thread
137
+ void Execute() override
121
138
  {
122
- if(!pk2hkqobj){
123
- Nan::ReferenceError("No object is associated to async worker");
139
+ if(!_pk2hkqobj){
140
+ SetError("No object is associated to async worker");
124
141
  return;
125
142
  }
126
143
 
127
144
  // count
128
- rescount = pk2hkqobj->GetCount();
129
-
130
- // [NOTE]
131
- // This execution does not return error at any case.
145
+ _rescount = static_cast<int>(_pk2hkqobj->GetCount());
132
146
  }
133
147
 
134
- void HandleOKCallback()
148
+ // handler for success
149
+ void OnOK() override
135
150
  {
136
- Nan::HandleScope scope;
137
- const int argc = 2;
138
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(rescount) };
151
+ Napi::Env env = Env();
152
+ Napi::HandleScope scope(env);
139
153
 
140
- if(callback){
141
- callback->Call(argc, argv);
154
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
155
+ _callbackRef.Call({ env.Null(), Napi::Number::New(env, _rescount) });
142
156
  }else{
143
- Nan::ThrowSyntaxError("Internal error in async worker");
144
- return;
157
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
145
158
  }
146
159
  }
147
160
 
148
- void HandleErrorCallback()
149
- {
150
- // This method does not call anytime.
151
- //
152
- Nan::HandleScope scope;
153
- const int argc = 1;
154
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
161
+ // handler for failure (by calling SetError)
162
+ void OnError(const Napi::Error& err) override
163
+ {
164
+ Napi::Env env = Env();
165
+ Napi::HandleScope scope(env);
155
166
 
156
- if(callback){
157
- callback->Call(argc, argv);
167
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
168
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
158
169
  }else{
159
- Nan::ThrowSyntaxError("Internal error in async worker");
160
- return;
170
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
161
171
  }
162
- }
172
+ }
163
173
 
164
174
  private:
165
- K2HKeyQueue* pk2hkqobj;
166
- int rescount;
175
+ Napi::FunctionReference _callbackRef;
176
+ K2HKeyQueue* _pk2hkqobj;
177
+ int _rescount;
167
178
  };
168
179
 
169
180
  //---------------------------------------------------------
@@ -173,78 +184,86 @@ class K2hkqCountWorker : public Nan::AsyncWorker
173
184
  // Callback function: function(string error[, string key, string value])
174
185
  //
175
186
  //---------------------------------------------------------
176
- class K2hkqReadWorker : public Nan::AsyncWorker
187
+ class K2hkqReadAsyncWorker : public Napi::AsyncWorker
177
188
  {
178
189
  public:
179
- K2hkqReadWorker(Nan::Callback* callback, K2HKeyQueue* pobj, int pos, const char* ppass) : Nan::AsyncWorker(callback), pk2hkqobj(pobj), getpos(pos), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), reskey(""), resval("") {}
180
- ~K2hkqReadWorker() {}
190
+ K2hkqReadAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, int pos, const char* ppass) :
191
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _getpos(pos), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _reskey(), _resval()
192
+ {
193
+ _callbackRef.Ref();
194
+ }
195
+
196
+ ~K2hkqReadAsyncWorker()
197
+ {
198
+ if(_callbackRef){
199
+ _callbackRef.Unref();
200
+ _callbackRef.Reset();
201
+ }
202
+ }
181
203
 
182
- void Execute()
204
+ // Runs on the worker thread
205
+ void Execute() override
183
206
  {
184
- if(!pk2hkqobj){
185
- Nan::ReferenceError("No object is associated to async worker");
207
+ if(!_pk2hkqobj){
208
+ SetError("No object is associated to async worker");
186
209
  return;
187
210
  }
188
- if(0 > getpos){
189
- Nan::ReferenceError("Specified position is under zero.");
211
+ if(0 > _getpos){
212
+ SetError("Specified position is under zero.");
190
213
  return;
191
214
  }
192
215
 
193
- // read
194
216
  unsigned char* pkey = NULL;
195
217
  unsigned char* pval = NULL;
196
218
  size_t keylen = 0;
197
219
  size_t vallen = 0;
198
- bool result = pk2hkqobj->Read(&pkey, keylen, &pval, vallen, getpos, (is_pass_set ? strpass.c_str() : NULL));
220
+ bool result = _pk2hkqobj->Read(&pkey, keylen, &pval, vallen, _getpos, (_is_pass_set ? _strpass.c_str() : NULL));
199
221
  if(!result || !pkey || 0 == keylen){
200
- // set error
201
- this->SetErrorMessage("Failed to read data for K2HKeyQueue.");
222
+ SetError("Failed to read data for K2HKeyQueue.");
202
223
  }else{
203
- // copy result
204
- reskey = std::string(reinterpret_cast<const char*>(pkey), keylen);
224
+ _reskey.assign(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))));
205
225
  if(pval && 0 < vallen){
206
- resval = std::string(reinterpret_cast<const char*>(pval), vallen);
226
+ _resval.assign(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))));
207
227
  }
208
228
  }
209
229
  K2H_Free(pkey);
210
230
  K2H_Free(pval);
211
231
  }
212
232
 
213
- void HandleOKCallback()
233
+ // handler for success
234
+ void OnOK() override
214
235
  {
215
- Nan::HandleScope scope;
216
- const int argc = 3;
217
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(reskey.c_str()).ToLocalChecked(), Nan::New<v8::String>(resval.c_str()).ToLocalChecked() };
236
+ Napi::Env env = Env();
237
+ Napi::HandleScope scope(env);
218
238
 
219
- if(callback){
220
- callback->Call(argc, argv);
239
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
240
+ _callbackRef.Call({ env.Null(), Napi::String::New(env, _reskey.c_str(), static_cast<size_t>(strlen(_reskey.c_str()))), Napi::String::New(env, _resval.c_str(), static_cast<size_t>(strlen(_resval.c_str()))) });
221
241
  }else{
222
- Nan::ThrowSyntaxError("Internal error in async worker");
223
- return;
242
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
224
243
  }
225
244
  }
226
245
 
227
- void HandleErrorCallback()
228
- {
229
- Nan::HandleScope scope;
230
- const int argc = 1;
231
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
246
+ // handler for failure (by calling SetError)
247
+ void OnError(const Napi::Error& err) override
248
+ {
249
+ Napi::Env env = Env();
250
+ Napi::HandleScope scope(env);
232
251
 
233
- if(callback){
234
- callback->Call(argc, argv);
252
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
253
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
235
254
  }else{
236
- Nan::ThrowSyntaxError("Internal error in async worker");
237
- return;
255
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
238
256
  }
239
- }
257
+ }
240
258
 
241
259
  private:
242
- K2HKeyQueue* pk2hkqobj;
243
- int getpos;
244
- bool is_pass_set;
245
- std::string strpass;
246
- std::string reskey;
247
- std::string resval;
260
+ Napi::FunctionReference _callbackRef;
261
+ K2HKeyQueue* _pk2hkqobj;
262
+ int _getpos;
263
+ bool _is_pass_set;
264
+ std::string _strpass;
265
+ std::string _reskey;
266
+ std::string _resval;
248
267
  };
249
268
 
250
269
  //---------------------------------------------------------
@@ -254,73 +273,81 @@ class K2hkqReadWorker : public Nan::AsyncWorker
254
273
  // Callback function: function(string error[, string key, string value])
255
274
  //
256
275
  //---------------------------------------------------------
257
- class K2hkqPopWorker : public Nan::AsyncWorker
276
+ class K2hkqPopAsyncWorker : public Napi::AsyncWorker
258
277
  {
259
278
  public:
260
- K2hkqPopWorker(Nan::Callback* callback, K2HKeyQueue* pobj, const char* ppass) : Nan::AsyncWorker(callback), pk2hkqobj(pobj), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), reskey(""), resval("") {}
261
- ~K2hkqPopWorker() {}
279
+ K2hkqPopAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, const char* ppass) :
280
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _reskey(), _resval()
281
+ {
282
+ _callbackRef.Ref();
283
+ }
262
284
 
263
- void Execute()
285
+ ~K2hkqPopAsyncWorker()
264
286
  {
265
- if(!pk2hkqobj){
266
- Nan::ReferenceError("No object is associated to async worker");
287
+ if(_callbackRef){
288
+ _callbackRef.Unref();
289
+ _callbackRef.Reset();
290
+ }
291
+ }
292
+
293
+ // Runs on the worker thread
294
+ void Execute() override
295
+ {
296
+ if(!_pk2hkqobj){
297
+ SetError("No object is associated to async worker");
267
298
  return;
268
299
  }
269
300
 
270
- // pop
271
301
  unsigned char* pkey = NULL;
272
302
  unsigned char* pval = NULL;
273
303
  size_t keylen = 0;
274
304
  size_t vallen = 0;
275
- bool result = pk2hkqobj->Pop(&pkey, keylen, &pval, vallen, (is_pass_set ? strpass.c_str() : NULL));
305
+ bool result = _pk2hkqobj->Pop(&pkey, keylen, &pval, vallen, (_is_pass_set ? _strpass.c_str() : NULL));
276
306
  if(!result || !pkey || 0 == keylen){
277
- // set error
278
- this->SetErrorMessage("Failed to pop data for K2HKeyQueue.");
307
+ SetError("Failed to pop data for K2HKeyQueue.");
279
308
  }else{
280
- // copy result
281
- reskey = std::string(reinterpret_cast<const char*>(pkey), keylen);
309
+ _reskey.assign(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))));
282
310
  if(pval && 0 < vallen){
283
- resval = std::string(reinterpret_cast<const char*>(pval), vallen);
311
+ _resval.assign(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))));
284
312
  }
285
313
  }
286
314
  K2H_Free(pkey);
287
315
  K2H_Free(pval);
288
316
  }
289
317
 
290
- void HandleOKCallback()
318
+ // handler for success
319
+ void OnOK() override
291
320
  {
292
- Nan::HandleScope scope;
293
- const int argc = 3;
294
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(reskey.c_str()).ToLocalChecked(), Nan::New<v8::String>(resval.c_str()).ToLocalChecked() };
321
+ Napi::Env env = Env();
322
+ Napi::HandleScope scope(env);
295
323
 
296
- if(callback){
297
- callback->Call(argc, argv);
324
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
325
+ _callbackRef.Call({ env.Null(), Napi::String::New(env, _reskey.c_str(), static_cast<size_t>(strlen(_reskey.c_str()))), Napi::String::New(env, _resval.c_str(), static_cast<size_t>(strlen(_resval.c_str()))) });
298
326
  }else{
299
- Nan::ThrowSyntaxError("Internal error in async worker");
300
- return;
327
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
301
328
  }
302
329
  }
303
330
 
304
- void HandleErrorCallback()
305
- {
306
- Nan::HandleScope scope;
307
- const int argc = 1;
308
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
331
+ // handler for failure (by calling SetError)
332
+ void OnError(const Napi::Error& err) override
333
+ {
334
+ Napi::Env env = Env();
335
+ Napi::HandleScope scope(env);
309
336
 
310
- if(callback){
311
- callback->Call(argc, argv);
337
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
338
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
312
339
  }else{
313
- Nan::ThrowSyntaxError("Internal error in async worker");
314
- return;
340
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
315
341
  }
316
- }
342
+ }
317
343
 
318
344
  private:
319
- K2HKeyQueue* pk2hkqobj;
320
- bool is_pass_set;
321
- std::string strpass;
322
- std::string reskey;
323
- std::string resval;
345
+ Napi::FunctionReference _callbackRef;
346
+ K2HKeyQueue* _pk2hkqobj;
347
+ bool _is_pass_set;
348
+ std::string _strpass;
349
+ std::string _reskey;
350
+ std::string _resval;
324
351
  };
325
352
 
326
353
  //---------------------------------------------------------
@@ -330,65 +357,76 @@ class K2hkqPopWorker : public Nan::AsyncWorker
330
357
  // Callback function: function(string error[, int removed_queue_count])
331
358
  //
332
359
  //---------------------------------------------------------
333
- class K2hkqRemoveWorker : public Nan::AsyncWorker
360
+ class K2hkqRemoveAsyncWorker : public Napi::AsyncWorker
334
361
  {
335
362
  public:
336
- K2hkqRemoveWorker(Nan::Callback* callback, K2HKeyQueue* pobj, int count, const char* ppass) : Nan::AsyncWorker(callback), pk2hkqobj(pobj), rmcount(count), is_pass_set(NULL != ppass), strpass(ppass ? ppass : ""), removed_count(0) {}
337
- ~K2hkqRemoveWorker() {}
363
+ K2hkqRemoveAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, int count, const char* ppass) :
364
+ Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _rmcount(count), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _removed_count(0)
365
+ {
366
+ _callbackRef.Ref();
367
+ }
368
+
369
+ ~K2hkqRemoveAsyncWorker()
370
+ {
371
+ if(_callbackRef){
372
+ _callbackRef.Unref();
373
+ _callbackRef.Reset();
374
+ }
375
+ }
338
376
 
339
- void Execute()
377
+ // Runs on worker thread
378
+ void Execute() override
340
379
  {
341
- if(!pk2hkqobj){
342
- Nan::ReferenceError("No object is associated to async worker");
380
+ if(!_pk2hkqobj){
381
+ SetError("No object is associated to async worker");
343
382
  return;
344
383
  }
345
- if(0 >= rmcount){
346
- Nan::ReferenceError("Specified remove count is under 1.");
384
+ if(_rmcount <= 0){
385
+ SetError("Specified remove count is under 1.");
347
386
  return;
348
387
  }
349
388
 
350
389
  // remove
351
- removed_count = pk2hkqobj->Remove(rmcount, NULL, NULL, (is_pass_set ? strpass.c_str() : NULL));
352
- if(-1 == removed_count){
353
- // set error
354
- this->SetErrorMessage("Failed to remove queue for K2HKeyQueue.");
390
+ _removed_count = _pk2hkqobj->Remove(_rmcount, NULL, NULL, (_is_pass_set ? _strpass.c_str() : NULL));
391
+ if(-1 == _removed_count){
392
+ SetError("Failed to remove queue for K2HKeyQueue.");
393
+ return;
355
394
  }
356
395
  }
357
396
 
358
- void HandleOKCallback()
397
+ // handler for success
398
+ void OnOK() override
359
399
  {
360
- Nan::HandleScope scope;
361
- const int argc = 2;
362
- v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(removed_count) };
400
+ Napi::Env env = Env();
401
+ Napi::HandleScope scope(env);
363
402
 
364
- if(callback){
365
- callback->Call(argc, argv);
403
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
404
+ _callbackRef.Call({ env.Null(), Napi::Number::New(env, static_cast<double>(_removed_count)) });
366
405
  }else{
367
- Nan::ThrowSyntaxError("Internal error in async worker");
368
- return;
406
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
369
407
  }
370
408
  }
371
409
 
372
- void HandleErrorCallback()
373
- {
374
- Nan::HandleScope scope;
375
- const int argc = 1;
376
- v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
410
+ // handler for failure (by calling SetError)
411
+ void OnError(const Napi::Error& err) override
412
+ {
413
+ Napi::Env env = Env();
414
+ Napi::HandleScope scope(env);
377
415
 
378
- if(callback){
379
- callback->Call(argc, argv);
416
+ if(_callbackRef && _callbackRef.Value().IsFunction()){
417
+ _callbackRef.Call({ Napi::String::New(env, err.Message()) });
380
418
  }else{
381
- Nan::ThrowSyntaxError("Internal error in async worker");
382
- return;
419
+ Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
383
420
  }
384
- }
421
+ }
385
422
 
386
423
  private:
387
- K2HKeyQueue* pk2hkqobj;
388
- int rmcount;
389
- bool is_pass_set;
390
- std::string strpass;
391
- int removed_count;
424
+ Napi::FunctionReference _callbackRef;
425
+ K2HKeyQueue* _pk2hkqobj;
426
+ int _rmcount;
427
+ bool _is_pass_set;
428
+ std::string _strpass;
429
+ int _removed_count;
392
430
  };
393
431
 
394
432
  #endif