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

API parity with clearTimeout/clearInterval and much housekeeping #9

Merged
merged 6 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ node_js:
- '4'
- '6'
- '8'
script:
- npm run cov
after_script:
- codecov
11 changes: 0 additions & 11 deletions component.json

This file was deleted.

6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ exports.setInterval = function (cb, interval) {
timer.fireEvery(interval);
return timer;
};

exports.clearTimeout = exports.clearInterval = function (timer) {
if (timer && typeof timer.clear === 'function') {
timer.clear();
}
};
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"name": "safe-timers",
"version": "1.0.1",
"version": "1.1.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to bump version numbers in PRs. That's part of the release process.

"description": "Timers with near-infinite duration support",
"main": "index.js",
"files": [
"index.js"
],
"directories": {
"test": "test"
},
"scripts": {
"test": "tape test/*.js | tap-spec"
"test": "tape test/*.js | tap-spec",
"cov": "nyc npm test && nyc report --reporter=lcov"
},
"repository": {
"type": "git",
Expand All @@ -17,8 +21,10 @@
"setTimeout",
"setInterval",
"timer",
"timeout",
"interval",
"timers",
"timeouts",
"intervals"
],
"author": "Ron Korving <[email protected]>",
Expand All @@ -28,6 +34,8 @@
},
"homepage": "https://github.com/Wizcorp/safe-timers#readme",
"devDependencies": {
"codecov": "^3.0.0",
"nyc": "^11.2.1",
"tap-spec": "^4.1.1",
"tape": "^4.5.1"
}
Expand Down
19 changes: 19 additions & 0 deletions test/setInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,24 @@ test('setInterval', function (t) {
}, 10);
});

t.test('clearInterval', function (t) {
const interval = timers.setInterval(function () {
t.end('interval fired despite clearInterval()');
}, 0);

timers.clearInterval(interval);

setTimeout(function () {
t.pass('interval did not fire');
t.end();
}, 10);
});

t.test('clearInterval without timer', function (t) {
timers.clearInterval(null);
t.pass('no error was thrown');
t.end();
});

t.end();
});
19 changes: 19 additions & 0 deletions test/setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,24 @@ test('setTimeout', function (t) {
}, 10);
});

t.test('clearTimeout', function (t) {
const timeout = timers.setTimeout(function () {
t.end('timeout fired despite clearTimeout()');
}, 0);

timers.clearTimeout(timeout);

setTimeout(function () {
t.pass('timeout did not fire');
t.end();
}, 10);
});

t.test('clearTimeout without timer', function (t) {
timers.clearTimeout(null);
t.pass('no error was thrown');
t.end();
});

t.end();
});
139 changes: 79 additions & 60 deletions test/setTimeoutAt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,83 @@ const test = require('tape');
const timers = require('..');

test('setTimeoutAt', function (t) {
const originalMaxInterval = timers.maxInterval;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about this one. Apparently when I copied the raw file contents from PR #1 (and then added to them), I didn't notice it was using spaces instead of tabs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed last time, but until we add linting rules (I'll do to that later) I wasn't gonna moan about it ;)


t.test('Crossing the maxInterval border', function (t) {
timers.maxInterval = 2;

timers.setTimeoutAt(function () {
timers.maxInterval = originalMaxInterval;

t.pass('timeout fired');
t.equal(arguments.length, 0);
t.end();
}, Date.now() + 30);
});

t.test('Crossing the maxInterval border with args', function (t) {
timers.maxInterval = 2;

timers.setTimeoutAt(function (a, b) {
timers.maxInterval = originalMaxInterval;

t.pass('timeout fired');
t.equal(arguments.length, 2);
t.equal(a, 1);
t.equal(b, 2);
t.end();
}, Date.now() + 30, 1, 2);
});

t.test('Not crossing the maxInterval border', function (t) {
timers.setTimeoutAt(function () {
t.pass('timeout fired');
t.equal(arguments.length, 0);
t.end();
}, Date.now() + 5);
});

t.test('Not crossing the maxInterval border with args', function (t) {
timers.setTimeoutAt(function (a, b) {
t.pass('timeout fired');
t.equal(arguments.length, 2);
t.equal(a, 1);
t.equal(b, 2);
t.end();
}, Date.now() + 5, 1, 2);
});

t.test('clear()', function (t) {
const timeout = timers.setTimeoutAt(function () {
t.end('timeout fired despite clear()');
}, Date.now() + 0);

timeout.clear();

setTimeout(function () {
t.pass('timeout did not fire');
t.end();
}, 10);
});

t.end();
const originalMaxInterval = timers.maxInterval;

t.test('Crossing the maxInterval border', function (t) {
timers.maxInterval = 2;

timers.setTimeoutAt(function () {
timers.maxInterval = originalMaxInterval;

t.pass('timeout fired');
t.equal(arguments.length, 0);
t.end();
}, Date.now() + 30);
});

t.test('Crossing the maxInterval border with args', function (t) {
timers.maxInterval = 2;

timers.setTimeoutAt(function (a, b) {
timers.maxInterval = originalMaxInterval;

t.pass('timeout fired');
t.equal(arguments.length, 2);
t.equal(a, 1);
t.equal(b, 2);
t.end();
}, Date.now() + 30, 1, 2);
});

t.test('Not crossing the maxInterval border', function (t) {
timers.setTimeoutAt(function () {
t.pass('timeout fired');
t.equal(arguments.length, 0);
t.end();
}, Date.now() + 5);
});

t.test('Not crossing the maxInterval border with args', function (t) {
timers.setTimeoutAt(function (a, b) {
t.pass('timeout fired');
t.equal(arguments.length, 2);
t.equal(a, 1);
t.equal(b, 2);
t.end();
}, Date.now() + 5, 1, 2);
});

t.test('clear()', function (t) {
const timeout = timers.setTimeoutAt(function () {
t.end('timeout fired despite clear()');
}, Date.now() + 0);

timeout.clear();

setTimeout(function () {
t.pass('timeout did not fire');
t.end();
}, 10);
});

t.test('clearTimeout', function (t) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And the actual new changes begin here.

const timeout = timers.setTimeoutAt(function () {
t.end('timeout fired despite clearTimeout()');
}, Date.now() + 0);

timers.clearTimeout(timeout);

setTimeout(function () {
t.pass('timeout did not fire');
t.end();
}, 10);
});

t.test('clearTimeout without timer', function (t) {
timers.clearTimeout(null);
t.pass('no error was thrown');
t.end();
});

t.end();
});