npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jfabello/simple-timer

v1.0.1

Published

Promise-based simple timer for Node.js.

Downloads

46

Readme

Promise-based simple timer for Node.js

License: MIT

This module provides a simple, promise-based timer for Node.js. It allows the creation of timers that can be awaited asynchronously, making it easier to manage timeouts and delays in your code. The SimpleTimer class supports starting and cancelling the timers, and also provides information about the timer's state.

Table of Contents

Installation

You can install this module via npm:

npm install @jfabello/simple-timer

Usage

To use the simple-timer module, first import it into your code and then create an instance of the SimpleTimer class.

Here are some examples:

Starting a Timer

const SimpleTimer = require('@jfabello/simple-timer');

async function someAsyncFunction() {
    // Creates a new SimpleTimer instance with a 2000 ms timeout
    let timer = new SimpleTimer(2000);

	// Start the timer and wait for it to finish
    await timer.start();
	console.log("Timer done!");
}

someAsyncFunction();

Cancelling a Timer

const SimpleTimer = require('@jfabello/simple-timer');

async function someAsyncFunction() {
    // Creates a new SimpleTimer instance with a 2000 ms timeout
    let timer = new SimpleTimer(2000);

	// Start the timer and wait for it to finish
    let timerPromise = timer.start();

	// Cancel the timer after 1000 ms
	setTimeout(async () => {
		timer.cancel();
		await timerPromise;
		console.log("Timer cancelled!");
	}, 1000);
}

someAsyncFunction();

Using Timer States

const SimpleTimer = require('@jfabello/simple-timer');

async function someAsyncFunction() {
	// Creates a new SimpleTimer instance with a 2000 ms timeout
	let timer = new SimpleTimer(2000);

	// Start the timer and wait for it to finish
	let timerPromise = timer.start();

	// Report the state of the timer every 500 ms
	let interval = setInterval(() => {
		console.log(`Timer state: ${timer.state.toString()}`);
		if (timer.state === SimpleTimer.DONE || timer.state === SimpleTimer.CANCELLED) {
			clearInterval(interval);
			clearTimeout(timeout);
		}
	}, 500);

	// Cancel the timer after 1000 ms
	let timeout = setTimeout(async () => {
		timer.cancel();
		await timerPromise;
		console.log("Timer cancelled!");
	}, 1500);
}

someAsyncFunction();

The code above should output the following to the console:

Timer state: Symbol(RUNNING)
Timer state: Symbol(RUNNING)
Timer cancelled!
Timer state: Symbol(CANCELLED)

Timer States

The SimpleTimer class provides the following states:

stateDiagram-v2
	[*] --> SET : new SimpleTimer(timeout)
	SET --> RUNNING : start()
	RUNNING --> DONE : "timerup" event
	RUNNING --> CANCELLED : cancel() and then the "timercancelled" event

The SimpleTimer Class

A simple timer class that provides a way to set, start, and cancel a timer with a specified timeout.

Static Properties

  • SET: Read-only property representing the SET timer state.
  • RUNNING: Read-only property representing the RUNNING timer state.
  • DONE: Read-only property representing the DONE timer state.
  • CANCELLED: Read-only property representing the CANCELLED timer state.
  • errors: Read-only property that contains the simple timer error classes as its properties.

Instance Properties

  • state: The timer instance state.

Instance Methods

constructor(timeout)

Creates a new SimpleTimer instance with the specified timeout.

Parameters
  • timeout: The timeout duration in milliseconds. Must be an integer greater than 0.
Throws
  • ERROR_SIMPLE_TIMER_TIMEOUT_TYPE_INVALID: If the timeout is not an integer.
  • ERROR_SIMPLE_TIMER_TIMEOUT_OUT_OF_BOUNDS: If the timeout is less than 1.

start()

Starts the timer if it is in the SET state. If the timer is already running, it returns the existing timer promise. If the timer is not in the SET or RUNNING states, it throws an error.

Returns

A promise that fulfills to SimpleTimer.DONE if the timer times out, or SimpleTimer.CANCELLED if the timer is cancelled before it times out.

Throws
  • ERROR_SIMPLE_TIMER_NOT_IN_SET_OR_RUNNING_STATES: If the timer is not in the SET or RUNNING states.

cancel()

Cancels the simple timer. If the timer is not in the RUNNING state, it throws an error.

Returns
  • A promise that fulfills to SimpleTimer.CANCELLED once the timer is cancelled.
Throws
  • ERROR_SIMPLE_TIMER_NOT_RUNNING: If the timer is not running.

Testing

To run the tests for this module, first clone the repository using the following command:

git clone https://github.com/jfabello/simple-timer.git

Then, navigate to the project directory and install the npm dependencies, this will install the Jest testing framework:

cd simple-timer
npm install

Finally, run the tests using the following command:

npm test

Contributing

Unfortunately, we are not able to accept contributions at this time.

If you find a bug in the code, please open an issue.

Thank you for your understanding.

License

This project is licensed under the MIT License. See the LICENSE file for details.