Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: remove redundant code from timers.js #3143

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
// value = list
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
// with them.
function insert(item, msecs) {
item._idleStart = Timer.now();
item._idleTimeout = msecs;

exports.active = function(item) {
const msecs = item._idleTimeout;
if (msecs < 0) return;

item._idleStart = Timer.now();

var list;

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

L.append(list, item);
assert(!L.isEmpty(list)); // list is not empty
}
};

function listOnTimeout() {
var msecs = this.msecs;
Expand Down Expand Up @@ -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
*/
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-timers-active.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const active = require('timers').active;

// active() should not create a timer for these
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should, or should not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH, this should be "should". :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the typo. Thanks for the catch.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these?

assert.deepStrictEqual(bogus, {_idleTimeout: savedTimeout});
});