diff --git a/.npmignore b/.npmignore index 20c469a..8e6e23f 100644 --- a/.npmignore +++ b/.npmignore @@ -4,3 +4,5 @@ node_modules/ .gitattributes .gitignore index.test.js +src/ +tsconfig.json diff --git a/global.d.ts b/global.d.ts index 2f539d1..f80ad18 100644 --- a/global.d.ts +++ b/global.d.ts @@ -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 {}; diff --git a/index.js b/index.js deleted file mode 100644 index abd6efe..0000000 --- a/index.js +++ /dev/null @@ -1,110 +0,0 @@ -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(); -}; -Timeout.prototype.start = function start(timeMS) { - 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 = 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; - } - } - return this; -}; -Timeout.prototype.pause = function pause() { - if (this.state != 1) return this; - clearTimeout(this._timerId); - this._timerId = -1; - this._timeLeft -= Date.now() - this._startedAt; - this.state = 2; - return this; -}; -Timeout.prototype.stop = function stop() { - clearTimeout(this._timerId); - this._timerId = -1; - this._timeLeft = 0; - this.state = 0; - return this; -}; -Object.defineProperty(Timeout.prototype, "timeLeft", { - 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(); -}; -Interval.prototype.start = function start(timeMS) { - 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 = 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; - } - } - return this; -}; -Interval.prototype.pause = function pause() { - if (this.state != 1) return this; - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; - this._timeLeft -= Date.now() - this._lastTrigger; - this.state = 2; - return this; -}; -Interval.prototype.stop = function stop() { - this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); - this._timerId = -1; - this._timeLeft = 0; - this.state = 0; - return this; -}; -Object.defineProperty(Interval.prototype, "timeLeft", { - get: function () { - if (this.state == 0) return 0; - return Date.now() - this._lastTrigger; - } -}); - -var TimerState = {}; -TimerState[TimerState["Reset"] = 0] = "Reset"; -TimerState[TimerState["Running"] = 1] = "Running"; -TimerState[TimerState["Paused"] = 2] = "Paused"; -TimerState[TimerState["Done"] = 3] = "Done"; - -module.exports = { Timeout: Timeout, Interval: Interval, TimerState: TimerState }; diff --git a/index.test.js b/index.test.js index 6effad8..5107bcd 100644 --- a/index.test.js +++ b/index.test.js @@ -1,4 +1,4 @@ -const { Timeout, Interval } = require("date-timeout-interval"); +const { Timeout, Interval } = require("."); require('jest'); beforeEach(() => { diff --git a/package-lock.json b/package-lock.json index 13d26cd..56f1f38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4441,6 +4441,12 @@ "is-typedarray": "^1.0.0" } }, + "typescript": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", + "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "dev": true + }, "union-value": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz", diff --git a/package.json b/package.json index 4173488..4b9a8c6 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" } } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..585754a --- /dev/null +++ b/src/index.ts @@ -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 = 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; + } + } + return this; + } + + public pause(): this { + if (this.state != 1) return this; + clearTimeout(this._timerId); + this._timerId = -1; + this._timeLeft -= Date.now() - this._startedAt; + this.state = 2; + return this; + } + + public stop(): this { + clearTimeout(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 = 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; + } + } + return this; + } + + public pause(): this { + if (this.state != 1) return this; + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); + this._timerId = -1; + this._timeLeft -= Date.now() - this._lastTrigger; + this.state = 2; + return this; + } + + public stop(): this { + this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(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 +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9d3a744 --- /dev/null +++ b/tsconfig.json @@ -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/"] +}