diff options
author | Roman Reiss <me@silverwind.io> | 2015-05-22 19:35:57 +0300 |
---|---|---|
committer | Roman Reiss <me@silverwind.io> | 2015-05-23 07:57:00 +0300 |
commit | 39dde3222e4733fc1b59c45e392d9ff1a88ae4cc (patch) | |
tree | 58561a0bbbbf02422b8b34d1b41e58fcb3b388b9 | |
parent | 9da168b71fb729635ad71e839630480e815623d0 (diff) |
net,dgram: return this from ref and unref methods
Modifies the following methods to return the instance instead
of undefined, to allow for chaining these methods:
- net.Server.ref
- net.Server.unref
- net.Socket.ref
- net.Socket.unref
- dgram.Socket.ref
- dgram.Socket.unref
PR-URL: https://github.com/nodejs/io.js/pull/1768
Reviewed-By: Evan Lucas <evanlucas@me.com>
-rw-r--r-- | doc/api/dgram.markdown | 4 | ||||
-rw-r--r-- | doc/api/net.markdown | 8 | ||||
-rw-r--r-- | lib/dgram.js | 4 | ||||
-rw-r--r-- | lib/net.js | 12 | ||||
-rw-r--r-- | test/parallel/test-ref-unref-return.js | 12 |
5 files changed, 38 insertions, 2 deletions
diff --git a/doc/api/dgram.markdown b/doc/api/dgram.markdown index 202e907f5a5..7c13d1746f7 100644 --- a/doc/api/dgram.markdown +++ b/doc/api/dgram.markdown @@ -306,8 +306,12 @@ Calling `unref` on a socket will allow the program to exit if this is the only active socket in the event system. If the socket is already `unref`d calling `unref` again will have no effect. +Returns `socket`. + ### socket.ref() Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not* let the program exit if it's the only socket left (the default behavior). If the socket is `ref`d calling `ref` again will have no effect. + +Returns `socket`. diff --git a/doc/api/net.markdown b/doc/api/net.markdown index a3a1134476f..46fc46fde4f 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -261,12 +261,16 @@ Calling `unref` on a server will allow the program to exit if this is the only active server in the event system. If the server is already `unref`d calling `unref` again will have no effect. +Returns `server`. + ### server.ref() Opposite of `unref`, calling `ref` on a previously `unref`d server will *not* let the program exit if it's the only server left (the default behavior). If the server is `ref`d calling `ref` again will have no effect. +Returns `server`. + ### server.maxConnections Set this property to reject connections when the server's connection count gets @@ -484,12 +488,16 @@ Calling `unref` on a socket will allow the program to exit if this is the only active socket in the event system. If the socket is already `unref`d calling `unref` again will have no effect. +Returns `socket`. + ### socket.ref() Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not* let the program exit if it's the only socket left (the default behavior). If the socket is `ref`d calling `ref` again will have no effect. +Returns `socket`. + ### socket.remoteAddress The string representation of the remote IP address. For example, diff --git a/lib/dgram.js b/lib/dgram.js index 1cce233eab8..f95b4492699 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -481,10 +481,14 @@ function onMessage(nread, handle, buf, rinfo) { Socket.prototype.ref = function() { if (this._handle) this._handle.ref(); + + return this; }; Socket.prototype.unref = function() { if (this._handle) this._handle.unref(); + + return this; }; diff --git a/lib/net.js b/lib/net.js index 76c6f1d8891..8412bcd38b8 100644 --- a/lib/net.js +++ b/lib/net.js @@ -984,20 +984,24 @@ function connectErrorNT(self, err, options) { Socket.prototype.ref = function() { if (!this._handle) { this.once('connect', this.ref); - return; + return this; } this._handle.ref(); + + return this; }; Socket.prototype.unref = function() { if (!this._handle) { this.once('connect', this.unref); - return; + return this; } this._handle.unref(); + + return this; }; @@ -1506,6 +1510,8 @@ Server.prototype.ref = function() { if (this._handle) this._handle.ref(); + + return this; }; Server.prototype.unref = function() { @@ -1513,6 +1519,8 @@ Server.prototype.unref = function() { if (this._handle) this._handle.unref(); + + return this; }; diff --git a/test/parallel/test-ref-unref-return.js b/test/parallel/test-ref-unref-return.js new file mode 100644 index 00000000000..a82a433ab1a --- /dev/null +++ b/test/parallel/test-ref-unref-return.js @@ -0,0 +1,12 @@ +'use strict'; +var assert = require('assert'); +var net = require('net'); +var dgram = require('dgram'); +var common = require('../common'); + +assert.ok((new net.Server()).ref() instanceof net.Server); +assert.ok((new net.Server()).unref() instanceof net.Server); +assert.ok((new net.Socket()).ref() instanceof net.Socket); +assert.ok((new net.Socket()).unref() instanceof net.Socket); +assert.ok((new dgram.Socket('udp4')).ref() instanceof dgram.Socket); +assert.ok((new dgram.Socket('udp6')).unref() instanceof dgram.Socket); |