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

@sourcetoad/retry-session

v2.0.0

Published

A simple-but-flexible utility for executing tasks on intervals and resolving with promises

Downloads

3

Readme

RetrySession

A simple-but-flexible utility for executing tasks on intervals and resolving with promises.

Install (NPM)

Add package to package.json

yarn add @sourcetoad/retry-session

Install (GPR)

Create .npmrc next to package.json

@sourcetoad:registry=https://npm.pkg.github.com

Add package to package.json

yarn add @sourcetoad/retry-session

Import

import RetrySession from 'retry-session';

Usage

Waiting

Retry until successful.

const getProperty = async () => {
    if (window.aProperty) {
        return window.aProperty;
    }

    throw new Error('Property is not yet available');
};

const propertyRetrySession = new RetrySession(
    getProperty, // Async callback
    1000, // Poll every second
    5000, // Timeout after 5 seconds (optional)
    false // Execute callback immediately on start
);

// Once it exists, the value of window.aProperty is assigned to result
const result = await propertyRetrySession.start();

Polling

Repeat until failure.

Assuming an async function fetchApiData that resolves with the data on success and rejects on failure.

We construct a promise explicitly to invert these results--so a successful API request causes our wrapper function to reject, and the RetrySession to retry.

The difference from a simple timeout or interval is that upon API failure, the session resolves so it can be handled. The user doesn't have to manage timers, and cancel them at the right times.

const primaryPollRequest = () => new Promise((resolve, reject) => {
    this.fetchApiData()
        .then(data => {
            // Do something with the data
            reject(/* An API request succeeded; retry */);
        })
        .catch(() => {
            resolve(); // An API request failed; end this retry session
        });
});

const primaryPollSession = new RetrySession(
    primaryPollRequest,
    300000 // Refresh data every 5 minutes
);

primaryPollSession.start().then(() => /* An API request failed */);

/*
 * An ongoing session can be canceled.
 * It can be restarted. The timeout (if any) will reset.
 */
primaryPollSession.cancel();

Cascading

Repeat unless failure, then retry faster until success.

const primaryPollRequest = () => new Promise((resolve, reject) => {
    this.fetchApiData()
        .then(data => {
            // Do something with the data
            reject(/* An API request succeeded; retry */);
        })
        .catch(() => {
            resolve(); // An API request failed; end this retry session
        });
});

const primaryPollSession = new RetrySession(
    primaryPollRequest,
    300000, // Refresh data every 5 minutes
    null,
    true
);

const secondaryPollSession = new RetrySession(
    this.fetchApiData, // We don't need to wrap this one in our own promise
    30000, // Retry every 30 seconds until we get a success
    null,
    true
);

/*
 * Poll on primary interval; if all requests fail, poll on secondary interval
 * If secondary interval succeeds, revert to primary interval
 */
const startPrimaryPoll = () => {
    primaryPollSession.start()
        .then(() => {
            // Failed; switch to secondary interval
            secondaryPollSession.start()
                .then(() => {
                    // Recovered; back to primary poll interval
                    startPrimaryPoll();
                });
        });
};

startPrimaryPoll();

Configuration

The following parameters are available to configure the session on instantiation:

  1. callback async function - If it resolves, resolve this promise with the result. If it rejects, retry.
  2. retryPeriod number - Time to retry in milliseconds
  3. timeLimit bool (optional) - Time to give up in milliseconds. falsy to never timeout
  4. waitFirst bool (optional) - Whether to wait retryPeriod before trying the first time

An instance of RetrySession has a public property waitFirst which can be modified at any time.

Creating a release

  • Update package.json with "version": "X.Y.Z"
  • Create branch release-X.Y.Z
  • Commit as build: version X.Y.Z tagged
  • Tag like git tag -a vX.Y.Z -m "updated to version vX.Y.Z"