Skip to content

Commit 70293a4

Browse files
committed
API: Move Promise and EventEmitter into 'events' module
1 parent 8e6dd52 commit 70293a4

12 files changed

+261
-237
lines changed

doc/api.txt

+10-6
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ sys.exec("ls /").addCallback(function (stdout, stderr) {
222222

223223
Many objects in Node emit events: a TCP server emits an event each time
224224
there is a connection, a child process emits an event when it exits. All
225-
objects which emit events are instances of +process.EventEmitter+.
225+
objects which emit events are instances of +events.EventEmitter+.
226226

227227
Events are represented by a camel-cased string. Here are some examples:
228228
+"connection"+, +"receive"+, +"messageBegin"+.
@@ -234,7 +234,9 @@ Some asynchronous file operations return an +EventEmitter+ called a
234234
_promise_. A promise emits just a single event when the operation is
235235
complete.
236236

237-
==== +process.EventEmitter+
237+
==== +events.EventEmitter+
238+
239+
+require('events')+ to access the events module.
238240

239241
All EventEmitters emit the event +"newListener"+ when new listeners are
240242
added.
@@ -268,9 +270,11 @@ manipulated, e.g. to remove listeners.
268270
+emitter.emit(event, arg1, arg2, ...)+ ::
269271
Execute each of the listeners in order with the supplied arguments.
270272

271-
==== +process.Promise+
273+
==== +events.Promise+
274+
275+
+require('events')+ to access the events module.
272276

273-
+process.Promise+ inherits from +process.eventEmitter+. A promise emits one of two
277+
+events.Promise+ inherits from +process.eventEmitter+. A promise emits one of two
274278
events: +"success"+ or +"error"+. After emitting its event, it will not
275279
emit anymore events.
276280

@@ -292,7 +296,7 @@ Adds a listener for the +"error"+ event. Returns the same promise object.
292296
Adds a listener for the +"cancel"+ event. Returns the same promise object.
293297

294298
+promise.emitSuccess(arg1, arg2, ...)+ ::
295-
If you created the promise (by doing +new process.Promise()+) then call
299+
If you created the promise (by doing +new events.Promise()+) then call
296300
+emitSuccess+ to emit the +"success"+ event with the given arguments.
297301
+
298302
(+promise.emit("success", arg1, arg2, ...)+ should also work, but doesn't at
@@ -565,7 +569,7 @@ File I/O is provided by simple wrappers around standard POSIX functions. To
565569
use this module do +require("posix")+.
566570

567571
All POSIX wrappers have a similar form. They return a promise
568-
(+process.Promise+). Example of deleting a file:
572+
(+events.Promise+). Example of deleting a file:
569573

570574
------------------------------------------------------------------------------
571575
var posix = require("posix"),

lib/dns.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var events = require('events');
2+
13
function callback (promise) {
24
return function () {
35
if (arguments[0] instanceof Error) {
@@ -9,19 +11,19 @@ function callback (promise) {
911
}
1012

1113
exports.resolve4 = function (domain) {
12-
var promise = new process.Promise();
14+
var promise = new events.Promise();
1315
process.dns.resolve4(domain, callback(promise));
1416
return promise;
1517
};
1618

1719
exports.resolve6 = function (domain) {
18-
var promise = new process.Promise();
20+
var promise = new events.Promise();
1921
process.dns.resolve6(domain, callback(promise));
2022
return promise;
2123
};
2224

2325
exports.reverse = function (ip) {
24-
var promise = new process.Promise();
26+
var promise = new events.Promise();
2527
process.dns.reverse(ip, callback(promise));
2628
return promise;
2729
};

lib/file.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var posix = require("./posix");
2+
var events = require('events');
23
/*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
34
/*globals exports, node, __filename */
45

@@ -19,7 +20,7 @@ function debugObject (obj) {
1920
exports.read = posix.cat;
2021

2122
exports.write = function (filename, data, encoding) {
22-
var promise = new process.Promise();
23+
var promise = new events.Promise();
2324

2425
encoding = encoding || "utf8"; // default to utf8
2526

@@ -140,7 +141,7 @@ proto._maybeDispatch = function () {
140141
};
141142

142143
proto._queueAction = function (method, args) {
143-
var userPromise = new process.Promise();
144+
var userPromise = new events.Promise();
144145
this.actionQueue.push({ method: method, args: args, promise: userPromise });
145146
this._maybeDispatch();
146147
return userPromise;

lib/http.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var sys = require('sys');
2+
var events = require('events');
23

34
var CRLF = "\r\n";
45
var STATUS_CODES = exports.STATUS_CODES = {
@@ -50,7 +51,7 @@ var content_length_expression = /Content-Length/i;
5051

5152
/* Abstract base class for ServerRequest and ClientResponse. */
5253
function IncomingMessage (connection) {
53-
process.EventEmitter.call(this);
54+
events.EventEmitter.call(this);
5455

5556
this.connection = connection;
5657
this.httpVersion = null;
@@ -65,7 +66,7 @@ function IncomingMessage (connection) {
6566
this.statusCode = null;
6667
this.client = this.connection;
6768
}
68-
sys.inherits(IncomingMessage, process.EventEmitter);
69+
sys.inherits(IncomingMessage, events.EventEmitter);
6970
exports.IncomingMessage = IncomingMessage;
7071

7172
IncomingMessage.prototype._parseQueryString = function () {
@@ -96,7 +97,7 @@ IncomingMessage.prototype._addHeaderLine = function (field, value) {
9697
};
9798

9899
function OutgoingMessage () {
99-
process.EventEmitter.call(this);
100+
events.EventEmitter.call(this);
100101

101102
this.output = [];
102103
this.outputEncodings = [];
@@ -110,7 +111,7 @@ function OutgoingMessage () {
110111

111112
this.finished = false;
112113
}
113-
sys.inherits(OutgoingMessage, process.EventEmitter);
114+
sys.inherits(OutgoingMessage, events.EventEmitter);
114115
exports.OutgoingMessage = OutgoingMessage;
115116

116117
OutgoingMessage.prototype.send = function (data, encoding) {
@@ -272,7 +273,7 @@ ClientRequest.prototype.finish = function (responseListener) {
272273

273274

274275
function createIncomingMessageStream (connection, incoming_listener) {
275-
var stream = new process.EventEmitter();
276+
var stream = new events.EventEmitter();
276277

277278
stream.addListener("incoming", incoming_listener);
278279

@@ -527,7 +528,7 @@ process.http.Client.prototype.request = function (method, url, headers) {
527528

528529

529530
exports.cat = function (url, encoding, headers) {
530-
var promise = new process.Promise();
531+
var promise = new events.Promise();
531532

532533
encoding = encoding || "utf8";
533534

lib/multipart.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var sys = require("sys");
2+
var events = require('events');
23

34
exports.parse = function(options) {
4-
var promise = new process.Promise();
5+
var promise = new events.Promise();
56

67
try {
78
var stream = new exports.Stream(options);
@@ -34,11 +35,11 @@ exports.parse = function(options) {
3435
};
3536

3637
exports.Stream = function(options) {
37-
process.EventEmitter.call(this);
38+
events.EventEmitter.call(this);
3839

3940
this.init(options);
4041
};
41-
sys.inherits(exports.Stream, process.EventEmitter);
42+
sys.inherits(exports.Stream, events.EventEmitter);
4243

4344

4445
var proto = exports.Stream.prototype;
@@ -111,7 +112,7 @@ proto.write = function(chunk) {
111112
};
112113

113114
function Part(stream) {
114-
process.EventEmitter.call(this);
115+
events.EventEmitter.call(this);
115116

116117
this.headers = {};
117118
this.name = null;
@@ -126,7 +127,7 @@ function Part(stream) {
126127

127128
this._headersComplete = false;
128129
}
129-
sys.inherits(Part, process.EventEmitter);
130+
sys.inherits(Part, events.EventEmitter);
130131

131132
Part.prototype.parsedHeaders = function() {
132133
for (var header in this.headers) {

lib/sys.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var events = require('events');
2+
13
exports.print = function (x) {
24
process.stdio.write(x);
35
};
@@ -32,7 +34,7 @@ exports.exec = function (command) {
3234
var child = process.createChildProcess("/bin/sh", ["-c", command]);
3335
var stdout = "";
3436
var stderr = "";
35-
var promise = new process.Promise();
37+
var promise = new events.Promise();
3638

3739
child.addListener("output", function (chunk) {
3840
if (chunk) stdout += chunk;

0 commit comments

Comments
 (0)