Save the prototype of EventEmitter
authorCheng Zhao <zcbenz@gmail.com>
Tue, 2 Aug 2016 09:07:58 +0000 (18:07 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 2 Aug 2016 09:07:58 +0000 (18:07 +0900)
atom/browser/api/event_emitter.cc
atom/browser/api/event_emitter.h
lib/browser/init.js

index e98008b..2e13aa7 100644 (file)
 #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<v8::Object> g_event_emitter_prototype;
+
 v8::Persistent<v8::ObjectTemplate> event_template;
 
 void PreventDefault(mate::Arguments* args) {
@@ -75,6 +80,29 @@ v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags) {
   return obj.GetHandle();
 }
 
+void InheritFromEventEmitter(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> constructor) {
+}
+
+void SetEventEmitterPrototype(v8::Isolate* isolate,
+                              v8::Local<v8::Object> prototype) {
+  g_event_emitter_prototype.Reset(isolate, prototype);
+}
+
 }  // namespace internal
 
 }  // namespace mate
+
+
+namespace {
+
+void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
+                v8::Local<v8::Context> context, void* priv) {
+  mate::Dictionary(context->GetIsolate(), exports)
+      .SetMethod("setEventEmitterPrototype",
+                 &mate::internal::SetEventEmitterPrototype);
+}
+
+}  // namespace
+
+NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_event_emitter, Initialize)
index ead3bed..12f11d0 100644 (file)
@@ -32,6 +32,9 @@ v8::Local<v8::Object> CreateCustomEvent(
     v8::Local<v8::Object> event);
 v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
 
+void InheritFromEventEmitter(v8::Isolate* isolate,
+                             v8::Local<v8::FunctionTemplate> constructor);
+
 }  // namespace internal
 
 // Provide helperers to emit event in JavaScript.
@@ -87,6 +90,11 @@ class EventEmitter : public Wrappable<T> {
  protected:
   EventEmitter() {}
 
+  static void InheritFromEventEmitter(
+      v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> constructor) {
+    internal::InheritFromEventEmitter(isolate, constructor);
+  }
+
  private:
   // this.emit(name, event, args...);
   template<typename... Args>
index f7619fd..c0e135b 100644 (file)
@@ -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)