events-timer

1.0.3 • Public • Published

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 from 'events-timer';

// Create an instance of EventsTimer with an external state object
const externalState = {};
const eventsTimer = new EventsTimer(externalState);

// Create a custom React hook for managing timers
function useEventsTimer() {
  const [timers, setTimers] = useState(externalState);

  const startTimer = (eventName) => {
    const timerId = eventsTimer.startTimer(eventName);
    // Update the state with the new timers data
    setTimers({ ...externalState });
    return timerId;
  };

  const stopTimer = (eventName, timerId) => {
    eventsTimer.stopTimer(eventName, timerId);
    // Update the state with the updated timers data
    setTimers({ ...externalState });
  };

  const getAverageExecutionTime = (eventName) => {
    return eventsTimer.getAverageExecutionTime(eventName);
  };

  return { startTimer, stopTimer, getAverageExecutionTime, timers };
}

// Example usage in a React component
function TimerComponent() {
  const { startTimer, stopTimer, getAverageExecutionTime, timers } = useEventsTimer();

  const handleStartTimer = () => {
    const timerId = startTimer('example-event');
    // ... Perform some task
    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.

Package Sidebar

Install

npm i events-timer

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

18.8 kB

Total Files

7

Last publish

Collaborators

  • rradoychev