cached-runner

1.1.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i cached-runner

Weekly Downloads

1

Version

1.1.2

License

MIT

Unpacked Size

112 kB

Total Files

7

Last publish

Collaborators

  • dinigo