Browse Source

API: Change arguments of emit(), emitSuccess(), emitError()

Instead of

  myemitter.emit("event", [arg1, arg2, arg3]);

the API is now

  myemitter.emit("event", arg1, arg2, arg3);

This change saves the creation of an extra array object for each event.
The implementation is also slightly more simple.
v0.7.4-release
Ryan 16 years ago
parent
commit
dbe116ddfe
  1. 6
      doc/api.html
  2. 4
      doc/api.txt
  3. 10
      doc/node.1
  4. 34
      src/events.cc
  5. 2
      src/events.js
  6. 2
      src/file.js
  7. 12
      src/http.js
  8. 4
      src/node.js
  9. 2
      test/mjsunit/test-event-emitter-add-listeners.js
  10. 4
      test/mjsunit/test-promise-wait.js

6
doc/api.html

@ -221,11 +221,11 @@ manipulated, e.g. to remove listeners.
</p> </p>
</dd> </dd>
<dt class="hdlist1"> <dt class="hdlist1">
<tt>emitter.emit(event, args)</tt> <tt>emitter.emit(event, arg1, arg2, &#8230;)</tt>
</dt> </dt>
<dd> <dd>
<p> <p>
Execute each of the listeners in order with the array <tt>args</tt> as arguments. Execute each of the listeners in order with the supplied arguments.
</p> </p>
</dd> </dd>
</dl></div> </dl></div>
@ -1862,7 +1862,7 @@ init (Handle&lt;Object&gt; target)
<div id="footer"> <div id="footer">
<div id="footer-text"> <div id="footer-text">
Version 0.1.10<br /> Version 0.1.10<br />
Last updated 2009-09-11 21:26:05 CEST Last updated 2009-09-12 13:07:32 CEST
</div> </div>
</div> </div>
</body> </body>

4
doc/api.txt

@ -143,8 +143,8 @@ server.addListener("connection", function (socket) {
Returns an array of listeners for the specified event. This array can be Returns an array of listeners for the specified event. This array can be
manipulated, e.g. to remove listeners. manipulated, e.g. to remove listeners.
+emitter.emit(event, args)+ :: +emitter.emit(event, arg1, arg2, ...)+ ::
Execute each of the listeners in order with the array +args+ as arguments. Execute each of the listeners in order with the supplied arguments.
==== +node.Promise+ ==== +node.Promise+

10
doc/node.1

@ -1,11 +1,11 @@
.\" Title: node .\" Title: node
.\" Author: .\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/> .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" Date: 09/11/2009 .\" Date: 09/12/2009
.\" Manual: .\" Manual:
.\" Source: .\" Source:
.\" .\"
.TH "NODE" "1" "09/11/2009" "" "" .TH "NODE" "1" "09/12/2009" "" ""
.\" disable hyphenation .\" disable hyphenation
.nh .nh
.\" disable justification (adjust text to left margin only) .\" disable justification (adjust text to left margin only)
@ -175,11 +175,9 @@ emitter\.listeners(event)
Returns an array of listeners for the specified event\. This array can be manipulated, e\.g\. to remove listeners\. Returns an array of listeners for the specified event\. This array can be manipulated, e\.g\. to remove listeners\.
.RE .RE
.PP .PP
emitter\.emit(event, args) emitter\.emit(event, arg1, arg2, \&...)
.RS 4 .RS 4
Execute each of the listeners in order with the array Execute each of the listeners in order with the supplied arguments\.
args
as arguments\.
.RE .RE
.RE .RE
.sp .sp

34
src/events.cc

@ -77,17 +77,19 @@ Handle<Value>
EventEmitter::Emit (const Arguments& args) EventEmitter::Emit (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Local<String> event = args[0]->ToString();
int argc = 0; if (args.Length() == 0) {
Local<Array> emit_args; return ThrowException(Exception::TypeError(
if (args[1]->IsArray()) { String::New("Must give event name.")));
emit_args = Local<Array>::Cast(args[1]);
argc = emit_args->Length();
} }
Local<String> event = args[0]->ToString();
int argc = args.Length() - 1;
Local<Value> argv[argc]; Local<Value> argv[argc];
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
argv[i] = emit_args->Get(Integer::New(i)); argv[i] = args[i+1];
} }
bool r = ReallyEmit(args.Holder(), event, argc, argv); bool r = ReallyEmit(args.Holder(), event, argc, argv);
@ -150,15 +152,10 @@ Promise::EmitSuccess (const v8::Arguments& args)
HandleScope scope; HandleScope scope;
Promise *promise = ObjectWrap::Unwrap<Promise>(args.Holder()); Promise *promise = ObjectWrap::Unwrap<Promise>(args.Holder());
int argc = 0; int argc = args.Length();
Local<Array> emit_args;
if (args[0]->IsArray()) {
emit_args = Local<Array>::Cast(args[0]);
argc = emit_args->Length();
}
Local<Value> argv[argc]; Local<Value> argv[argc];
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
argv[i] = emit_args->Get(Integer::New(i)); argv[i] = args[i];
} }
bool r = promise->EmitSuccess(argc, argv); bool r = promise->EmitSuccess(argc, argv);
@ -172,15 +169,10 @@ Promise::EmitError (const v8::Arguments& args)
HandleScope scope; HandleScope scope;
Promise *promise = ObjectWrap::Unwrap<Promise>(args.Holder()); Promise *promise = ObjectWrap::Unwrap<Promise>(args.Holder());
int argc = 0; int argc = args.Length();
Local<Array> emit_args;
if (args[0]->IsArray()) {
emit_args = Local<Array>::Cast(args[0]);
argc = emit_args->Length();
}
Local<Value> argv[argc]; Local<Value> argv[argc];
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
argv[i] = emit_args->Get(Integer::New(i)); argv[i] = args[i];
} }
bool r = promise->EmitError(argc, argv); bool r = promise->EmitError(argc, argv);

2
src/events.js

@ -8,7 +8,7 @@ node.EventEmitter.prototype.addListener = function (type, listener) {
if (!this._events.hasOwnProperty(type)) this._events[type] = []; if (!this._events.hasOwnProperty(type)) this._events[type] = [];
// To avoid recursion in the case that type == "newListeners"! Before // To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners". // adding it to the listeners, first emit "newListeners".
this.emit("newListener", [type, listener]); this.emit("newListener", type, listener);
this._events[type].push(listener); this._events[type].push(listener);
} }
return this; return this;

2
src/file.js

@ -30,7 +30,7 @@ node.fs.cat = function (path, encoding) {
pos += bytes_read; pos += bytes_read;
readChunk(); readChunk();
} else { } else {
cat_promise.emitSuccess([content]); cat_promise.emitSuccess(content);
node.fs.close(fd); node.fs.close(fd);
} }
}); });

12
src/http.js

@ -347,11 +347,11 @@ function createIncomingMessageStream (connection, incoming_listener) {
incoming.statusCode = info.statusCode; incoming.statusCode = info.statusCode;
} }
stream.emit("incoming", [incoming, info.should_keep_alive]); stream.emit("incoming", incoming, info.should_keep_alive);
}); });
connection.addListener("body", function (chunk) { connection.addListener("body", function (chunk) {
incoming.emit("body", [chunk]); incoming.emit("body", chunk);
}); });
connection.addListener("message_complete", function () { connection.addListener("message_complete", function () {
@ -421,7 +421,7 @@ function connectionListener (connection) {
}); });
responses.push(res); responses.push(res);
connection.server.emit("request", [req, res]); connection.server.emit("request", req, res);
}); });
} }
@ -477,7 +477,7 @@ node.http.createClient = function (port, host) {
}); });
var req = requests.shift(); var req = requests.shift();
req.emit("response", [res]); req.emit("response", res);
}); });
return client; return client;
@ -529,13 +529,13 @@ node.http.cat = function (url, encoding) {
req.finish(function (res) { req.finish(function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) { if (res.statusCode < 200 || res.statusCode >= 300) {
promise.emitError([res.statusCode]); promise.emitError(res.statusCode);
return; return;
} }
res.setBodyEncoding(encoding); res.setBodyEncoding(encoding);
res.addListener("body", function (chunk) { content += chunk; }); res.addListener("body", function (chunk) { content += chunk; });
res.addListener("complete", function () { res.addListener("complete", function () {
promise.emitSuccess([content]); promise.emitSuccess(content);
}); });
}); });

4
src/node.js

@ -107,7 +107,7 @@ node.Module.prototype.loadObject = function (callback) {
if (does_exist) { if (does_exist) {
self.loaded = true; self.loaded = true;
node.dlopen(self.filename, self.target); // FIXME synchronus node.dlopen(self.filename, self.target); // FIXME synchronus
loadPromise.emitSuccess([self.target]); loadPromise.emitSuccess(self.target);
} else { } else {
node.stdio.writeError("Error reading " + self.filename + "\n"); node.stdio.writeError("Error reading " + self.filename + "\n");
loadPromise.emitError(); loadPromise.emitError();
@ -167,7 +167,7 @@ node.Module.prototype.loadScript = function (callback) {
self.waitChildrenLoad(function () { self.waitChildrenLoad(function () {
self.loaded = true; self.loaded = true;
loadPromise.emitSuccess([self.target]); loadPromise.emitSuccess(self.target);
}); });
}); });
}; };

2
test/mjsunit/test-event-emitter-add-listeners.js

@ -19,7 +19,7 @@ e.addListener("hello", function (a, b) {
puts("start"); puts("start");
e.emit("hello", ["a", "b"]); e.emit("hello", "a", "b");
process.addListener("exit", function () { process.addListener("exit", function () {
assertArrayEquals(["hello"], events_new_listener_emited); assertArrayEquals(["hello"], events_new_listener_emited);

4
test/mjsunit/test-promise-wait.js

@ -13,7 +13,7 @@ var p2 = new node.Promise();
p2.addCallback(function () { p2.addCallback(function () {
p2_done = true; p2_done = true;
setTimeout(function () { setTimeout(function () {
p1.emitSuccess(["single arg"]); p1.emitSuccess("single arg");
}, 100); }, 100);
}); });
@ -40,7 +40,7 @@ var p5 = new node.Promise();
p5.addCallback(function () { p5.addCallback(function () {
p5_done = true; p5_done = true;
setTimeout(function () { setTimeout(function () {
p4.emitSuccess(["a","b","c"]); p4.emitSuccess("a","b","c");
}, 100); }, 100);
}); });

Loading…
Cancel
Save