1.0.0 • Published 8 months ago

events-timer-ts v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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';

// Initialize EventsTimer with an empty eventTimers object
const eventsTimer = new EventsTimer({});

// Start a timer for an event
const timerId = eventsTimer.startTimer('eventName');

// Stop a timer for an event
eventsTimer.stopTimer('eventName', timerId);

// Get the average execution time for an event
const averageTime = eventsTimer.getAverageExecutionTime('eventName');

// Show all timers
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, { Timer } from 'events-timer';

const [externalState, setExternalState] = useState<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);

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.