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 15 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>
</dd>
<dt class="hdlist1">
<tt>emitter.emit(event, args)</tt>
<tt>emitter.emit(event, arg1, arg2, &#8230;)</tt>
</dt>
<dd>
<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>
</dd>
</dl></div>
@ -1862,7 +1862,7 @@ init (Handle&lt;Object&gt; target)
<div id="footer">
<div id="footer-text">
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>
</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
manipulated, e.g. to remove listeners.
+emitter.emit(event, args)+ ::
Execute each of the listeners in order with the array +args+ as arguments.
+emitter.emit(event, arg1, arg2, ...)+ ::
Execute each of the listeners in order with the supplied arguments.
==== +node.Promise+

10
doc/node.1

@ -1,11 +1,11 @@
.\" Title: node
.\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" Date: 09/11/2009
.\" Date: 09/12/2009
.\" Manual:
.\" Source:
.\"
.TH "NODE" "1" "09/11/2009" "" ""
.TH "NODE" "1" "09/12/2009" "" ""
.\" disable hyphenation
.nh
.\" 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\.
.RE
.PP
emitter\.emit(event, args)
emitter\.emit(event, arg1, arg2, \&...)
.RS 4
Execute each of the listeners in order with the array
args
as arguments\.
Execute each of the listeners in order with the supplied arguments\.
.RE
.RE
.sp

34
src/events.cc

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

2
src/events.js

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

2
src/file.js

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

12
src/http.js

@ -347,11 +347,11 @@ function createIncomingMessageStream (connection, incoming_listener) {
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) {
incoming.emit("body", [chunk]);
incoming.emit("body", chunk);
});
connection.addListener("message_complete", function () {
@ -421,7 +421,7 @@ function connectionListener (connection) {
});
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();
req.emit("response", [res]);
req.emit("response", res);
});
return client;
@ -529,13 +529,13 @@ node.http.cat = function (url, encoding) {
req.finish(function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
promise.emitError([res.statusCode]);
promise.emitError(res.statusCode);
return;
}
res.setBodyEncoding(encoding);
res.addListener("body", function (chunk) { content += chunk; });
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) {
self.loaded = true;
node.dlopen(self.filename, self.target); // FIXME synchronus
loadPromise.emitSuccess([self.target]);
loadPromise.emitSuccess(self.target);
} else {
node.stdio.writeError("Error reading " + self.filename + "\n");
loadPromise.emitError();
@ -167,7 +167,7 @@ node.Module.prototype.loadScript = function (callback) {
self.waitChildrenLoad(function () {
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");
e.emit("hello", ["a", "b"]);
e.emit("hello", "a", "b");
process.addListener("exit", function () {
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_done = true;
setTimeout(function () {
p1.emitSuccess(["single arg"]);
p1.emitSuccess("single arg");
}, 100);
});
@ -40,7 +40,7 @@ var p5 = new node.Promise();
p5.addCallback(function () {
p5_done = true;
setTimeout(function () {
p4.emitSuccess(["a","b","c"]);
p4.emitSuccess("a","b","c");
}, 100);
});

Loading…
Cancel
Save