mirror of https://github.com/nodejs/node.git
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: https://github.com/nodejs/node/pull/3143 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>pull/3212/head
parent
8dfdee3733
commit
63644dd1cd
|
@ -23,14 +23,17 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
|
||||||
// value = list
|
// value = list
|
||||||
var lists = {};
|
var lists = {};
|
||||||
|
|
||||||
|
|
||||||
|
// call this whenever the item is active (not idle)
|
||||||
|
// it will reset its timeout.
|
||||||
// the main function - creates lists on demand and the watchers associated
|
// the main function - creates lists on demand and the watchers associated
|
||||||
// with them.
|
// with them.
|
||||||
function insert(item, msecs) {
|
exports.active = function(item) {
|
||||||
item._idleStart = Timer.now();
|
const msecs = item._idleTimeout;
|
||||||
item._idleTimeout = msecs;
|
|
||||||
|
|
||||||
if (msecs < 0) return;
|
if (msecs < 0) return;
|
||||||
|
|
||||||
|
item._idleStart = Timer.now();
|
||||||
|
|
||||||
var list;
|
var list;
|
||||||
|
|
||||||
if (lists[msecs]) {
|
if (lists[msecs]) {
|
||||||
|
@ -48,7 +51,7 @@ function insert(item, msecs) {
|
||||||
|
|
||||||
L.append(list, item);
|
L.append(list, item);
|
||||||
assert(!L.isEmpty(list)); // list is not empty
|
assert(!L.isEmpty(list)); // list is not empty
|
||||||
}
|
};
|
||||||
|
|
||||||
function listOnTimeout() {
|
function listOnTimeout() {
|
||||||
var msecs = this.msecs;
|
var msecs = this.msecs;
|
||||||
|
@ -156,15 +159,6 @@ exports.enroll = function(item, msecs) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// call this whenever the item is active (not idle)
|
|
||||||
// it will reset its timeout.
|
|
||||||
exports.active = function(item) {
|
|
||||||
var msecs = item._idleTimeout;
|
|
||||||
if (msecs >= 0)
|
|
||||||
insert(item, msecs);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* DOM-style timers
|
* DOM-style timers
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const active = require('timers').active;
|
||||||
|
|
||||||
|
// active() should create timers for these
|
||||||
|
var legitTimers = [
|
||||||
|
{ _idleTimeout: 0 },
|
||||||
|
{ _idleTimeout: 1 }
|
||||||
|
];
|
||||||
|
|
||||||
|
legitTimers.forEach(function(legit) {
|
||||||
|
const savedTimeout = legit._idleTimeout;
|
||||||
|
active(legit);
|
||||||
|
// active() should mutate these objects
|
||||||
|
assert(legit._idleTimeout === savedTimeout);
|
||||||
|
assert(Number.isInteger(legit._idleStart));
|
||||||
|
assert(legit._idleNext);
|
||||||
|
assert(legit._idlePrev);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// active() should not create a timer for these
|
||||||
|
var bogusTimers = [
|
||||||
|
{ _idleTimeout: -1 }
|
||||||
|
];
|
||||||
|
|
||||||
|
bogusTimers.forEach(function(bogus) {
|
||||||
|
const savedTimeout = bogus._idleTimeout;
|
||||||
|
active(bogus);
|
||||||
|
// active() should not mutate these objects
|
||||||
|
assert.deepStrictEqual(bogus, {_idleTimeout: savedTimeout});
|
||||||
|
});
|
Loading…
Reference in New Issue