diff --git a/atom/browser/api/event_emitter.cc b/atom/browser/api/event_emitter.cc index e98008b85d..2e13aa7d22 100644 --- a/atom/browser/api/event_emitter.cc +++ b/atom/browser/api/event_emitter.cc @@ -10,10 +10,15 @@ #include "native_mate/object_template_builder.h" #include "ui/events/event_constants.h" +#include "atom/common/node_includes.h" + namespace mate { namespace { +// The prototype of Node's EventEmitter. +v8::Persistent g_event_emitter_prototype; + v8::Persistent event_template; void PreventDefault(mate::Arguments* args) { @@ -75,6 +80,29 @@ v8::Local CreateEventFromFlags(v8::Isolate* isolate, int flags) { return obj.GetHandle(); } +void InheritFromEventEmitter(v8::Isolate* isolate, + v8::Local constructor) { +} + +void SetEventEmitterPrototype(v8::Isolate* isolate, + v8::Local prototype) { + g_event_emitter_prototype.Reset(isolate, prototype); +} + } // namespace internal } // namespace mate + + +namespace { + +void Initialize(v8::Local exports, v8::Local unused, + v8::Local context, void* priv) { + mate::Dictionary(context->GetIsolate(), exports) + .SetMethod("setEventEmitterPrototype", + &mate::internal::SetEventEmitterPrototype); +} + +} // namespace + +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_event_emitter, Initialize) diff --git a/atom/browser/api/event_emitter.h b/atom/browser/api/event_emitter.h index ead3beddaa..12f11d0958 100644 --- a/atom/browser/api/event_emitter.h +++ b/atom/browser/api/event_emitter.h @@ -32,6 +32,9 @@ v8::Local CreateCustomEvent( v8::Local event); v8::Local CreateEventFromFlags(v8::Isolate* isolate, int flags); +void InheritFromEventEmitter(v8::Isolate* isolate, + v8::Local constructor); + } // namespace internal // Provide helperers to emit event in JavaScript. @@ -87,6 +90,11 @@ class EventEmitter : public Wrappable { protected: EventEmitter() {} + static void InheritFromEventEmitter( + v8::Isolate* isolate, v8::Local constructor) { + internal::InheritFromEventEmitter(isolate, constructor); + } + private: // this.emit(name, event, args...); template diff --git a/lib/browser/init.js b/lib/browser/init.js index f7619fd17b..c0e135bb80 100644 --- a/lib/browser/init.js +++ b/lib/browser/init.js @@ -6,6 +6,10 @@ const util = require('util') const Module = require('module') const v8 = require('v8') +// Save the prototype of EventEmitter, must be called before using native API. +const {setEventEmitterPrototype} = process.binding('atom_browser_event_emitter') +setEventEmitterPrototype(require('events').EventEmitter) + // We modified the original process.argv to let node.js load the atom.js, // we need to restore it here. process.argv.splice(1, 1)