Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

- Update documentation.

- Depreciation message for onExit().
  • Loading branch information...
commit 723c7d9f7c4e5ba9600adae95bd481fad70a3d60 1 parent 6025da2
@ry ry authored
Showing with 77 additions and 74 deletions.
  1. +15 −12 src/node.js
  2. +2 −2 test/mjsunit/fixtures/a.js
  3. +2 −2 test/mjsunit/fixtures/b/c.js
  4. +2 −2 test/mjsunit/fixtures/b/d.js
  5. +2 −2 test/mjsunit/test-event-emitter-add-listeners.js
  6. +2 −2 test/mjsunit/test-file-cat-noexist.js
  7. +2 −2 test/mjsunit/test-fs-stat.js
  8. +2 −2 test/mjsunit/test-http-cat.js
  9. +2 −2 test/mjsunit/test-http-client-race.js
  10. +2 −2 test/mjsunit/test-http-client-upload.js
  11. +2 −2 test/mjsunit/test-http-proxy.js
  12. +2 −2 test/mjsunit/test-http-server.js
  13. +2 −2 test/mjsunit/test-http.js
  14. +2 −2 test/mjsunit/test-module-loading.js
  15. +2 −2 test/mjsunit/test-node-cat.js
  16. +2 −2 test/mjsunit/test-process-buffering.js
  17. +2 −2 test/mjsunit/test-process-kill.js
  18. +2 −2 test/mjsunit/test-process-simple.js
  19. +2 −2 test/mjsunit/test-process-spawn-loop.js
  20. +2 −2 test/mjsunit/test-promise-wait.js
  21. +2 −2 test/mjsunit/test-tcp-many-clients.js
  22. +2 −2 test/mjsunit/test-tcp-pingpong-delay.js
  23. +2 −2 test/mjsunit/test-tcp-pingpong.js
  24. +2 −2 test/mjsunit/test-tcp-raw.js
  25. +2 −2 test/mjsunit/test-tcp-reconnect.js
  26. +2 −2 test/mjsunit/test-tcp-throttle-kernel-buffer.js
  27. +2 −2 test/mjsunit/test-tcp-throttle.js
  28. +2 −2 test/mjsunit/test-timers.js
  29. +8 −8 website/api.txt
View
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);
});
};
View
4 test/mjsunit/fixtures/a.js
@@ -13,6 +13,6 @@ exports.D = function () {
return c.D();
};
-function onExit () {
+process.addListener("exit", function () {
string = "A done";
-}
+});
View
4 test/mjsunit/fixtures/b/c.js
@@ -10,6 +10,6 @@ exports.D = function () {
return d.D();
};
-function onExit () {
+process.addListener("exit", function () {
string = "C done";
-}
+});
View
4 test/mjsunit/fixtures/b/d.js
@@ -4,7 +4,7 @@ exports.D = function () {
return string;
};
-function onExit () {
+process.addListener("exit", function () {
string = "D done";
-}
+});
View
4 test/mjsunit/test-event-emitter-add-listeners.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-file-cat-noexist.js
@@ -16,6 +16,6 @@ promise.addErrback(function () {
got_error = true;
});
-function onExit () {
+process.addListener("exit", function () {
assertTrue(got_error);
-}
+});
View
4 test/mjsunit/test-fs-stat.js
@@ -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);
-}
+});
View
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;
});
-function onExit () {
+process.addListener("exit", function () {
assertTrue(got_good_server_content);
assertTrue(bad_server_got_error);
-}
+});
View
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(body2_s, body2);
-}
+});
View
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);
assertTrue(server_req_complete);
assertTrue(client_res_complete);
-}
+});
View
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");
-}
+});
View
4 test/mjsunit/test-http-server.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-http.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-module-loading.js
@@ -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());
-}
+});
View
4 test/mjsunit/test-node-cat.js
@@ -45,7 +45,7 @@ promise.addErrback(function () {
errors += 1;
});
-function onExit () {
+process.addListener("exit", function () {
assertEquals(2, successes);
assertEquals(0, errors);
-}
+});
View
4 test/mjsunit/test-process-buffering.js
@@ -24,6 +24,6 @@ pwd(function (result) {
assertEquals("\n", result[result.length-1]);
});
-function onExit () {
+process.addListener("exit", function () {
assertTrue(pwd_called);
-}
+});
View
4 test/mjsunit/test-process-kill.js
@@ -10,6 +10,6 @@ cat.addListener("exit", function (status) { exit_status = status; });
cat.kill();
-function onExit () {
+process.addListener("exit", function () {
assertTrue(exit_status > 0);
-}
+});
View
4 test/mjsunit/test-process-simple.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-process-spawn-loop.js
@@ -22,6 +22,6 @@ function spawn (i) {
spawn(0);
-function onExit () {
+process.addListener("exit", function () {
assertTrue(finished);
-}
+});
View
4 test/mjsunit/test-promise-wait.js
@@ -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);
-}
+});
View
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);
puts("\nokay!");
-}
+});
View
4 test/mjsunit/test-tcp-pingpong-delay.js
@@ -85,6 +85,6 @@ function pingPongTest (port, host, on_complete) {
pingPongTest(21988);
-function onExit () {
+process.addListener("exit", function () {
assertEquals(1, tests_run);
-}
+});
View
4 test/mjsunit/test-tcp-pingpong.js
@@ -82,6 +82,6 @@ pingPongTest(20989, "localhost");
pingPongTest(20988, null);
pingPongTest(20997, "::1");
-function onExit () {
+process.addListener("exit", function () {
assertEquals(3, tests_run);
-}
+});
View
4 test/mjsunit/test-tcp-raw.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-tcp-reconnect.js
@@ -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);
-}
+});
View
4 test/mjsunit/test-tcp-throttle-kernel-buffer.js
@@ -49,7 +49,7 @@ client.addListener("eof", function () {
client.close();
});
-function onExit () {
+process.addListener("exit", function () {
assertEquals(N, chars_recved);
assertTrue(npauses > 2);
-}
+});
View
4 test/mjsunit/test-tcp-throttle.js
@@ -61,6 +61,6 @@ client.addListener("eof", function () {
client.close();
});
-function onExit () {
+process.addListener("exit", function () {
assertEquals(N, recv.length);
-}
+});
View
4 test/mjsunit/test-timers.js
@@ -35,7 +35,7 @@ setInterval(function () {
clearInterval(this);
}, 1000);
-function onExit () {
+process.addListener("exit", function () {
assertTrue(setTimeout_called);
assertEquals(3, interval_count);
-}
+});
View
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.
-==== +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.
Please sign in to comment.
Something went wrong with that request. Please try again.