Replace onExit() with process.addListener("exit")
- Update documentation. - Depreciation message for onExit().
This commit is contained in:
Родитель
6025da2153
Коммит
723c7d9f7c
27
src/node.js
27
src/node.js
|
@ -20,14 +20,14 @@ node.tcp.createConnection = function (port, host) {
|
|||
// Timers
|
||||
|
||||
function setTimeout (callback, after) {
|
||||
var timer = new node.Timer();
|
||||
var timer = new node.Timer();
|
||||
timer.addListener("timeout", callback);
|
||||
timer.start(after, 0);
|
||||
return timer;
|
||||
}
|
||||
|
||||
function setInterval (callback, repeat) {
|
||||
var timer = new node.Timer();
|
||||
var timer = new node.Timer();
|
||||
timer.addListener("timeout", callback);
|
||||
timer.start(repeat, repeat);
|
||||
return timer;
|
||||
|
@ -96,7 +96,7 @@ node.Module.prototype.load = function (callback) {
|
|||
self.loadPromise = loadPromise;
|
||||
|
||||
var cat_promise = node.cat(self.filename, "utf8");
|
||||
|
||||
|
||||
cat_promise.addErrback(function () {
|
||||
node.stdio.writeError("Error reading " + self.filename + "\n");
|
||||
loadPromise.emitError();
|
||||
|
@ -124,13 +124,15 @@ node.Module.prototype.load = function (callback) {
|
|||
|
||||
self.onLoad = self.target.__onLoad;
|
||||
self.onExit = self.target.__onExit;
|
||||
if (self.onLoad || self.onExit) {
|
||||
node.stdio.writeError( "(node) onLoad is depreciated it will be "
|
||||
+ "removed in the future. Don't want it to "
|
||||
+ "leave? Discuss on mailing list.\n"
|
||||
);
|
||||
}
|
||||
|
||||
self.waitChildrenLoad(function () {
|
||||
if (self.onLoad) {
|
||||
node.stdio.writeError( "(node) onLoad is depreciated it will be "
|
||||
+ "removed in the future. Don't want it to "
|
||||
+ "leave? Discuss on mailing list.\n"
|
||||
);
|
||||
self.onLoad();
|
||||
}
|
||||
self.loaded = true;
|
||||
|
@ -140,7 +142,7 @@ node.Module.prototype.load = function (callback) {
|
|||
};
|
||||
|
||||
node.Module.prototype.newChild = function (path, target) {
|
||||
var child = new node.Module({
|
||||
var child = new node.Module({
|
||||
target: target,
|
||||
path: path,
|
||||
base_directory: node.path.dirname(this.filename),
|
||||
|
@ -152,7 +154,7 @@ node.Module.prototype.newChild = function (path, target) {
|
|||
};
|
||||
|
||||
node.Module.prototype.waitChildrenLoad = function (callback) {
|
||||
var nloaded = 0;
|
||||
var nloaded = 0;
|
||||
var children = this.children;
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
var child = children[i];
|
||||
|
@ -175,7 +177,7 @@ node.Module.prototype.exitChildren = function (callback) {
|
|||
for (var i = 0; i < children.length; i++) {
|
||||
children[i].exit(function () {
|
||||
nexited += 1;
|
||||
if (nexited == children.length && callback) callback();
|
||||
if (nexited == children.length && callback) callback();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -197,14 +199,15 @@ node.Module.prototype.exit = function (callback) {
|
|||
(function () {
|
||||
// Load the root module--the command line argument.
|
||||
var root_module = new node.Module({
|
||||
path: node.path.filename(ARGV[1]),
|
||||
path: node.path.filename(ARGV[1]),
|
||||
base_directory: node.path.dirname(ARGV[1]),
|
||||
target: this
|
||||
target: this
|
||||
});
|
||||
root_module.load();
|
||||
|
||||
node.exit = function (code) {
|
||||
root_module.exit(function () {
|
||||
process.emit("exit");
|
||||
node.reallyExit(code);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -13,6 +13,6 @@ exports.D = function () {
|
|||
return c.D();
|
||||
};
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
string = "A done";
|
||||
}
|
||||
});
|
||||
|
|
|
@ -10,6 +10,6 @@ exports.D = function () {
|
|||
return d.D();
|
||||
};
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
string = "C done";
|
||||
}
|
||||
});
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.D = function () {
|
|||
return string;
|
||||
};
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
string = "D done";
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -21,9 +21,9 @@ puts("start");
|
|||
|
||||
e.emit("hello", ["a", "b"]);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertArrayEquals(["hello"], events_new_listener_emited);
|
||||
assertEquals(1, times_hello_emited);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,6 @@ promise.addErrback(function () {
|
|||
got_error = true;
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(got_error);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -16,9 +16,9 @@ promise.addErrback(function () {
|
|||
got_error = true;
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(got_success);
|
||||
assertFalse(got_error);
|
||||
assertTrue(stats.mtime instanceof Date);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ node.http.cat("http://localhost:12312/", "utf8").addErrback(function () {
|
|||
bad_server_got_error = true;
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(got_good_server_content);
|
||||
assertTrue(bad_server_got_error);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -35,7 +35,7 @@ client.get("/1").finish(function (res1) {
|
|||
});
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(body1_s, body1);
|
||||
assertEquals(body2_s, body2);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -43,8 +43,8 @@ req.finish(function(res) {
|
|||
});
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals("1\n2\n3\n", sent_body);
|
||||
assertTrue(server_req_complete);
|
||||
assertTrue(client_res_complete);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -47,6 +47,6 @@ req.finish(function (res) {
|
|||
});
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(body, "hello world\n");
|
||||
}
|
||||
});
|
||||
|
|
|
@ -59,7 +59,7 @@ c.addListener("close", function () {
|
|||
assertEquals(c.readyState, "closed");
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(2, request_number);
|
||||
assertEquals(2, requests_sent);
|
||||
|
||||
|
@ -70,4 +70,4 @@ function onExit () {
|
|||
assertTrue(quit.exec(server_response) != null);
|
||||
|
||||
assertTrue(client_got_eof);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -56,7 +56,7 @@ setTimeout(function () {
|
|||
});
|
||||
}, 1);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
node.debug("responses_recvd: " + responses_recvd);
|
||||
assertEquals(2, responses_recvd);
|
||||
|
||||
|
@ -65,5 +65,5 @@ function onExit () {
|
|||
|
||||
assertEquals("The path was /hello", body0);
|
||||
assertEquals("The path was /world", body1);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ assertEquals("D", d.D());
|
|||
assertInstanceof(d2.D, Function);
|
||||
assertEquals("D", d2.D());
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertInstanceof(a.A, Function);
|
||||
assertEquals("A done", a.A());
|
||||
|
||||
|
@ -35,4 +35,4 @@ function onExit () {
|
|||
|
||||
assertInstanceof(d2.D, Function);
|
||||
assertEquals("D done", d2.D());
|
||||
}
|
||||
});
|
||||
|
|
|
@ -45,7 +45,7 @@ promise.addErrback(function () {
|
|||
errors += 1;
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(2, successes);
|
||||
assertEquals(0, errors);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -24,6 +24,6 @@ pwd(function (result) {
|
|||
assertEquals("\n", result[result.length-1]);
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(pwd_called);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -10,6 +10,6 @@ cat.addListener("exit", function (status) { exit_status = status; });
|
|||
|
||||
cat.kill();
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(exit_status > 0);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -28,7 +28,7 @@ cat.write("hello");
|
|||
cat.write(" ");
|
||||
cat.write("world");
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(0, exit_status);
|
||||
assertEquals("hello world", response);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -22,6 +22,6 @@ function spawn (i) {
|
|||
|
||||
spawn(0);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(finished);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -73,10 +73,10 @@ assertArrayEquals(["a","b","c"], ret4);
|
|||
|
||||
assertTrue(p4_done);
|
||||
|
||||
function onExit() {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(p1_done);
|
||||
assertTrue(p2_done);
|
||||
assertTrue(p3_done);
|
||||
assertTrue(p4_done);
|
||||
assertTrue(p5_done);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -62,7 +62,7 @@ for (var i = 0; i < concurrency; i++) {
|
|||
});
|
||||
}
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(connections_per_client * concurrency, total_connections);
|
||||
puts("\nokay!");
|
||||
}
|
||||
});
|
||||
|
|
|
@ -85,6 +85,6 @@ function pingPongTest (port, host, on_complete) {
|
|||
|
||||
pingPongTest(21988);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(1, tests_run);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -82,6 +82,6 @@ pingPongTest(20989, "localhost");
|
|||
pingPongTest(20988, null);
|
||||
pingPongTest(20997, "::1");
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(3, tests_run);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -36,10 +36,10 @@ c.addListener("close", function () {
|
|||
echoServer.close();
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
var expected = [];
|
||||
for (var i = 0; i < 256; i++) {
|
||||
expected.push(i);
|
||||
}
|
||||
assertEquals(expected, recv);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -46,7 +46,7 @@ client.addListener("close", function (had_error) {
|
|||
server.close();
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(N+1, disconnect_count);
|
||||
assertEquals(N+1, client_recv_count);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -49,7 +49,7 @@ client.addListener("eof", function () {
|
|||
client.close();
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(N, chars_recved);
|
||||
assertTrue(npauses > 2);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -61,6 +61,6 @@ client.addListener("eof", function () {
|
|||
client.close();
|
||||
});
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertEquals(N, recv.length);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -35,7 +35,7 @@ setInterval(function () {
|
|||
clearInterval(this);
|
||||
}, 1000);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(setTimeout_called);
|
||||
assertEquals(3, interval_count);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -258,12 +258,12 @@ puts("The area of a cirlce of radius 4 is " + area(4));
|
|||
Functions +require_async()+ and +include_async()+ also exist.
|
||||
|
||||
|
||||
==== +onExit()+
|
||||
==== +process.addListener("exit", function () { })+
|
||||
|
||||
When the program exits a callback +onExit()+ will be called for each module
|
||||
(children first).
|
||||
When the program exits a special object called +process+ will emit an
|
||||
+"exit"+ event.
|
||||
|
||||
The +onExit()+ callback cannot perform I/O since the process is going to
|
||||
The +"exit"+ event cannot perform I/O since the process is going to
|
||||
forcably exit in less than microsecond. However, it is a good hook to
|
||||
perform constant time checks of the module's state. E.G. for unit tests:
|
||||
|
||||
|
@ -276,13 +276,13 @@ setTimeout(function () {
|
|||
timer_executed = true
|
||||
}, 1000);
|
||||
|
||||
function onExit () {
|
||||
process.addListener("exit", function () {
|
||||
assertTrue(timer_executed);
|
||||
}
|
||||
});
|
||||
----------------------------------------
|
||||
|
||||
Just to reiterate: +onExit()+, is not the place to close files or shutdown
|
||||
servers. The process will exit before they get performed.
|
||||
Just to reiterate: the +"exit"+ event, is not the place to close files or
|
||||
shutdown servers. The process will exit before they get performed.
|
||||
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче