diff --git a/.editorconfig b/.editorconfig index 980ef21..c1e2c64 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,7 +4,7 @@ root = true [*] -indent_style = tab +indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 diff --git a/global.d.ts b/global.d.ts index e613ac0..2f539d1 100644 --- a/global.d.ts +++ b/global.d.ts @@ -1,6 +1,6 @@ import { Timeout, Interval } from "./index.d.ts"; interface Date { - static Timeout: Timeout; - static Interval: Interval; -} \ No newline at end of file + static Timeout: Timeout; + static Interval: Interval; +} diff --git a/global.js b/global.js index 7834dc9..d1bf7bb 100644 --- a/global.js +++ b/global.js @@ -1,3 +1,3 @@ var ti = require("./"); Date.Timeout = ti.Timeout; -Date.Interval = ti.Interval; \ No newline at end of file +Date.Interval = ti.Interval; diff --git a/index.d.ts b/index.d.ts index 1f3c7e4..5f4c2bc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,24 +1,24 @@ export declare class Timeout { - public constructor(callback: () => void, timeMS: number, autoStart: boolean); - public start(): this; - public stop(): this; - public pause(): this; - public readonly state: 0 | 1 | 2 | 3; - public readonly currentTime: number; - public readonly timeLeft: number; + public constructor(callback: () => void, timeMS: number, autoStart: boolean); + public start(): this; + public stop(): this; + public pause(): this; + public readonly state: 0 | 1 | 2 | 3; + public readonly currentTime: number; + public readonly timeLeft: number; } export declare class Interval { - public constructor(callback: () => void, timeMS: number, autoStart: boolean); - public start(): this; - public stop(): this; - public pause(): this; - public readonly state: 0 | 1 | 2; - public readonly currentTime: number; - public readonly timeLeft: number; + public constructor(callback: () => void, timeMS: number, autoStart: boolean); + public start(): this; + public stop(): this; + public pause(): this; + public readonly state: 0 | 1 | 2; + public readonly currentTime: number; + public readonly timeLeft: number; } export enum TimerState { - Reset = 0, - Running = 1, - Paused = 2, - Done = 3 + Reset = 0, + Running = 1, + Paused = 2, + Done = 3 } diff --git a/index.js b/index.js index 550b156..6931153 100644 --- a/index.js +++ b/index.js @@ -1,98 +1,98 @@ function Timeout(callback, time, autoStart = false) { - var self = this; - this._callback = function() { self.state = 3; callback(); } - this.currentTime = time; - this._startedAt = 0; - this._timeLeft = time; - this._timerId = -1; - this.state = 0; - if (autoStart) this.start(); + var self = this; + this._callback = function () { self.state = 3; callback(); } + this.currentTime = time; + this._startedAt = 0; + this._timeLeft = time; + this._timerId = -1; + this.state = 0; + if (autoStart) this.start(); }; Timeout.prototype.start = function start(time) { - if (arguments.length > 0) { // warning: undocumented feature, not really working - this.currentTime = time; - } - if (this._timerId == -1) { - if (this.state == 2) { - this._startedAt = Date.now(); - this._timerId = setTimeout(this._callback, this._timeLeft); - this.state = 1; - } - else if (this.state == 0) { - this._startedAt = Date.now(); - this._timerId = setTimeout(this._callback, this.currentTime); - this.state = 1; - } - } + if (arguments.length > 0) { // warning: undocumented feature, not really working + this.currentTime = time; + } + if (this._timerId == -1) { + if (this.state == 2) { + this._startedAt = Date.now(); + this._timerId = setTimeout(this._callback, this._timeLeft); + this.state = 1; + } + else if (this.state == 0) { + this._startedAt = Date.now(); + this._timerId = setTimeout(this._callback, this.currentTime); + this.state = 1; + } + } }; Timeout.prototype.pause = function pause() { - if (this.state != 1) return; - clearTimeout(this._timerId); - this._timerId = -1; - this._timeLeft -= Date.now() - this._startedAt; - this.state = 2; + if (this.state != 1) return; + clearTimeout(this._timerId); + this._timerId = -1; + this._timeLeft -= Date.now() - this._startedAt; + this.state = 2; }; Timeout.prototype.stop = function stop() { - clearTimeout(this._timerId); - this._timerId = -1; - this._timeLeft = 0; - this.state = 0; + clearTimeout(this._timerId); + this._timerId = -1; + this._timeLeft = 0; + this.state = 0; }; Object.defineProperty(Timeout.prototype, "timeLeft", { - get: function() { - if (this.state == 0) return 0; - return Date.now() - this._startedAt; - } + get: function () { + if (this.state == 0) return 0; + return Date.now() - this._startedAt; + } }); function Interval(callback, time, autoStart = false) { - var self = this; - this._callback = function() { self._lastTrigger = Date.now(); callback(); }; - this.currentTime = time; - this._lastTrigger = 0; - this._timeLeft = time; - this._timerId = -1; - this.state = 0; - this._isInTimeout = false; - if (autoStart) this.start(); + var self = this; + this._callback = function () { self._lastTrigger = Date.now(); callback(); }; + this.currentTime = time; + this._lastTrigger = 0; + this._timeLeft = time; + this._timerId = -1; + this.state = 0; + this._isInTimeout = false; + if (autoStart) this.start(); }; Interval.prototype.start = function start() { - if (this._timerId == -1) { - if (this.state == 2) { - this._lastTrigger = Date.now(); - var self = this; - this._timerId = setTimeout(function () { - this._isInTimeout = false; - this._timerId = setInterval(self._callback, self.currentTime); - }, this._timeLeft); - this.state = 1; - this._isInTimeout = true; - } - else if (this.state == 0) { - this._lastTrigger = Date.now(); - this._timerId = setInterval(this._callback, this.currentTime); - this.state = 1; - } - } + if (this._timerId == -1) { + if (this.state == 2) { + this._lastTrigger = Date.now(); + var self = this; + this._timerId = setTimeout(function () { + this._isInTimeout = false; + this._timerId = setInterval(self._callback, self.currentTime); + }, this._timeLeft); + this.state = 1; + this._isInTimeout = true; + } + else if (this.state == 0) { + this._lastTrigger = Date.now(); + this._timerId = setInterval(this._callback, this.currentTime); + this.state = 1; + } + } }; Interval.prototype.pause = function pause() { - if (this.state != 1) return; - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; - this._timeLeft -= Date.now() - this._lastTrigger; - this.state = 2; + if (this.state != 1) return; + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); + this._timerId = -1; + this._timeLeft -= Date.now() - this._lastTrigger; + this.state = 2; }; Interval.prototype.stop = function stop() { - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; - this._timeLeft = 0; - this.state = 0; + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); + this._timerId = -1; + this._timeLeft = 0; + this.state = 0; }; Object.defineProperty(Interval.prototype, "timeLeft", { - get: function() { - if (this.state == 0) return 0; - return Date.now() - this._lastTrigger; - } + get: function () { + if (this.state == 0) return 0; + return Date.now() - this._lastTrigger; + } }); var TimerState = {}; @@ -101,4 +101,4 @@ TimerState[TimerState["Running"] = 1] = "Running"; TimerState[TimerState["Paused"] = 2] = "Paused"; TimerState[TimerState["Done"] = 3] = "Done"; -module.exports = { Timeout: Timeout, Interval: Interval, TimerState: TimerState }; \ No newline at end of file +module.exports = { Timeout: Timeout, Interval: Interval, TimerState: TimerState }; diff --git a/index.test.js b/index.test.js index 5b609d1..44c5a1f 100644 --- a/index.test.js +++ b/index.test.js @@ -1,4 +1,4 @@ -const { Timeout, Interval } = require( "date-timeout-interval"); +const { Timeout, Interval } = require("date-timeout-interval"); require('jest'); beforeEach(() => { @@ -7,7 +7,7 @@ beforeEach(() => { describe('state tests', () => { test('state is set to 0 after creation of timer', () => { - const to = new Timeout(() => {}, 0); + const to = new Timeout(() => { }, 0); expect(to.state).toBe(0); }); @@ -34,18 +34,18 @@ describe('state tests', () => { describe('time start tests', () => { test('timeout is not started when autostart is false', () => { - const to = new Timeout(() => {}, 0, false); + const to = new Timeout(() => { }, 0, false); expect(setTimeout).toHaveBeenCalledTimes(0); }); test('timeout is not started when autostart is not specified', () => { - const to = new Timeout(() => {}, 0); + const to = new Timeout(() => { }, 0); expect(setTimeout).toHaveBeenCalledTimes(0); }); test('timeout is started when autostart is true', () => { const waitTime = 1000; - const to = new Timeout(() => {}, waitTime, true); + const to = new Timeout(() => { }, waitTime, true); expect(setTimeout).toHaveBeenCalledTimes(1); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), waitTime); });