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

use-relax

v2.0.0

Published

A light wrapper for async functions to prevent concurrent calls and memorize returned value

Downloads

22

Readme

use-relax

pipeline status

Relax your async methods calls !

use-relax is written in Typescript and contains only a single function useRelax that allows memorizing an async method call. Once "relaxed", your async method will only be called once at a time with the same parameters.

Installation

use-relax can be installed through yarn and npm.

To install from npm, run:

npm install use-relax

To install from yarn, run:

yarn add use-relax

Usage

useRelax takes as a first argument the async method to relax. The second argument is the configuration that allows setting a custom predicate for parameters equality and determine if the value returned should be memorized or not.

Basic usage

With no configuration given, the equality of parameters is based on strict equality and the value is not memorized.

import { useRelax } from 'use-relax';

const myAsyncFunction = async (value1: string, value2: number) =>
  new Promise<string>((yeah) => {
    setTimeout(() => {
      console.log(`Called with "${value1}" and "${value2}"`);
      yeah(value1 + value2.toString());
    }, 1000);
  });

const [myRelaxedFunction] = useRelax(myAsyncFunction);

await Promise.all([
  myRelaxedFunction('relax', 42), 
  myRelaxedFunction('relax', 42), 

  myRelaxedFunction('stay cool', 42)
]);
// Logs:
// Called with "relax" and "42"
// Called with "stay cool" and "42"

await myRelaxedFunction('relax', 42);
// Logs:
// Called with "relax" and "42"

Configuration

The configuration is composed of two optional properties: parametersPredicate and memorizeValue.

parametersPredicate is a predicate function used to check if two list of arguments of the async function are equal. By default, it uses strict equality between arguments.

const myCustomPredicate = <U extends any[]>(args1: U, args2: U) =>
  args1.every((arg, index) => arg === args2[index]);

const [myRelaxedFunction] = useRelax(myAsyncFunction, { 
  parametersPredicate: myCustomPredicate,
});
// "myCustomPredicate" will be used to determine if two list of parameters are equal

memorizeValue is a boolean. If set to true, it will memorize the value returned by the async function. Default value is false.

const myAsyncFunction = async (value1: string, value2: number) =>
  new Promise<string>((yeah) => {
    setTimeout(() => {
      console.log(`Called with "${value1}" and "${value2}"`);
      yeah(value1 + value2.toString());
    }, 1000);
  });

const [myRelaxedFunction] = useRelax(myAsyncFunction, {
  memorizeValue: true,
});

await Promise.all([
  myRelaxedFunction('relax', 42), 
  myRelaxedFunction('relax', 42), 

  myRelaxedFunction('stay cool', 42)
]);
// Logs:
// Called with "relax" and "42"
// Called with "stay cool" and "42"

await myRelaxedFunction('relax', 42);
// Logs nothing !

Unmemorize

useRelax also returns a method to unmemorize the promise and the value.

const myAsyncFunction = async (value1: string, value2: number) =>
  new Promise<string>((yeah) => {
    setTimeout(() => {
      console.log(`Called with "${value1}" and "${value2}"`);
      yeah(value1 + value2.toString());
    }, 1000);
  });

const [myRelaxedFunction, resetRelax] = useRelax(myAsyncFunction, {
  memorizeValue: true,
});

const promises = [
  myRelaxedFunction('relax', 42), 
  myRelaxedFunction('relax', 42), 

  myRelaxedFunction('stay cool', 42)
];

resetRelax('relax', 42);

promises.push(
  myRelaxedFunction('relax', 42)
);

await Promise.all(promises);
// Logs:
// Called with "relax" and "42"
// Called with "relax" and "42"
// Called with "stay cool" and "42"

Development

Prerequisites

The project has been developed with NodeJS v12.18.0 and yarn v1.22.4. To install the dependencies, run:

yarn install

Launch tests

To run the tests, run:

yarn test

To run the linter, run:

yarn lint

Build project

To build the project, run:

yarn build

The build will be available in the dist/ directory.