Skip to content

Commit 63644dd

Browse files
Trottjasnell
authored andcommitted
lib: remove redundant code, add tests in timers.js
insert() is only called from one place where there is already a check that msecs is greater than or equal to zero, so do not repeat the check inside insert(). timers.active() is not documented and should not be exposed, but since it is exposed for now, let's test it. PR-URL: #3143 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 8dfdee3 commit 63644dd

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

lib/timers.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
2323
// value = list
2424
var lists = {};
2525

26+
27+
// call this whenever the item is active (not idle)
28+
// it will reset its timeout.
2629
// the main function - creates lists on demand and the watchers associated
2730
// with them.
28-
function insert(item, msecs) {
29-
item._idleStart = Timer.now();
30-
item._idleTimeout = msecs;
31-
31+
exports.active = function(item) {
32+
const msecs = item._idleTimeout;
3233
if (msecs < 0) return;
3334

35+
item._idleStart = Timer.now();
36+
3437
var list;
3538

3639
if (lists[msecs]) {
@@ -48,7 +51,7 @@ function insert(item, msecs) {
4851

4952
L.append(list, item);
5053
assert(!L.isEmpty(list)); // list is not empty
51-
}
54+
};
5255

5356
function listOnTimeout() {
5457
var msecs = this.msecs;
@@ -156,15 +159,6 @@ exports.enroll = function(item, msecs) {
156159
};
157160

158161

159-
// call this whenever the item is active (not idle)
160-
// it will reset its timeout.
161-
exports.active = function(item) {
162-
var msecs = item._idleTimeout;
163-
if (msecs >= 0)
164-
insert(item, msecs);
165-
};
166-
167-
168162
/*
169163
* DOM-style timers
170164
*/

test/parallel/test-timers-active.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const active = require('timers').active;
5+
6+
// active() should create timers for these
7+
var legitTimers = [
8+
{ _idleTimeout: 0 },
9+
{ _idleTimeout: 1 }
10+
];
11+
12+
legitTimers.forEach(function(legit) {
13+
const savedTimeout = legit._idleTimeout;
14+
active(legit);
15+
// active() should mutate these objects
16+
assert(legit._idleTimeout === savedTimeout);
17+
assert(Number.isInteger(legit._idleStart));
18+
assert(legit._idleNext);
19+
assert(legit._idlePrev);
20+
});
21+
22+
23+
// active() should not create a timer for these
24+
var bogusTimers = [
25+
{ _idleTimeout: -1 }
26+
];
27+
28+
bogusTimers.forEach(function(bogus) {
29+
const savedTimeout = bogus._idleTimeout;
30+
active(bogus);
31+
// active() should not mutate these objects
32+
assert.deepStrictEqual(bogus, {_idleTimeout: savedTimeout});
33+
});

0 commit comments

Comments
 (0)