Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 63644dd

Browse filesBrowse 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 <fishrock123@rocketmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 8dfdee3 commit 63644dd
Copy full SHA for 63644dd

File tree

Expand file treeCollapse file tree

2 files changed

+41
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-14
lines changed
Open diff view settings
Collapse file

‎lib/timers.js‎

Copy file name to clipboardExpand all lines: lib/timers.js
+8-14Lines changed: 8 additions & 14 deletions
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
*/
Collapse file
+33Lines changed: 33 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.