From 625143426a23507e7fb70714d29314b2f8a17bca Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 29 Jul 2015 14:24:45 +0800 Subject: [PATCH] Enable using custom events when emitting --- atom/browser/api/event_emitter.cc | 13 ++++++++++--- atom/browser/api/event_emitter.h | 28 +++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/atom/browser/api/event_emitter.cc b/atom/browser/api/event_emitter.cc index 7337830780f5..aa0b6c8781a3 100644 --- a/atom/browser/api/event_emitter.cc +++ b/atom/browser/api/event_emitter.cc @@ -37,9 +37,8 @@ v8::Local CreateEventObject(v8::Isolate* isolate) { EventEmitter::EventEmitter() { } -v8::Local EventEmitter::CreateJSEvent(v8::Isolate* isolate, - content::WebContents* sender, - IPC::Message* message) { +v8::Local EventEmitter::CreateJSEvent( + v8::Isolate* isolate, content::WebContents* sender, IPC::Message* message) { v8::Local event; bool use_native_event = sender && message; @@ -54,4 +53,12 @@ v8::Local EventEmitter::CreateJSEvent(v8::Isolate* isolate, return event; } +v8::Local EventEmitter::CreateCustomEvent( + v8::Isolate* isolate, v8::Local custom_event) { + v8::Local event = CreateEventObject(isolate); + event->SetPrototype(custom_event->CreationContext(), custom_event); + mate::Dictionary(isolate, event).Set("sender", GetWrapper(isolate)); + return event; +} + } // namespace mate diff --git a/atom/browser/api/event_emitter.h b/atom/browser/api/event_emitter.h index 6910df3a28d0..178c61d853ca 100644 --- a/atom/browser/api/event_emitter.h +++ b/atom/browser/api/event_emitter.h @@ -25,6 +25,14 @@ class EventEmitter : public Wrappable { public: typedef std::vector> ValueArray; + // this.emit(name, event, args...); + template + bool EmitCustomEvent(const base::StringPiece& name, + v8::Local event, + const Args&... args) { + return EmitWithEvent(name, CreateCustomEvent(isolate(), event), args...); + } + // this.emit(name, new Event(), args...); template bool Emit(const base::StringPiece& name, const Args&... args) { @@ -37,21 +45,31 @@ class EventEmitter : public Wrappable { content::WebContents* sender, IPC::Message* message, const Args&... args) { - v8::Locker locker(isolate()); - v8::HandleScope handle_scope(isolate()); v8::Local event = CreateJSEvent(isolate(), sender, message); - EmitEvent(isolate(), GetWrapper(isolate()), name, event, args...); - return event->Get( - StringToV8(isolate(), "defaultPrevented"))->BooleanValue(); + return EmitWithEvent(name, event, args...); } protected: EventEmitter(); private: + // this.emit(name, event, args...); + template + bool EmitWithEvent(const base::StringPiece& name, + v8::Local event, + const Args&... args) { + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + EmitEvent(isolate(), GetWrapper(isolate()), name, event, args...); + return event->Get( + StringToV8(isolate(), "defaultPrevented"))->BooleanValue(); + } + v8::Local CreateJSEvent(v8::Isolate* isolate, content::WebContents* sender, IPC::Message* message); + v8::Local CreateCustomEvent( + v8::Isolate* isolate, v8::Local event); DISALLOW_COPY_AND_ASSIGN(EventEmitter); };