Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
events-timer
Advanced tools
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.
To install this package, you can use npm or yarn:
npm install events-timer
# or
yarn add events-timer
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();
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.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.
This package has no external dependencies.
This package is released under the MIT License.
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.
FAQs
Tracks time between two actions for an event and records entries.
We found that events-timer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.