API: Move Promise and EventEmitter into 'events' module
This commit is contained in:
Родитель
8e6dd52683
Коммит
70293a43c9
16
doc/api.txt
16
doc/api.txt
|
@ -222,7 +222,7 @@ sys.exec("ls /").addCallback(function (stdout, stderr) {
|
|||
|
||||
Many objects in Node emit events: a TCP server emits an event each time
|
||||
there is a connection, a child process emits an event when it exits. All
|
||||
objects which emit events are instances of +process.EventEmitter+.
|
||||
objects which emit events are instances of +events.EventEmitter+.
|
||||
|
||||
Events are represented by a camel-cased string. Here are some examples:
|
||||
+"connection"+, +"receive"+, +"messageBegin"+.
|
||||
|
@ -234,7 +234,9 @@ Some asynchronous file operations return an +EventEmitter+ called a
|
|||
_promise_. A promise emits just a single event when the operation is
|
||||
complete.
|
||||
|
||||
==== +process.EventEmitter+
|
||||
==== +events.EventEmitter+
|
||||
|
||||
+require('events')+ to access the events module.
|
||||
|
||||
All EventEmitters emit the event +"newListener"+ when new listeners are
|
||||
added.
|
||||
|
@ -268,9 +270,11 @@ manipulated, e.g. to remove listeners.
|
|||
+emitter.emit(event, arg1, arg2, ...)+ ::
|
||||
Execute each of the listeners in order with the supplied arguments.
|
||||
|
||||
==== +process.Promise+
|
||||
==== +events.Promise+
|
||||
|
||||
+process.Promise+ inherits from +process.eventEmitter+. A promise emits one of two
|
||||
+require('events')+ to access the events module.
|
||||
|
||||
+events.Promise+ inherits from +process.eventEmitter+. A promise emits one of two
|
||||
events: +"success"+ or +"error"+. After emitting its event, it will not
|
||||
emit anymore events.
|
||||
|
||||
|
@ -292,7 +296,7 @@ Adds a listener for the +"error"+ event. Returns the same promise object.
|
|||
Adds a listener for the +"cancel"+ event. Returns the same promise object.
|
||||
|
||||
+promise.emitSuccess(arg1, arg2, ...)+ ::
|
||||
If you created the promise (by doing +new process.Promise()+) then call
|
||||
If you created the promise (by doing +new events.Promise()+) then call
|
||||
+emitSuccess+ to emit the +"success"+ event with the given arguments.
|
||||
+
|
||||
(+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
|
|||
use this module do +require("posix")+.
|
||||
|
||||
All POSIX wrappers have a similar form. They return a promise
|
||||
(+process.Promise+). Example of deleting a file:
|
||||
(+events.Promise+). Example of deleting a file:
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
var posix = require("posix"),
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
var events = require('events');
|
||||
|
||||
function callback (promise) {
|
||||
return function () {
|
||||
if (arguments[0] instanceof Error) {
|
||||
|
@ -9,19 +11,19 @@ function callback (promise) {
|
|||
}
|
||||
|
||||
exports.resolve4 = function (domain) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.dns.resolve4(domain, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
||||
exports.resolve6 = function (domain) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.dns.resolve6(domain, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
||||
exports.reverse = function (ip) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.dns.reverse(ip, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
var posix = require("./posix");
|
||||
var events = require('events');
|
||||
/*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
|
||||
/*globals exports, node, __filename */
|
||||
|
||||
|
@ -19,7 +20,7 @@ function debugObject (obj) {
|
|||
exports.read = posix.cat;
|
||||
|
||||
exports.write = function (filename, data, encoding) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
|
||||
encoding = encoding || "utf8"; // default to utf8
|
||||
|
||||
|
@ -140,7 +141,7 @@ proto._maybeDispatch = function () {
|
|||
};
|
||||
|
||||
proto._queueAction = function (method, args) {
|
||||
var userPromise = new process.Promise();
|
||||
var userPromise = new events.Promise();
|
||||
this.actionQueue.push({ method: method, args: args, promise: userPromise });
|
||||
this._maybeDispatch();
|
||||
return userPromise;
|
||||
|
|
13
lib/http.js
13
lib/http.js
|
@ -1,4 +1,5 @@
|
|||
var sys = require('sys');
|
||||
var events = require('events');
|
||||
|
||||
var CRLF = "\r\n";
|
||||
var STATUS_CODES = exports.STATUS_CODES = {
|
||||
|
@ -50,7 +51,7 @@ var content_length_expression = /Content-Length/i;
|
|||
|
||||
/* Abstract base class for ServerRequest and ClientResponse. */
|
||||
function IncomingMessage (connection) {
|
||||
process.EventEmitter.call(this);
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
this.connection = connection;
|
||||
this.httpVersion = null;
|
||||
|
@ -65,7 +66,7 @@ function IncomingMessage (connection) {
|
|||
this.statusCode = null;
|
||||
this.client = this.connection;
|
||||
}
|
||||
sys.inherits(IncomingMessage, process.EventEmitter);
|
||||
sys.inherits(IncomingMessage, events.EventEmitter);
|
||||
exports.IncomingMessage = IncomingMessage;
|
||||
|
||||
IncomingMessage.prototype._parseQueryString = function () {
|
||||
|
@ -96,7 +97,7 @@ IncomingMessage.prototype._addHeaderLine = function (field, value) {
|
|||
};
|
||||
|
||||
function OutgoingMessage () {
|
||||
process.EventEmitter.call(this);
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
this.output = [];
|
||||
this.outputEncodings = [];
|
||||
|
@ -110,7 +111,7 @@ function OutgoingMessage () {
|
|||
|
||||
this.finished = false;
|
||||
}
|
||||
sys.inherits(OutgoingMessage, process.EventEmitter);
|
||||
sys.inherits(OutgoingMessage, events.EventEmitter);
|
||||
exports.OutgoingMessage = OutgoingMessage;
|
||||
|
||||
OutgoingMessage.prototype.send = function (data, encoding) {
|
||||
|
@ -272,7 +273,7 @@ ClientRequest.prototype.finish = function (responseListener) {
|
|||
|
||||
|
||||
function createIncomingMessageStream (connection, incoming_listener) {
|
||||
var stream = new process.EventEmitter();
|
||||
var stream = new events.EventEmitter();
|
||||
|
||||
stream.addListener("incoming", incoming_listener);
|
||||
|
||||
|
@ -527,7 +528,7 @@ process.http.Client.prototype.request = function (method, url, headers) {
|
|||
|
||||
|
||||
exports.cat = function (url, encoding, headers) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
|
||||
encoding = encoding || "utf8";
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
var sys = require("sys");
|
||||
var events = require('events');
|
||||
|
||||
exports.parse = function(options) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
|
||||
try {
|
||||
var stream = new exports.Stream(options);
|
||||
|
@ -34,11 +35,11 @@ exports.parse = function(options) {
|
|||
};
|
||||
|
||||
exports.Stream = function(options) {
|
||||
process.EventEmitter.call(this);
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
this.init(options);
|
||||
};
|
||||
sys.inherits(exports.Stream, process.EventEmitter);
|
||||
sys.inherits(exports.Stream, events.EventEmitter);
|
||||
|
||||
|
||||
var proto = exports.Stream.prototype;
|
||||
|
@ -111,7 +112,7 @@ proto.write = function(chunk) {
|
|||
};
|
||||
|
||||
function Part(stream) {
|
||||
process.EventEmitter.call(this);
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
this.headers = {};
|
||||
this.name = null;
|
||||
|
@ -126,7 +127,7 @@ function Part(stream) {
|
|||
|
||||
this._headersComplete = false;
|
||||
}
|
||||
sys.inherits(Part, process.EventEmitter);
|
||||
sys.inherits(Part, events.EventEmitter);
|
||||
|
||||
Part.prototype.parsedHeaders = function() {
|
||||
for (var header in this.headers) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
var events = require('events');
|
||||
|
||||
exports.print = function (x) {
|
||||
process.stdio.write(x);
|
||||
};
|
||||
|
@ -32,7 +34,7 @@ exports.exec = function (command) {
|
|||
var child = process.createChildProcess("/bin/sh", ["-c", command]);
|
||||
var stdout = "";
|
||||
var stderr = "";
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
|
||||
child.addListener("output", function (chunk) {
|
||||
if (chunk) stdout += chunk;
|
||||
|
|
428
src/node.js
428
src/node.js
|
@ -1,6 +1,6 @@
|
|||
(function () { // annonymous namespace
|
||||
|
||||
// deprecation errors
|
||||
/** deprecation errors ************************************************/
|
||||
|
||||
GLOBAL.__module = function () {
|
||||
throw new Error("'__module' has been renamed to 'module'");
|
||||
|
@ -70,6 +70,51 @@ node.inherits = function () {
|
|||
throw new Error("node.inherits() has moved. Use require('sys') to access it.");
|
||||
};
|
||||
|
||||
/**********************************************************************/
|
||||
|
||||
// Module
|
||||
|
||||
function Module (id, parent) {
|
||||
this.id = id;
|
||||
this.exports = {};
|
||||
this.parent = parent;
|
||||
|
||||
this.filename = null;
|
||||
this.loaded = false;
|
||||
this.loadPromise = null;
|
||||
this.exited = false;
|
||||
this.children = [];
|
||||
};
|
||||
|
||||
var moduleCache = {};
|
||||
|
||||
function createModule (id, parent) {
|
||||
if (id in moduleCache) {
|
||||
debug("found " + JSON.stringify(id) + " in cache");
|
||||
return moduleCache[id];
|
||||
}
|
||||
debug("didn't found " + JSON.stringify(id) + " in cache. creating new module");
|
||||
var m = new Module(id, parent);
|
||||
moduleCache[id] = m;
|
||||
return m;
|
||||
};
|
||||
|
||||
function createInternalModule (id, constructor) {
|
||||
var m = createModule(id);
|
||||
constructor(m.exports);
|
||||
m.loaded = true;
|
||||
return m;
|
||||
};
|
||||
|
||||
|
||||
process.inherits = function (ctor, superCtor) {
|
||||
var tempCtor = function(){};
|
||||
tempCtor.prototype = superCtor.prototype;
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = new tempCtor();
|
||||
ctor.prototype.constructor = ctor;
|
||||
};
|
||||
|
||||
|
||||
process.createChildProcess = function (file, args, env) {
|
||||
var child = new process.ChildProcess();
|
||||
|
@ -147,194 +192,189 @@ process.mixin = function() {
|
|||
return target;
|
||||
};
|
||||
|
||||
|
||||
// Event
|
||||
|
||||
// process.EventEmitter is defined in src/events.cc
|
||||
// process.EventEmitter.prototype.emit() is also defined there.
|
||||
process.EventEmitter.prototype.addListener = function (type, listener) {
|
||||
if (listener instanceof Function) {
|
||||
var eventsModule = createInternalModule('events', function (exports) {
|
||||
exports.EventEmitter = process.EventEmitter;
|
||||
|
||||
// process.EventEmitter is defined in src/events.cc
|
||||
// process.EventEmitter.prototype.emit() is also defined there.
|
||||
process.EventEmitter.prototype.addListener = function (type, listener) {
|
||||
if (listener instanceof Function) {
|
||||
if (!this._events) this._events = {};
|
||||
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._events[type].push(listener);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
||||
if (listener instanceof Function) {
|
||||
// does not use listeners(), so no side effect of creating _events[type]
|
||||
if (!this._events || !this._events.hasOwnProperty(type)) return;
|
||||
var list = this._events[type];
|
||||
if (list.indexOf(listener) < 0) return;
|
||||
list.splice(list.indexOf(listener), 1);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
process.EventEmitter.prototype.listeners = function (type) {
|
||||
if (!this._events) this._events = {};
|
||||
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._events[type].push(listener);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
return this._events[type];
|
||||
};
|
||||
|
||||
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
||||
if (listener instanceof Function) {
|
||||
// does not use listeners(), so no side effect of creating _events[type]
|
||||
if (!this._events || !this._events.hasOwnProperty(type)) return;
|
||||
var list = this._events[type];
|
||||
if (list.indexOf(listener) < 0) return;
|
||||
list.splice(list.indexOf(listener), 1);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
exports.Promise = function () {
|
||||
exports.EventEmitter.call();
|
||||
this._blocking = false;
|
||||
this._hasFired = false;
|
||||
};
|
||||
process.inherits(exports.Promise, exports.EventEmitter);
|
||||
|
||||
process.EventEmitter.prototype.listeners = function (type) {
|
||||
if (!this._events) this._events = {};
|
||||
if (!this._events.hasOwnProperty(type)) this._events[type] = [];
|
||||
return this._events[type];
|
||||
};
|
||||
process.Promise = exports.Promise;
|
||||
|
||||
process.inherits = function (ctor, superCtor) {
|
||||
var tempCtor = function(){};
|
||||
tempCtor.prototype = superCtor.prototype;
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = new tempCtor();
|
||||
ctor.prototype.constructor = ctor;
|
||||
};
|
||||
exports.Promise.prototype.timeout = function(timeout) {
|
||||
if (timeout === undefined) {
|
||||
return this._timeoutDuration;
|
||||
}
|
||||
|
||||
|
||||
// Promise
|
||||
|
||||
process.Promise = function () {
|
||||
process.EventEmitter.call();
|
||||
this._blocking = false;
|
||||
this._hasFired = false;
|
||||
};
|
||||
process.inherits(process.Promise, process.EventEmitter);
|
||||
|
||||
process.Promise.prototype.timeout = function(timeout) {
|
||||
if (timeout === undefined) {
|
||||
return this._timeoutDuration;
|
||||
}
|
||||
|
||||
this._timeoutDuration = timeout;
|
||||
if (this._timer) {
|
||||
clearTimeout(this._timer);
|
||||
this._timer = null;
|
||||
}
|
||||
|
||||
var promiseComplete = false;
|
||||
var onComplete = function() {
|
||||
promiseComplete = true;
|
||||
this._timeoutDuration = timeout;
|
||||
if (this._timer) {
|
||||
clearTimeout(this._timer);
|
||||
this._timer = null;
|
||||
}
|
||||
|
||||
var promiseComplete = false;
|
||||
var onComplete = function() {
|
||||
promiseComplete = true;
|
||||
if (this._timer) {
|
||||
clearTimeout(this._timer);
|
||||
this._timer = null;
|
||||
}
|
||||
};
|
||||
|
||||
this
|
||||
.addCallback(onComplete)
|
||||
.addCancelback(onComplete)
|
||||
.addErrback(onComplete);
|
||||
|
||||
var self = this;
|
||||
this._timer = setTimeout(function() {
|
||||
self._timer = null;
|
||||
if (promiseComplete) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.emitError(new Error('timeout'));
|
||||
}, this._timeoutDuration);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
this
|
||||
.addCallback(onComplete)
|
||||
.addCancelback(onComplete)
|
||||
.addErrback(onComplete);
|
||||
exports.Promise.prototype.cancel = function() {
|
||||
if(this._cancelled) return;
|
||||
this._cancelled = true;
|
||||
|
||||
var self = this;
|
||||
this._timer = setTimeout(function() {
|
||||
self._timer = null;
|
||||
if (promiseComplete) {
|
||||
return;
|
||||
this._events['success'] = [];
|
||||
this._events['error'] = [];
|
||||
|
||||
this.emitCancel();
|
||||
};
|
||||
|
||||
exports.Promise.prototype.emitCancel = function() {
|
||||
Array.prototype.unshift.call(arguments, 'cancel')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
exports.Promise.prototype.emitSuccess = function() {
|
||||
if (this.hasFired) return;
|
||||
this.hasFired = true;
|
||||
Array.prototype.unshift.call(arguments, 'success')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
exports.Promise.prototype.emitError = function() {
|
||||
if (this.hasFired) return;
|
||||
this.hasFired = true;
|
||||
Array.prototype.unshift.call(arguments, 'error')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
exports.Promise.prototype.addCallback = function (listener) {
|
||||
this.addListener("success", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.Promise.prototype.addErrback = function (listener) {
|
||||
this.addListener("error", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.Promise.prototype.addCancelback = function (listener) {
|
||||
this.addListener("cancel", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
/* Poor Man's coroutines */
|
||||
var coroutineStack = [];
|
||||
|
||||
exports.Promise.prototype._destack = function () {
|
||||
this._blocking = false;
|
||||
|
||||
while (coroutineStack.length > 0 &&
|
||||
!coroutineStack[coroutineStack.length-1]._blocking)
|
||||
{
|
||||
coroutineStack.pop();
|
||||
process.unloop("one");
|
||||
}
|
||||
};
|
||||
|
||||
self.emitError(new Error('timeout'));
|
||||
}, this._timeoutDuration);
|
||||
exports.Promise.prototype.wait = function () {
|
||||
var self = this;
|
||||
var ret;
|
||||
var hadError = false;
|
||||
|
||||
return this;
|
||||
};
|
||||
self.addCallback(function () {
|
||||
if (arguments.length == 1) {
|
||||
ret = arguments[0];
|
||||
} else if (arguments.length > 1) {
|
||||
ret = Array.prototype.slice.call(arguments);
|
||||
}
|
||||
self._destack();
|
||||
});
|
||||
|
||||
process.Promise.prototype.cancel = function() {
|
||||
if(this._cancelled) return;
|
||||
this._cancelled = true;
|
||||
self.addErrback(function (arg) {
|
||||
hadError = true;
|
||||
ret = arg;
|
||||
self._destack();
|
||||
});
|
||||
|
||||
this._events['success'] = [];
|
||||
this._events['error'] = [];
|
||||
|
||||
this.emitCancel();
|
||||
};
|
||||
|
||||
process.Promise.prototype.emitCancel = function() {
|
||||
Array.prototype.unshift.call(arguments, 'cancel')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
process.Promise.prototype.emitSuccess = function() {
|
||||
if (this.hasFired) return;
|
||||
this.hasFired = true;
|
||||
Array.prototype.unshift.call(arguments, 'success')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
process.Promise.prototype.emitError = function() {
|
||||
if (this.hasFired) return;
|
||||
this.hasFired = true;
|
||||
Array.prototype.unshift.call(arguments, 'error')
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
||||
process.Promise.prototype.addCallback = function (listener) {
|
||||
this.addListener("success", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
process.Promise.prototype.addErrback = function (listener) {
|
||||
this.addListener("error", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
process.Promise.prototype.addCancelback = function (listener) {
|
||||
this.addListener("cancel", listener);
|
||||
return this;
|
||||
};
|
||||
|
||||
/* Poor Man's coroutines */
|
||||
var coroutineStack = [];
|
||||
|
||||
process.Promise.prototype._destack = function () {
|
||||
this._blocking = false;
|
||||
|
||||
while (coroutineStack.length > 0 &&
|
||||
!coroutineStack[coroutineStack.length-1]._blocking)
|
||||
{
|
||||
coroutineStack.pop();
|
||||
process.unloop("one");
|
||||
}
|
||||
};
|
||||
|
||||
process.Promise.prototype.wait = function () {
|
||||
var self = this;
|
||||
var ret;
|
||||
var hadError = false;
|
||||
|
||||
self.addCallback(function () {
|
||||
if (arguments.length == 1) {
|
||||
ret = arguments[0];
|
||||
} else if (arguments.length > 1) {
|
||||
ret = Array.prototype.slice.call(arguments);
|
||||
coroutineStack.push(self);
|
||||
if (coroutineStack.length > 10) {
|
||||
process.stdio.writeError("WARNING: promise.wait() is being called too often.\n");
|
||||
}
|
||||
self._destack();
|
||||
});
|
||||
self._blocking = true;
|
||||
|
||||
self.addErrback(function (arg) {
|
||||
hadError = true;
|
||||
ret = arg;
|
||||
self._destack();
|
||||
});
|
||||
process.loop();
|
||||
|
||||
coroutineStack.push(self);
|
||||
if (coroutineStack.length > 10) {
|
||||
process.stdio.writeError("WARNING: promise.wait() is being called too often.\n");
|
||||
}
|
||||
self._blocking = true;
|
||||
process.assert(self._blocking == false);
|
||||
|
||||
process.loop();
|
||||
|
||||
process.assert(self._blocking == false);
|
||||
|
||||
if (hadError) {
|
||||
if (ret) {
|
||||
throw ret;
|
||||
} else {
|
||||
throw new Error("Promise completed with error (No arguments given.)");
|
||||
if (hadError) {
|
||||
if (ret) {
|
||||
throw ret;
|
||||
} else {
|
||||
throw new Error("Promise completed with error (No arguments given.)");
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
return ret;
|
||||
};
|
||||
});
|
||||
|
||||
var events = eventsModule.exports;
|
||||
|
||||
|
||||
// Signal Handlers
|
||||
|
@ -465,38 +505,6 @@ function debug (x) {
|
|||
}
|
||||
|
||||
|
||||
// private constructor
|
||||
function Module (id, parent) {
|
||||
this.id = id;
|
||||
this.exports = {};
|
||||
this.parent = parent;
|
||||
|
||||
this.filename = null;
|
||||
this.loaded = false;
|
||||
this.loadPromise = null;
|
||||
this.exited = false;
|
||||
this.children = [];
|
||||
};
|
||||
|
||||
var moduleCache = {};
|
||||
|
||||
function createModule (id, parent) {
|
||||
if (id in moduleCache) {
|
||||
debug("found " + JSON.stringify(id) + " in cache");
|
||||
return moduleCache[id];
|
||||
}
|
||||
debug("didn't found " + JSON.stringify(id) + " in cache. creating new module");
|
||||
var m = new Module(id, parent);
|
||||
moduleCache[id] = m;
|
||||
return m;
|
||||
};
|
||||
|
||||
function createInternalModule (id, constructor) {
|
||||
var m = createModule(id);
|
||||
constructor(m.exports);
|
||||
m.loaded = true;
|
||||
return m;
|
||||
};
|
||||
|
||||
var posixModule = createInternalModule("posix", function (exports) {
|
||||
exports.Stats = process.Stats;
|
||||
|
@ -515,7 +523,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
// list to make the arguments clear.
|
||||
|
||||
exports.close = function (fd) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.close(fd, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -525,7 +533,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.open = function (path, flags, mode) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.open(path, flags, mode, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -535,7 +543,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.read = function (fd, length, position, encoding) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
encoding = encoding || "binary";
|
||||
process.fs.read(fd, length, position, encoding, callback(promise));
|
||||
return promise;
|
||||
|
@ -547,7 +555,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.write = function (fd, data, position, encoding) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
encoding = encoding || "binary";
|
||||
process.fs.write(fd, data, position, encoding, callback(promise));
|
||||
return promise;
|
||||
|
@ -559,7 +567,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.rename = function (oldPath, newPath) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.rename(oldPath, newPath, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -569,7 +577,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.rmdir = function (path) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.rmdir(path, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -579,7 +587,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.mkdir = function (path, mode) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.mkdir(path, mode, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -589,7 +597,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.sendfile = function (outFd, inFd, inOffset, length) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.sendfile(outFd, inFd, inOffset, length, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -599,7 +607,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.readdir = function (path) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.readdir(path, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -609,7 +617,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.stat = function (path) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.stat(path, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -619,7 +627,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
};
|
||||
|
||||
exports.unlink = function (path) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
process.fs.unlink(path, callback(promise));
|
||||
return promise;
|
||||
};
|
||||
|
@ -630,7 +638,7 @@ var posixModule = createInternalModule("posix", function (exports) {
|
|||
|
||||
|
||||
exports.cat = function (path, encoding) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
|
||||
encoding = encoding || "utf8"; // default to utf8
|
||||
|
||||
|
@ -799,7 +807,7 @@ function findModulePath (id, dirs, callback) {
|
|||
|
||||
function loadModule (request, parent) {
|
||||
// This is the promise which is actually returned from require.async()
|
||||
var loadPromise = new process.Promise();
|
||||
var loadPromise = new events.Promise();
|
||||
|
||||
// debug("loadModule REQUEST " + (request) + " parent: " + JSON.stringify(parent));
|
||||
|
||||
|
@ -873,7 +881,7 @@ function cat (id, loadPromise) {
|
|||
debug(id);
|
||||
|
||||
if (id.match(/^http:\/\//)) {
|
||||
promise = new process.Promise();
|
||||
promise = new events.Promise();
|
||||
loadModule('http', process.mainModule)
|
||||
.addCallback(function(http) {
|
||||
http.cat(id)
|
||||
|
@ -973,7 +981,7 @@ if (process.ARGV[1].charAt(0) != "/" && !(/^http:\/\//).exec(process.ARGV[1])) {
|
|||
|
||||
// Load the main module--the command line argument.
|
||||
process.mainModule = createModule(".");
|
||||
var loadPromise = new process.Promise();
|
||||
var loadPromise = new events.Promise();
|
||||
process.mainModule.load(process.ARGV[1], loadPromise);
|
||||
|
||||
loadPromise.addErrback(function(e) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
process.mixin(require("./common"));
|
||||
var events = require('events');
|
||||
|
||||
var e = new process.EventEmitter();
|
||||
var e = new events.EventEmitter();
|
||||
|
||||
var events_new_listener_emited = [];
|
||||
var times_hello_emited = 0;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
process.mixin(require('./common'));
|
||||
events = require('events');
|
||||
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
var cancelled = false;
|
||||
promise.addCancelback(function(){
|
||||
if(cancelled){
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
process.mixin(require("./common"));
|
||||
events = require('events');
|
||||
|
||||
var timeouts = 0;
|
||||
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
promise.timeout(250);
|
||||
assert.equal(250, promise.timeout());
|
||||
|
||||
|
@ -20,7 +21,7 @@ setTimeout(function() {
|
|||
promise.emitSuccess('Am I too late?');
|
||||
}, 500);
|
||||
|
||||
var waitPromise = new process.Promise();
|
||||
var waitPromise = new events.Promise();
|
||||
try {
|
||||
waitPromise.timeout(250).wait()
|
||||
} catch (e) {
|
||||
|
@ -29,7 +30,7 @@ try {
|
|||
timeouts++;
|
||||
}
|
||||
|
||||
var successPromise = new process.Promise();
|
||||
var successPromise = new events.Promise();
|
||||
successPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
successPromise.emitSuccess();
|
||||
|
@ -39,7 +40,7 @@ successPromise.addErrback(function() {
|
|||
assert.ok(false, 'addErrback should not fire if there is no timeout');
|
||||
});
|
||||
|
||||
var errorPromise = new process.Promise();
|
||||
var errorPromise = new events.Promise();
|
||||
errorPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
errorPromise.emitError(new Error('intentional'));
|
||||
|
@ -50,7 +51,7 @@ errorPromise.addErrback(function(e) {
|
|||
assert.equal('intentional', e.message);
|
||||
});
|
||||
|
||||
var cancelPromise = new process.Promise();
|
||||
var cancelPromise = new events.Promise();
|
||||
cancelPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
cancelPromise.cancel();
|
||||
|
@ -68,7 +69,7 @@ cancelPromise.addErrback(function(e) {
|
|||
assert.ok(false, 'addErrback should not fire if the promise is canceled');
|
||||
});
|
||||
|
||||
var cancelTimeoutPromise = new process.Promise();
|
||||
var cancelTimeoutPromise = new events.Promise();
|
||||
cancelTimeoutPromise.timeout(500);
|
||||
setTimeout(function() {
|
||||
cancelPromise.emitCancel();
|
||||
|
@ -80,4 +81,4 @@ cancelPromise.addErrback(function(e) {
|
|||
|
||||
process.addListener('exit', function() {
|
||||
assert.equal(2, timeouts);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
process.mixin(require("./common"));
|
||||
events = require('events');
|
||||
|
||||
var p1_done = false;
|
||||
var p1 = new process.Promise();
|
||||
var p1 = new events.Promise();
|
||||
p1.addCallback(function () {
|
||||
assert.equal(1, arguments.length);
|
||||
assert.equal("single arg", arguments[0]);
|
||||
|
@ -9,7 +10,7 @@ p1.addCallback(function () {
|
|||
});
|
||||
|
||||
var p2_done = false;
|
||||
var p2 = new process.Promise();
|
||||
var p2 = new events.Promise();
|
||||
p2.addCallback(function () {
|
||||
p2_done = true;
|
||||
setTimeout(function () {
|
||||
|
@ -18,7 +19,7 @@ p2.addCallback(function () {
|
|||
});
|
||||
|
||||
var p3_done = false;
|
||||
var p3 = new process.Promise();
|
||||
var p3 = new events.Promise();
|
||||
p3.addCallback(function () {
|
||||
p3_done = true;
|
||||
});
|
||||
|
@ -26,7 +27,7 @@ p3.addCallback(function () {
|
|||
|
||||
|
||||
var p4_done = false;
|
||||
var p4 = new process.Promise();
|
||||
var p4 = new events.Promise();
|
||||
p4.addCallback(function () {
|
||||
assert.equal(3, arguments.length);
|
||||
assert.equal("a", arguments[0]);
|
||||
|
@ -36,7 +37,7 @@ p4.addCallback(function () {
|
|||
});
|
||||
|
||||
var p5_done = false;
|
||||
var p5 = new process.Promise();
|
||||
var p5 = new events.Promise();
|
||||
p5.addCallback(function () {
|
||||
p5_done = true;
|
||||
setTimeout(function () {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
process.mixin(require("./common"));
|
||||
var events = require('events');
|
||||
|
||||
function timer (t) {
|
||||
var promise = new process.Promise();
|
||||
var promise = new events.Promise();
|
||||
setTimeout(function () {
|
||||
promise.emitSuccess();
|
||||
}, t);
|
||||
|
|
Загрузка…
Ссылка в новой задаче