k2hdkc 1.0.13 → 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.
package/src/k2hdkc_cbs.cc CHANGED
@@ -19,7 +19,6 @@
19
19
  #include <fullock/flckbaselist.tcc>
20
20
  #include "k2hdkc_cbs.h"
21
21
 
22
- using namespace v8;
23
22
  using namespace std;
24
23
  using namespace fullock;
25
24
 
@@ -37,68 +36,55 @@ StackEmitCB::~StackEmitCB()
37
36
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
38
37
  }
39
38
 
40
- // [NOTE]
41
- // This method does not lock, thus must lock before calling this.
42
- //
43
- Nan::Callback* StackEmitCB::RawFind(const char* pemitname)
39
+ bool StackEmitCB::Set(const std::string& emitter, const Napi::Function& cb)
44
40
  {
45
- string stremit = pemitname ? pemitname : "";
46
- Nan::Callback* cbfunc = NULL;
47
- if(stremit.empty()){
48
- return cbfunc;
49
- }
50
- if(EmitCbsMap.end() != EmitCbsMap.find(stremit)){
51
- cbfunc = EmitCbsMap[stremit];
41
+ while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
42
+
43
+ // clear if existed
44
+ auto it = EmitCbsMap.find(emitter);
45
+ if(EmitCbsMap.end() != it){
46
+ it->second.Reset();
47
+ EmitCbsMap.erase(it);
52
48
  }
53
- return cbfunc;
54
- }
55
49
 
56
- Nan::Callback* StackEmitCB::Find(const char* pemitname)
57
- {
58
- while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
59
- Nan::Callback* cbfunc = RawFind(pemitname);
50
+ // Insert new persistent reference
51
+ Napi::FunctionReference ref = Napi::Persistent(cb);
52
+ EmitCbsMap.emplace(emitter, std::move(ref));
53
+
60
54
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
61
55
 
62
- return cbfunc;
56
+ return true;
63
57
  }
64
58
 
65
- bool StackEmitCB::Set(const char* pemitname, Nan::Callback* cbfunc)
59
+ bool StackEmitCB::Unset(const std::string& emitter)
66
60
  {
67
- string stremit = pemitname ? pemitname : "";
68
- if(stremit.empty()){
69
- return false;
70
- }
71
-
72
61
  while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
73
62
 
74
- const Nan::Callback* oldcbfunc = RawFind(pemitname);
75
- if(oldcbfunc){
76
- EmitCbsMap.erase(stremit);
77
- }
78
- if(cbfunc){
79
- EmitCbsMap[stremit] = cbfunc;
63
+ auto it = EmitCbsMap.find(emitter);
64
+ if(EmitCbsMap.end() == it){
65
+ flck_unlock_noshared_mutex(&lockval); // UNLOCK
66
+ return false;
80
67
  }
68
+ it->second.Reset();
69
+ EmitCbsMap.erase(it);
70
+
81
71
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
82
72
 
83
73
  return true;
84
74
  }
85
75
 
86
- bool StackEmitCB::Unset(const char* pemitname)
76
+ Napi::FunctionReference* StackEmitCB::Find(const std::string& emitter)
87
77
  {
88
- string stremit = pemitname ? pemitname : "";
89
- if(stremit.empty()){
90
- return false;
91
- }
92
-
93
78
  while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
94
79
 
95
- const Nan::Callback* oldcbfunc = RawFind(pemitname);
96
- if(oldcbfunc){
97
- EmitCbsMap.erase(stremit);
80
+ auto it = EmitCbsMap.find(emitter);
81
+ if(EmitCbsMap.end() == it){
82
+ flck_unlock_noshared_mutex(&lockval); // UNLOCK
83
+ return nullptr;
98
84
  }
99
85
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
100
86
 
101
- return true;
87
+ return &it->second;
102
88
  }
103
89
 
104
90
  /*
package/src/k2hdkc_cbs.h CHANGED
@@ -18,32 +18,36 @@
18
18
  #ifndef K2HDKC_CBS_H
19
19
  #define K2HDKC_CBS_H
20
20
 
21
+ #include <string>
22
+ #include <unordered_map>
21
23
  #include "k2hdkc_common.h"
22
24
 
23
25
  //---------------------------------------------------------
24
26
  // Typedefs
25
27
  //---------------------------------------------------------
26
- typedef std::map<std::string, Nan::Callback*> cbsmap;
28
+ typedef std::unordered_map<std::string, Napi::FunctionReference> cbsmap;
27
29
 
28
30
  //---------------------------------------------------------
29
31
  // StackEmitCB Class
30
32
  //---------------------------------------------------------
31
33
  class StackEmitCB
32
34
  {
33
- protected:
34
- cbsmap EmitCbsMap;
35
- volatile int lockval; // lock variable for mapping
36
-
37
- protected:
38
- Nan::Callback* RawFind(const char* pemitname);
39
-
40
35
  public:
41
36
  StackEmitCB();
42
37
  virtual ~StackEmitCB();
43
38
 
44
- Nan::Callback* Find(const char* pemitname);
45
- bool Set(const char* pemitname, Nan::Callback* cbfunc);
46
- bool Unset(const char* pemitname);
39
+ // Set returns true if set succeeded
40
+ bool Set(const std::string& emitter, const Napi::Function& cb);
41
+
42
+ // Unset returns true if removed
43
+ bool Unset(const std::string& emitter);
44
+
45
+ // Find returns pointer to FunctionReference if set, otherwise nullptr
46
+ Napi::FunctionReference* Find(const std::string& emitter);
47
+
48
+ private:
49
+ cbsmap EmitCbsMap;
50
+ volatile int lockval; // lock variable for mapping
47
51
  };
48
52
 
49
53
  #endif
@@ -21,15 +21,15 @@
21
21
  //---------------------------------------------------------
22
22
  // common headers
23
23
  //---------------------------------------------------------
24
- #include <node.h>
24
+ #include <cstddef>
25
+
25
26
  #include <k2hdkc/k2hdkccommon.h>
26
27
  #include <k2hdkc/k2hdkc.h>
27
28
  #include <k2hdkc/k2hdkcslave.h>
28
29
  #include <k2hdkc/k2hdkccom.h>
29
30
  #include <k2hdkc/k2hdkcutil.h>
30
31
 
31
- #include <nan.h>
32
-
32
+ #include <napi.h>
33
33
  #include <string>
34
34
  #include <iostream>
35
35
  #include <map>