mirror of https://github.com/lukechilds/node.git
Ryan Dahl
15 years ago
6 changed files with 183 additions and 193 deletions
@ -1,113 +0,0 @@ |
|||
(function () { |
|||
|
|||
// 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.listeners = function (type) { |
|||
if (!this._events) this._events = {}; |
|||
if (!this._events.hasOwnProperty(type)) this._events[type] = []; |
|||
return this._events[type]; |
|||
}; |
|||
|
|||
process.Promise.prototype.timeout = function(timeout) { |
|||
if (timeout === undefined) { |
|||
return this._timeoutDuration; |
|||
} |
|||
|
|||
this._timeoutDuration = timeout; |
|||
if (this._timer) { |
|||
clearTimeout(this._timer); |
|||
} |
|||
|
|||
var promiseComplete = false; |
|||
var onComplete = function() { |
|||
promiseComplete = true; |
|||
}; |
|||
|
|||
this |
|||
.addCallback(onComplete) |
|||
.addCancelback(onComplete) |
|||
.addErrback(onComplete); |
|||
|
|||
var self = this |
|||
this._timer = setTimeout(function() { |
|||
if (promiseComplete) { |
|||
return; |
|||
} |
|||
|
|||
self.emitError(new Error('timeout')); |
|||
}, this._timeoutDuration); |
|||
|
|||
return this; |
|||
}; |
|||
|
|||
process.Promise.prototype.cancel = function() { |
|||
this._events['success'] = []; |
|||
this._events['error'] = []; |
|||
|
|||
this.emitCancel(); |
|||
}; |
|||
|
|||
process.Promise.prototype.emitCancel = function() { |
|||
var args = Array.prototype.slice.call(arguments); |
|||
args.unshift('cancel'); |
|||
|
|||
this.emit.apply(this, args); |
|||
}; |
|||
|
|||
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; |
|||
}; |
|||
|
|||
process.Promise.prototype.wait = function () { |
|||
var ret; |
|||
var had_error = false; |
|||
this.addCallback(function () { |
|||
if (arguments.length == 1) { |
|||
ret = arguments[0]; |
|||
} else if (arguments.length > 1) { |
|||
ret = []; |
|||
for (var i = 0; i < arguments.length; i++) { |
|||
ret.push(arguments[i]); |
|||
} |
|||
} |
|||
}) |
|||
.addErrback(function (arg) { |
|||
had_error = true; |
|||
ret = arg; |
|||
}) |
|||
.block(); |
|||
|
|||
if (had_error) { |
|||
if (ret) { |
|||
throw ret; |
|||
} else { |
|||
throw new Error("Promise completed with error (No arguments given.)"); |
|||
} |
|||
} |
|||
return ret; |
|||
}; |
|||
|
|||
})(); // end anonymous namespace
|
@ -1,34 +0,0 @@ |
|||
|
|||
process.fs.cat = function (path, encoding) { |
|||
var promise = new process.Promise(); |
|||
|
|||
encoding = encoding || "utf8"; // default to utf8
|
|||
|
|||
process.fs.open(path, process.O_RDONLY, 0666).addCallback(function (fd) { |
|||
var content = "", pos = 0; |
|||
|
|||
function readChunk () { |
|||
process.fs.read(fd, 16*1024, pos, encoding).addCallback(function (chunk, bytes_read) { |
|||
if (chunk) { |
|||
if (chunk.constructor === String) { |
|||
content += chunk; |
|||
} else { |
|||
content = content.concat(chunk); |
|||
} |
|||
|
|||
pos += bytes_read; |
|||
readChunk(); |
|||
} else { |
|||
promise.emitSuccess(content); |
|||
process.fs.close(fd); |
|||
} |
|||
}).addErrback(function () { |
|||
promise.emitError(); |
|||
}); |
|||
} |
|||
readChunk(); |
|||
}).addErrback(function () { |
|||
promise.emitError(new Error("Could not open " + path)); |
|||
}); |
|||
return promise; |
|||
}; |
@ -1,37 +0,0 @@ |
|||
/** |
|||
* Inherit the prototype methods from one constructor into another. |
|||
* |
|||
* The Function.prototype.inherits from lang.js rewritten as a standalone |
|||
* function (not on Function.prototype). NOTE: If this file is to be loaded |
|||
* during bootstrapping this function needs to be revritten using some native |
|||
* functions as prototype setup using normal JavaScript does not work as |
|||
* expected during bootstrapping (see mirror.js in r114903). |
|||
* |
|||
* @param {function} ctor Constructor function which needs to inherit the |
|||
* prototype |
|||
* @param {function} superCtor Constructor function to inherit prototype from |
|||
*/ |
|||
process.inherits = function (ctor, superCtor) { |
|||
var tempCtor = function(){}; |
|||
tempCtor.prototype = superCtor.prototype; |
|||
ctor.super_ = superCtor.prototype; |
|||
ctor.prototype = new tempCtor(); |
|||
ctor.prototype.constructor = ctor; |
|||
}; |
|||
|
|||
process.assert = function (x, msg) { |
|||
if (!(x)) throw new Error(msg || "assertion error"); |
|||
}; |
|||
|
|||
process.cat = function(location, encoding) { |
|||
var url_re = new RegExp("^http:\/\/"); |
|||
if (url_re.exec(location)) { |
|||
throw new Error("process.cat for http urls is temporarally disabled."); |
|||
} |
|||
//var f = url_re.exec(location) ? process.http.cat : process.fs.cat;
|
|||
//return f(location, encoding);
|
|||
return process.fs.cat(location, encoding); |
|||
}; |
|||
|
|||
|
|||
|
Loading…
Reference in new issue