Socket
Socket
Sign inDemoInstall

async-call-reducer

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-call-reducer

Reduce calls using browser storage


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
6.33 kB
Created
Weekly downloads
 

Changelog

Source

1.0.0

New features

  • First release of asyncCallReducer

Readme

Source

AsyncCallReducer

GitHub Workflow Status (branch) Coverage Status

The AsyncCallReducer is a function to reduce calls using browser storage. A Promise can be encapsulated, a single HTTP request will be triggered regardless of the number of calls. Concurrent calls are added on a queue and return's data are stored in the browser.

Rules:

  • The first call is granted
  • Concurrent calls are blocked and added on a queue, then execute when the first call is ready
  • Next calls get the data from the browser storage

Installation

The function is available as the async-call-reducer package name on npm and Github.

npm i --save-dev async-call-reducer
yarn add --dev async-call-reducer

Environment

AsyncCallReducer was built for Node.js >=12.

Demo

The project includes an example of an implementation of AsyncCallReducer in the directory ./demo/.

Usage

Imagine the following function to get data from an API. Each call will trigger an HTTP request.

async function getData() {
  const data = await fetch('https://swapi.dev/api/people/?page=1');
  return await data.json();
}

By wrapping the function with the AsyncCallReducer, a single HTTP request will be triggered. Other calls will return exactly the same value, either from the storage or from the queue if the calls was concurrent.

async function getData() {
+   return await asyncCallReducer({
+       key: 'swapi',
+       callback: async () => {
            const data = await fetch('https://swapi.dev/api/people/?page=1');
            return await data.json();
+        }
+    });
}

Parameters

key

string = asyncCallReducer

Tells to the function the storage key used as a prefix. Following keys will be created:

  • asyncCallReducerData: Stored data
  • asyncCallReducerLoading: Blocker for concurrent calls
asyncCallReducer({
  key: 'swapi'
});

storage

string = sessionStorage

Tells to the function which browser storage method to use (sessionStorage|localStorage).

asyncCallReducer({
  storage: 'localStorage'
});

callback

Promise

Tells the function the promise to be executed.

💡 Keep the async keyword, the callback function must returns a Promise.

asyncCallReducer({
  callback: async () => {
    // Request data from an API and return data here
  }
});

Licence

AsyncCallReducer is licensed under the MIT License.

Created with ♥ by @yoriiis.

Keywords

FAQs

Last updated on 01 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc