Skip to content

Commit 30a5051

Browse files
committed
fix: use the Napi::Addon interface for the module
1 parent 31947d8 commit 30a5051

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/module.cc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ Module::Global::Shared Module::Global::Instance() {
102102
return instance;
103103
}
104104

105-
Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) {
106-
exports.Set("version", zmq::Version(exports.Env()));
107-
exports.Set("capability", zmq::Capabilities(exports.Env()));
108-
exports.Set("curveKeyPair", Napi::Function::New(exports.Env(), zmq::CurveKeyPair));
105+
Module::Module(Napi::Env env, Napi::Object exports) : MsgTrash(env) {
106+
exports.Set("version", zmq::Version(env));
107+
exports.Set("capability", zmq::Capabilities(env));
108+
exports.Set("curveKeyPair", Napi::Function::New(env, zmq::CurveKeyPair));
109109

110110
Context::Initialize(*this, exports);
111111
Socket::Initialize(*this, exports);
@@ -117,13 +117,5 @@ Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) {
117117
}
118118
} // namespace zmq
119119

120-
/* This initializer can be called in multiple contexts, like worker threads. */
121-
NAPI_MODULE_INIT(/* env, exports */) {
122-
auto* module = new zmq::Module(Napi::Object(env, exports));
123-
auto terminate = [](void* data) { delete reinterpret_cast<zmq::Module*>(data); };
124-
125-
/* Tear down the module class when the env/agent/thread is closed.*/
126-
[[maybe_unused]] auto status = napi_add_env_cleanup_hook(env, terminate, module);
127-
assert(status == napi_ok);
128-
return exports;
129-
}
120+
using Module = zmq::Module;
121+
NODE_API_ADDON(Module)

src/module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Terminator {
4646
}
4747
};
4848

49-
class Module {
49+
class Module : public Napi::Addon<Module> {
5050
/* Contains shared global state that will be accessible by all
5151
agents/threads. */
5252
class Global {
@@ -67,7 +67,7 @@ class Module {
6767
};
6868

6969
public:
70-
explicit Module(Napi::Object exports);
70+
explicit Module(Napi::Env env, Napi::Object exports);
7171

7272
class Global& Global() {
7373
return *global;

0 commit comments

Comments
 (0)