Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cache-async-fn

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-async-fn

Cache asynchronous functions where they should be

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cache-async-fn

npm version npm downloads bundle JSDocs

Usage

Initialization

With MobX:

import { observable } from 'mobx';
import { cacheAsyncFactory } from 'cache-async-fn';

function createCache<T>(): { value: Record<string, T> } {
  return observable(
    { value: {} },
    {
      value: observable.ref,
    },
  );
}
const { cacheAsync } = cacheAsyncFactory(createCache);

With Vue:

import { ref } from 'vue';
import { cacheAsyncFactory } from 'cache-async-fn';

function createCache<T>(): { value: Record<string, T> } {
  return ref();
}
const { cacheAsync } = cacheAsyncFactory(createCache);

Cache APIs

const myApi = cacheAsync(myApiCall, {
  resolver: (params) => [groupKey, cacheKey],
});

// Call from anywhere
async function someAction() {
  const response = await myApi(params);
}

// Get the current value anytime
function getValueSync() {
  return myApi.get(params);
}

Each groupKey has its own cache. The cache is invalidated when the cacheKey changes.

Use Cases

Load once globally

The is the default behavior.

const loadOnceGlobally = cacheAsync(api);
// or
const loadOnceGlobally = cacheAsync(api, {
  resolver: () => '',
});

Load on param change

const loadOnParamChange = cacheAsync(api, {
  resolver: (params) => ['', JSON.stringify(params)],
});

Cache data for multiple tabs

The data for each tab will be cached in a different group, with the parameters as its cache key.

const loadOnParamChange = cacheAsync(api, {
  resolver: (params) => [params.tab, JSON.stringify(params)],
});

FAQs

Package last updated on 12 Jan 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc