Skip to content

Commit

Permalink
Formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Jan 10, 2021
1 parent 97f1049 commit 7d0e90a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
root = true

[*]
indent_style = tab
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
Expand Down
6 changes: 3 additions & 3 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Timeout, Interval } from "./index.d.ts";

interface Date {
static Timeout: Timeout;
static Interval: Interval;
}
static Timeout: Timeout;
static Interval: Interval;
}
2 changes: 1 addition & 1 deletion global.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var ti = require("./");
Date.Timeout = ti.Timeout;
Date.Interval = ti.Interval;
Date.Interval = ti.Interval;
36 changes: 18 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
152 changes: 76 additions & 76 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand All @@ -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 };
module.exports = { Timeout: Timeout, Interval: Interval, TimerState: TimerState };
10 changes: 5 additions & 5 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Timeout, Interval } = require( "date-timeout-interval");
const { Timeout, Interval } = require("date-timeout-interval");
require('jest');

beforeEach(() => {
Expand All @@ -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);
});

Expand All @@ -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);
});
Expand Down

0 comments on commit 7d0e90a

Please sign in to comment.