Simply C++ event emitter
This commit is contained in:
parent
b69c6a03b1
commit
5aadeae888
@ -28,9 +28,6 @@ void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
|
|||||||
|
|
||||||
constructor_template = Persistent<FunctionTemplate>::New(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"));
|
constructor_template->SetClassName(String::NewSymbol("EventEmitter"));
|
||||||
|
|
||||||
events_symbol = NODE_PSYMBOL("_events");
|
events_symbol = NODE_PSYMBOL("_events");
|
||||||
@ -38,13 +35,12 @@ void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
|
|||||||
// All other prototype methods are defined in events.js
|
// All other prototype methods are defined in events.js
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ReallyEmit(Handle<Object> self,
|
|
||||||
Handle<String> event,
|
bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
|
||||||
int argc,
|
HandleScope scope;
|
||||||
Handle<Value> argv[]) {
|
|
||||||
// HandleScope not needed here because only called from one of the two
|
// HandleScope not needed here because only called from one of the two
|
||||||
// functions below
|
// functions below
|
||||||
Local<Value> events_v = self->Get(events_symbol);
|
Local<Value> events_v = handle_->Get(events_symbol);
|
||||||
if (!events_v->IsObject()) return false;
|
if (!events_v->IsObject()) return false;
|
||||||
Local<Object> events = events_v->ToObject();
|
Local<Object> events = events_v->ToObject();
|
||||||
|
|
||||||
@ -56,7 +52,7 @@ static bool ReallyEmit(Handle<Object> self,
|
|||||||
// Optimized one-listener case
|
// Optimized one-listener case
|
||||||
Local<Function> listener = Local<Function>::Cast(listeners_v);
|
Local<Function> listener = Local<Function>::Cast(listeners_v);
|
||||||
|
|
||||||
listener->Call(self, argc, argv);
|
listener->Call(handle_, argc, argv);
|
||||||
|
|
||||||
if (try_catch.HasCaught()) {
|
if (try_catch.HasCaught()) {
|
||||||
FatalException(try_catch);
|
FatalException(try_catch);
|
||||||
@ -71,7 +67,7 @@ static bool ReallyEmit(Handle<Object> self,
|
|||||||
if (!listener_v->IsFunction()) continue;
|
if (!listener_v->IsFunction()) continue;
|
||||||
Local<Function> listener = Local<Function>::Cast(listener_v);
|
Local<Function> listener = Local<Function>::Cast(listener_v);
|
||||||
|
|
||||||
listener->Call(self, argc, argv);
|
listener->Call(handle_, argc, argv);
|
||||||
|
|
||||||
if (try_catch.HasCaught()) {
|
if (try_catch.HasCaught()) {
|
||||||
FatalException(try_catch);
|
FatalException(try_catch);
|
||||||
@ -86,31 +82,4 @@ static bool ReallyEmit(Handle<Object> self,
|
|||||||
return true;
|
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
|
} // namespace node
|
||||||
|
@ -17,8 +17,6 @@ class EventEmitter : public ObjectWrap {
|
|||||||
v8::Handle<v8::Value> argv[]);
|
v8::Handle<v8::Value> argv[]);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static v8::Handle<v8::Value> Emit(const v8::Arguments& args);
|
|
||||||
|
|
||||||
EventEmitter() : ObjectWrap () { }
|
EventEmitter() : ObjectWrap () { }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user