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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
.tmp_*~
*#
.#*

dist/
13 changes: 13 additions & 0 deletions example/example-stopwatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Stopwatch} from "../src/index";

const stopwatch = new Stopwatch(1, { seconds: 10 });

stopwatch.on('tick', (seconds) => {
console.log(`${seconds} seconds remaining`);
});

stopwatch.on('end', () => {
console.log('Stopwatch ended');
});

stopwatch.start();
1 change: 0 additions & 1 deletion index.js

This file was deleted.

60 changes: 0 additions & 60 deletions lib/stopwatch.js

This file was deleted.

38 changes: 38 additions & 0 deletions package-lock.json

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

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"type": "git",
"url": "git://github.com/emerleite/node-stopwatch.git"
},
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "node_modules/mocha/bin/mocha -R spec"
"build": "tsc -d -p ."
},
"engines": {
"node": ">=0.8.0"
},
"dependencies": {},
"devDependencies": {
"mocha" : "2.0.1",
"should" : "4.3.0"
"devDependencies": {
"@types/node": "^18.15.11",
"typescript": "^5.0.3"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Stopwatch,StopwatchManager} from "./lib/stopwatch";
70 changes: 70 additions & 0 deletions src/lib/stopwatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { EventEmitter } from 'events';

interface StopwatchOptions {
seconds?: number;
interval?: number;
}

class Stopwatch extends EventEmitter {
private seconds: number;
private interval: number;
private timer: NodeJS.Timeout | null = null;

constructor(private id: number, options: StopwatchOptions = {}) {
super();
this.seconds = options.seconds ?? 10;
this.interval = (options.interval || options.interval === 0) ? options.interval : 1000;
}

public stop(): void {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}

public start(): boolean {
if (this.started()) {
return false;
}

this.timer = setInterval(() => {
this.emit('tick', this.seconds);

if (--this.seconds < 0) {
this.stop();
this.emit('end');
}
}, this.interval);

return true;
}

public started(): boolean {
return !!this.timer;
}

public restart(): void {
this.stop();
this.removeAllListeners('tick');
this.removeAllListeners('end');
this.start();
}
}

class StopwatchManager {
private static stopwatches: Record<number, Stopwatch> = {};

public static get(id: number, options: StopwatchOptions = {}): Stopwatch {
if (!StopwatchManager.stopwatches[id]) {
StopwatchManager.stopwatches[id] = new Stopwatch(id, options);
StopwatchManager.stopwatches[id].on('end', () => {
delete StopwatchManager.stopwatches[id];
});
}

return StopwatchManager.stopwatches[id];
}
}

export { Stopwatch, StopwatchManager};
31 changes: 0 additions & 31 deletions test/manager.test.js

This file was deleted.

114 changes: 0 additions & 114 deletions test/stopwatch.test.js

This file was deleted.

29 changes: 29 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2017",
"esModuleInterop": true,
"noImplicitAny": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"strict": true,
"lib": [
"es2017",
"esnext.asynciterable",
"dom"
],
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"include": [
"src/**/*"
]
}