Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[node] Properly inherit from EventEmitter #4576

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions platform/node/src/node_mapbox_gl_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mbgl::util::RunLoop& NodeRunLoop() {

}

NAN_MODULE_INIT(RegisterModule) {
void RegisterModule(v8::Local<v8::Object> target, v8::Local<v8::Object> module) {
// This has the effect of:
// a) Ensuring that the static local variable is initialized before any thread contention.
// b) unreffing an async handle, which otherwise would keep the default loop running.
Expand Down Expand Up @@ -63,16 +63,19 @@ NAN_MODULE_INIT(RegisterModule) {
Nan::New("Resource").ToLocalChecked(),
resource);

// Make the exported object inherit from process.EventEmitter
v8::Local<v8::Object> process = Nan::Get(
Nan::GetCurrentContext()->Global(),
Nan::New("process").ToLocalChecked()).ToLocalChecked()->ToObject();
// Make the exported object inherit from EventEmitter
v8::Local<v8::Function> require = Nan::Get(module,
Nan::New("require").ToLocalChecked()).ToLocalChecked().As<v8::Function>();

v8::Local<v8::Object> EventEmitter = Nan::Get(process,
v8::Local<v8::Value> eventsString = Nan::New("events").ToLocalChecked();
v8::Local<v8::Object> events = Nan::Call(require, module, 1, &eventsString).ToLocalChecked()->ToObject();

v8::Local<v8::Object> EventEmitter = Nan::Get(events,
Nan::New("EventEmitter").ToLocalChecked()).ToLocalChecked()->ToObject();

Nan::SetPrototype(target,
Nan::Get(EventEmitter, Nan::New("prototype").ToLocalChecked()).ToLocalChecked());
Nan::CallAsFunction(EventEmitter, target, 0, nullptr);

mbgl::Log::setObserver(std::make_unique<node_mbgl::NodeLogObserver>(target->ToObject()));
}
Expand Down