0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

Simply C++ event emitter

This commit is contained in:
Ryan Dahl 2010-06-28 21:09:44 -07:00
parent b69c6a03b1
commit 5aadeae888
2 changed files with 6 additions and 39 deletions

View File

@ -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

View File

@ -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 () { }
};