Skip to content
Open
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
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
extends: 'airbnb-base',
env: {
node: true
},
rules: {
'no-underscore-dangle': 'off',
'no-param-reassign': 'off',
}
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ coverage.*
lib-cov
complexity.md
.eslintcache
coverage/**/*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sudo: false
language: node_js
node_js:
- "6"
- "8"
- "10"
- "12"
- "node"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Big-Time

[![Current Version](https://img.shields.io/npm/v/big-time.svg)](https://www.npmjs.org/package/big-time)
[![Build Status via Travis CI](https://travis-ci.org/arb/big-time.svg?branch=master)](https://travis-ci.org/arb/big-time)
[![belly-button-style](https://img.shields.io/badge/eslint-bellybutton-4B32C3.svg)](https://github.com/continuationlabs/belly-button)
[![Current Version](https://flat.badgen.net/npm/v/big-time)](https://www.npmjs.org/package/big-time)
[![Build Status](https://flat.badgen.net/travis/arb/big-time)](https://travis-ci.org/arb/big-time)
[![airbnb-style](https://flat.badgen.net/badge/eslint/airbnb/ff5a5f?icon=airbnb)](https://github.com/airbnb/javascript)


Reworking of [long-timeout](https://github.com/tellnes/long-timeout) that has more features, follows correct semver, and has unit tests. Big-Time is a custom timer class to allow really long values into `setTimeout` that are larger than JavaScript would normally support (2^31-1).
Expand Down
30 changes: 30 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const watchPathIgnorePatterns = ['<rootDir>/node_modules/'];
const testEnvironment = 'node';

module.exports = {
collectCoverage: true,
collectCoverageFrom: ['<rootDir>/lib/index.js'],
coverageDirectory: '<rootDir>/coverage',
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
projects: [{
displayName: 'test',
testEnvironment,
// Hack because of broken jest https://github.com/facebook/jest/issues/8088
watchPathIgnorePatterns,
}, {
displayName: 'linter',
runner: 'jest-runner-eslint',
testEnvironment,
testMatch: ['<rootDir>/lib/**/*.js', '<rootDir>/test/**/*.js', '<rootDir>/jest.config.js'],
// Hack because of broken jest https://github.com/facebook/jest/issues/8088
watchPathIgnorePatterns,
}],
watchPlugins: ['jest-runner-eslint/watch-fix'],
};
20 changes: 11 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const TIMEOUT_MAX = 2147483647; // 2^31-1

function start (timer, args) {
const start = (timer, args) => {
const max = module.exports._TIMEOUT_MAX; // Use the exported value for testing purposes.

if (timer._delay <= max) {
Expand All @@ -18,10 +17,10 @@ function start (timer, args) {
if (timer._ref === false) {
timer._timeout.unref();
}
}
};

class Timeout {
constructor (callback, delay, ...args) {
constructor(callback, delay, ...args) {
this._callback = callback;

if (delay instanceof Date) {
Expand All @@ -32,26 +31,29 @@ class Timeout {

this._timeout = null;
this._ref = true;

start(this, args);
}
ref () {

ref() {
this._ref = true;
this._timeout.ref();
return this;
}
unref () {

unref() {
this._ref = false;
this._timeout.unref();
return this;
}
}

const _setTimeout = (...args) => { return new Timeout(...args); };
const _setTimeout = (...args) => new Timeout(...args);

const _clearTimeout = (timer) => { return timer && clearTimeout(timer._timeout); };
const _clearTimeout = (timer) => timer && clearTimeout(timer._timeout);

module.exports = {
setTimeout: _setTimeout,
clearTimeout: _clearTimeout,
_TIMEOUT_MAX: TIMEOUT_MAX
_TIMEOUT_MAX: TIMEOUT_MAX,
};
Loading