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

loopy

v0.3.0

Published

asynchronous loop to properly manage regular function calls

Downloads

4

Readme

Build Status

Loopy

Executes asynchronous tasks at regular intervals with the ability to be serialized and resumed later.

Quick start

var Loopy = require('loopy')

var loop = new Loopy({
  interval: 60 * 1000,
  count: -1, // number of loops to run. -1 for infinity
  onError: Loopy.OnError.IGNORE || Loopy.OnError.EXPONENTIAL_BACKOFF || Loopy.OnError.STOP // behaviour when there is an error,
  maxInterval: 60*60*1000
})

// loop.status() === Loopy.Status.STOPPED

loop.on('tick', function(callback) {

  callback(new Error('...')) // optional error argument

})

loop.on('stop', function(err) {

  if(loop.status() === Loopy.Status.ERROR) {
    // if the loop was stopped due to an error
    // err is defined
  }
})

loop.on('error', function(err) {
  // this event fires for each error that is reported by a ```tick```
  // event handler. It cannot fire after ```loop.stop()``` is called.
  // That means that if a ```tick``` is in process when you call
  // ```stop```, an error reported by that last tick will be ignored
})

loop.start()
// loop.status() === Loopy.Status.STARTED

API

new Loopy(options)

var loop = new Loopy({
  timeout: 30 * 1000,
  interval: 60 * 1000,
  count: -1, // number of loop iterations to run. -1 for infinity
  onError: Loopy.OnError.IGNORE || Loopy.OnError.EXPONENTIAL_BACKOFF || Loopy.OnError.STOP // behaviour when there is an error.
  maxInterval: 60 * 60 * 1000
})
  • timeout: NOT YET IMPLEMENTED. open an issue to have it delay in milliseconds after which an async action is considered failed. An 'timeout' event is triggered and the subsequent behaviour is defined by the 'onError' configuration option
  • interval: the interval between each async function in milliseconds. If the interval is 10 seconds and your async function takes 2 seconds to complete, async function will be called every 12 seconds.
  • count: the number of loop iterations to execute. The loop will stop after the count is reached.
  • onError: defines the behaviour when the asynchronous 'tick' handler reports an error
    • Loopy.OnError.IGNORE: continue to the next iteration like nothing happened. An 'error' event will be fired.
    • Loopy.OnError.EXPONENTIAL_BACKOFF: continue to the next iteration but multiply the last iteration interval by 2. That means that the interval will keep growing if there are many subsequent errors. The interval will resume to the regular value on the first successful tick. It is possible to cap this value with the maxInterval option.
    • Loopy.OnError.STOP: stop the loop. Both an error and a stop event will fire.
  • maxInterval: the exponential backoff upper limit, in milliseconds.

loopy.start(now)

Starts the loop. If the loop is already started, it will behave as if the loop was not started and but the iterations count will not be reset.

  • now: boolean, set to true if you wish the first tick to fire immediately instead of waiting the interval

loopy.stop()

Immediately stops the loop iterations. If an iteration (tick) is currently in progress, any tick error will be ignored.

loopy.reset()

Resets the number of iterations.

loopy.count()

Returns the number of iterations executed.

loopy.status()

Returns one of:

  • Loopy.Status.ERROR
  • Loopy.Status.STARTED
  • Loopy.Status.STOPPED