timer: ref/unref return self
authorSam Roberts <vieuxtech@gmail.com>
Sun, 13 Sep 2015 15:21:51 +0000 (16:21 +0100)
committerJeremiah Senkpiel <fishrock123@rocketmail.com>
Wed, 16 Sep 2015 20:07:58 +0000 (16:07 -0400)
Most calls to ref() and unref() are chainable, timers should be
chainable, too.

Typical use:

    var to = setTimeout(ontimeout, 123).unref();

PR-URL: https://github.com/nodejs/node/pull/2905
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trevor Norris <trevnorris@nodejs.org>
doc/api/timers.markdown
lib/timers.js
test/parallel/test-timers-unref.js

index fd788cd..ed7da50 100644 (file)
@@ -41,12 +41,16 @@ In the case of `setTimeout` when you `unref` you create a separate timer that
 will wakeup the event loop, creating too many of these may adversely effect
 event loop performance -- use wisely.
 
+Returns the timer.
+
 ## ref()
 
 If you had previously `unref()`d a timer you can call `ref()` to explicitly
 request the timer hold the program open. If the timer is already `ref`d calling
 `ref` again will have no effect.
 
+Returns the timer.
+
 ## setImmediate(callback[, arg][, ...])
 
 To schedule the "immediate" execution of `callback` after I/O events
index e3d0eb1..aa9f316 100644 (file)
@@ -330,11 +330,13 @@ Timeout.prototype.unref = function() {
     this._handle.domain = this.domain;
     this._handle.unref();
   }
+  return this;
 };
 
 Timeout.prototype.ref = function() {
   if (this._handle)
     this._handle.ref();
+  return this;
 };
 
 Timeout.prototype.close = function() {
@@ -345,6 +347,7 @@ Timeout.prototype.close = function() {
   } else {
     exports.unenroll(this);
   }
+  return this;
 };
 
 
index ab2e33a..c0b24a4 100644 (file)
@@ -12,6 +12,14 @@ var interval_fired = false,
 var LONG_TIME = 10 * 1000;
 var SHORT_TIME = 100;
 
+assert.doesNotThrow(function() {
+  setTimeout(function() {}, 10).unref().ref().unref();
+}, 'ref and unref are chainable');
+
+assert.doesNotThrow(function() {
+  setInterval(function() {}, 10).unref().ref().unref();
+}, 'ref and unref are chainable');
+
 setInterval(function() {
   interval_fired = true;
 }, LONG_TIME).unref();