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

polling-timers

v1.0.1

Published

Javascript timer that negates the effects of OS hibernation and thread suspension

Downloads

5

Readme

polling-timers

Build Status

Summary

polling-timers offers a number of async timers that allow for more precise handling of timeouts. In addition they are aware of what moment in time they were started. This means that if an OS goes into hibernation, when it awakes the timers will know if they should have timed-out within the hibernation period. A good example of this, is backgrounding an app or working in a separate browser tab.

Installation

Install with npm i polling-timers or with yarn yarn add polling-timers.

Usage

The API for polling-timers is simply a factory that returns a promise that will resolve once the timer has completed. Additionally this promise is cancellable if you need to terminate a timer early.

delay

Returns a promise with the following additional methods:


import {delay} from 'polling-timers'

const delayForOneSecond = 1000
const whenDelayHasExpired = delay(delayForOneMilliSecond)

whenDelayHasExpired.then(() => {
    console.log('do something')
})

// the timer can be cancelled at any point
whenDelayHasExpired.cancel().onCancel(() =>  console.log('cancelled'))

timeout


import {timeout} from 'polling-timers'

const delayForOneSecond = 1000
const thingToDo = (args) => {
    console.log('called with', args)
}
const startTimeout = timeout(thingToDo, delayForOneMilliSecond)
startTimeout('me!')

# output after 1 sec
'called with me!'

throttle

A timer that will not invoke its given function until the timeout has been reached.


import {throttle} from 'polling-timers'

const throttleForOneSecond = 1000
const thingToDo = (args) => {
    console.log('called with', args)
}
const startThrottling = throttle(thingToDo, throttleForOneSecond)

async function() {
    startThrottling('me!')
    startThrottling('me!')
    startThrottling('me!')
    await delay(1000)
    startThrottling('me!') // <-- only this call will be invoked
}

# output
'called with me!' is only printed once

debounce

Returns a timer that restarts if the timer is invoked before the debounce timeout has been reached


import {debounce} from 'polling-timers'

const debounceForOneSecond = 2000
const thingToDo = (args) => {
    console.log('called with', args)
}
const startDebouncing = debounce(thingToDo, debounceForOneSecond)

async function() {
    startDebouncing('me!') // <-- thingToDo is not called and timer is restarted
    await delay(1000)
    startDebouncing('me!') // <-- thingToDo is not called and timer is restarted
    await delay(2000)
    startDebouncing('me!') // <-- thingToDo is called
}

# output
'called with me!' is only printed once

Api

delay

Returns a cancellable promise that will resolve once a given delay has passed

Arguments:

| Argument | Type | Optional | Description | | -------- | ------ | -------- | ---------------------- | | timeout | Number | No | Number of milliseconds delay should last. | | frequency | Number | Yes | Milliseconds between each poll of the timer. Defaults to 1000 milliseconds. |

Returns a promise with the following additional methods:

| Argument | Type | Description | | -------- | ------ | ---------------------- | | cancel | Function | Method used to cancel delay timer. | | onCancel | Function | Accepts a callback that will be called if the promise is called. |

const tenSeconds = 10000
const pollEverySecond = 1000
const timer = delay(tenSeconds, pollEverySecond)

timer.then(() => {
    console.log('Delay complete')
})

timer.canel()
timer.onCancel(() => console.log('cancelled'))

timeout

Returns a function that when called starts the timer that. All arguments passed to the timer will be passed to the callback.

Arguments:

| Argument | Type | Optional | Description | | -------- | ------ | -------- | ---------------------- | | callback | Function | No | Function that will be called once the timeout period has expired. | | timeout | Number | No | Number of milliseconds delay should last. | | frequency | Number | Yes | Milliseconds between each poll of the timer. Defaults to 1000 milliseconds. |

import {timeout} from 'polling-timers'

const callback = (args) => console.log(args)
const tenSeconds = 10000
const pollEverySecond = 1000

const timer = timeout(callback, tenSeconds, pollEverySecond)
timer('foo')

# after 10 secs outputs `foo`

throttle

Returns a function that when called starts the timer that prevents calls to it's callback function until the timeout has expired. All arguments passed to the timer will be passed to the callback.

If the timer is called again before it's timeout has expire, the previous timer will be cancelled and a new one will be started in it's place.

Arguments:

| Argument | Type | Optional | Description | | -------- | ------ | -------- | ---------------------- | | callback | Function | No | Function that will be called once the timeout period has expired. | | timeout | Number | No | Number of milliseconds delay should last. | | frequency | Number | Yes | Milliseconds between each poll of the timer. Defaults to 1000 milliseconds. |

import {throttle} from 'polling-timers'

const callback = (args) => console.log(args)
const tenSeconds = 10000
const pollEverySecond = 1000

const timer = throttle(callback, tenSeconds, pollEverySecond)
timer('bar') 
await delay(tenSeconds)
timer('foo')

# after 10 secs outputs `foo`

debounce

Returns a function that when called will restart the timer and wait again for timout period to expire before calling the callback method. All arguments passed to the timer will be passed to the callback.

If the timer is restarted, the previous timer will be cancelled and a new one will be started in it's place.

Arguments:

| Argument | Type | Optional | Description | | -------- | ------ | -------- | ---------------------- | | callback | Function | No | Function that will be called once the timeout period has expired. | | timeout | Number | No | Number of milliseconds delay should last. | | frequency | Number | Yes | Milliseconds between each poll of the timer. Defaults to 1000 milliseconds. |

import {debounce} from 'polling-timers'

const callback = (args) => console.log(args)
const oneSecond = 10000
const tenSeconds = 10000
const pollEverySecond = 1000

const timer = debounce(callback, tenSeconds, pollEverySecond)
timer('bar') // starts timer
await delay(oneSecond)
timer('foo') // restarts timer
await delay(tenSeconds)

# after 10 secs outputs `foo`