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

cached-runner

v1.1.2

Published

Receives a period of time and a request function, and performs all async operations to get the data. If you retry the request in a time shorter than the period it returns the cached result instead.

Downloads

2

Readme

Cached Runner

pipeline status coverage report npm version npmjs downloads

Receives a period of time and a request function, and performs all async operations to get the data. If you retry the request in a time shorter than the period it returns the cached result instead. You can also set a postprocess function and another function that runs every time.

Inmediate after invocation request -> process -> cached -> allways -> returned. New timestamp and new random number:

> "1513186989013 --> ~(4)~"

One second after invocation data isn't refreshed since period hasn't expired so: cached -> allways -> returned. New timestamp and cached random number:

> "1513186990016 --> ~(4)~"

Three secons after first invocation cache has expired, so request is rerun: request -> process -> cached -> allways -> returned. New timestamp and new random number:

> "1513186992017 --> ~(90)~"

Running tests

Using your favorite node package manager, first install dependencies:

$ yarn

And then run the test command:

$ yarn test

Compile to javascript es5:

Once dependencies are installed you just have to run the build command and compiled files will be placed under dist/ directory:

$ yarn run build

Lint the code:

If you are making a commit please check beforehand that your code complies with the linter:

$ yarn run lint

Examples

Simple example

Usage

const simpleCahedRun = new CachedRun(2500, ()=>Math.random());
simpleCahedRun.start().then(res => console.log(res)); // run instantly
setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),1000); // run after 1 second
setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),3000); // run after 3 secons
setTimeout(() => simpleCahedRun.refresh(),3500); // force refresh
setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),4000); // run after 4 secons```

Output

> 0.06756653717504979 // run instantly: new cache until 2.5s
> 0.06756653717504979 // run after 1 second: get from cache
> 0.04633045049386242 // run after 3 seconds: new cache until 5s
                      // force refresh
> 0.20170561495875194 // run after 4 seconds: forced refresh

Complete Example

Here we set all available options.

const period = 2000;  // ms
const allways = data => +new Date() + ' --> ' + data;  // an allways run function that adds a timestamp
const request = () => Math.random();  // a request function that generates a random number
const process = num => `~(${Math.ceil(num*100)})~`;
const cachedRun = new CachedRun(period, request, process, allways);
cachedRun.start().then(res => console.log(res));
setTimeout(() => cachedRun.start().then(res => console.log(res)),1000);
setTimeout(() => cachedRun.start().then(res => console.log(res)),3000);

Documentation

For further reference you can take a look here