Browse Source

Replace onExit() with process.addListener("exit")

- Update documentation.

- Depreciation message for onExit().
v0.7.4-release
Ryan 16 years ago
parent
commit
723c7d9f7c
  1. 27
      src/node.js
  2. 4
      test/mjsunit/fixtures/a.js
  3. 4
      test/mjsunit/fixtures/b/c.js
  4. 4
      test/mjsunit/fixtures/b/d.js
  5. 4
      test/mjsunit/test-event-emitter-add-listeners.js
  6. 4
      test/mjsunit/test-file-cat-noexist.js
  7. 4
      test/mjsunit/test-fs-stat.js
  8. 4
      test/mjsunit/test-http-cat.js
  9. 4
      test/mjsunit/test-http-client-race.js
  10. 4
      test/mjsunit/test-http-client-upload.js
  11. 4
      test/mjsunit/test-http-proxy.js
  12. 4
      test/mjsunit/test-http-server.js
  13. 4
      test/mjsunit/test-http.js
  14. 4
      test/mjsunit/test-module-loading.js
  15. 4
      test/mjsunit/test-node-cat.js
  16. 4
      test/mjsunit/test-process-buffering.js
  17. 4
      test/mjsunit/test-process-kill.js
  18. 4
      test/mjsunit/test-process-simple.js
  19. 4
      test/mjsunit/test-process-spawn-loop.js
  20. 4
      test/mjsunit/test-promise-wait.js
  21. 4
      test/mjsunit/test-tcp-many-clients.js
  22. 4
      test/mjsunit/test-tcp-pingpong-delay.js
  23. 4
      test/mjsunit/test-tcp-pingpong.js
  24. 4
      test/mjsunit/test-tcp-raw.js
  25. 4
      test/mjsunit/test-tcp-reconnect.js
  26. 4
      test/mjsunit/test-tcp-throttle-kernel-buffer.js
  27. 4
      test/mjsunit/test-tcp-throttle.js
  28. 4
      test/mjsunit/test-timers.js
  29. 16
      website/api.txt

27
src/node.js

@ -20,14 +20,14 @@ node.tcp.createConnection = function (port, host) {
// Timers // Timers
function setTimeout (callback, after) { function setTimeout (callback, after) {
var timer = new node.Timer(); var timer = new node.Timer();
timer.addListener("timeout", callback); timer.addListener("timeout", callback);
timer.start(after, 0); timer.start(after, 0);
return timer; return timer;
} }
function setInterval (callback, repeat) { function setInterval (callback, repeat) {
var timer = new node.Timer(); var timer = new node.Timer();
timer.addListener("timeout", callback); timer.addListener("timeout", callback);
timer.start(repeat, repeat); timer.start(repeat, repeat);
return timer; return timer;
@ -96,7 +96,7 @@ node.Module.prototype.load = function (callback) {
self.loadPromise = loadPromise; self.loadPromise = loadPromise;
var cat_promise = node.cat(self.filename, "utf8"); var cat_promise = node.cat(self.filename, "utf8");
cat_promise.addErrback(function () { cat_promise.addErrback(function () {
node.stdio.writeError("Error reading " + self.filename + "\n"); node.stdio.writeError("Error reading " + self.filename + "\n");
loadPromise.emitError(); loadPromise.emitError();
@ -124,13 +124,15 @@ node.Module.prototype.load = function (callback) {
self.onLoad = self.target.__onLoad; self.onLoad = self.target.__onLoad;
self.onExit = self.target.__onExit; 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 () { self.waitChildrenLoad(function () {
if (self.onLoad) { 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.onLoad();
} }
self.loaded = true; self.loaded = true;
@ -140,7 +142,7 @@ node.Module.prototype.load = function (callback) {
}; };
node.Module.prototype.newChild = function (path, target) { node.Module.prototype.newChild = function (path, target) {
var child = new node.Module({ var child = new node.Module({
target: target, target: target,
path: path, path: path,
base_directory: node.path.dirname(this.filename), base_directory: node.path.dirname(this.filename),
@ -152,7 +154,7 @@ node.Module.prototype.newChild = function (path, target) {
}; };
node.Module.prototype.waitChildrenLoad = function (callback) { node.Module.prototype.waitChildrenLoad = function (callback) {
var nloaded = 0; var nloaded = 0;
var children = this.children; var children = this.children;
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
var child = children[i]; var child = children[i];
@ -175,7 +177,7 @@ node.Module.prototype.exitChildren = function (callback) {
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
children[i].exit(function () { children[i].exit(function () {
nexited += 1; 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 () { (function () {
// Load the root module--the command line argument. // Load the root module--the command line argument.
var root_module = new node.Module({ 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]), base_directory: node.path.dirname(ARGV[1]),
target: this target: this
}); });
root_module.load(); root_module.load();
node.exit = function (code) { node.exit = function (code) {
root_module.exit(function () { root_module.exit(function () {
process.emit("exit");
node.reallyExit(code); node.reallyExit(code);
}); });
}; };

4
test/mjsunit/fixtures/a.js

@ -13,6 +13,6 @@ exports.D = function () {
return c.D(); return c.D();
}; };
function onExit () { process.addListener("exit", function () {
string = "A done"; string = "A done";
} });

4
test/mjsunit/fixtures/b/c.js

@ -10,6 +10,6 @@ exports.D = function () {
return d.D(); return d.D();
}; };
function onExit () { process.addListener("exit", function () {
string = "C done"; string = "C done";
} });

4
test/mjsunit/fixtures/b/d.js

@ -4,7 +4,7 @@ exports.D = function () {
return string; return string;
}; };
function onExit () { process.addListener("exit", function () {
string = "D done"; string = "D done";
} });

4
test/mjsunit/test-event-emitter-add-listeners.js

@ -21,9 +21,9 @@ puts("start");
e.emit("hello", ["a", "b"]); e.emit("hello", ["a", "b"]);
function onExit () { process.addListener("exit", function () {
assertArrayEquals(["hello"], events_new_listener_emited); assertArrayEquals(["hello"], events_new_listener_emited);
assertEquals(1, times_hello_emited); assertEquals(1, times_hello_emited);
} });

4
test/mjsunit/test-file-cat-noexist.js

@ -16,6 +16,6 @@ promise.addErrback(function () {
got_error = true; got_error = true;
}); });
function onExit () { process.addListener("exit", function () {
assertTrue(got_error); assertTrue(got_error);
} });

4
test/mjsunit/test-fs-stat.js

@ -16,9 +16,9 @@ promise.addErrback(function () {
got_error = true; got_error = true;
}); });
function onExit () { process.addListener("exit", function () {
assertTrue(got_success); assertTrue(got_success);
assertFalse(got_error); assertFalse(got_error);
assertTrue(stats.mtime instanceof Date); assertTrue(stats.mtime instanceof Date);
} });

4
test/mjsunit/test-http-cat.js

@ -28,7 +28,7 @@ node.http.cat("http://localhost:12312/", "utf8").addErrback(function () {
bad_server_got_error = true; bad_server_got_error = true;
}); });
function onExit () { process.addListener("exit", function () {
assertTrue(got_good_server_content); assertTrue(got_good_server_content);
assertTrue(bad_server_got_error); assertTrue(bad_server_got_error);
} });

4
test/mjsunit/test-http-client-race.js

@ -35,7 +35,7 @@ client.get("/1").finish(function (res1) {
}); });
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(body1_s, body1); assertEquals(body1_s, body1);
assertEquals(body2_s, body2); assertEquals(body2_s, body2);
} });

4
test/mjsunit/test-http-client-upload.js

@ -43,8 +43,8 @@ req.finish(function(res) {
}); });
}); });
function onExit () { process.addListener("exit", function () {
assertEquals("1\n2\n3\n", sent_body); assertEquals("1\n2\n3\n", sent_body);
assertTrue(server_req_complete); assertTrue(server_req_complete);
assertTrue(client_res_complete); assertTrue(client_res_complete);
} });

4
test/mjsunit/test-http-proxy.js

@ -47,6 +47,6 @@ req.finish(function (res) {
}); });
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(body, "hello world\n"); assertEquals(body, "hello world\n");
} });

4
test/mjsunit/test-http-server.js

@ -59,7 +59,7 @@ c.addListener("close", function () {
assertEquals(c.readyState, "closed"); assertEquals(c.readyState, "closed");
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(2, request_number); assertEquals(2, request_number);
assertEquals(2, requests_sent); assertEquals(2, requests_sent);
@ -70,4 +70,4 @@ function onExit () {
assertTrue(quit.exec(server_response) != null); assertTrue(quit.exec(server_response) != null);
assertTrue(client_got_eof); assertTrue(client_got_eof);
} });

4
test/mjsunit/test-http.js

@ -56,7 +56,7 @@ setTimeout(function () {
}); });
}, 1); }, 1);
function onExit () { process.addListener("exit", function () {
node.debug("responses_recvd: " + responses_recvd); node.debug("responses_recvd: " + responses_recvd);
assertEquals(2, responses_recvd); assertEquals(2, responses_recvd);
@ -65,5 +65,5 @@ function onExit () {
assertEquals("The path was /hello", body0); assertEquals("The path was /hello", body0);
assertEquals("The path was /world", body1); assertEquals("The path was /world", body1);
} });

4
test/mjsunit/test-module-loading.js

@ -20,7 +20,7 @@ assertEquals("D", d.D());
assertInstanceof(d2.D, Function); assertInstanceof(d2.D, Function);
assertEquals("D", d2.D()); assertEquals("D", d2.D());
function onExit () { process.addListener("exit", function () {
assertInstanceof(a.A, Function); assertInstanceof(a.A, Function);
assertEquals("A done", a.A()); assertEquals("A done", a.A());
@ -35,4 +35,4 @@ function onExit () {
assertInstanceof(d2.D, Function); assertInstanceof(d2.D, Function);
assertEquals("D done", d2.D()); assertEquals("D done", d2.D());
} });

4
test/mjsunit/test-node-cat.js

@ -45,7 +45,7 @@ promise.addErrback(function () {
errors += 1; errors += 1;
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(2, successes); assertEquals(2, successes);
assertEquals(0, errors); assertEquals(0, errors);
} });

4
test/mjsunit/test-process-buffering.js

@ -24,6 +24,6 @@ pwd(function (result) {
assertEquals("\n", result[result.length-1]); assertEquals("\n", result[result.length-1]);
}); });
function onExit () { process.addListener("exit", function () {
assertTrue(pwd_called); assertTrue(pwd_called);
} });

4
test/mjsunit/test-process-kill.js

@ -10,6 +10,6 @@ cat.addListener("exit", function (status) { exit_status = status; });
cat.kill(); cat.kill();
function onExit () { process.addListener("exit", function () {
assertTrue(exit_status > 0); assertTrue(exit_status > 0);
} });

4
test/mjsunit/test-process-simple.js

@ -28,7 +28,7 @@ cat.write("hello");
cat.write(" "); cat.write(" ");
cat.write("world"); cat.write("world");
function onExit () { process.addListener("exit", function () {
assertEquals(0, exit_status); assertEquals(0, exit_status);
assertEquals("hello world", response); assertEquals("hello world", response);
} });

4
test/mjsunit/test-process-spawn-loop.js

@ -22,6 +22,6 @@ function spawn (i) {
spawn(0); spawn(0);
function onExit () { process.addListener("exit", function () {
assertTrue(finished); assertTrue(finished);
} });

4
test/mjsunit/test-promise-wait.js

@ -73,10 +73,10 @@ assertArrayEquals(["a","b","c"], ret4);
assertTrue(p4_done); assertTrue(p4_done);
function onExit() { process.addListener("exit", function () {
assertTrue(p1_done); assertTrue(p1_done);
assertTrue(p2_done); assertTrue(p2_done);
assertTrue(p3_done); assertTrue(p3_done);
assertTrue(p4_done); assertTrue(p4_done);
assertTrue(p5_done); assertTrue(p5_done);
} });

4
test/mjsunit/test-tcp-many-clients.js

@ -62,7 +62,7 @@ for (var i = 0; i < concurrency; i++) {
}); });
} }
function onExit () { process.addListener("exit", function () {
assertEquals(connections_per_client * concurrency, total_connections); assertEquals(connections_per_client * concurrency, total_connections);
puts("\nokay!"); puts("\nokay!");
} });

4
test/mjsunit/test-tcp-pingpong-delay.js

@ -85,6 +85,6 @@ function pingPongTest (port, host, on_complete) {
pingPongTest(21988); pingPongTest(21988);
function onExit () { process.addListener("exit", function () {
assertEquals(1, tests_run); assertEquals(1, tests_run);
} });

4
test/mjsunit/test-tcp-pingpong.js

@ -82,6 +82,6 @@ pingPongTest(20989, "localhost");
pingPongTest(20988, null); pingPongTest(20988, null);
pingPongTest(20997, "::1"); pingPongTest(20997, "::1");
function onExit () { process.addListener("exit", function () {
assertEquals(3, tests_run); assertEquals(3, tests_run);
} });

4
test/mjsunit/test-tcp-raw.js

@ -36,10 +36,10 @@ c.addListener("close", function () {
echoServer.close(); echoServer.close();
}); });
function onExit () { process.addListener("exit", function () {
var expected = []; var expected = [];
for (var i = 0; i < 256; i++) { for (var i = 0; i < 256; i++) {
expected.push(i); expected.push(i);
} }
assertEquals(expected, recv); assertEquals(expected, recv);
} });

4
test/mjsunit/test-tcp-reconnect.js

@ -46,7 +46,7 @@ client.addListener("close", function (had_error) {
server.close(); server.close();
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(N+1, disconnect_count); assertEquals(N+1, disconnect_count);
assertEquals(N+1, client_recv_count); assertEquals(N+1, client_recv_count);
} });

4
test/mjsunit/test-tcp-throttle-kernel-buffer.js

@ -49,7 +49,7 @@ client.addListener("eof", function () {
client.close(); client.close();
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(N, chars_recved); assertEquals(N, chars_recved);
assertTrue(npauses > 2); assertTrue(npauses > 2);
} });

4
test/mjsunit/test-tcp-throttle.js

@ -61,6 +61,6 @@ client.addListener("eof", function () {
client.close(); client.close();
}); });
function onExit () { process.addListener("exit", function () {
assertEquals(N, recv.length); assertEquals(N, recv.length);
} });

4
test/mjsunit/test-timers.js

@ -35,7 +35,7 @@ setInterval(function () {
clearInterval(this); clearInterval(this);
}, 1000); }, 1000);
function onExit () { process.addListener("exit", function () {
assertTrue(setTimeout_called); assertTrue(setTimeout_called);
assertEquals(3, interval_count); assertEquals(3, interval_count);
} });

16
website/api.txt

@ -258,12 +258,12 @@ puts("The area of a cirlce of radius 4 is " + area(4));
Functions +require_async()+ and +include_async()+ also exist. 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 When the program exits a special object called +process+ will emit an
(children first). +"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 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: perform constant time checks of the module's state. E.G. for unit tests:
@ -276,13 +276,13 @@ setTimeout(function () {
timer_executed = true timer_executed = true
}, 1000); }, 1000);
function onExit () { process.addListener("exit", function () {
assertTrue(timer_executed); assertTrue(timer_executed);
} });
---------------------------------------- ----------------------------------------
Just to reiterate: +onExit()+, is not the place to close files or shutdown Just to reiterate: the +"exit"+ event, is not the place to close files or
servers. The process will exit before they get performed. shutdown servers. The process will exit before they get performed.

Loading…
Cancel
Save