Browse Source

Simply C++ event emitter

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
5aadeae888
  1. 43
      src/node_events.cc
  2. 2
      src/node_events.h

43
src/node_events.cc

@ -28,9 +28,6 @@ void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
constructor_template = Persistent<FunctionTemplate>::New(ctemplate);
Local<FunctionTemplate> __emit = FunctionTemplate::New(Emit);
constructor_template->PrototypeTemplate()->Set(String::NewSymbol("emit"),
__emit);
constructor_template->SetClassName(String::NewSymbol("EventEmitter"));
events_symbol = NODE_PSYMBOL("_events");
@ -38,13 +35,12 @@ void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
// All other prototype methods are defined in events.js
}
static bool ReallyEmit(Handle<Object> self,
Handle<String> event,
int argc,
Handle<Value> argv[]) {
bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
HandleScope scope;
// HandleScope not needed here because only called from one of the two
// functions below
Local<Value> events_v = self->Get(events_symbol);
Local<Value> events_v = handle_->Get(events_symbol);
if (!events_v->IsObject()) return false;
Local<Object> events = events_v->ToObject();
@ -56,7 +52,7 @@ static bool ReallyEmit(Handle<Object> self,
// Optimized one-listener case
Local<Function> listener = Local<Function>::Cast(listeners_v);
listener->Call(self, argc, argv);
listener->Call(handle_, argc, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
@ -71,7 +67,7 @@ static bool ReallyEmit(Handle<Object> self,
if (!listener_v->IsFunction()) continue;
Local<Function> listener = Local<Function>::Cast(listener_v);
listener->Call(self, argc, argv);
listener->Call(handle_, argc, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
@ -86,31 +82,4 @@ static bool ReallyEmit(Handle<Object> self,
return true;
}
Handle<Value> EventEmitter::Emit(const Arguments& args) {
HandleScope scope;
if (args.Length() == 0) {
return ThrowException(Exception::TypeError(
String::New("Must give event name.")));
}
Local<String> event = args[0]->ToString();
int argc = args.Length() - 1;
Local<Value> argv[argc];
for (int i = 0; i < argc; i++) {
argv[i] = args[i+1];
}
bool r = ReallyEmit(args.Holder(), event, argc, argv);
return scope.Close(r ? True() : False());
}
bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
HandleScope scope;
return ReallyEmit(handle_, event, argc, argv);
}
} // namespace node

2
src/node_events.h

@ -17,8 +17,6 @@ class EventEmitter : public ObjectWrap {
v8::Handle<v8::Value> argv[]);
protected:
static v8::Handle<v8::Value> Emit(const v8::Arguments& args);
EventEmitter() : ObjectWrap () { }
};

Loading…
Cancel
Save