Skip to content

Commit

Permalink
Rewrite everything to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed Jan 17, 2021
1 parent e51afac commit 33d973c
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 118 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules/
.gitattributes
.gitignore
index.test.js
src/
tsconfig.json
10 changes: 6 additions & 4 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Timeout, Interval } from "./index.d.ts";
import { Timeout, Interval } from ".";

interface Date {
static Timeout: Timeout;
static Interval: Interval;
interface DateConstructor {
Timeout: Timeout;
Interval: Interval;
}

export {};
110 changes: 0 additions & 110 deletions index.js

This file was deleted.

2 changes: 1 addition & 1 deletion 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(".");
require('jest');

beforeEach(() => {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "date-timeout-interval",
"version": "1.2.0",
"description": "Implementation of setTimeout and setInterval with pause",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "jest"
},
Expand All @@ -16,8 +16,13 @@
"url": "https://github.com/blaumeise20/date-timeout-interval/issues"
},
"homepage": "https://github.com/blaumeise20/date-timeout-interval#readme",
"keywords": ["timeout", "date", "interval"],
"keywords": [
"timeout",
"date",
"interval"
],
"devDependencies": {
"jest": "^26.6.3"
"jest": "^26.6.3",
"typescript": "^4.1.3"
}
}
138 changes: 138 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
export class Timeout {
public state: 0 | 1 | 2 | 3;
public currentTime: number;
public get timeLeft(): number {
if (this.state == 0 || this.state == 3) return 0;
return Date.now() - this._startedAt;
}

private _startedAt: number;
private _callback: () => void;
private _timeLeft: number;
private _timerId: number;

public constructor(callback: () => void, timeMS: number, autoStart?: boolean) {
var self = this;
this._callback = function () { self.state = 3; callback(); }
this.currentTime = timeMS;
this._startedAt = 0;
this._timeLeft = timeMS;
this._timerId = -1;
this.state = 0;
if (autoStart) this.start();
}

public start(): this;
public start(timeMS: number): this;
public start(timeMS?: number): this {
if (this.state == 3) this.stop();
if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS;
if (this._timerId == -1) {
if (this.state == 2) {
this._startedAt = Date.now();
this._timerId = <any>setTimeout(this._callback, this._timeLeft);
this.state = 1;
}
else if (this.state == 0) {
this._startedAt = Date.now();
this._timerId = <any>setTimeout(this._callback, this.currentTime);
this.state = 1;
}
}
return this;
}

public pause(): this {
if (this.state != 1) return this;
clearTimeout(<any>this._timerId);
this._timerId = -1;
this._timeLeft -= Date.now() - this._startedAt;
this.state = 2;
return this;
}

public stop(): this {
clearTimeout(<any>this._timerId);
this._timerId = -1;
this._timeLeft = 0;
this.state = 0;
return this;
}
}

export class Interval {
public state: 0 | 1 | 2;
public currentTime: number;
public get timeLeft(): number {
if (this.state == 0) return 0;
return Date.now() - this._lastTrigger;
}

private _lastTrigger: number;
private _callback: () => void;
private _timeLeft: number;
private _timerId: number;
private _isInTimeout: boolean;

public constructor(callback: (() => void), timeMS?: number, autoStart?: boolean) {
var self = this;
this._callback = () => { this._lastTrigger = Date.now(); callback(); };
this.currentTime = timeMS;
this._lastTrigger = 0;
this._timeLeft = timeMS;
this._timerId = -1;
this.state = 0;
this._isInTimeout = false;
if (autoStart) this.start();
}

public start(): this;
public start(timeMS: number): this;
public start(timeMS?: number): this {
if (arguments.length > 0 && this.state == 0) this.currentTime = timeMS;
if (this._timerId == -1) {
if (this.state == 2) {
this._lastTrigger = Date.now();
var self = this;
this._timerId = <any>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 = <any>setInterval(this._callback, this.currentTime);
this.state = 1;
}
}
return this;
}

public pause(): this {
if (this.state != 1) return this;
this._isInTimeout ? clearTimeout(<any>this._timerId) : clearInterval(<any>this._timerId);
this._timerId = -1;
this._timeLeft -= Date.now() - this._lastTrigger;
this.state = 2;
return this;
}

public stop(): this {
this._isInTimeout ? clearTimeout(<any>this._timerId) : clearInterval(<any>this._timerId);
this._timerId = -1;
this._timeLeft = 0;
this.state = 0;
this._isInTimeout = false;
return this;
}
}


export enum TimerState {
Reset = 0,
Running = 1,
Paused = 2,
Done = 3
}
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/",
"target": "es5",
"lib": [
"es2015"
],
"noImplicitAny": false,
"noImplicitThis": false,
"alwaysStrict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"declaration": true
},
"include": ["src/"]
}

0 comments on commit 33d973c

Please sign in to comment.