EventsTimer NPM Package
The EventsTimer NPM package is a TypeScript module designed to manage timers for various events. It includes features for starting, stopping, and retrieving statistics about timers associated with specific events.
Installation
To install this package, you can use npm or yarn:
npm install events-timer
# or
yarn add events-timer
Usage
Here's how you can use the EventsTimer class in your TypeScript or JavaScript code:
import EventsTimer from 'events-timer';
const eventsTimer = new EventsTimer({});
const timerId = eventsTimer.startTimer('eventName');
eventsTimer.stopTimer('eventName', timerId);
const averageTime = eventsTimer.getAverageExecutionTime('eventName');
const allTimers = eventsTimer.showTimers();
Methods
startTimer(eventName: string): string
: Starts a new timer for the specified event and returns a unique timer ID.stopTimer(eventName: string, timerId: string): void
: Stops the timer with the provided timer ID for the specified event.getAverageExecutionTime(eventName: string): number
: Calculates and returns the average execution time (in seconds) for timers associated with a specific event.showTimers(): Record<string, Timer[]>
: Returns a record of all timers, organized by event name.
Example
Here's a simple example of how to use the EventsTimer:
import EventsTimer from 'events-timer';
const eventsTimer = new EventsTimer({});
const timerId = eventsTimer.startTimer('exampleEvent');
setTimeout(() => {
eventsTimer.stopTimer('exampleEvent', timerId);
const averageTime = eventsTimer.getAverageExecutionTime('exampleEvent');
console.log(`Average Execution Time: ${averageTime} seconds`);
}, 1000);
Example #2 (external state):
import EventsTimer, { Timer } from 'events-timer';
const externalState: Record<string, Timer[]> = {};
const eventsTimer = new EventsTimer(externalState);
const timerId = eventsTimer.startTimer('exampleEvent');
setTimeout(() => {
eventsTimer.stopTimer('exampleEvent', timerId);
const averageTime = eventsTimer.getAverageExecutionTime('exampleEvent');
console.log(`Average Execution Time: ${averageTime} seconds`);
}, 1000);
Example #3 (React useState):
import React, { useState } from 'react';
import EventsTimer from 'events-timer';
const externalState = {};
const eventsTimer = new EventsTimer(externalState);
function useEventsTimer() {
const [timers, setTimers] = useState(externalState);
const startTimer = (eventName) => {
const timerId = eventsTimer.startTimer(eventName);
setTimers({ ...externalState });
return timerId;
};
const stopTimer = (eventName, timerId) => {
eventsTimer.stopTimer(eventName, timerId);
setTimers({ ...externalState });
};
const getAverageExecutionTime = (eventName) => {
return eventsTimer.getAverageExecutionTime(eventName);
};
return { startTimer, stopTimer, getAverageExecutionTime, timers };
}
function TimerComponent() {
const { startTimer, stopTimer, getAverageExecutionTime, timers } = useEventsTimer();
const handleStartTimer = () => {
const timerId = startTimer('example-event');
setTimeout(() => {
stopTimer('example-event', timerId);
const averageExecutionTime = getAverageExecutionTime('example-event');
console.log(`Average Execution Time: ${averageExecutionTime} seconds`);
}, 3000);
};
return (
<div>
<button onClick={handleStartTimer}>Start Timer</button>
<p>Timers: {JSON.stringify(timers)}</p>
</div>
);
}
export default TimerComponent;
You can use any external storage for saving the data for the events and the timers.
Dependencies
This package has no external dependencies.
License
This package is released under the MIT License.
Contribution
Feel free to contribute by opening issues or creating pull requests on GitHub.
This README provides a basic overview of the EventsTimer NPM package. For more detailed information and usage examples, please refer to the package's documentation or explore the codebase.