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

shared-intervals

v1.0.1

Published

An npm package to share and batch setInterval calls with the same delay

Downloads

85

Readme

Shared Intervals

Shared Intervals is a TypeScript package designed to efficiently manage and synchronize multiple intervals with shared delays. For all intervals with the equal delays, it will use a single call to setInterval, so that all callbacks are triggered at the same time.

Features

  • Sychronization of intervals: All intervals with the same delay will be synchronized
  • Efficient Interval Management: Manages multiple intervals with shared delays using a single setInterval per delay.
  • Batch Function Support: Supports setting a batch function to wrap or modify callback execution.
  • Dynamic Callback Management: Allows for the dynamic addition and removal of callbacks.

Installation

To install Shared Intervals, run the following command in your project directory:

npm install shared-intervals

or if you are using yarn:

yarn add shared-intervals

Usage

Setting a Shared Interval

To set a shared interval, you import setSharedInterval and call it like a typical call to setInterval.

import { setSharedInterval } from 'shared-intervals'

const callbackId = setSharedInterval(() => {
  console.log('This will log every second globally.')
}, 1000)

const anotherCallbackId = setSharedInterval(() => {
  console.log('This will re-use the existing interval for 1000ms')
}, 1000)

Clearing a Shared Interval

To clear a shared interval, use clearSharedInterval with the unique identifier returned by setSharedInterval.

import { clearSharedInterval } from 'shared-intervals'

// Removes callback for callbackId, interval continues running for other callbacks
clearSharedInterval(callbackId)
// Remove last callback, which will clear the interval entirely
clearSharedInterval(anotherCallbackId)

Setting a Batch Function

To set a batch function that wraps or modifies the execution of all interval callbacks, use setBatchFunction.

import { setBatchFunction } from 'shared-intervals'

setBatchFunction((executeCallbacksForInterval) => {
  console.log('Batch function: Before executing callbacks')
  executeCallbacksForInterval()
  console.log('Batch function: After executing callbacks')
})

Multiple Shared Interval Instances

You may wish to have multipe independent instances of shared interval management. Use SharedIntervalManager for this.

import { SharedIntervalManager } from 'shared-intervals'

const intervalManager1 = new SharedIntervalManager()
intervalManager1.setBatchFunction((executeAll) => {
  console.log('Batch function: Runs only for intervalManager1')
  executeAll()
})
const intervalManager2 = new SharedIntervalManager()

const callbackId1 = intervalManager1.setInterval(() => {
  console.log('Only shares intervals with callbacks from intervalManager1')
}, 1000)
const callbackId2 = intervalManager2.setInterval(() => {
  console.log('Only shares intervals with callbacks from intervalManager2')
}, 1000)

intervalManager1.clearInterval(callbackId1)
intervalManager2.clearInterval(callbackId2)

License

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