Skip to content

Commit 39dde32

Browse files
committed
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: #1768 Reviewed-By: Evan Lucas <[email protected]>
1 parent 9da168b commit 39dde32

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

doc/api/dgram.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,12 @@ Calling `unref` on a socket will allow the program to exit if this is the only
306306
active socket in the event system. If the socket is already `unref`d calling
307307
`unref` again will have no effect.
308308

309+
Returns `socket`.
310+
309311
### socket.ref()
310312

311313
Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
312314
let the program exit if it's the only socket left (the default behavior). If
313315
the socket is `ref`d calling `ref` again will have no effect.
316+
317+
Returns `socket`.

doc/api/net.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,16 @@ Calling `unref` on a server will allow the program to exit if this is the only
261261
active server in the event system. If the server is already `unref`d calling
262262
`unref` again will have no effect.
263263

264+
Returns `server`.
265+
264266
### server.ref()
265267

266268
Opposite of `unref`, calling `ref` on a previously `unref`d server will *not*
267269
let the program exit if it's the only server left (the default behavior). If
268270
the server is `ref`d calling `ref` again will have no effect.
269271

272+
Returns `server`.
273+
270274
### server.maxConnections
271275

272276
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
484488
active socket in the event system. If the socket is already `unref`d calling
485489
`unref` again will have no effect.
486490

491+
Returns `socket`.
492+
487493
### socket.ref()
488494

489495
Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
490496
let the program exit if it's the only socket left (the default behavior). If
491497
the socket is `ref`d calling `ref` again will have no effect.
492498

499+
Returns `socket`.
500+
493501
### socket.remoteAddress
494502

495503
The string representation of the remote IP address. For example,

lib/dgram.js

+4
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,14 @@ function onMessage(nread, handle, buf, rinfo) {
481481
Socket.prototype.ref = function() {
482482
if (this._handle)
483483
this._handle.ref();
484+
485+
return this;
484486
};
485487

486488

487489
Socket.prototype.unref = function() {
488490
if (this._handle)
489491
this._handle.unref();
492+
493+
return this;
490494
};

lib/net.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -984,20 +984,24 @@ function connectErrorNT(self, err, options) {
984984
Socket.prototype.ref = function() {
985985
if (!this._handle) {
986986
this.once('connect', this.ref);
987-
return;
987+
return this;
988988
}
989989

990990
this._handle.ref();
991+
992+
return this;
991993
};
992994

993995

994996
Socket.prototype.unref = function() {
995997
if (!this._handle) {
996998
this.once('connect', this.unref);
997-
return;
999+
return this;
9981000
}
9991001

10001002
this._handle.unref();
1003+
1004+
return this;
10011005
};
10021006

10031007

@@ -1506,13 +1510,17 @@ Server.prototype.ref = function() {
15061510

15071511
if (this._handle)
15081512
this._handle.ref();
1513+
1514+
return this;
15091515
};
15101516

15111517
Server.prototype.unref = function() {
15121518
this._unref = true;
15131519

15141520
if (this._handle)
15151521
this._handle.unref();
1522+
1523+
return this;
15161524
};
15171525

15181526

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
var assert = require('assert');
3+
var net = require('net');
4+
var dgram = require('dgram');
5+
var common = require('../common');
6+
7+
assert.ok((new net.Server()).ref() instanceof net.Server);
8+
assert.ok((new net.Server()).unref() instanceof net.Server);
9+
assert.ok((new net.Socket()).ref() instanceof net.Socket);
10+
assert.ok((new net.Socket()).unref() instanceof net.Socket);
11+
assert.ok((new dgram.Socket('udp4')).ref() instanceof dgram.Socket);
12+
assert.ok((new dgram.Socket('udp6')).unref() instanceof dgram.Socket);

0 commit comments

Comments
 (0)